-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (40 loc) · 1.01 KB
/
index.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
var gpio = require("rpi-gpio");
exports.init = function(node, app_config, main, host_info) {
if (typeof app_config.pin !== "number") {
throw new Error("gpio: pin not defined.");
}
var invert = !!app_config.invert;
var pin = app_config.pin;
var mode = gpio.DIR_OUT;
var value = 0;
if (typeof app_config.initial_value === "number") {
if (app_config.initial_value ^ invert) {
mode = gpio.DIR_HIGH;
value = 1;
} else {
mode = gpio.DIR_LOW;
}
}
gpio.setup(pin, mode, function(err) {
if (err) throw err;
});
node.publish(undefined, value);
node.rpc_set = function(reply, value, time) {
gpio.write(pin, value ^ invert, function(err) {
if (err) return reply(err);
node.publish(time, value);
reply(null, "okay");
});
};
node.rpc_publish = function(reply, time, value) {
return this.rpc_set(reply, value, time);
};
node.announce([{
"type": "output.state"
}, app_config.metadata]);
return [node, function() {
gpio.destroy(function(err) {
if (err) console.error(err);
});
}];
};