1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

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. -->
<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">
<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="P8_7">GPIO2_2 (P8 pin 7)</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>
</select>
</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">
<label for="node-input-updateInterval"><i class="icon-repeat"></i>Update every</label>
<input id="node-input-updateInterval" type="text">
<label>s</label>
<label for="node-input-updateInterval"><i class="icon-repeat"></i> Update at</label>
<input id="node-input-updateInterval" type="text" style="width: 65px">
<label>sec intervals</label>
</div>
<div class="form-row">
<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. -->
<script type="text/x-red" data-help-name="BBB-discrete-in">
<!-- data-help-name identifies the node type this help is for -->
<!-- This content appears in the Info sidebar when a node is selected -->
<!-- The first <p> is used as the pop-up tool tip when hovering over a -->
<!-- node in the palette. -->
<p>Discrete input for the Beaglebone Black. Sends a message on the first output
each time the pin changes state, and
records the total time in the active state</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 type="text/x-red" data-help-name="discrete-in">
<p>Discrete input for the Beaglebone Black. Sends a message on the first output
each time the pin changes state, and records the total time in the active state</p>
<p>A timer updates the total active time, sending a message on the second output
at the chosen update interval.
Any input message will reset the total active time</p><p>The active
state may be set to be high or low: this only affects the active time, not the
value read from the pin and sent on the first output</p>
</script>
<!-- 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-->
<script type="text/javascript">
RED.nodes.registerType('BBB-discrete-in',{
RED.nodes.registerType('discrete-in',{
category: 'advanced-input', // the palette category
color:"#c6abef",
color:"#c6baef",
defaults: { // defines the editable properties of the node
name: { value:"" }, // along with default values.
updateInterval: { value:5 },
updateInterval: { value:30 },
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
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 {
var bs = require("bonescript");
} 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
@ -31,6 +31,10 @@ function DiscreteInputNode(n) {
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; // the topic is not currently used
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.interruptAttached = false; // Flag: should we detach interrupt when we are closed?
@ -52,26 +56,28 @@ function DiscreteInputNode(n) {
} else {
node.currentState = x.value;
var now = Date.now();
if (node.currentState == "1") {
if (node.currentState == node.activeState) {
node.lastActiveTime = now;
} else {
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
// message on the second output with the latest value of the total active time, in seconds
var timerCallback = function () {
if (node.currentState == "1") {
if (node.currentState == node.activeState) {
var now = Date.now();
node.totalActiveTime += now - node.lastActiveTime;
node.lastActiveTime = now;
}
var msg = {};
msg.topic = node.topic;
msg.payload = node.totalActiveTime/1000;
node.send([null, msg]);
};
@ -80,21 +86,18 @@ function DiscreteInputNode(n) {
// (so we start counting from zero again)
var inputCallback = function (msg) {
node.totalActiveTime = 0;
if (node.currentState == "1") {
if (node.currentState == node.activeState) {
node.lastActiveTime = Date.now();
}
if (node.starting) {
node.starting = false;
var msg1 = {};
msg1.payload = "hello";
var msg2 = {};
msg2.payload = "world";
this.send([msg1, msg2]);
node.log("Initial message " + msg1.payload + " " + msg2.payload);
node.log("currentState: " + node.currentState);
node.log("activeTime: " + node.totalActiveTime);
msg1 = null;
msg2 = null;
var msg = [{topic:node.topic}, {topic:node.topic}];
msg[0].payload = node.currentState;
msg[1].payload = node.totalActiveTime;
this.send(msg);
node.log("Initial message: " + msg[0].payload + " " + msg[1].payload);
node.log("currentState: " + this.currentState);
node.log("activeTime: " + this.totalActiveTime);
}
};
@ -108,7 +111,7 @@ function DiscreteInputNode(n) {
// Initialise the currentState and lastActveTime variables based on the value read
node.currentState = x.value;
node.error("First read - currentState: " + node.currentState);
if (node.currentState == "1") {
if (node.currentState == node.activeState) {
node.lastActiveTime = Date.now();
}
// 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.
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)
DiscreteInputNode.prototype.close = function () {