-
Notifications
You must be signed in to change notification settings - Fork 1
/
pins.js
165 lines (147 loc) · 3.52 KB
/
pins.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
const async = require('async')
const fs = require('fs')
const path = require('path')
const Epoll = require('epoll').Epoll
const EventEmitter = require('events')
class Pins extends EventEmitter {
constructor (pinNumbers, basePath = '/sys/class/gpio') {
super()
var self = this
self._pins = {}
self._fds = {}
self._poller = null
self.ready = false
async.parallel(Object.keys(pinNumbers).map(function (pinNum) {
const config = pinNumbers[pinNum]
const dirIn = !!config.in
const edge = config.edge ?? 'none'
const edgeCb = config.change
var pin = self._pins[pinNum] = {
in: dirIn
}
return function (cb) {
var pindir = path.join(basePath, `gpio${pinNum}`)
async.series([
function (cb) {
fs.access(pindir, function (err) {
if (err)
fs.writeFile(path.join(basePath, 'export'), pinNum, cb)
else
cb()
})
},
function (cb) {
fs.writeFile(path.join(pindir, 'direction'), dirIn ? 'in' : 'out', cb)
},
function (cb) {
fs.writeFile(path.join(pindir, 'edge'), edge, cb)
},
function (cb) {
fs.open(path.join(pindir, 'value'), 'r+', function (err, fd) {
if (err) return cb(err)
pin.fd = fd
self._fds[fd] = parseInt(pinNum)
if (edge !== 'none') {
self._createPoller()
// prevent initial interrupt
self._read(fd, function (err) {
if (err) return cb(err)
self._poller.add(fd, Epoll.EPOLLPRI)
cb()
})
} else {
cb()
}
})
},
function (cb) {
if (!dirIn)
fs.writeFile(path.join(pindir, 'value'), '0', cb)
else
cb()
}
], cb)
}
}), function (err) {
if (err) {
self.emit('error', err)
} else {
self.ready = true
self.emit('ready')
}
})
}
_createPoller () {
var self = this
// To disable for non-linux testing
// self._poller = {
// add: () => {}
// }
if (self._poller)
return
self._poller = new Epoll(function (err, fd, events) {
if (err) return self.emit('error', err)
var pinNum = self._fds[fd]
if (pinNum === undefined) return self.emit('error', new Error('unexpected pin interrupt'))
// clear interrupt
self.get(pinNum, function (err, val) {
if (err) return self.emit('error', err)
self.emit('edge', pinNum, val)
})
})
}
set (pin, value, cb) {
var self = this
if (!self._pins[pin] || self._pins[pin].in) {
process.nextTick(function () {
cb(new Error('pin not configured: ' + pin))
})
return
}
if (!self.ready) {
self.once('ready', self.set.bind(self, pin, value, cb))
return
}
var buf
if (value) {
buf = Buffer.from('1')
} else {
buf = Buffer.from('0')
}
cb = cb ?? function () {}
fs.write(self._pins[pin].fd, buf, 0, 1, 0, cb)
}
get (pin, cb) {
var self = this
if (!self._pins[pin]) {
process.nextTick(function () {
cb(new Error('pin not configured: ' + pin))
})
return
}
if (!self.ready) {
self.once('ready', self.get.bind(self, pin, cb))
return
}
self._read(self._pins[pin].fd, function (err, buf) {
if (err) return cb(err)
const str = buf.toString()
if (str === '0') {
cb(null, false)
} else if (str === '1') {
cb(null, true)
} else {
cb(new Error('unknown value'))
}
})
}
_read (fd, cb) {
var buf = Buffer.alloc(1)
fs.read(fd, buf, 0, 1, 0, function (err, bytesRead) {
if (err) return cb(err)
if (bytesRead !== 1) return cb(new Error('failed to read'))
cb(null, buf)
})
}
}
module.exports = Pins