interrupt working with debounce

master
David Kebler 2017-01-21 23:48:17 -08:00
parent e6735ed883
commit 87fdcd0df8
4 changed files with 49 additions and 36 deletions

View File

@ -36,7 +36,7 @@
// The Good Parts. // The Good Parts.
"asi" : true, // Tolerate Automatic Semicolon Insertion (no semicolons). "asi" : true, // Tolerate Automatic Semicolon Insertion (no semicolons).
"laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without 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. "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. "curly" : true, // Require {} for every new block or scope.
"eqeqeq" : true, // Require triple equals i.e. `===`. "eqeqeq" : true, // Require triple equals i.e. `===`.

View File

@ -5,6 +5,10 @@ const fs = require('mz/fs'),
Epoll = require('epoll').Epoll, Epoll = require('epoll').Epoll,
_ = require('uci-utils') _ = 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/' const GPIO_ROOT_PATH = '/sys/class/gpio/'
class Interrupt extends EventEmitter { class Interrupt extends EventEmitter {
@ -15,27 +19,37 @@ class Interrupt extends EventEmitter {
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 ? opts.edge : 'falling' this.edge = opts.edge ? opts.edge : 'falling'
this.debounce = opts.debounce ? opts.debounce : 200 this.delay = opts.delay ? opts.delay : 50
this.poller = new Epoll(function (err, fd, events) { this.dbt = opts.debounce ? opts.debounce : 300
if (err) { this.emit('error', err) } this.poller = new Epoll(
this.clear() debounce((err, fd, events) => {
this.emit('fired', this.hook) if (err) { this.emit('error', err) } else { this.clear().then(this.emit('fired', this.hook)) }
}.bind(this)) }, this.dbt, true)
)
} }
init() { init() {
let tasks = [ let tasks = [
() => fs.writeFile(GPIO_ROOT_PATH + 'export', this.num).then((resp) => { console.log('exported', resp) }), () => { return fs.writeFile(GPIO_ROOT_PATH + 'export', this.num).then(() => { console.log('exported') }) },
() => fs.writeFile(this.path + 'direction', 'in').then((resp) => { console.log('direction', resp) }), () => {
// fs.writeFile(this.path + 'edge', this.edge), return pWaitFor(() => pathExists(this.path + 'direction')).then(_.pDelay(this.delay))
// fs.open(this.path + 'value', 'r+').then(fd => { this.valuefd = fd }), .then(() => { return fs.writeFile(this.path + 'direction', 'in').then(() => { console.log('direction set') }) })
// this.clear().then(() => { this.start() }) },
() => { 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() process.on('SIGINT', () => {
.then(() => { return pSeries(tasks) }) this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c
// .then(() => { return this.exit() }) })
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() { exit() {
return pathExists(this.path)
return fs.access(this.path)
.then(() => { .then(() => {
console.log('unexporting')
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num) return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
}) })
.then(() => { .then(() => {
@ -61,7 +73,7 @@ class Interrupt extends EventEmitter {
return fs.close(this.valuefd) return fs.close(this.valuefd)
} }
}) })
.catch(err => { return Promise.resolve('already exited') }) .catch(err => { return Promise.resolve('already unexported') })
} }
start() { start() {

View File

@ -29,8 +29,11 @@
}, },
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme", "homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
"dependencies": { "dependencies": {
"debounce": "^1.0.0",
"epoll": "^0.1.20", "epoll": "^0.1.20",
"mz": "^2.6.0", "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", "require-all": "git+https://github.com/dkebler/node-require-all.git#merge",
"uci-utils": "git+https://git.kebler.net/uCOMmandIt/uci-utils.git" "uci-utils": "git+https://git.kebler.net/uCOMmandIt/uci-utils.git"
}, },

View File

@ -11,26 +11,24 @@ 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' })
// console.log('inter17', inter17) // inter17.on('fired', hook => {
// console.log('Listener fired and returned:', hook)
// })
inter17.on('fired', hook => { inter17.on('fired', hook => { counter(hook) })
console.log('Listener fired and returned:', hook)
})
inter17.init() inter17.init()
.then((resp) => { .then(() => {
console.log('return from init()', resp) console.log('initialzed, waiting for interrupt to fire')
// setInterval(function () {
// inter17.fire()
// }, 1000)
}) })
.catch(err => console.log("returned error:", err)) .catch(err => console.log("returned error:", err))
// describe('Interrupt Class', function () { let count = 0
//
// it('can be manually fired', function () { function counter(hook) {
// expect(hello('Forest Gump')).to.equal() count++
// }) console.log(`******${count}********`)
// console.log(hook)
// }) console.log(`----------------------`)
// })
}