2015-09-21 18:03:50 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
var m = require('mraa');
|
|
|
|
//console.log("BOARD :",m.getPlatformName());
|
|
|
|
|
|
|
|
function gpioDin(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.pin = n.pin;
|
|
|
|
this.interrupt = n.interrupt;
|
2022-07-12 11:10:49 +02:00
|
|
|
this.mode = n.mode;
|
|
|
|
this.initialMsg = n.initial;
|
2015-09-21 18:03:50 +02:00
|
|
|
this.x = new m.Gpio(parseInt(this.pin));
|
|
|
|
this.board = m.getPlatformName();
|
2022-07-12 11:10:49 +02:00
|
|
|
this.defaultTimeout = 100;
|
2015-09-21 18:03:50 +02:00
|
|
|
var node = this;
|
2022-07-12 11:10:49 +02:00
|
|
|
node.x.mode(parseInt(this.mode));
|
2015-09-21 18:03:50 +02:00
|
|
|
node.x.dir(m.DIR_IN);
|
2022-07-12 11:10:49 +02:00
|
|
|
|
|
|
|
var eventHandler = function(g) {
|
2015-09-21 18:03:50 +02:00
|
|
|
var msg = { payload:g, topic:node.board+"/D"+node.pin };
|
|
|
|
switch (g) {
|
2017-01-29 18:45:44 +01:00
|
|
|
case 0: {
|
2015-09-21 18:03:50 +02:00
|
|
|
node.status({fill:"green",shape:"ring",text:"low"});
|
|
|
|
if (node.interrupt=== "f" || node.interrupt === "b") {
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
break;
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
case 1: {
|
2015-09-21 18:03:50 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"high"});
|
|
|
|
if (node.interrupt=== "r" || node.interrupt === "b") {
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
break;
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
default: {
|
2015-09-21 18:03:50 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:"unknown"});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
2015-09-21 18:03:50 +02:00
|
|
|
}
|
2022-07-12 11:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var isrCallback = function() {
|
|
|
|
eventHandler(node.x.read());
|
|
|
|
}
|
|
|
|
|
|
|
|
node.x.isr(m.EDGE_BOTH, isrCallback);
|
|
|
|
var initialState = node.x.read();
|
|
|
|
switch (initialState) {
|
2017-01-29 18:45:44 +01:00
|
|
|
case 0: {
|
2015-09-21 18:03:50 +02:00
|
|
|
node.status({fill:"green",shape:"ring",text:"low"});
|
|
|
|
break;
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
case 1: {
|
2015-09-21 18:03:50 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"high"});
|
|
|
|
break;
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
default: {
|
2015-09-21 18:03:50 +02:00
|
|
|
node.status({});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
2015-09-21 18:03:50 +02:00
|
|
|
}
|
2022-07-12 11:10:49 +02:00
|
|
|
|
|
|
|
if (this.initialMsg) {
|
|
|
|
setTimeout(() => {
|
|
|
|
node.send( { payload: node.x.read(), topic:node.board+"/D"+node.pin } );
|
|
|
|
}, this.defaultTimeout);
|
|
|
|
}
|
|
|
|
|
2015-09-21 18:03:50 +02:00
|
|
|
this.on('close', function() {
|
|
|
|
node.x.isr(m.EDGE_BOTH, null);
|
2022-07-12 11:10:49 +02:00
|
|
|
node.x.isrExit();
|
|
|
|
node.x.close();
|
2015-09-21 18:03:50 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("mraa-gpio-din", gpioDin);
|
|
|
|
}
|