-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAI.js
55 lines (54 loc) · 2.14 KB
/
AI.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module.exports = function(RED) {
const AI1PID = '/sys/bus/iio/devices/iio:device3/in_voltage3_raw'
const AI2PID = '/sys/bus/iio/devices/iio:device3/in_voltage0_raw'
// Read Analog Input 1
function readAI1(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
const fs = require('fs');
fs.readFile(AI1PID,function(err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}
else {
console.log("Read AI 1 on CC100 was successful.");
console.log("Raw Data: " + data.toString());
node.status({fill:"green",shape:"ring",text:"OK"});
var numb = Math.round(data / 560) / 10.0;
numb = numb.toFixed(2);
msg.payload = numb;
msg.payload = Number(msg.payload);
node.send(msg);
}
});
});
}
RED.nodes.registerType("Read-AI1",readAI1);
// Read Analog Input 2
function readAI2(config) {
const fs = require('fs');
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
fs.readFile(AI2PID,function(err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}
else {
console.log("Read AI 2 on CC100 was successful.");
console.log("Raw Data: " + data.toString());
node.status({fill:"green",shape:"ring",text:"OK"});
var numb = Math.round(data / 560) / 10.0;
numb = numb.toFixed(2);
msg.payload = numb;
msg.payload = Number(msg.payload);
node.send(msg);
}
});
});
}
RED.nodes.registerType("Read-AI2",readAI2);
};