working interrupt with both ports

master
David Kebler 2017-01-31 23:28:09 -08:00
parent e333378c36
commit 39b0e43d56
1 changed files with 17 additions and 17 deletions

View File

@ -10,6 +10,7 @@ class MCP23008 extends Device {
// this.registers = registers // load in register database
this.ports = {}
opts.portID = 'A'
opts.pids = opts.pids ? opts.pids : opts.pidsA
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
@ -34,6 +35,7 @@ class MCP23008 extends Device {
}
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) }
inter(port = 'A') {
return this.ports[port].interrupt
@ -61,7 +63,7 @@ class MCP23008 extends Device {
let pin = pins[i]
byte += pin.address.toFmt('DEC') * pin.cfg[setting]
}
// console.log(`port ${port} - setting ${setting} - reg ${reg} - byte ${byte}`)
//console.log(`port ${port} - setting ${setting} - reg ${reg} - byte ${byte}`)
jobs.push(
super.write(reg, byte).then(() => Promise.resolve(`config: wrote ${byte} to register ${reg} on port ${port}`))
)
@ -71,16 +73,14 @@ class MCP23008 extends Device {
} // end writePinsCfg
// reads all pins in all ports
readPins(cmd, fmt) {
readPins(cmd = 'gpio', fmt) {
let jobs = []
cmd = cmd ? cmd : 'gpio'
let reg = registers.pin_cmd[cmd]
for (let port in this.ports) {
jobs.push(() => super.read(reg))
reg += 0x10
}
return _u.pSeries(jobs)
}
pinRead(id, opts) {
@ -94,22 +94,21 @@ class MCP23008 extends Device {
reject('no pin found for given id')
}
if (gpio.port === 'B') {
reg = reg + 16
reg = reg + 0x10
}
// call device class read (response is always decimal)
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')
let addr = gpio.adr.toFmt('ARY')
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
// .then(state => console.log(`pin state ${state}`))
// .catch(err => console.log(err)) // end Promise
} // end pinRead
pinWrite(id, opts) {
@ -148,6 +147,7 @@ class MCP23017 extends MCP23008 {
super(busObj, i2cAddress, opts)
// add a second port
opts.portID = 'B'
opts.pids = opts.pidsB
this.ports.B = new Port(opts)
this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt
}
@ -159,6 +159,13 @@ class MCP23017 extends MCP23008 {
return this.ports[port].pin(id)
}
pid(address, port) {
if (!port) {
return this.ports.A.pid(address) ? this.ports.A.pid(address) : this.ports.B.pid(address)
}
return this.ports[port].pid(address)
}
} // end MCP23017 Class
module.exports.MCP23017 = MCP23017
@ -175,7 +182,7 @@ let chip_config = {
// byte: ['NULL','INTPOL','ODR','HAEN','DISSLW','SEQOP','MIRROR','BANK'] // see page 18 of 23017 datasheet for 8 setting details
cmd: 0x0A, // IOCON.BANK=0 (msb) at powerup so need to use 0x0A, if set to 1 then use
default: {
val: '10100010', // Split Banks port A + 0x10 = Port B,(ignored by 23008), Sequential operation disabled, open drain int
val: '10100010', // Split Banks port A + 0x10 = Port B,(ignored by 23008), Sequential operation disabled, active high=pulldown
fmt: 'STR'
},
oneint: {
@ -184,13 +191,6 @@ let chip_config = {
}
}
// function chipSetByte(setting = 'default') {
// setting = chip_config[setting]
// return _u.byteFormat(setting.val, { in: setting.fmt,
// out: 'DEC'
// })
// }
let registers = {
pin_config: {
dir: 0,