Added activeLow property to discrete-in

Further attempts to get the initial message working. Added activeLow
property. Output messages now include topic. Better help text. Change
name to discrete-in
This commit is contained in:
Maxwell Hadley 2014-02-04 20:34:04 +00:00
parent 3b319e7d83
commit e9a860f36f
2 changed files with 43 additions and 38 deletions

View File

@ -16,10 +16,10 @@
<!-- First, the content of the edit dialog is defined. --> <!-- First, the content of the edit dialog is defined. -->
<script type="text/x-red" data-template-name="BBB-discrete-in"> <script type="text/x-red" data-template-name="discrete-in">
<div class="form-row"> <div class="form-row">
<label for="node-input-pin"><i class="icon-asterisk"></i>Input pin</label> <label for="node-input-pin"><i class="icon-asterisk"></i>Input pin</label>
<select type="text" id="node-input-pin" style="width: 180px;"> <select type="text" id="node-input-pin" style="width: 200px;">
<option value="">select pin</option> <option value="">select pin</option>
<option value="P8_7">GPIO2_2 (P8 pin 7)</option> <option value="P8_7">GPIO2_2 (P8 pin 7)</option>
<option value="P8_8">GPIO2_3 (P8 pin 8)</option> <option value="P8_8">GPIO2_3 (P8 pin 8)</option>
@ -54,10 +54,13 @@
<option value="P9_42">GPIO0_7 (P9 pin 42)</option> <option value="P9_42">GPIO0_7 (P9 pin 42)</option>
</select> </select>
</div> </div>
<div class="form-row">
<label for="node-input-activeLow">Active low</label>
<input type="checkbox" id="node-input-activeLow" style="display:inline-block; width:auto; vertical-align:top;">
<div class="form-row"> <div class="form-row">
<label for="node-input-updateInterval"><i class="icon-repeat"></i>Update every</label> <label for="node-input-updateInterval"><i class="icon-repeat"></i> Update at</label>
<input id="node-input-updateInterval" type="text"> <input id="node-input-updateInterval" type="text" style="width: 65px">
<label>s</label> <label>sec intervals</label>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label> <label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
@ -74,29 +77,28 @@
<!-- Next, some simple help text is provided for the node. --> <!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="BBB-discrete-in"> <script type="text/x-red" data-help-name="discrete-in">
<!-- data-help-name identifies the node type this help is for --> <p>Discrete input for the Beaglebone Black. Sends a message on the first output
<!-- This content appears in the Info sidebar when a node is selected --> each time the pin changes state, and records the total time in the active state</p>
<!-- The first <p> is used as the pop-up tool tip when hovering over a --> <p>A timer updates the total active time, sending a message on the second output
<!-- node in the palette. --> at the chosen update interval.
<p>Discrete input for the Beaglebone Black. Sends a message on the first output Any input message will reset the total active time</p><p>The active
each time the pin changes state, and state may be set to be high or low: this only affects the active time, not the
records the total time in the active state</p> value read from the pin and sent on the first output</p>
<p>A timer updates the total active time, sending a message on the 2nd output at the chosen interval.
An input (any input) message will reset the total active time</p>
</script> </script>
<!-- Finally, the node type is registered along with all of its properties --> <!-- Finally, the node type is registered along with all of its properties -->
<!-- The example below shows a small subset of the properties that can be set--> <!-- The example below shows a small subset of the properties that can be set-->
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('BBB-discrete-in',{ RED.nodes.registerType('discrete-in',{
category: 'advanced-input', // the palette category category: 'advanced-input', // the palette category
color:"#c6abef", color:"#c6baef",
defaults: { // defines the editable properties of the node defaults: { // defines the editable properties of the node
name: { value:"" }, // along with default values. name: { value:"" }, // along with default values.
updateInterval: { value:5 }, updateInterval: { value:30 },
topic: { value:"", required:true }, topic: { value:"", required:true },
pin: { value:"", required:true }, pin: { value:"", required:true },
activeLow: { value:false, required:true}
}, },
inputs:1, // set the number of inputs - only 0 or 1 inputs:1, // set the number of inputs - only 0 or 1
outputs:2, // set the number of outputs - 0 to n outputs:2, // set the number of outputs - 0 to n

View File

@ -21,7 +21,7 @@ var RED = require(process.env.NODE_RED_HOME + "/red/red");
try { try {
var bs = require("bonescript"); var bs = require("bonescript");
} catch(err) { } catch(err) {
require("util").log("[BBB-discrete-in] Error: cannot find module 'bonescript'"); require("util").log("[145-digital-in] Error: cannot find module 'bonescript'");
} }
// The node constructor // The node constructor
@ -31,6 +31,10 @@ function DiscreteInputNode(n) {
// Store local copies of the node configuration (as defined in the .html) // Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; // the topic is not currently used this.topic = n.topic; // the topic is not currently used
this.pin = n.pin; // The Beaglebone Black pin identifying string this.pin = n.pin; // The Beaglebone Black pin identifying string
if (n.activeLow) // Set the 'active' state 0 or 1 as appropriate
this.activeState = "0";
else
this.activeState = "1";
this.updateInterval = n.updateInterval*1000; // How often to send total active time messages this.updateInterval = n.updateInterval*1000; // How often to send total active time messages
this.interruptAttached = false; // Flag: should we detach interrupt when we are closed? this.interruptAttached = false; // Flag: should we detach interrupt when we are closed?
@ -52,26 +56,28 @@ function DiscreteInputNode(n) {
} else { } else {
node.currentState = x.value; node.currentState = x.value;
var now = Date.now(); var now = Date.now();
if (node.currentState == "1") { if (node.currentState == node.activeState) {
node.lastActiveTime = now; node.lastActiveTime = now;
} else { } else {
node.totalActiveTime += now - node.lastActiveTime; node.totalActiveTime += now - node.lastActiveTime;
} }
var msg = {};
msg.topic = node.topic;
msg.payload = node.currentState;
node.send([msg, null]);
} }
var msg = {};
msg.payload = node.currentState;
node.send([msg, null]);
}; };
// This function is called by the timer. It updates the ActiveTime variables, and sends a // This function is called by the timer. It updates the ActiveTime variables, and sends a
// message on the second output with the latest value of the total active time, in seconds // message on the second output with the latest value of the total active time, in seconds
var timerCallback = function () { var timerCallback = function () {
if (node.currentState == "1") { if (node.currentState == node.activeState) {
var now = Date.now(); var now = Date.now();
node.totalActiveTime += now - node.lastActiveTime; node.totalActiveTime += now - node.lastActiveTime;
node.lastActiveTime = now; node.lastActiveTime = now;
} }
var msg = {}; var msg = {};
msg.topic = node.topic;
msg.payload = node.totalActiveTime/1000; msg.payload = node.totalActiveTime/1000;
node.send([null, msg]); node.send([null, msg]);
}; };
@ -80,21 +86,18 @@ function DiscreteInputNode(n) {
// (so we start counting from zero again) // (so we start counting from zero again)
var inputCallback = function (msg) { var inputCallback = function (msg) {
node.totalActiveTime = 0; node.totalActiveTime = 0;
if (node.currentState == "1") { if (node.currentState == node.activeState) {
node.lastActiveTime = Date.now(); node.lastActiveTime = Date.now();
} }
if (node.starting) { if (node.starting) {
node.starting = false; node.starting = false;
var msg1 = {}; var msg = [{topic:node.topic}, {topic:node.topic}];
msg1.payload = "hello"; msg[0].payload = node.currentState;
var msg2 = {}; msg[1].payload = node.totalActiveTime;
msg2.payload = "world"; this.send(msg);
this.send([msg1, msg2]); node.log("Initial message: " + msg[0].payload + " " + msg[1].payload);
node.log("Initial message " + msg1.payload + " " + msg2.payload); node.log("currentState: " + this.currentState);
node.log("currentState: " + node.currentState); node.log("activeTime: " + this.totalActiveTime);
node.log("activeTime: " + node.totalActiveTime);
msg1 = null;
msg2 = null;
} }
}; };
@ -108,7 +111,7 @@ function DiscreteInputNode(n) {
// Initialise the currentState and lastActveTime variables based on the value read // Initialise the currentState and lastActveTime variables based on the value read
node.currentState = x.value; node.currentState = x.value;
node.error("First read - currentState: " + node.currentState); node.error("First read - currentState: " + node.currentState);
if (node.currentState == "1") { if (node.currentState == node.activeState) {
node.lastActiveTime = Date.now(); node.lastActiveTime = Date.now();
} }
// Attempt to attach a change-of-state interrupt handler to the pin. If we succeed, // Attempt to attach a change-of-state interrupt handler to the pin. If we succeed,
@ -129,7 +132,7 @@ function DiscreteInputNode(n) {
} }
// Register the node by name. This must be called before overriding any of the Node functions. // Register the node by name. This must be called before overriding any of the Node functions.
RED.nodes.registerType("BBB-discrete-in", DiscreteInputNode); RED.nodes.registerType("discrete-in", DiscreteInputNode);
// On close, detach the interrupt (if we attaced one) and clear the interval (if we set one) // On close, detach the interrupt (if we attaced one) and clear the interval (if we set one)
DiscreteInputNode.prototype.close = function () { DiscreteInputNode.prototype.close = function () {