129 lines
3.0 KiB
JavaScript
129 lines
3.0 KiB
JavaScript
"use strict";
|
|
|
|
const fs = require('mz/fs'),
|
|
EventEmitter = require('events'),
|
|
Epoll = require('epoll').Epoll,
|
|
_ = require('uci-utils')
|
|
|
|
const pWaitFor = require('p-wait-for')
|
|
const pathExists = require('path-exists');
|
|
const debounce = require('debounce');
|
|
|
|
const GPIO_ROOT_PATH = '/sys/class/gpio/'
|
|
|
|
class Interrupt extends EventEmitter {
|
|
// bus is i2c-bus bus object
|
|
constructor(pin_number, opts = {}) {
|
|
super()
|
|
this.num = pin_number;
|
|
this.hook = opts.hook // will be passed back with the emit
|
|
this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/'
|
|
this.edge = opts.edge
|
|
this.delay = opts.delay
|
|
this.dbt = opts.debounce ? opts.debounce : 300
|
|
this.poller = new Epoll(
|
|
debounce((err, fd, events) => {
|
|
if (err) { this.emit('error', err) } else { this.clear().then(this.emit('fired', this.hook)) }
|
|
}, this.dbt, true)
|
|
)
|
|
}
|
|
|
|
init() {
|
|
|
|
process.on('SIGINT', () => {
|
|
this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c
|
|
})
|
|
|
|
return _.pSeries([
|
|
this.exit(),
|
|
this._export(this.num),
|
|
this._direction('in', this.delay),
|
|
() => this._edge(this.edge),
|
|
() => this._fd(),
|
|
() => this.clear().then(() => {
|
|
this.start()
|
|
return Promise.resolve('cleared and started')
|
|
})
|
|
])
|
|
|
|
}
|
|
|
|
_test(num) {
|
|
return Promise.resolve(num)
|
|
}
|
|
|
|
fire(name = 'fired') {
|
|
this.emit(name, this.hook)
|
|
}
|
|
|
|
clear() {
|
|
let buffer = Buffer.from([0x00])
|
|
return fs.read(this.valuefd, buffer, 0, 1, 0);
|
|
}
|
|
|
|
exit() {
|
|
return pathExists(this.path)
|
|
.then((exists) => {
|
|
// console.log('exists', exists)
|
|
if (exists) {
|
|
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
|
|
.then(() => {
|
|
if (this.valuefd) {
|
|
this.stop()
|
|
return fs.close(this.valuefd)
|
|
}
|
|
return Promise.resolve('unexported - exited')
|
|
})
|
|
}
|
|
return Promise.resolve('already unexported')
|
|
})
|
|
}
|
|
|
|
start() {
|
|
this.poller.add(this.valuefd, Epoll.EPOLLPRI);
|
|
}
|
|
|
|
stop() {
|
|
this.poller.remove(this.valuefd).close();
|
|
}
|
|
|
|
_fd() {
|
|
return fs.open(this.path + 'value', 'r+')
|
|
.then(fd => {
|
|
this.valuefd = fd
|
|
return Promise.resolve('value fd set')
|
|
})
|
|
}
|
|
|
|
_export(num) {
|
|
return new Promise(function (resolve, reject) {
|
|
fs.writeFile(GPIO_ROOT_PATH + 'export', num)
|
|
.then(() => { return resolve('exported') })
|
|
.catch((err) => { return reject(err) })
|
|
})
|
|
}
|
|
|
|
_direction(dir = 'in', delay = 50) {
|
|
return pWaitFor(() => pathExists(this.path + 'direction'))
|
|
.then(_.pDelay(delay))
|
|
.then(() => {
|
|
return fs.writeFile(dir + 'direction', dir)
|
|
.then(() => {
|
|
return Promise.resolve('direction set')
|
|
})
|
|
})
|
|
}
|
|
|
|
_edge(edge = 'falling') {
|
|
return fs.writeFile(this.path + 'edge', edge)
|
|
.then(() => {
|
|
return Promise.resolve('edge set')
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
Interrupt
|
|
}
|