-
Notifications
You must be signed in to change notification settings - Fork 591
/
38-rpi-pibrella.js
223 lines (195 loc) · 7.64 KB
/
38-rpi-pibrella.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module.exports = function(RED) {
"use strict";
var util = require("util");
var spawn = require('child_process').spawn;
var fs = require('fs');
var gpioCommand = __dirname+'/nrgpio';
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
//util.log("Info : Ignoring Raspberry Pibrella specific node.");
throw "Info : Ignoring Raspberry Pibrella specific node.";
}
if (!fs.existsSync("/usr/share/doc/python3-rpi.gpio")) {
util.log("[rpi-pibrella] Info : Can't find RPi.GPIO python library.");
throw "Warning : Can't find RPi.GPIO python library.";
}
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
util.log("[rpi-pibrella] Error : "+gpioCommand+" needs to be executable.");
throw "Error : " + gpioCommand + " must to be executable.";
}
var pinsInUse = {};
var pinTypes = {"out":"digital output", "tri":"input", "up":"input with pull up", "down":"input with pull down", "pwm":"PWM output"};
var pintable = {
// Name : Pin
"Amber LED":"11",
"Buzzer ":"12",
"Red LED":"13",
"Out E":"15",
"Out F":"16",
"Out G":"18",
"Out H":"22",
"Green LED":"7",
"In D":"19",
"In A":"21",
"In B":"26",
"In C":"24",
"Red Button":"23"
}
var tablepin = {
// Pin : Name
"11":"Amber",
"12":"Buzzer",
"13":"Red",
"15":"E",
"16":"F",
"18":"G",
"22":"H",
"7":"Green",
"19":"D",
"21":"A",
"26":"B",
"24":"C",
"23":"R"
}
function PibrellaIn(n) {
RED.nodes.createNode(this,n);
this.buttonState = -1;
this.pin = pintable[n.pin];
this.read = n.read || false;
if (this.read) { this.buttonState = -2; }
var node = this;
if (!pinsInUse.hasOwnProperty(this.pin)) {
pinsInUse[this.pin] = "tri";
}
else {
if ((pinsInUse[this.pin] !== "tri")||(pinsInUse[this.pin] === "pwm")) {
node.error("GPIO pin "+this.pin+" already set as "+pinTypes[pinsInUse[this.pin]]);
}
}
if (node.pin !== undefined) {
node.child = spawn(gpioCommand, ["in",node.pin,"down",35]);
node.running = true;
node.status({fill:"green",shape:"dot",text:"OK"});
node.child.stdout.on('data', function (data) {
data = data.toString().trim();
if (data.length > 0) {
if (node.buttonState !== -1) {
node.send({ topic:"pibrella/"+tablepin[node.pin], payload:Number(data) });
}
node.buttonState = data;
node.status({fill:"green",shape:"dot",text:data});
if (RED.settings.verbose) { node.log("out: "+data+" :"); }
}
});
node.child.stderr.on('data', function (data) {
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
});
node.child.on('close', function (code) {
if (RED.settings.verbose) { node.log("ret: "+code+" :"); }
node.child = null;
node.running = false;
node.status({fill:"red",shape:"circle",text:""});
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
});
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
if (node.child != null) {
node.child.stdin.write(" close "+node.pin);
node.child.kill('SIGKILL');
}
node.status({fill:"red",shape:"circle",text:""});
delete pinsInUse[node.pin];
if (RED.settings.verbose) { node.log("end"); }
});
}
RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
function PibrellaOut(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
this.set = n.set || false;
this.level = n.level || 0;
this.out = n.out || "out";
var node = this;
if (!pinsInUse.hasOwnProperty(this.pin)) {
pinsInUse[this.pin] = this.out;
}
else {
if ((pinsInUse[this.pin] !== this.out)||(pinsInUse[this.pin] === "pwm")) {
node.error("GPIO pin "+this.pin+" already set as "+pinTypes[pinsInUse[this.pin]]);
}
}
function inputlistener(msg) {
if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; }
var out = Number(msg.payload);
var limit = 1;
if (node.out === "pwm") { limit = 100; }
if (node.pin === "12") {
limit = 4096;
if (out === 1) { out = 262; }
}
if ((out >= 0) && (out <= limit)) {
if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
else { node.warn("Command not running"); }
node.status({fill:"green",shape:"dot",text:msg.payload});
}
else { node.warn("Invalid input: "+out); }
}
if (node.pin !== undefined) {
if (node.pin === "12") {
node.child = spawn(gpioCommand, ["buzz",node.pin]);
}
else {
if (node.set && (node.out === "out")) {
node.child = spawn(gpioCommand, [node.out,node.pin,node.level]);
}
else {
node.child = spawn(gpioCommand, [node.out,node.pin]);
}
}
node.running = true;
node.status({fill:"green",shape:"dot",text:"OK"});
node.on("input", inputlistener);
node.child.stdout.on('data', function (data) {
if (RED.settings.verbose) { node.log("out: "+data+" :"); }
});
node.child.stderr.on('data', function (data) {
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
});
node.child.on('close', function (code) {
if (RED.settings.verbose) { node.log("ret: "+code+" :"); }
node.child = null;
node.running = false;
node.status({fill:"red",shape:"circle",text:""});
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
});
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
if (node.child != null) {
node.child.stdin.write(" close "+node.pin);
node.child.kill('SIGKILL');
}
node.status({fill:"red",shape:"circle",text:""});
delete pinsInUse[node.pin];
if (RED.settings.verbose) { node.log("end"); }
});
}
RED.nodes.registerType("rpi-pibrella out",PibrellaOut);
RED.httpAdmin.get('/rpi-pibpins/:id',RED.auth.needsPermission('rpi-pibrella.read'),function(req,res) {
res.json(pinsInUse);
});
}