add start method fix pinRead for port B

master
David Kebler 2017-02-21 21:40:09 -08:00
parent 3f0d0ad407
commit ecb455e7ea
1 changed files with 39 additions and 5 deletions

View File

@ -36,9 +36,34 @@ class MCP23008 extends Device {
return _u.pSeries(jobs) 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(() => {
return Promise.resolve(`interrupt port ${port} started`)
})
})
)
}
}
return Promise.all(starts)
}
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
portByPin(id) {
if (this.ports.A.pin(id)) { return 'A' }
return false
}
inter(port = 'A') { inter(port = 'A') {
return this.ports[port].interrupt return this.ports[port].interrupt
} }
@ -95,11 +120,9 @@ class MCP23008 extends Device {
if (!gpio) { if (!gpio) {
reject('no pin found for given id') reject('no pin found for given id')
} }
if (gpio.port === 'B') { console.log('id of fired pin', id)
reg = reg + 0x10 console.log('port of fired pin', mcpdev.portByPin(id))
} return mcpdev.read(portReg(reg, mcpdev.portByPin(id))).then(resp => {
// console.log('port - reg', gpio.port, reg)
return mcpdev.read(reg).then(resp => {
resp = _u.byteFormat(resp, { in: 'DEC', resp = _u.byteFormat(resp, { in: 'DEC',
out: 'ARY' out: 'ARY'
}) })
@ -168,9 +191,20 @@ class MCP23017 extends MCP23008 {
return this.ports[port].pid(address) return this.ports[port].pid(address)
} }
portByPin(id) {
console.log('pin id in portbypin', id)
if (this.ports.A.pin(id)) { return 'A' }
if (this.ports.B.pin(id)) { return 'B' }
return false
}
} // end MCP23017 Class } // end MCP23017 Class
module.exports.MCP23017 = MCP23017 module.exports.MCP23017 = MCP23017
function portReg(reg, port) {
if (port === 'B') { return reg += 0x10 } else { return reg }
}
/* ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf /* ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
* or see MCP23017.pdf and MCP 23008.pdf in /docs * or see MCP23017.pdf and MCP 23008.pdf in /docs
* see table 1.5 and details in sections 1.6.x following * see table 1.5 and details in sections 1.6.x following