fully working control example with interrupts
parent
cd52c3ffab
commit
3f0d0ad407
|
@ -14,6 +14,7 @@ class MCP23008 extends Device {
|
|||
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.ports.A.interrupt = opts.interruptA ? opts.interruptA : opts.interrupt
|
||||
if (this.ports.A.interrupt) { this.ports.A.interrupt.hook = 'A' }
|
||||
|
||||
} // end constructor
|
||||
|
||||
|
@ -25,7 +26,8 @@ class MCP23008 extends Device {
|
|||
if (this.inter(port)) {
|
||||
// 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()) // set up listeners if interrupts exits
|
||||
|
||||
// jobs.push(this.inter(port).init.bind(this.inter(port))())
|
||||
}
|
||||
}
|
||||
|
@ -96,19 +98,18 @@ class MCP23008 extends Device {
|
|||
if (gpio.port === 'B') {
|
||||
reg = reg + 0x10
|
||||
}
|
||||
console.log('port - reg', gpio.port, reg)
|
||||
// console.log('port - reg', gpio.port, reg)
|
||||
return mcpdev.read(reg).then(resp => {
|
||||
resp = _u.byteFormat(resp, { in: 'DEC',
|
||||
out: 'ARY'
|
||||
})
|
||||
let addr = gpio.adr.toFmt('ARY')
|
||||
console.log(addr)
|
||||
console.log(resp)
|
||||
// console.log(addr)
|
||||
// console.log(resp)
|
||||
resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0
|
||||
})
|
||||
})
|
||||
// .then(state => console.log(`pin state ${state}`))
|
||||
// .catch(err => console.log(err)) // end Promise
|
||||
}).catch(err => reject(err)) // end Promise
|
||||
}) // end promise
|
||||
|
||||
} // end pinRead
|
||||
|
||||
pinWrite(id, opts) {
|
||||
|
@ -124,18 +125,18 @@ class MCP23008 extends Device {
|
|||
reg = reg + 16
|
||||
}
|
||||
// call device class read
|
||||
console.log('port - reg', gpio.port, reg)
|
||||
// console.log('port - reg', gpio.port, reg)
|
||||
return mcpdev.read(reg).then(resp => {
|
||||
resp = _u.byteFormat(resp, { in: 'DEC',
|
||||
out: 'ARY'
|
||||
})
|
||||
let addr = gpio.pin.address.toFmt('ARY')
|
||||
console.log(addr)
|
||||
console.log(resp)
|
||||
// // console.log(addr)
|
||||
// console.log(resp)
|
||||
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
|
||||
} // end pinRead
|
||||
|
||||
|
@ -150,6 +151,7 @@ class MCP23017 extends MCP23008 {
|
|||
opts.pids = opts.pidsB
|
||||
this.ports.B = new Port(opts)
|
||||
this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt
|
||||
if (this.ports.B.interrupt) { this.ports.B.interrupt.hook = 'B' }
|
||||
}
|
||||
|
||||
pin(id, port) {
|
||||
|
@ -161,7 +163,7 @@ class MCP23017 extends MCP23008 {
|
|||
|
||||
pid(address, 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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue