fully working control example with interrupts

master
David Kebler 2017-02-02 01:41:42 -08:00
parent cd52c3ffab
commit 3f0d0ad407
1 changed files with 21 additions and 19 deletions

View File

@ -14,6 +14,7 @@ class MCP23008 extends Device {
this.ports.A = new Port(opts) this.ports.A = new Port(opts)
this.chip_config = opts.chip_config // TODO allow opts.chip_config to be a byte instead of a string pointer 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
if (this.ports.A.interrupt) { this.ports.A.interrupt.hook = 'A' }
} // end constructor } // end constructor
@ -25,8 +26,9 @@ class MCP23008 extends Device {
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())
// console.log('interrupt', this.inter(port).init) // console.log('interrupt', this.inter(port).init)
jobs.push(this.inter(port).init()) jobs.push(this.inter(port).init()) // set up listeners if interrupts exits
// jobs.push(this.inter(port).init.bind(this.inter(port))())
// jobs.push(this.inter(port).init.bind(this.inter(port))())
} }
} }
jobs.push(this.writePinsCfg()) jobs.push(this.writePinsCfg())
@ -96,19 +98,18 @@ class MCP23008 extends Device {
if (gpio.port === 'B') { if (gpio.port === 'B') {
reg = reg + 0x10 reg = reg + 0x10
} }
console.log('port - reg', gpio.port, reg) // console.log('port - reg', gpio.port, reg)
return mcpdev.read(reg).then(resp => { return mcpdev.read(reg).then(resp => {
resp = _u.byteFormat(resp, { in: 'DEC', resp = _u.byteFormat(resp, { in: 'DEC',
out: 'ARY' out: 'ARY'
}) })
let addr = gpio.adr.toFmt('ARY') let addr = gpio.adr.toFmt('ARY')
console.log(addr) // console.log(addr)
console.log(resp) // console.log(resp)
resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0 resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0
}) }).catch(err => reject(err)) // end Promise
}) }) // end promise
// .then(state => console.log(`pin state ${state}`))
// .catch(err => console.log(err)) // end Promise
} // end pinRead } // end pinRead
pinWrite(id, opts) { pinWrite(id, opts) {
@ -124,18 +125,18 @@ class MCP23008 extends Device {
reg = reg + 16 reg = reg + 16
} }
// call device class read // call device class read
console.log('port - reg', gpio.port, reg) // console.log('port - reg', gpio.port, reg)
return mcpdev.read(reg).then(resp => { return mcpdev.read(reg).then(resp => {
resp = _u.byteFormat(resp, { in: 'DEC', resp = _u.byteFormat(resp, { in: 'DEC',
out: 'ARY' out: 'ARY'
}) })
let addr = gpio.pin.address.toFmt('ARY') let addr = gpio.pin.address.toFmt('ARY')
console.log(addr) // // console.log(addr)
console.log(resp) // console.log(resp)
resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0 resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0
}) })
}) })
.then(state => console.log(`pin state ${state}`)) // .then(state => console.log(`pin state ${state}`))
.catch(err => console.log(err)) // end Promise .catch(err => console.log(err)) // end Promise
} // end pinRead } // end pinRead
@ -150,6 +151,7 @@ class MCP23017 extends MCP23008 {
opts.pids = opts.pidsB opts.pids = opts.pidsB
this.ports.B = new Port(opts) this.ports.B = new Port(opts)
this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt
if (this.ports.B.interrupt) { this.ports.B.interrupt.hook = 'B' }
} }
pin(id, port) { pin(id, port) {
@ -161,7 +163,7 @@ class MCP23017 extends MCP23008 {
pid(address, port) { pid(address, port) {
if (!port) { if (!port) {
return this.ports.A.pid(address) ? this.ports.A.pid(address) : this.ports.B.pid(address) return this.ports.A.pid(address)
} }
return this.ports[port].pid(address) return this.ports[port].pid(address)
} }