From 4485f981a556f1a73cdbccaddfb1e1f21ddb8954 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Tue, 31 Jan 2017 23:28:09 -0800 Subject: [PATCH] working interrupt with both ports --- lib/interrupt.js | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/lib/interrupt.js b/lib/interrupt.js index 7629333..0858e67 100644 --- a/lib/interrupt.js +++ b/lib/interrupt.js @@ -21,8 +21,9 @@ class Interrupt extends EventEmitter { this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/' this.edge = opts.edge // default 'both' 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.pull = opts.pull // default = pu + 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.poller = new Epoll( debounce((err, fd, events) => { @@ -39,30 +40,22 @@ class Interrupt extends EventEmitter { return _.pSeries([ this.exit(), - this._pull(this.num), + this._pull(this.num, this.pull), this._export(this.num), this._direction('in', this.delay), () => this._edge(this.edge), // TODO determine why these need additional function - () => this._fd(), - () => this.clear().then(() => { - this.start() - return Promise.resolve('cleared and started') - }) + () => this._fd() ]) } - // _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); + return fs.read(this.valuefd, buffer, 0, 1, 0).then(() => { return Promise.resolve('interrupt cleared') }) } exit() { @@ -88,7 +81,7 @@ class Interrupt extends EventEmitter { } start() { - this.poller.add(this.valuefd, Epoll.EPOLLPRI); + this.clear().then(() => this.poller.add(this.valuefd, Epoll.EPOLLPRI)); } stop() { @@ -98,9 +91,9 @@ class Interrupt extends EventEmitter { /*******private methods*********/ _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') }) + if (this.puller === 'external') { return Promise.resolve('pull must be set externally') } else { + return exec(this.puller(num, state)) + .then(() => { return Promise.resolve(`pull set ${state}`) }) } } @@ -108,7 +101,7 @@ class Interrupt extends EventEmitter { return fs.open(this.path + 'value', 'r+') .then(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(() => { return fs.writeFile(dir + 'direction', dir) .then(() => { - return Promise.resolve('direction set') + return Promise.resolve(`direction set ${dir}`) }) }) } @@ -134,7 +127,7 @@ class Interrupt extends EventEmitter { _edge(edge = 'both') { return fs.writeFile(this.path + 'edge', edge) .then(() => { - return Promise.resolve('edge set') + return Promise.resolve(`edge set ${edge}`) }) }