forked from rakeshpai/pi-gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi-gpio.js
128 lines (101 loc) · 2.98 KB
/
pi-gpio.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
"use strict";
var fs = require("fs"),
path = require("path"),
exec = require("child_process").exec;
var gpioAdmin = "gpio-admin",
sysFsPath = "/sys/devices/virtual/gpio";
var pinMapping = {
"3": 0,
"5": 1,
"7": 4,
"8": 14,
"10": 15,
"11": 17,
"12": 18,
"13": 21,
"15": 22,
"16": 23,
"18": 24,
"19": 10,
"21": 9,
"22": 25,
"23": 11,
"24": 8,
"26": 7
};
function isNumber(number) {
return !isNaN(parseInt(number, 10));
}
function noop(){}
function handleExecResponse(method, pinNumber, callback) {
return function(err, stdout, stderr) {
if(err) {
console.error("Error when trying to", method, "pin", pinNumber);
console.error(stderr);
callback(err);
} else {
callback();
}
}
}
function sanitizePinNumber(pinNumber) {
if(!isNumber(pinNumber) || !isNumber(pinMapping[pinNumber])) {
throw new Error("Pin number isn't valid");
}
return parseInt(pinNumber, 10);
}
function sanitizeDirection(direction) {
direction = (direction || "").toLowerCase().trim();
if(direction === "in" || direction === "input") {
return "in";
} else if(direction === "out" || direction === "output" || !direction) {
return "out";
} else {
throw new Error("Direction must be 'input' or 'output'");
}
}
var gpio = {
open: function(pinNumber, direction, callback) {
pinNumber = sanitizePinNumber(pinNumber);
if(!callback && typeof direction === "function") {
callback = direction;
direction = "out";
}
direction = sanitizeDirection(direction);
exec(gpioAdmin + " export " + pinMapping[pinNumber], handleExecResponse("open", pinNumber, function(err) {
if(err) return (callback || noop)(err);
gpio.setDirection(pinNumber, direction, callback);
}));
},
setDirection: function(pinNumber, direction, callback) {
pinNumber = sanitizePinNumber(pinNumber);
direction = sanitizeDirection(direction);
fs.writeFile(sysFsPath + "/gpio" + pinMapping[pinNumber] + "/direction", direction, callback);
},
getDirection: function(pinNumber, callback) {
pinNumber = sanitizePinNumber(pinNumber);
fs.readFile(sysFsPath + "/gpio" + pinMapping[pinNumber] + "/direction", "utf8", function(err, direction) {
if(err) return callback(err);
callback(null, sanitizeDirection(direction.trim()));
});
},
close: function(pinNumber, callback) {
pinNumber = sanitizePinNumber(pinNumber);
exec(gpioAdmin + " unexport " + pinMapping[pinNumber], handleExecResponse("close", pinNumber, callback || noop));
},
read: function(pinNumber, callback) {
pinNumber = sanitizePinNumber(pinNumber);
fs.readFile(sysFsPath + "/gpio" + pinMapping[pinNumber] + "/value", function(err, data) {
if(err) return (callback || noop)(err);
(callback || noop)(null, parseInt(data, 10));
});
},
write: function(pinNumber, value, callback) {
pinNumber = sanitizePinNumber(pinNumber);
value = !!value?"1":"0";
fs.writeFile(sysFsPath + "/gpio" + pinMapping[pinNumber] + "/value", value, "utf8", callback);
}
};
gpio.export = gpio.open;
gpio.unexport = gpio.close;
module.exports = gpio;