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.
"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. `===`.

View File

@ -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() {

View File

@ -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"
},

View File

@ -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(`----------------------`)
}