operation interrupt processing from mcp to rpi and resetting

master
David Kebler 2017-01-27 21:05:43 -08:00
parent ba2db815e5
commit e333378c36
1 changed files with 27 additions and 15 deletions

View File

@ -11,7 +11,7 @@ class MCP23008 extends Device {
this.ports = {} this.ports = {}
opts.portID = 'A' opts.portID = 'A'
this.ports.A = new Port(opts) this.ports.A = new Port(opts)
this.chip_config = opts.chip_config 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
} // end constructor } // end constructor
@ -19,24 +19,35 @@ class MCP23008 extends Device {
init() { init() {
// console.log('chip configuration', chip_config.cmd, chipSetByte()) // console.log('chip configuration', chip_config.cmd, chipSetByte())
// console.log(super.write.toString()) // console.log(super.write.toString())
let jobs = [() => { return super.write(chip_config.cmd, chipSetByte(this.chip_config)) }] // configure chip let jobs = [this.writeChipCfg(this.chip_config)] // configure chip
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()) // console.log('interrupt init', this.inter(port).init().toString())
jobs.push(() => { return this.inter(port).init() }) // console.log('interrupt', this.inter(port).init)
jobs.push(this.inter(port).init())
// jobs.push(this.inter(port).init.bind(this.inter(port))())
} }
} }
jobs.push(() => { return this.writePinsCfg() }) jobs.push(this.writePinsCfg())
return _u.pSeries(jobs) return _u.pSeries(jobs)
} }
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
inter(port = 'A') { inter(port = 'A') {
return this.ports[port].interrupt return this.ports[port].interrupt
} }
writeChipCfg(cfg = 'default') {
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`))
}
// pin configurations should already be set before calling // pin configurations should already be set before calling
writePinsCfg() { writePinsCfg() {
let jobs = []; let jobs = [];
@ -52,7 +63,7 @@ class MCP23008 extends Device {
} }
// 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(reg, byte) //.then(() => console.log(`done writing config`)) super.write(reg, byte).then(() => Promise.resolve(`config: wrote ${byte} to register ${reg} on port ${port}`))
) )
} }
} }
@ -164,21 +175,22 @@ let chip_config = {
// byte: ['NULL','INTPOL','ODR','HAEN','DISSLW','SEQOP','MIRROR','BANK'] // see page 18 of 23017 datasheet for 8 setting details // byte: ['NULL','INTPOL','ODR','HAEN','DISSLW','SEQOP','MIRROR','BANK'] // see page 18 of 23017 datasheet for 8 setting details
cmd: 0x0A, // IOCON.BANK=0 (msb) at powerup so need to use 0x0A, if set to 1 then use cmd: 0x0A, // IOCON.BANK=0 (msb) at powerup so need to use 0x0A, if set to 1 then use
default: { default: {
val: '10000000', // int pins not connected, port A + 0x10 = Port B -- ignored by 23008 val: '10100010', // Split Banks port A + 0x10 = Port B,(ignored by 23008), Sequential operation disabled, open drain int
fmt: 'STR' fmt: 'STR'
}, },
oneint: { oneint: {
val: '11000000', // int pins connected, port A + 0x10 = Port B -- ignored by 23008 val: '11100100', // same as default execpt int pins connected
fmt: 'STR' fmt: 'STR'
} }
} }
function chipSetByte(setting = 'default') { // function chipSetByte(setting = 'default') {
setting = chip_config[setting] // setting = chip_config[setting]
return _u.byteFormat(setting.val, { in: setting.fmt, // return _u.byteFormat(setting.val, { in: setting.fmt,
out: 'DEC' // out: 'DEC'
}) // })
} // }
let registers = { let registers = {
pin_config: { pin_config: {
dir: 0, dir: 0,