refactoring using async/await and new interrupt class code based on pigpio. Handler keeps proper scope. Interrupts working!!!

master
David Kebler 2017-05-20 22:23:24 -07:00
parent 1d659aeb58
commit 832a507204
1 changed files with 27 additions and 4 deletions

View File

@ -44,16 +44,29 @@ class MCP23008 extends Device {
} }
async start() { async start() {
console.log(`begin starting ${ this.id }`) // console.log(`starting ${ this.id }`)
for (let port in this.ports) { for (let port in this.ports) {
// if there are interrupts being used then start them and listeners
if (this.inter(port)) { if (this.inter(port)) {
await this.read(portReg(0x08, port)) await this.interruptReset(port)
await pause(200) // give enough time for mcp to reset its interupt await this.inter(port).start()
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() { // start() {
// let starts = [] // let starts = []
// for (let port in this.ports) { // for (let port in this.ports) {
@ -82,6 +95,10 @@ class MCP23008 extends Device {
// return Promise.all(starts) // 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 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 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) 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) { portByPin(id) {
console.log('pin id in portbypin', id) console.log('pin id in portbypin', id)
if (this.ports.A.pin(id)) { return 'A' } if (this.ports.A.pin(id)) { return 'A' }