From 87fdcd0df8306a4f74e6fe996fdd68e889a04c46 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sat, 21 Jan 2017 23:48:17 -0800 Subject: [PATCH] interrupt working with debounce --- .jshintrc | 2 +- lib/interrupt.js | 48 ++++++++++++++++++++++++++---------------- package.json | 3 +++ test/interrupt.test.js | 32 +++++++++++++--------------- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/.jshintrc b/.jshintrc index 29bdf7a..6ef7c9f 100644 --- a/.jshintrc +++ b/.jshintrc @@ -36,7 +36,7 @@ // The Good Parts. "asi" : true, // Tolerate Automatic Semicolon Insertion (no semicolons). "laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. - "bitwise" : true, // Allow bitwise operators (&, |, ^, etc.). + "bitwise" : false, // Allow bitwise operators (&, |, ^, etc.). "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. "curly" : true, // Require {} for every new block or scope. "eqeqeq" : true, // Require triple equals i.e. `===`. diff --git a/lib/interrupt.js b/lib/interrupt.js index eba6158..7d4516b 100644 --- a/lib/interrupt.js +++ b/lib/interrupt.js @@ -5,6 +5,10 @@ const fs = require('mz/fs'), 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 { @@ -15,27 +19,37 @@ class Interrupt extends EventEmitter { this.hook = opts.hook // will be passed back with the emit this.path = GPIO_ROOT_PATH + 'gpio' + this.num + '/' this.edge = opts.edge ? opts.edge : 'falling' - this.debounce = opts.debounce ? opts.debounce : 200 - this.poller = new Epoll(function (err, fd, events) { - if (err) { this.emit('error', err) } - this.clear() - this.emit('fired', this.hook) - }.bind(this)) + this.delay = opts.delay ? opts.delay : 50 + 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() { let tasks = [ - () => fs.writeFile(GPIO_ROOT_PATH + 'export', this.num).then((resp) => { console.log('exported', resp) }), - () => fs.writeFile(this.path + 'direction', 'in').then((resp) => { console.log('direction', resp) }), - // fs.writeFile(this.path + 'edge', this.edge), - // fs.open(this.path + 'value', 'r+').then(fd => { this.valuefd = fd }), - // this.clear().then(() => { this.start() }) + () => { return fs.writeFile(GPIO_ROOT_PATH + 'export', this.num).then(() => { console.log('exported') }) }, + () => { + return pWaitFor(() => pathExists(this.path + 'direction')).then(_.pDelay(this.delay)) + .then(() => { return fs.writeFile(this.path + 'direction', 'in').then(() => { console.log('direction set') }) }) + }, + () => { return fs.writeFile(this.path + 'edge', this.edge).then(() => { console.log('edge') }) }, + () => { return fs.open(this.path + 'value', 'r+').then(fd => { this.valuefd = fd }) }, + () => { return this.clear().then(() => { return this.start() }) } ] - return this.exit() - .then(() => { return pSeries(tasks) }) - // .then(() => { return this.exit() }) + process.on('SIGINT', () => { + this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c + }) + + return this.exit() // make sure pin is unexported + .then((resp) => { + console.log(resp) + return _.pSeries(tasks) + }) } @@ -49,10 +63,8 @@ class Interrupt extends EventEmitter { } exit() { - - return fs.access(this.path) + return pathExists(this.path) .then(() => { - console.log('unexporting') return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num) }) .then(() => { @@ -61,7 +73,7 @@ class Interrupt extends EventEmitter { return fs.close(this.valuefd) } }) - .catch(err => { return Promise.resolve('already exited') }) + .catch(err => { return Promise.resolve('already unexported') }) } start() { diff --git a/package.json b/package.json index 17fc2d0..5556957 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,11 @@ }, "homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme", "dependencies": { + "debounce": "^1.0.0", "epoll": "^0.1.20", "mz": "^2.6.0", + "p-wait-for": "^1.0.0", + "path-exists": "^3.0.0", "require-all": "git+https://github.com/dkebler/node-require-all.git#merge", "uci-utils": "git+https://git.kebler.net/uCOMmandIt/uci-utils.git" }, diff --git a/test/interrupt.test.js b/test/interrupt.test.js index a7dbf92..a502781 100644 --- a/test/interrupt.test.js +++ b/test/interrupt.test.js @@ -11,26 +11,24 @@ console.log(date.getMinutes(), "\:", date.getSeconds()) let inter17 = new Interrupt(17, { hook: 'a hook to something to do' }) -// console.log('inter17', inter17) +// inter17.on('fired', hook => { +// console.log('Listener fired and returned:', hook) +// }) -inter17.on('fired', hook => { - console.log('Listener fired and returned:', hook) -}) +inter17.on('fired', hook => { counter(hook) }) inter17.init() - .then((resp) => { - console.log('return from init()', resp) - // setInterval(function () { - // inter17.fire() - // }, 1000) + .then(() => { + console.log('initialzed, waiting for interrupt to fire') }) .catch(err => console.log("returned error:", err)) -// describe('Interrupt Class', function () { -// -// it('can be manually fired', function () { -// expect(hello('Forest Gump')).to.equal() -// }) -// -// }) -// }) +let count = 0 + +function counter(hook) { + count++ + console.log(`******${count}********`) + console.log(hook) + console.log(`----------------------`) + +}