-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
185 lines (158 loc) · 4.48 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
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
'use strict';
var fs = require('fs');
var Epoll = require('epoll').Epoll;
var os = require("os");
var MAPPING;
if (os.release().startsWith("4.4.13-ntc-mlc")) {
MAPPING = [1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020];
} else if (os.release().startsWith("4.4")) {
MAPPING = [1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023];
} else {
MAPPING = [408, 409, 410, 411, 412, 413, 414, 415];
}
var GPIO_ROOT = '/sys/class/gpio/';
function pollerEventHandler(err, fd, events) {
var value = this.read();
var callbacks = this.listeners.slice(0);
if (this.opts.debounceTimeout > 0) {
setTimeout(function () {
if (this.listeners.length > 0) {
this.read();
this.poller.modify(this.valueFd, Epoll.EPOLLPRI | Epoll.EPOLLONESHOT);
}
}.bind(this), this.opts.debounceTimeout);
}
callbacks.forEach(function (callback) {
callback(err, value);
});
}
function Gpio(gpio, direction, activeLow, edge, options) {
var valuePath;
var directionSet = false;
var tries = 0;
if (!(this instanceof Gpio)) {
return new Gpio(gpio, direction, activeLow, edge, options);
}
if (typeof edge === 'object' && !options) {
options = edge;
edge = undefined;
}
options = options || {};
//
// MenloParkInnovcation: If the port specified is greater than the MAPPING
// maximum, take it as a direct map.
//
// This is to allow access to all C.H.I.P. ports.
//
if (gpio == null) {
throw new Error('Invalid GPIO Pin')
}
if (gpio > MAPPING.length) {
this.gpio = gpio;
}
else {
this.gpio = MAPPING[gpio];
}
this.gpioPath = GPIO_ROOT + 'gpio' + this.gpio + '/';
this.opts = {};
this.opts.debounceTimeout = options.debounceTimeout || 0;
this.listeners = [];
this.readBuffer = new Buffer(16);
valuePath = this.gpioPath + 'value';
if (!fs.existsSync(this.gpioPath)) {
fs.writeFileSync(GPIO_ROOT + 'export', this.gpio);
directionSet = false;
while (!directionSet) {
try {
tries += 1;
fs.writeFileSync(this.gpioPath + 'direction', direction);
directionSet = true;
} catch (e) {
if (tries === 10000) {
throw e;
}
}
}
if (activeLow) {
fs.writeFileSync(this.gpioPath + 'active_low', activeLow);
}
if (edge) {
fs.writeFileSync(this.gpioPath + 'edge', edge);
}
} else {
try {
fs.writeFileSync(this.gpioPath + 'direction', direction);
} catch (ignore) {}
try {
if (activeLow) {
fs.writeFileSync(this.gpioPath + 'active_low', activeLow);
}
} catch (ignore) {}
try {
if (edge) {
fs.writeFileSync(this.gpioPath + 'edge', edge);
}
} catch (ignore) {}
}
this.valueFd = fs.openSync(valuePath, 'r+'); // Cache fd for performance.
this.read();
this.poller = new Epoll(pollerEventHandler.bind(this));
};
exports.Gpio = Gpio;
Gpio.prototype.read = function () {
fs.readSync(this.valueFd, this.readBuffer, 0, 1, 0);
var valString = fs.readFileSync(this.gpioPath + 'value', 'utf8');
return parseInt(valString.split('\n')[0]);
//return this.readBuffer[0] === new Buffer('1')[0] ? 1 : 0;
};
Gpio.prototype.write = function (value) {
if (value != 0 && value != 1) {
throw new Error('Invalid Value')
}
return fs.writeFileSync(this.gpioPath + 'value', value);
};
Gpio.prototype.unexport = function () {
this.unwatch();
fs.closeSync(this.valueFd);
fs.writeFileSync(GPIO_ROOT + 'unexport', this.gpio);
};
Gpio.prototype.watch = function (callback) {
var events;
this.listeners.push(callback);
if (this.listeners.length === 1) {
events = Epoll.EPOLLPRI;
if (this.opts.debounceTimeout > 0) {
events |= Epoll.EPOLLONESHOT;
}
this.poller.add(this.valueFd, events);
}
};
Gpio.prototype.unwatch = function (callback) {
if (this.listeners.length > 0) {
if (typeof callback !== 'function') {
this.listeners = [];
} else {
this.listeners = this.listeners.filter(function (listener) {
return callback !== listener;
});
}
if (this.listeners.length === 0) {
this.poller.remove(this.valueFd);
}
}
};
Gpio.prototype.direction = function () {
return fs.readFileSync(this.gpioPath + 'direction').toString().trim();
};
Gpio.prototype.setDirection = function (direction) {
fs.writeFileSync(this.gpioPath + 'direction', direction);
};
Gpio.prototype.edge = function () {
return fs.readFileSync(this.gpioPath + 'edge').toString().trim();
};
Gpio.prototype.setEdge = function (edge) {
fs.writeFileSync(this.gpioPath + 'edge', edge);
};
Gpio.prototype.setActiveLow = function (activeLow) {
fs.writeFileSync(this.gpioPath + 'active_low', activeLow);
};