-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAO.js
153 lines (149 loc) · 6.27 KB
/
AO.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module.exports = function(RED) {
const AO1OnPID ='/sys/bus/iio/devices/iio:device0/out_voltage1_powerdown'
const AO1PID = '/sys/bus/iio/devices/iio:device0/out_voltage1_raw'
const AO2OnPID = '/sys/bus/iio/devices/iio:device1/out_voltage2_powerdown'
const AO2PID = '/sys/bus/iio/devices/iio:device1/out_voltage2_raw'
// Set Analog Output 1
function setAO1(config) {
RED.nodes.createNode(this,config);
var node = this;
var volt;
var raw;
const on = 0;
const fs = require('fs');
node.on('input', function(msg) {
// Calculate Volt to RawData
if (msg.payload >= 0 && msg.payload <= 10)
{
volt = msg.payload;
raw = volt * 335;
raw = Math.round(raw);
// Switch AO on
const fs = require('fs');
fs.writeFile(AO1OnPID, String(on), function(err) {
if(err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}else {
console.log("AO 1 turned on was successful.");
// Write the Analog Output 1 to file
const fs = require('fs');
fs.writeFile(AO1PID, String(raw), function(err) {
if(err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}else {
console.log("Write AO 1 on CC100 was successful.");
console.log("Set RawData=" + raw);
node.status({fill:"green",shape:"ring",text:"OK"});
}
});
}
});
node.send(msg);
}else
{
console.log("Value not allowed, >=0 and <=10.");
node.status({fill:"red",shape:"ring",text:"Failed"});
}
});
}
RED.nodes.registerType("Set-AO1",setAO1);
// Set Analog Output 2
function setAO2(config) {
RED.nodes.createNode(this,config);
var node = this;
var volt;
var raw;
const on = 0;
const fs = require('fs');
node.on('input', function(msg) {
// Calculate Volt to RawData
if (msg.payload >= 0 && msg.payload <= 10)
{
volt = msg.payload;
raw = volt * 335;
raw = Math.round(raw);
// Switch AO on
const fs = require('fs');
fs.writeFile(AO2OnPID, String(on), function(err) {
if(err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}else {
console.log("AO 2 turned on was successful.");
// Write the Analog Output 1 to file
const fs = require('fs');
fs.writeFile(AO2PID, String(raw), function(err) {
if(err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}else {
console.log("Write AO 2 on CC100 was successful.");
console.log("Set RawData=" + raw);
node.status({fill:"green",shape:"ring",text:"OK"});
}
});
}
});
node.send(msg);
}else
{
console.log("Value not allowed, >=0 and <=10.");
node.status({fill:"red",shape:"ring",text:"Failed"});
}
});
}
RED.nodes.registerType("Set-AO2",setAO2);
function readAO(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
const fs = require('fs');
var read1 = false;
var read2 = false;
var msg1 = {};
var msg2 = {};
var numb;
fs.readFile(AO1PID,function(err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}
else {
read1 = true;
numb = data / 335.0;
numb = numb.toFixed(2);
msg1.payload = Number(numb);
console.log("Read AO 1 on CC100 was successful.");
console.log("Raw Data: " + data.toString());
}
fs.readFile(AO2PID,function(err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
}
else {
read2 = true;
numb = data / 335.0;
numb = numb.toFixed(2);
msg2.payload = Number(numb);
console.log("Read AO 2 on CC100 was successful.");
console.log("Raw Data: " + data.toString());
}
if (read1 === true && read2 === true) {
node.status({fill:"green",shape:"ring",text:"OK"});
console.log("Scaled Values, AO1: " + msg1.payload + " AO2: " + msg2.payload);
node.send([msg1,msg2]);
}
else
{
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log("Read of AO's on CC100 failed.");
}
});
});
});
}
RED.nodes.registerType("Read-AO",readAO);
};