added custom pullup command line option with RPI as default

master
David Kebler 2017-01-25 11:41:00 -08:00
parent d8172b092b
commit 19ea1f88ff
2 changed files with 46 additions and 26 deletions

View File

@ -1,6 +1,7 @@
"use strict";
const fs = require('mz/fs'),
exec = require('mz/child_process').exec,
EventEmitter = require('events'),
Epoll = require('epoll').Epoll,
_ = require('uci-utils')
@ -18,8 +19,10 @@ class Interrupt extends EventEmitter {
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.edge = opts.edge // default 'falling'
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.poller = new Epoll(
debounce((err, fd, events) => {
@ -31,11 +34,12 @@ class Interrupt extends EventEmitter {
init() {
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([
this.exit(),
this._pull(this.num),
this._export(this.num),
this._direction('in', this.delay),
() => this._edge(this.edge),
@ -48,9 +52,9 @@ class Interrupt extends EventEmitter {
}
_test(num) {
return Promise.resolve(num)
}
// _test(num) {
// return Promise.resolve(num)
// }
fire(name = 'fired') {
this.emit(name, this.hook)
@ -62,21 +66,25 @@ class Interrupt extends EventEmitter {
}
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')
})
return new Promise((resolve, reject) => {
pathExists(this.path)
.then((exists) => {
if (exists) {
fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
.then(() => {
if (this.valuefd) {
this.stop()
fs.close(this.valuefd)
}
this._pull(this.num, 'pn')
})
.then(resolve('unexported and exited'))
} else {
resolve('already unexported')
}
})
.catch(err => resolve(err))
})
}
start() {
@ -87,6 +95,15 @@ class Interrupt extends EventEmitter {
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() {
return fs.open(this.path + 'value', 'r+')
.then(fd => {

View File

@ -9,7 +9,11 @@ const Interrupt = require('../').Interrupt
let date = new Date(Date.now())
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 => {
// console.log('Listener fired and returned:', hook)
@ -21,9 +25,9 @@ inter17.init()
console.log('initialzed, waiting for interrupt to fire')
})
.catch(err => console.log("returned error:", err))
//
// inter17.on('fired', hook => { counter(hook) })
.catch(err => console.log("returned error:\n", err))
inter17.on('fired', hook => { counter(hook) })
let count = 0
@ -32,5 +36,4 @@ function counter(hook) {
console.log(`******${count}********`)
console.log(hook)
console.log(`----------------------`)
}