Created
October 17, 2017 12:38
-
-
Save eschava/1951755d302bd5dc072a65c880f33362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(RED) { | |
function HysteresisNode(config) { | |
RED.nodes.createNode(this, config); | |
var node = this; | |
this.on('input', function(msg) { | |
if (msg.hasOwnProperty('payload')) { | |
var this_value = Number(msg.payload); | |
if (! isNaN(this_value)) { | |
var last_value = Number(node.last_value); | |
var last_edge = node.last_edge; | |
if (! isNaN(last_value)) { | |
if (this_value > last_value && | |
this_value > config.rising_threshold && | |
last_value <= config.rising_threshold && | |
last_edge != 'rising') { | |
msg.edge = 'rising'; | |
node.send(msg); | |
node.last_edge = 'rising'; | |
} | |
if (this_value < last_value && | |
this_value < config.falling_threshold && | |
last_value >= config.falling_threshold && | |
last_edge != 'falling') { | |
msg.edge = 'falling'; | |
node.send(msg); | |
node.last_edge = 'falling'; | |
} | |
} | |
node.last_value = this_value; | |
} | |
} | |
}); | |
} | |
RED.nodes.registerType('hysteresis', HysteresisNode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment