From e333378c36ef4b1a553843154669e3da9e55a4a5 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Fri, 27 Jan 2017 21:05:43 -0800 Subject: [PATCH] operation interrupt processing from mcp to rpi and resetting --- lib/mcp23008-17.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/mcp23008-17.js b/lib/mcp23008-17.js index 5f2676b..36936fc 100644 --- a/lib/mcp23008-17.js +++ b/lib/mcp23008-17.js @@ -11,7 +11,7 @@ class MCP23008 extends Device { this.ports = {} opts.portID = 'A' 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 } // end constructor @@ -19,24 +19,35 @@ class MCP23008 extends Device { init() { // console.log('chip configuration', chip_config.cmd, chipSetByte()) // 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) { if (this.inter(port)) { - console.log('interrupt init', this.inter(port).init().toString()) - jobs.push(() => { return this.inter(port).init() }) + // console.log('interrupt init', this.inter(port).init().toString()) + // 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) } pin(id) { return this.ports.A.pin(id) } // get a reference to a particular pin's object inter(port = 'A') { - 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 writePinsCfg() { let jobs = []; @@ -52,7 +63,7 @@ class MCP23008 extends Device { } // console.log(`port ${port} - setting ${setting} - reg ${reg} - byte ${byte}`) 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 cmd: 0x0A, // IOCON.BANK=0 (msb) at powerup so need to use 0x0A, if set to 1 then use 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' }, 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' } } -function chipSetByte(setting = 'default') { - setting = chip_config[setting] - return _u.byteFormat(setting.val, { in: setting.fmt, - out: 'DEC' - }) -} +// function chipSetByte(setting = 'default') { +// setting = chip_config[setting] +// return _u.byteFormat(setting.val, { in: setting.fmt, +// out: 'DEC' +// }) +// } + let registers = { pin_config: { dir: 0,