mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	Debugging discrete-in
Coerce the currentState to always be numeric & use === tests Catch the first spurious interrupt when x.value is undefined Add more debug output Add a second timer to get the first message out. This still fails at node-RED startup when the node is not activeLow
This commit is contained in:
		@@ -19,9 +19,9 @@ var RED = require(process.env.NODE_RED_HOME + "/red/red");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Require bonescript
 | 
					// Require bonescript
 | 
				
			||||||
try {
 | 
					try {
 | 
				
			||||||
	var bs = require("bonescript");
 | 
					    var bs = require("bonescript");
 | 
				
			||||||
} catch(err) {
 | 
					} catch (err) {
 | 
				
			||||||
	require("util").log("[145-digital-in] Error: cannot find module 'bonescript'");
 | 
					    require("util").log("[145-digital-in] Error: cannot find module 'bonescript'");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The node constructor
 | 
					// The node constructor
 | 
				
			||||||
@@ -29,117 +29,135 @@ function DiscreteInputNode(n) {
 | 
				
			|||||||
    RED.nodes.createNode(this, n);
 | 
					    RED.nodes.createNode(this, 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
 | 
					    if (n.activeLow)                                // Set the 'active' state 0 or 1 as appropriate
 | 
				
			||||||
		this.activeState = "0";
 | 
					        this.activeState = 0;
 | 
				
			||||||
	else
 | 
					    else
 | 
				
			||||||
		this.activeState = "1";
 | 
					        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 totalActiveTime 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?
 | 
				
			||||||
	this.intervalId = null;			// Remember the timer ID so we can delete it when we are closed
 | 
					    this.intervalId = null;         // Remember the timer ID so we can delete it when we are closed
 | 
				
			||||||
	this.currentState = 0;			// The pin input state "1" or "0"
 | 
					    this.currentState = 0;          // The pin input state "1" or "0"
 | 
				
			||||||
	this.lastActiveTime = 0;		// The date (in ms since epoch) when the pin last went high
 | 
					    this.lastActiveTime = NaN;      // The date (in ms since epoch) when the pin last went high
 | 
				
			||||||
	this.totalActiveTime = 0;		// The total time in ms that the pin has been high (since reset)
 | 
					    this.totalActiveTime = 0;       // The total time in ms that the pin has been high (since reset)
 | 
				
			||||||
	this.starting = true;
 | 
					    this.starting = true;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
	// Define 'node' to allow us to access 'this' from within callbacks (the 'var' is essential -
 | 
					    // Define 'node' to allow us to access 'this' from within callbacks (the 'var' is essential -
 | 
				
			||||||
	// otherwise there is only one 'node' for all instances of DiscreteInputNode!)
 | 
					    // otherwise there is only one global 'node' for all instances of DiscreteInputNode!)
 | 
				
			||||||
    var node = this;
 | 
					    var node = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// This function is called whenever the input pin changes state. We update the currentState
 | 
					    // This function is called whenever the input pin changes state. We update the currentState
 | 
				
			||||||
	// and the ActiveTime variables, and send a message on the first output with the new state
 | 
					    // and the ActiveTime variables, and send a message on the first output with the new state
 | 
				
			||||||
	var interruptCallback = function (x) {
 | 
					    // Note: this function gets called spuriously when the interrupt is first enabled: in this
 | 
				
			||||||
			if (node.currentState == x.value) {
 | 
					    // case x.value is undefined - we must test for this
 | 
				
			||||||
				node.log("Spurious interrupt: " + x.value);
 | 
					    var interruptCallback = function (x) {
 | 
				
			||||||
			} else {
 | 
					            node.log("interruptCallback: x.value = " + x.value);
 | 
				
			||||||
				node.currentState = x.value;
 | 
					            node.log("interruptCallback: node.currentState = " + node.currentState);
 | 
				
			||||||
				var now = Date.now();
 | 
					            node.log("interruptCallback: node.totalActiveTime = " + node.totalActiveTime);
 | 
				
			||||||
				if (node.currentState == node.activeState) {
 | 
					            node.log("interruptCallback: node.lastActiveTime = " + node.lastActiveTime);
 | 
				
			||||||
					node.lastActiveTime = now;
 | 
					            if (node.currentState === x.value - 0) {
 | 
				
			||||||
				} else {
 | 
					                node.log("Spurious interrupt: " + x.value);
 | 
				
			||||||
					node.totalActiveTime += now - node.lastActiveTime;
 | 
					            } else if (x.value != undefined) {
 | 
				
			||||||
				}
 | 
					                node.currentState = x.value - 0;
 | 
				
			||||||
				var msg = {};
 | 
					                var now = Date.now();
 | 
				
			||||||
				msg.topic = node.topic;
 | 
					                if (node.currentState === node.activeState) {
 | 
				
			||||||
				msg.payload = node.currentState;
 | 
					                    node.lastActiveTime = now;
 | 
				
			||||||
				node.send([msg, null]);
 | 
					                } else if (!isNaN(node.lastActiveTime)) {
 | 
				
			||||||
			}
 | 
					                    node.totalActiveTime += now - node.lastActiveTime;
 | 
				
			||||||
		};
 | 
					                }
 | 
				
			||||||
 | 
					                var msg = {};
 | 
				
			||||||
 | 
					                msg.topic = node.topic;
 | 
				
			||||||
 | 
					                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 == node.activeState) {
 | 
					            node.log("timerCallback: node.currentState = " + node.currentState);
 | 
				
			||||||
				var now = Date.now();
 | 
					            node.log("timerCallback: node.totalActiveTime = " + node.totalActiveTime);
 | 
				
			||||||
				node.totalActiveTime += now - node.lastActiveTime;
 | 
					            node.log("timerCallback: node.lastActiveTime = " + node.lastActiveTime);
 | 
				
			||||||
				node.lastActiveTime = now;
 | 
					            if (node.currentState === node.activeState) {
 | 
				
			||||||
			}
 | 
					                var now = Date.now();
 | 
				
			||||||
			var msg = {};
 | 
					                node.totalActiveTime += now - node.lastActiveTime;
 | 
				
			||||||
			msg.topic = node.topic;
 | 
					                node.lastActiveTime = now;
 | 
				
			||||||
			msg.payload = node.totalActiveTime/1000;
 | 
					            }
 | 
				
			||||||
			node.send([null, msg]);
 | 
					            var msg = {};
 | 
				
			||||||
		};
 | 
					            msg.topic = node.topic;
 | 
				
			||||||
 | 
					            msg.payload = node.totalActiveTime / 1000;
 | 
				
			||||||
 | 
					            node.send([null, msg]);
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// This function is called when we receive an input message. Clear the ActiveTime variables
 | 
					    // This function is called when we receive an input message. Clear the ActiveTime variables
 | 
				
			||||||
	// (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.log("inputCallback: node.currentState = " + node.currentState);
 | 
				
			||||||
			if (node.currentState == node.activeState) {
 | 
					            node.log("inputCallback: node.totalActiveTime = " + node.totalActiveTime);
 | 
				
			||||||
				node.lastActiveTime = Date.now();
 | 
					            node.log("inputCallback: node.lastActiveTime = " + node.lastActiveTime);
 | 
				
			||||||
			}
 | 
					            node.totalActiveTime = 0;
 | 
				
			||||||
			if (node.starting) {
 | 
					            if (node.currentState === node.activeState) {
 | 
				
			||||||
			 	node.starting = false;
 | 
					                node.lastActiveTime = Date.now();
 | 
				
			||||||
				var msg = [{topic:node.topic}, {topic:node.topic}];
 | 
					            }
 | 
				
			||||||
				msg[0].payload = node.currentState;
 | 
					            if (node.starting) {
 | 
				
			||||||
				msg[1].payload = node.totalActiveTime;
 | 
					                node.starting = false;
 | 
				
			||||||
				this.send(msg);
 | 
					                var msg = [{topic:node.topic}, {topic:node.topic}];
 | 
				
			||||||
				node.log("Initial message: " + msg[0].payload + " " + msg[1].payload);
 | 
					                msg[0].payload = node.currentState;
 | 
				
			||||||
				node.log("currentState: " + this.currentState);
 | 
					                msg[1].payload = node.totalActiveTime;
 | 
				
			||||||
				node.log("activeTime: " + this.totalActiveTime);
 | 
					                node.send(msg);
 | 
				
			||||||
			}
 | 
					                node.log("Initial message: " + msg[0].payload + " " + msg[1].payload);
 | 
				
			||||||
		};
 | 
					                node.log("currentState: " + node.currentState);
 | 
				
			||||||
 | 
					                node.log("activeTime: " + node.totalActiveTime);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// If we have a valid pin, set it as an input and read the (digital) state
 | 
					    // If we have a valid pin, set it as an input and read the (digital) state
 | 
				
			||||||
	if (["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
 | 
					    if (["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
 | 
				
			||||||
		 "P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
 | 
					         "P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
 | 
				
			||||||
		 "P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
 | 
					         "P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
 | 
				
			||||||
		 "P9_27", "P9_30", "P9_41", "P9_42"].indexOf(node.pin) >= 0) {
 | 
					         "P9_27", "P9_30", "P9_41", "P9_42"].indexOf(node.pin) >= 0) {
 | 
				
			||||||
		bs.pinMode(node.pin, bs.INPUT);
 | 
					        setTimeout(function () {
 | 
				
			||||||
		bs.digitalRead(node.pin, function (x) {
 | 
								bs.pinMode(node.pin, bs.INPUT);
 | 
				
			||||||
				// Initialise the currentState and lastActveTime variables based on the value read
 | 
								bs.digitalRead(node.pin, function (x) {
 | 
				
			||||||
				node.currentState = x.value;
 | 
											// Initialise the currentState and lastActveTime variables based on the value read
 | 
				
			||||||
				node.error("First read - currentState: " + node.currentState);
 | 
											node.log("digitalRead: x.value = " + x.value);
 | 
				
			||||||
				if (node.currentState == node.activeState) {
 | 
											node.log("digitalRead: node.currentState = " + node.currentState);
 | 
				
			||||||
					node.lastActiveTime = Date.now();
 | 
											node.log("digitalRead: node.totalActiveTime = " + node.totalActiveTime);
 | 
				
			||||||
				}
 | 
											node.log("digitalRead: node.lastActiveTime = " + node.lastActiveTime);
 | 
				
			||||||
				// Attempt to attach a change-of-state interrupt handler to the pin. If we succeed,
 | 
											node.currentState = x.value - 0;
 | 
				
			||||||
				// set the input event and interval handlers, then send an initial message with the
 | 
											node.log("First read - currentState: " + node.currentState);
 | 
				
			||||||
				// pin state on the first output
 | 
											if (node.currentState === node.activeState) {
 | 
				
			||||||
				if (bs.attachInterrupt(node.pin, true, bs.CHANGE, interruptCallback)) {
 | 
												node.lastActiveTime = Date.now();
 | 
				
			||||||
					node.interruptAttached = true;
 | 
											}
 | 
				
			||||||
					node.on("input", inputCallback);
 | 
											// Attempt to attach a change-of-state interrupt handler to the pin. If we succeed,
 | 
				
			||||||
					node.intervalId = setInterval(timerCallback, node.updateInterval);
 | 
											// set the input event and interval handlers, then send an initial message with the
 | 
				
			||||||
				} else {
 | 
											// pin state on the first output
 | 
				
			||||||
					node.error("Failed to attach interrupt");
 | 
											if (bs.attachInterrupt(node.pin, true, bs.CHANGE, interruptCallback)) {
 | 
				
			||||||
				}
 | 
												node.interruptAttached = true;
 | 
				
			||||||
				setTimeout(function () { node.emit("input", {}); }, 50);
 | 
												node.on("input", inputCallback);
 | 
				
			||||||
			});
 | 
												node.intervalId = setInterval(timerCallback, node.updateInterval);
 | 
				
			||||||
	} else {
 | 
											} else {
 | 
				
			||||||
		node.error("Unconfigured input pin");
 | 
												node.error("Failed to attach interrupt");
 | 
				
			||||||
	}
 | 
											}
 | 
				
			||||||
 | 
											setTimeout(function () { node.emit("input", {}); }, 50);
 | 
				
			||||||
 | 
										});
 | 
				
			||||||
 | 
									}, 50);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        node.error("Unconfigured input pin");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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("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 attached one) and clear the interval (if we set one)
 | 
				
			||||||
DiscreteInputNode.prototype.close = function () {
 | 
					DiscreteInputNode.prototype.close = function () {
 | 
				
			||||||
	if (this.interruptAttached) {
 | 
					    if (this.interruptAttached) {
 | 
				
			||||||
		bs.detachInterrupt(this.pin);
 | 
					        bs.detachInterrupt(this.pin);
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
	if (this.intervalId != null) {
 | 
					    if (this.intervalId !== null) {
 | 
				
			||||||
		clearInterval(this.intervalId);
 | 
					        clearInterval(this.intervalId);
 | 
				
			||||||
	}
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user