forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mraa-gpio-ain.js
32 lines (29 loc) · 926 Bytes
/
mraa-gpio-ain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module.exports = function(RED) {
var m = require('mraa');
function gpioAin(n) {
RED.nodes.createNode(this, n);
this.pin = n.pin;
this.interval = n.interval;
this.x = new m.Aio(parseInt(this.pin));
this.board = m.getPlatformName();
var node = this;
var msg = { topic:node.board+"/A"+node.pin };
var old = -99999;
// ADC set to 12 for IOT2050
if (this.board === "SIMATIC IOT2050") {
node.x.setBit(12);
}
this.timer = setInterval(function() {
msg.payload = node.x.read();
if (msg.payload !== old) {
node.send(msg);
old = msg.payload;
}
}, node.interval);
this.on('close', function() {
clearInterval(this.timer);
node.x.close();
});
}
RED.nodes.registerType("mraa-gpio-ain", gpioAin);
}