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'
const Device = require('uci-dev').Device,
Port = require('uci-gpio').Port,
_u = require('uci-utils')
_u = require('uci-utils'),
pause = _u.pPause
class MCP23008 extends Device {
constructor(busObj, i2cAddress, opts = {}) {
@ -14,52 +15,72 @@ class MCP23008 extends Device {
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.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
init() {
async init() {
// console.log('chip configuration', chip_config.cmd, chipSetByte())
// console.log(super.write.toString())
let jobs = [this.writeChipCfg(this.chip_config)] // configure chip
// console.log(`begin initialize ${this.id}`)
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) {
if (this.inter(port)) {
// console.log('interrupt init', this.inter(port).init().toString())
// console.log('interrupt', this.inter(port).init)
jobs.push(this.inter(port).init()) // set up listeners if interrupts exits
// jobs.push(this.inter(port).init.bind(this.inter(port))())
await this.read(portReg(0x08, port))
await pause(200) // give enough time for mcp to reset its interupt
this.inter(port).start()
}
}
jobs.push(this.writePinsCfg())
return _u.pSeries(jobs)
}
start() {
let starts = []
for (let port in this.ports) {
if (this.inter(port)) {
starts.push(
this.read(portReg(0x08, port))
.then(_u.pDelay(100)) // give enough time for mcp to reset its interupt
.then(() => {
return this.inter(port).start()
.then(() => {
this.inter(port).on('fired', () => {
console.log(`interrupt port ${port} hook me \n ${relays}`)
// hook.bind(this)(port)
})
return Promise.resolve(`interrupt port ${port} started`)
})
.catch(err => console.log("error:", err))
})
)
}
}
return Promise.all(starts)
}
// start() {
// let starts = []
// for (let port in this.ports) {
// if (this.inter(port)) {
// starts.push(
// this.read(portReg(0x08, port))
// .then(_u.pDelay(100)) // give enough time for mcp to reset its interupt
// .then(() => {
// return this.inter(port).start()
// .then(resp => {
// console.log(resp)
// // this.inter(port).on('fired', () => {
// // console.log(`
//interrupt port $ { port } hook me\ n $ { relays }
// `)
// // // hook.bind(this)(port)
// return Promise.resolve()
// })
// return Promise.resolve(`
//interrupt port $ { port } started `)
// })
// .catch(err => console.log("error:", err))
// )
// }
// }
// return Promise.all(starts)
// }
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
}
// get a handle to the ports interrupt
inter(port = 'A') {
return this.ports[port].interrupt
}
writeChipCfg(cfg = 'default') {
// console.log(`writing mcp chip config ${this.id}`)
let setting = chip_config[cfg]
let byte = _u.byteFormat(setting.val, { in: setting.fmt,
out: 'DEC'
})
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
writePinsCfg() {
// console.log(`writing mcp pins config ${this.id}`)
let jobs = [];
for (let port in this.ports) {
for (let setting in registers.pin_config) {
@ -96,9 +120,11 @@ class MCP23008 extends Device {
let pin = pins[i]
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(
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
})
})
// .then(state => console.log(`pin state ${state}`))
// .then(state => console.log(`pin state $ { state }`))
.catch(err => console.log(err)) // end Promise
} // end pinRead
@ -179,7 +205,7 @@ class MCP23017 extends MCP23008 {
opts.pids = opts.pidsB
this.ports.B = new Port(opts)
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) {
@ -206,6 +232,8 @@ class MCP23017 extends MCP23008 {
} // end MCP23017 Class
module.exports.MCP23017 = MCP23017
// ==============================
function portReg(reg, port) {
if (port === 'B') { return reg += 0x10 } else { return reg }
}