diff --git a/lib/mcp23008-17.js b/lib/mcp23008-17.js index 1f7afb4..12b1e6b 100644 --- a/lib/mcp23008-17.js +++ b/lib/mcp23008-17.js @@ -44,16 +44,29 @@ class MCP23008 extends Device { } async start() { - console.log(`begin starting ${ this.id }`) + // console.log(`starting ${ this.id }`) for (let port in this.ports) { + // if there are interrupts being used then start them and listeners if (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() + await this.interruptReset(port) + await this.inter(port).start() + // bind handler to the chip to handler can read/write to chip/bank when interrupt is emitted + let ihandler = this.inter(port).handler.bind(this) + // inside the listener `this` is the interrupt not the chip/bank + this.inter(port).on('fired', function () { + console.log(`interrupt from ${this.pin_number}`) + ihandler(port) + }) } } } + async interruptReset(port = 'A') { + await this.read(portReg(0x08, port)) + await pause(300) // give enough time for mcp to reset its interupt + console.log(`interrupt reset on ${this.id} port ${port}`) + } + // start() { // let starts = [] // for (let port in this.ports) { @@ -82,6 +95,10 @@ class MCP23008 extends Device { // return Promise.all(starts) // } + portByInterPin(pin) { + return 'A' + } + pin(id) { return this.ports.A.pin(id) } // get a reference to a particular pin's object pid(address) { return this.ports.A.pid(address) } // return pin id for a given address on a port @@ -222,6 +239,12 @@ class MCP23017 extends MCP23008 { return this.ports[port].pid(address) } + portByInterPin(pin) { + if (this.ports.A.interrupt.pin_number === pin) { return 'A' } + if (this.ports.B.interrupt.pin_number === pin) { return 'B' } + return false + } + portByPin(id) { console.log('pin id in portbypin', id) if (this.ports.A.pin(id)) { return 'A' }