working out promise sequence of init tasks

master
David Kebler 2017-01-25 00:11:20 -08:00
parent 65e02f4fc6
commit d8172b092b
2 changed files with 76 additions and 35 deletions

View File

@ -18,39 +18,40 @@ class Interrupt extends EventEmitter {
this.num = pin_number; this.num = pin_number;
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
this.delay = opts.delay ? opts.delay : 50 this.delay = opts.delay
this.dbt = opts.debounce ? opts.debounce : 300 this.dbt = opts.debounce ? opts.debounce : 300
// this.poller = new Epoll( this.poller = new Epoll(
// debounce((err, fd, events) => { debounce((err, fd, events) => {
// if (err) { this.emit('error', err) } else { this.clear().then(this.emit('fired', this.hook)) } if (err) { this.emit('error', err) } else { this.clear().then(this.emit('fired', this.hook)) }
// }, this.dbt, true) }, this.dbt, true)
// ) )
} }
init() { init() {
let tasks = [
fs.writeFile(GPIO_ROOT_PATH + 'export', this.num).then(() => { console.log('exported', this.num) }),
pWaitFor(() => pathExists(this.path + 'direction')).then(_.pDelay(this.delay))
.then(() => { return fs.writeFile(this.path + 'direction', 'in').then(() => { console.log('direction', 'in') }) }),
fs.writeFile(this.path + 'edge', this.edge).then(() => { console.log('edge', this.edge) }),
fs.open(this.path + 'value', 'r+').then(fd => { this.valuefd = fd }),
this.clear().then(() => { return this.start() })
]
process.on('SIGINT', () => { process.on('SIGINT', () => {
this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c this.exit().then(() => console.log('\nunexported and closed')) // unexport on cntrl-c
}) })
return this.exit() // make sure pin is unexported return _.pSeries([
.then((resp) => { this.exit(),
console.log('ready to init', tasks) this._export(this.num),
return _.pSeries(tasks).then(console.log('finsihed tasks')) this._direction('in', this.delay),
() => this._edge(this.edge),
() => 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)
} }
@ -62,16 +63,20 @@ class Interrupt extends EventEmitter {
exit() { exit() {
return pathExists(this.path) return pathExists(this.path)
.then(() => { .then((exists) => {
// console.log('exists', exists)
if (exists) {
return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num) return fs.writeFile(GPIO_ROOT_PATH + 'unexport', this.num)
})
.then(() => { .then(() => {
if (this.valuefd) { if (this.valuefd) {
this.stop() this.stop()
return fs.close(this.valuefd) return fs.close(this.valuefd)
} }
return Promise.resolve('unexported - exited')
})
}
return Promise.resolve('already unexported')
}) })
.catch(err => { return Promise.resolve('already unexported') })
} }
start() { start() {
@ -82,6 +87,40 @@ class Interrupt extends EventEmitter {
this.poller.remove(this.valuefd).close(); this.poller.remove(this.valuefd).close();
} }
_fd() {
return fs.open(this.path + 'value', 'r+')
.then(fd => {
this.valuefd = fd
return Promise.resolve('value fd set')
})
}
_export(num) {
return new Promise(function (resolve, reject) {
fs.writeFile(GPIO_ROOT_PATH + 'export', num)
.then(() => { return resolve('exported') })
.catch((err) => { return reject(err) })
})
}
_direction(dir = 'in', delay = 50) {
return pWaitFor(() => pathExists(this.path + 'direction'))
.then(_.pDelay(delay))
.then(() => {
return fs.writeFile(dir + 'direction', dir)
.then(() => {
return Promise.resolve('direction set')
})
})
}
_edge(edge = 'falling') {
return fs.writeFile(this.path + 'edge', edge)
.then(() => {
return Promise.resolve('edge set')
})
}
} }
module.exports = { module.exports = {

View File

@ -15,13 +15,15 @@ let inter17 = new Interrupt(17, { hook: 'a hook to something to do' })
// console.log('Listener fired and returned:', hook) // console.log('Listener fired and returned:', hook)
// }) // })
// inter17.init() inter17.init()
// .then(() => { .then((resp) => {
// console.log('initialzed, waiting for interrupt to fire') console.log(resp)
// }) console.log('initialzed, waiting for interrupt to fire')
// .catch(err => console.log("returned error:", err))
// })
// inter17.on('fired', hook => { counter(hook) }) .catch(err => console.log("returned error:", err))
//
// inter17.on('fired', hook => { counter(hook) })
let count = 0 let count = 0