-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio-events.js
47 lines (37 loc) · 1.22 KB
/
gpio-events.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
const events = require('events')
const Gpio = require('onoff').Gpio
const gpioActionsMap = require('./settings.json').gpio
const gpioEvent = new events.EventEmitter()
let timer
Object.keys(gpioActionsMap).forEach(gpioNumber => {
let ignoreNext = false
const button = new Gpio(gpioNumber, 'in', 'both', { debounceTimeout: 10 })
button.watch((error, value) => {
// Ignore event immediately after 'hold' event was emitted
if (ignoreNext) {
ignoreNext = false
return
}
if (value === 1) {
// Emit button 'hold' event if pressed for 5 seconds
if (gpioActionsMap[gpioNumber].hold) {
timer = setTimeout(() => {
gpioEvent.emit(gpioActionsMap[gpioNumber].hold)
ignoreNext = true
}, 5000)
}
return
}
if (value === 0) {
// Reset timer on release
if (timer) {
clearTimeout(timer)
}
// Emit 'push' event for button
if (gpioActionsMap[gpioNumber].push) {
gpioEvent.emit(gpioActionsMap[gpioNumber].push)
}
}
})
})
module.exports = gpioEvent