working interrupt with both ports

master
David Kebler 2017-01-31 23:28:09 -08:00
parent 846979a48f
commit 4485f981a5
1 changed files with 13 additions and 20 deletions

View File

@ -21,8 +21,9 @@ class Interrupt extends EventEmitter {
this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/' this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/'
this.edge = opts.edge // default 'both' this.edge = opts.edge // default 'both'
this.delay = opts.delay // default = 50 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.pull = opts.pull // default = pu
// this.pullup = (num, state) => { return `raspi-gpio set ${num} ${state}` } this.puller = opts.puller ? opts.puller : (num, state) => { return `raspi-gpio set ${num} ${state}` } // default rpi using raspi-gpio
// this.puller = (num, state) => { return `raspi-gpio set ${num} ${state}` }
this.dbt = opts.debounce ? opts.debounce : 200 this.dbt = opts.debounce ? opts.debounce : 200
this.poller = new Epoll( this.poller = new Epoll(
debounce((err, fd, events) => { debounce((err, fd, events) => {
@ -39,30 +40,22 @@ class Interrupt extends EventEmitter {
return _.pSeries([ return _.pSeries([
this.exit(), this.exit(),
this._pull(this.num), this._pull(this.num, this.pull),
this._export(this.num), this._export(this.num),
this._direction('in', this.delay), this._direction('in', this.delay),
() => this._edge(this.edge), // TODO determine why these need additional function () => this._edge(this.edge), // TODO determine why these need additional function
() => this._fd(), () => this._fd()
() => this.clear().then(() => {
this.start()
return Promise.resolve('cleared and started')
})
]) ])
} }
// _test(num) {
// return Promise.resolve(num)
// }
fire(name = 'fired') { fire(name = 'fired') {
this.emit(name, this.hook) this.emit(name, this.hook)
} }
clear() { clear() {
let buffer = Buffer.from([0x00]) let buffer = Buffer.from([0x00])
return fs.read(this.valuefd, buffer, 0, 1, 0); return fs.read(this.valuefd, buffer, 0, 1, 0).then(() => { return Promise.resolve('interrupt cleared') })
} }
exit() { exit() {
@ -88,7 +81,7 @@ class Interrupt extends EventEmitter {
} }
start() { start() {
this.poller.add(this.valuefd, Epoll.EPOLLPRI); this.clear().then(() => this.poller.add(this.valuefd, Epoll.EPOLLPRI));
} }
stop() { stop() {
@ -98,9 +91,9 @@ class Interrupt extends EventEmitter {
/*******private methods*********/ /*******private methods*********/
_pull(num, state = 'pu') { _pull(num, state = 'pu') {
if (this.pullup === 'external') { return Promise.resolve('pull must be set externally') } else { if (this.puller === 'external') { return Promise.resolve('pull must be set externally') } else {
return exec(this.pullup(num, state)) return exec(this.puller(num, state))
.then(() => { return Promise.resolve('pull set') }) .then(() => { return Promise.resolve(`pull set ${state}`) })
} }
} }
@ -108,7 +101,7 @@ class Interrupt extends EventEmitter {
return fs.open(this.path + 'value', 'r+') return fs.open(this.path + 'value', 'r+')
.then(fd => { .then(fd => {
this.valuefd = fd this.valuefd = fd
return Promise.resolve('value fd set') return Promise.resolve(`value fd set ${fd}`)
}) })
} }
@ -126,7 +119,7 @@ class Interrupt extends EventEmitter {
.then(() => { .then(() => {
return fs.writeFile(dir + 'direction', dir) return fs.writeFile(dir + 'direction', dir)
.then(() => { .then(() => {
return Promise.resolve('direction set') return Promise.resolve(`direction set ${dir}`)
}) })
}) })
} }
@ -134,7 +127,7 @@ class Interrupt extends EventEmitter {
_edge(edge = 'both') { _edge(edge = 'both') {
return fs.writeFile(this.path + 'edge', edge) return fs.writeFile(this.path + 'edge', edge)
.then(() => { .then(() => {
return Promise.resolve('edge set') return Promise.resolve(`edge set ${edge}`)
}) })
} }