forked from rakeshpai/pi-gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi-gpio.js
191 lines (151 loc) · 4.28 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
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
"use strict";
var fs = require("fs"),
path = require("path"),
exec = require("child_process").exec;
var gpioAdmin = "gpio-admin",
sysFsPathOld = "/sys/devices/virtual/gpio", // pre 3.18.x kernel
sysFsPathNew = "/sys/class/gpio", // post 3.18.x kernel
sysFsPath;
var rev = fs.readFileSync("/proc/cpuinfo").toString().split("\n").filter(function(line) {
return line.indexOf("Revision") == 0;
})[0].split(":")[1].trim();
// tests the device tree directory to determine the actual gpio path
if (fs.existsSync(sysFsPathNew)) {
sysFsPath = sysFsPathNew;
} else {
sysFsPath = sysFsPathOld; // fallback for old kernels
}
rev = parseInt(rev, 16) < 3 ? 1 : 2; // http://elinux.org/RPi_HardwareHistory#Board_Revision_History
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,
// Model A+ and Model B+ pins
"29": 5,
"31": 6,
"32": 12,
"33": 13,
"35": 19,
"36": 16,
"37": 26,
"38": 20,
"40": 21
};
if (rev == 2) {
pinMapping["3"] = 2;
pinMapping["5"] = 3;
pinMapping["13"] = 27;
}
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'");
}
}
function sanitizeOptions(options) {
var sanitized = {};
options.split(" ").forEach(function(token) {
if (token == "in" || token == "input") {
sanitized.direction = "in";
}
if (token == "pullup" || token == "up") {
sanitized.pull = "pullup";
}
if (token == "pulldown" || token == "down") {
sanitized.pull = "pulldown";
}
});
if (!sanitized.direction) {
sanitized.direction = "out";
}
if (!sanitized.pull) {
sanitized.pull = "";
}
return sanitized;
}
var gpio = {
rev: rev,
open: function(pinNumber, options, callback) {
pinNumber = sanitizePinNumber(pinNumber);
if (!callback && typeof options === "function") {
callback = options;
options = "out";
}
options = sanitizeOptions(options);
exec(gpioAdmin + " export " + pinMapping[pinNumber] + " " + options.pull, handleExecResponse("open", pinNumber, function(err) {
if (err) return (callback || noop)(err);
gpio.setDirection(pinNumber, options.direction, callback);
}));
},
setDirection: function(pinNumber, direction, callback) {
pinNumber = sanitizePinNumber(pinNumber);
direction = sanitizeDirection(direction);
fs.writeFile(sysFsPath + "/gpio" + pinMapping[pinNumber] + "/direction", direction, (callback || noop));
},
getDirection: function(pinNumber, callback) {
pinNumber = sanitizePinNumber(pinNumber);
callback = callback || noop;
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;