added custom pullup command line option with RPI as default
parent
d8172b092b
commit
19ea1f88ff
|
@ -1,6 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const fs = require('mz/fs'),
|
const fs = require('mz/fs'),
|
||||||
|
exec = require('mz/child_process').exec,
|
||||||
EventEmitter = require('events'),
|
EventEmitter = require('events'),
|
||||||
Epoll = require('epoll').Epoll,
|
Epoll = require('epoll').Epoll,
|
||||||
_ = require('uci-utils')
|
_ = require('uci-utils')
|
||||||
|
@ -18,8 +19,10 @@ class Interrupt extends EventEmitter {
|
||||||
this.num = pin_number;
|
this.num = pin_number;
|
||||||
this.hook = opts.hook // will be passed back with the emit
|
this.hook = opts.hook // will be passed back with the emit
|
||||||
this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/'
|
this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/'
|
||||||
this.edge = opts.edge
|
this.edge = opts.edge // default 'falling'
|
||||||
this.delay = opts.delay
|
this.delay = opts.delay // default = 50
|
||||||
|
this.pullup = opts.pullup ? opts.pullup : (num, state) => { return `raspi-gpio set ${num} ${state}` } // default rpi using raspi-gpio
|
||||||
|
// this.pullup = (num, state) => { return `raspi-gpio set ${num} ${state}` }
|
||||||
this.dbt = opts.debounce ? opts.debounce : 300
|
this.dbt = opts.debounce ? opts.debounce : 300
|
||||||
this.poller = new Epoll(
|
this.poller = new Epoll(
|
||||||
debounce((err, fd, events) => {
|
debounce((err, fd, events) => {
|
||||||
|
@ -31,11 +34,12 @@ class Interrupt extends EventEmitter {
|
||||||
init() {
|
init() {
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c
|
this.exit().then((resp) => console.log("\n", resp)) // unexport on cntrl-c
|
||||||
})
|
})
|
||||||
|
|
||||||
return _.pSeries([
|
return _.pSeries([
|
||||||
this.exit(),
|
this.exit(),
|
||||||
|
this._pull(this.num),
|
||||||
this._export(this.num),
|
this._export(this.num),
|
||||||
this._direction('in', this.delay),
|
this._direction('in', this.delay),
|
||||||
() => this._edge(this.edge),
|
() => this._edge(this.edge),
|
||||||
|
@ -48,9 +52,9 @@ class Interrupt extends EventEmitter {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_test(num) {
|
// _test(num) {
|
||||||
return Promise.resolve(num)
|
// return Promise.resolve(num)
|
||||||
}
|
// }
|
||||||
|
|
||||||
fire(name = 'fired') {
|
fire(name = 'fired') {
|
||||||
this.emit(name, this.hook)
|
this.emit(name, this.hook)
|
||||||
|
@ -62,21 +66,25 @@ class Interrupt extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
exit() {
|
exit() {
|
||||||
return pathExists(this.path)
|
return new Promise((resolve, reject) => {
|
||||||
.then((exists) => {
|
pathExists(this.path)
|
||||||
// console.log('exists', exists)
|
.then((exists) => {
|
||||||
if (exists) {
|
if (exists) {
|
||||||
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
|
fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (this.valuefd) {
|
if (this.valuefd) {
|
||||||
this.stop()
|
this.stop()
|
||||||
return fs.close(this.valuefd)
|
fs.close(this.valuefd)
|
||||||
}
|
}
|
||||||
return Promise.resolve('unexported - exited')
|
this._pull(this.num, 'pn')
|
||||||
})
|
})
|
||||||
}
|
.then(resolve('unexported and exited'))
|
||||||
return Promise.resolve('already unexported')
|
} else {
|
||||||
})
|
resolve('already unexported')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => resolve(err))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
@ -87,6 +95,15 @@ class Interrupt extends EventEmitter {
|
||||||
this.poller.remove(this.valuefd).close();
|
this.poller.remove(this.valuefd).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**************/
|
||||||
|
|
||||||
|
_pull(num, state = 'pu') {
|
||||||
|
if (this.pullup === 'external') { return Promise.resolve('pull must be set externally') } else {
|
||||||
|
return exec(this.pullup(num, state))
|
||||||
|
.then(() => { return Promise.resolve('pull set') })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_fd() {
|
_fd() {
|
||||||
return fs.open(this.path + 'value', 'r+')
|
return fs.open(this.path + 'value', 'r+')
|
||||||
.then(fd => {
|
.then(fd => {
|
||||||
|
|
|
@ -9,7 +9,11 @@ const Interrupt = require('../').Interrupt
|
||||||
let date = new Date(Date.now())
|
let date = new Date(Date.now())
|
||||||
console.log(date.getMinutes(), "\:", date.getSeconds())
|
console.log(date.getMinutes(), "\:", date.getSeconds())
|
||||||
|
|
||||||
let inter17 = new Interrupt(17, { hook: 'a hook to something to do' })
|
let inter17 = new Interrupt(17, {
|
||||||
|
hook: 'a hook to something to do'
|
||||||
|
// pullup: (num, state) => { return `someccommand using num and state` } // if rpi then specify a function that returns a command line line pullup tool using num and state
|
||||||
|
// pullup: 'external' // pullup not set with a command line utility - use alternative method such as device tree overlays
|
||||||
|
})
|
||||||
|
|
||||||
// inter17.on('fired', hook => {
|
// inter17.on('fired', hook => {
|
||||||
// console.log('Listener fired and returned:', hook)
|
// console.log('Listener fired and returned:', hook)
|
||||||
|
@ -21,9 +25,9 @@ inter17.init()
|
||||||
console.log('initialzed, waiting for interrupt to fire')
|
console.log('initialzed, waiting for interrupt to fire')
|
||||||
|
|
||||||
})
|
})
|
||||||
.catch(err => console.log("returned error:", err))
|
.catch(err => console.log("returned error:\n", err))
|
||||||
//
|
|
||||||
// inter17.on('fired', hook => { counter(hook) })
|
inter17.on('fired', hook => { counter(hook) })
|
||||||
|
|
||||||
let count = 0
|
let count = 0
|
||||||
|
|
||||||
|
@ -32,5 +36,4 @@ function counter(hook) {
|
||||||
console.log(`******${count}********`)
|
console.log(`******${count}********`)
|
||||||
console.log(hook)
|
console.log(hook)
|
||||||
console.log(`----------------------`)
|
console.log(`----------------------`)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue