start of conversion to async/await

master
David Kebler 2017-05-20 17:13:14 -07:00
parent 15e9c2fcd2
commit 9c88903d42
1 changed files with 68 additions and 40 deletions

View File

@ -1,7 +1,8 @@
'use strict' 'use strict'
const Device = require('uci-dev').Device, const Device = require('uci-dev').Device,
Port = require('uci-gpio').Port, Port = require('uci-gpio').Port,
_u = require('uci-utils') _u = require('uci-utils'),
pause = _u.pPause
class MCP23008 extends Device { class MCP23008 extends Device {
constructor(busObj, i2cAddress, opts = {}) { constructor(busObj, i2cAddress, opts = {}) {
@ -14,52 +15,72 @@ class MCP23008 extends Device {
this.ports.A = new Port(opts) this.ports.A = new Port(opts)
this.chip_config = opts.chip_config // TODO allow opts.chip_config to be a byte instead of a string pointer this.chip_config = opts.chip_config // TODO allow opts.chip_config to be a byte instead of a string pointer
this.ports.A.interrupt = opts.interruptA ? opts.interruptA : opts.interrupt this.ports.A.interrupt = opts.interruptA ? opts.interruptA : opts.interrupt
if (this.ports.A.interrupt) { this.ports.A.interrupt.hook = 'A' } // if (this.ports.A.interrupt) { this.ports.A.interrupt.port = 'A' }
} // end constructor } // end constructor
init() { async init() {
// console.log('chip configuration', chip_config.cmd, chipSetByte()) // console.log('chip configuration', chip_config.cmd, chipSetByte())
// console.log(super.write.toString()) // console.log(`begin initialize ${this.id}`)
let jobs = [this.writeChipCfg(this.chip_config)] // configure chip await this.writeChipCfg(this.chip_config)
// let jobs = [this.writeChipCfg(this.chip_config)] // configure chip
// for (let port in this.ports) {
// if (this.inter(port)) {
// console.log(`initialize interrupt port ${port}`)
// jobs.push(
// this.inter(port).init()
// .then(sbci => {
// this.inter(port).sbci = sbci
// console.log(`sbci ${this.inter(port).sbci}`)
// })
// )
// // sbci = single board computer interrrupt to distinguish it from interrupt on MCP
// }
// }
// jobs.push(this.writePinsCfg())
await this.writePinsCfg()
// return _u.pSeries(jobs)
}
async start() {
console.log(`begin starting ${ this.id }`)
for (let port in this.ports) { for (let port in this.ports) {
if (this.inter(port)) { if (this.inter(port)) {
// console.log('interrupt init', this.inter(port).init().toString()) await this.read(portReg(0x08, port))
// console.log('interrupt', this.inter(port).init) await pause(200) // give enough time for mcp to reset its interupt
jobs.push(this.inter(port).init()) // set up listeners if interrupts exits this.inter(port).start()
// jobs.push(this.inter(port).init.bind(this.inter(port))())
} }
} }
jobs.push(this.writePinsCfg())
return _u.pSeries(jobs)
} }
start() { // start() {
let starts = [] // let starts = []
for (let port in this.ports) { // for (let port in this.ports) {
if (this.inter(port)) { // if (this.inter(port)) {
starts.push( // starts.push(
this.read(portReg(0x08, port)) // this.read(portReg(0x08, port))
.then(_u.pDelay(100)) // give enough time for mcp to reset its interupt // .then(_u.pDelay(100)) // give enough time for mcp to reset its interupt
.then(() => { // .then(() => {
return this.inter(port).start() // return this.inter(port).start()
.then(() => { // .then(resp => {
this.inter(port).on('fired', () => { // console.log(resp)
console.log(`interrupt port ${port} hook me \n ${relays}`) // // this.inter(port).on('fired', () => {
// hook.bind(this)(port) // // console.log(`
//interrupt port $ { port } hook me\ n $ { relays }
}) // `)
return Promise.resolve(`interrupt port ${port} started`) // // // hook.bind(this)(port)
}) // return Promise.resolve()
.catch(err => console.log("error:", err)) // })
}) // return Promise.resolve(`
) //interrupt port $ { port } started `)
} // })
} // .catch(err => console.log("error:", err))
return Promise.all(starts) // )
} // }
// }
// return Promise.all(starts)
// }
pin(id) { return this.ports.A.pin(id) } // get a reference to a particular pin's object pin(id) { return this.ports.A.pin(id) } // get a reference to a particular pin's object
@ -70,21 +91,24 @@ class MCP23008 extends Device {
return false return false
} }
// get a handle to the ports interrupt
inter(port = 'A') { inter(port = 'A') {
return this.ports[port].interrupt return this.ports[port].interrupt
} }
writeChipCfg(cfg = 'default') { writeChipCfg(cfg = 'default') {
// console.log(`writing mcp chip config ${this.id}`)
let setting = chip_config[cfg] let setting = chip_config[cfg]
let byte = _u.byteFormat(setting.val, { in: setting.fmt, let byte = _u.byteFormat(setting.val, { in: setting.fmt,
out: 'DEC' out: 'DEC'
}) })
return super.write(chip_config.cmd, byte) return super.write(chip_config.cmd, byte)
.then(() => Promise.resolve(`mcp chip config: reg ${chip_config.cmd} byte ${byte} written`)) .then(() => Promise.resolve(`mcp chip config: reg $ { chip_config.cmd } byte $ { byte } written `))
} }
// pin configurations should already be set before calling // pin configurations should already be set before calling
writePinsCfg() { writePinsCfg() {
// console.log(`writing mcp pins config ${this.id}`)
let jobs = []; let jobs = [];
for (let port in this.ports) { for (let port in this.ports) {
for (let setting in registers.pin_config) { for (let setting in registers.pin_config) {
@ -96,9 +120,11 @@ class MCP23008 extends Device {
let pin = pins[i] let pin = pins[i]
byte += pin.address.toFmt('DEC') * pin.cfg[setting] byte += pin.address.toFmt('DEC') * pin.cfg[setting]
} }
//console.log(`port ${port} - setting ${setting} - reg ${reg} - byte ${byte}`) //console.log(`port $ { port } - setting $ { setting } - reg $ { reg } - byte $ { byte }`)
jobs.push( jobs.push(
super.write(portReg(reg, port), byte).then(() => Promise.resolve(`config: wrote ${byte} to register ${reg} on port ${port}`)) super.write(portReg(reg, port), byte).then(() => Promise.resolve(`
config: wrote $ { byte } to register $ { reg } on port $ { port }
`))
) )
} }
} }
@ -164,7 +190,7 @@ class MCP23008 extends Device {
resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0 resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0
}) })
}) })
// .then(state => console.log(`pin state ${state}`)) // .then(state => console.log(`pin state $ { state }`))
.catch(err => console.log(err)) // end Promise .catch(err => console.log(err)) // end Promise
} // end pinRead } // end pinRead
@ -179,7 +205,7 @@ class MCP23017 extends MCP23008 {
opts.pids = opts.pidsB opts.pids = opts.pidsB
this.ports.B = new Port(opts) this.ports.B = new Port(opts)
this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt
if (this.ports.B.interrupt) { this.ports.B.interrupt.hook = 'B' } // if (this.ports.B.interrupt) { this.ports.B.interrupt.hook = 'B' }
} }
pin(id, port) { pin(id, port) {
@ -206,6 +232,8 @@ class MCP23017 extends MCP23008 {
} // end MCP23017 Class } // end MCP23017 Class
module.exports.MCP23017 = MCP23017 module.exports.MCP23017 = MCP23017
// ==============================
function portReg(reg, port) { function portReg(reg, port) {
if (port === 'B') { return reg += 0x10 } else { return reg } if (port === 'B') { return reg += 0x10 } else { return reg }
} }