diff --git a/hardware/intel/mraa-gpio-din.html b/hardware/intel/mraa-gpio-din.html index fdbcfc45..88e256bd 100644 --- a/hardware/intel/mraa-gpio-din.html +++ b/hardware/intel/mraa-gpio-din.html @@ -1,13 +1,15 @@ diff --git a/hardware/intel/mraa-gpio-din.js b/hardware/intel/mraa-gpio-din.js index 5966e448..cfb79612 100644 --- a/hardware/intel/mraa-gpio-din.js +++ b/hardware/intel/mraa-gpio-din.js @@ -7,13 +7,16 @@ module.exports = function(RED) { RED.nodes.createNode(this,n); this.pin = n.pin; this.interrupt = n.interrupt; + this.mode = n.mode; + this.initialMsg = n.initial; this.x = new m.Gpio(parseInt(this.pin)); this.board = m.getPlatformName(); + this.defaultTimeout = 100; var node = this; - node.x.mode(m.PIN_GPIO); + node.x.mode(parseInt(this.mode)); node.x.dir(m.DIR_IN); - node.x.isr(m.EDGE_BOTH, function() { - var g = node.x.read(); + + var eventHandler = function(g) { var msg = { payload:g, topic:node.board+"/D"+node.pin }; switch (g) { case 0: { @@ -34,8 +37,15 @@ module.exports = function(RED) { node.status({fill:"grey",shape:"ring",text:"unknown"}); } } - }); - switch (node.x.read()) { + } + + var isrCallback = function() { + eventHandler(node.x.read()); + } + + node.x.isr(m.EDGE_BOTH, isrCallback); + var initialState = node.x.read(); + switch (initialState) { case 0: { node.status({fill:"green",shape:"ring",text:"low"}); break; @@ -48,8 +58,17 @@ module.exports = function(RED) { node.status({}); } } + + if (this.initialMsg) { + setTimeout(() => { + node.send( { payload: node.x.read(), topic:node.board+"/D"+node.pin } ); + }, this.defaultTimeout); + } + this.on('close', function() { node.x.isr(m.EDGE_BOTH, null); + node.x.isrExit(); + node.x.close(); }); } RED.nodes.registerType("mraa-gpio-din", gpioDin);