2017-01-12 20:05:20 -08:00
|
|
|
'use strict'
|
2017-01-16 21:41:42 -08:00
|
|
|
const Device = require('uci-dev').Device,
|
2017-01-13 19:51:55 -08:00
|
|
|
Port = require('uci-gpio').Port,
|
2017-05-20 17:13:14 -07:00
|
|
|
_u = require('uci-utils'),
|
|
|
|
pause = _u.pPause
|
2017-01-12 20:05:20 -08:00
|
|
|
|
|
|
|
class MCP23008 extends Device {
|
2017-01-18 20:31:53 -08:00
|
|
|
constructor(busObj, i2cAddress, opts = {}) {
|
2017-01-12 20:05:20 -08:00
|
|
|
super(busObj, i2cAddress, opts)
|
|
|
|
// opts could pass in array of custom pin config, or single for all, or anything
|
|
|
|
// this.registers = registers // load in register database
|
|
|
|
this.ports = {}
|
|
|
|
opts.portID = 'A'
|
2017-01-31 23:28:09 -08:00
|
|
|
opts.pids = opts.pids ? opts.pids : opts.pidsA
|
2017-01-12 20:05:20 -08:00
|
|
|
this.ports.A = new Port(opts)
|
2017-01-27 21:05:43 -08:00
|
|
|
this.chip_config = opts.chip_config // TODO allow opts.chip_config to be a byte instead of a string pointer
|
2017-01-21 12:22:18 -08:00
|
|
|
this.ports.A.interrupt = opts.interruptA ? opts.interruptA : opts.interrupt
|
2017-05-20 17:13:14 -07:00
|
|
|
// if (this.ports.A.interrupt) { this.ports.A.interrupt.port = 'A' }
|
2017-01-21 12:22:18 -08:00
|
|
|
|
2017-01-12 20:05:20 -08:00
|
|
|
} // end constructor
|
|
|
|
|
2017-05-20 17:13:14 -07:00
|
|
|
async init() {
|
2017-01-16 16:26:28 -08:00
|
|
|
// console.log('chip configuration', chip_config.cmd, chipSetByte())
|
2017-05-20 17:13:14 -07:00
|
|
|
// console.log(`begin initialize ${this.id}`)
|
|
|
|
await this.writeChipCfg(this.chip_config)
|
|
|
|
// let jobs = [this.writeChipCfg(this.chip_config)] // configure chip
|
|
|
|
// for (let port in this.ports) {
|
|
|
|
// if (this.inter(port)) {
|
|
|
|
// console.log(`initialize interrupt port ${port}`)
|
|
|
|
// jobs.push(
|
|
|
|
// this.inter(port).init()
|
|
|
|
// .then(sbci => {
|
|
|
|
// this.inter(port).sbci = sbci
|
|
|
|
// console.log(`sbci ${this.inter(port).sbci}`)
|
|
|
|
// })
|
|
|
|
// )
|
|
|
|
// // sbci = single board computer interrrupt to distinguish it from interrupt on MCP
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// jobs.push(this.writePinsCfg())
|
|
|
|
await this.writePinsCfg()
|
|
|
|
|
|
|
|
// return _u.pSeries(jobs)
|
2017-01-12 20:05:20 -08:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:13:14 -07:00
|
|
|
async start() {
|
|
|
|
console.log(`begin starting ${ this.id }`)
|
2017-02-21 21:40:09 -08:00
|
|
|
for (let port in this.ports) {
|
|
|
|
if (this.inter(port)) {
|
2017-05-20 17:13:14 -07:00
|
|
|
await this.read(portReg(0x08, port))
|
|
|
|
await pause(200) // give enough time for mcp to reset its interupt
|
|
|
|
this.inter(port).start()
|
2017-02-21 21:40:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:13:14 -07:00
|
|
|
// 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(resp => {
|
|
|
|
// console.log(resp)
|
|
|
|
// // this.inter(port).on('fired', () => {
|
|
|
|
// // console.log(`
|
|
|
|
//interrupt port $ { port } hook me\ n $ { relays }
|
|
|
|
// `)
|
|
|
|
// // // hook.bind(this)(port)
|
|
|
|
// return Promise.resolve()
|
|
|
|
// })
|
|
|
|
// return Promise.resolve(`
|
|
|
|
//interrupt port $ { port } started `)
|
|
|
|
// })
|
|
|
|
// .catch(err => console.log("error:", err))
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return Promise.all(starts)
|
|
|
|
// }
|
|
|
|
|
2017-01-12 20:05:20 -08:00
|
|
|
pin(id) { return this.ports.A.pin(id) } // get a reference to a particular pin's object
|
2017-02-21 21:40:09 -08:00
|
|
|
|
2017-02-01 22:51:48 -08:00
|
|
|
pid(address) { return this.ports.A.pid(address) } // return pin id for a given address on a port
|
2017-01-12 20:05:20 -08:00
|
|
|
|
2017-02-21 21:40:09 -08:00
|
|
|
portByPin(id) {
|
|
|
|
if (this.ports.A.pin(id)) { return 'A' }
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:13:14 -07:00
|
|
|
// get a handle to the ports interrupt
|
2017-01-24 14:46:01 -08:00
|
|
|
inter(port = 'A') {
|
|
|
|
return this.ports[port].interrupt
|
|
|
|
}
|
2017-01-21 12:22:18 -08:00
|
|
|
|
2017-01-27 21:05:43 -08:00
|
|
|
writeChipCfg(cfg = 'default') {
|
2017-05-20 17:13:14 -07:00
|
|
|
// console.log(`writing mcp chip config ${this.id}`)
|
2017-01-27 21:05:43 -08:00
|
|
|
let setting = chip_config[cfg]
|
|
|
|
let byte = _u.byteFormat(setting.val, { in: setting.fmt,
|
|
|
|
out: 'DEC'
|
|
|
|
})
|
|
|
|
return super.write(chip_config.cmd, byte)
|
2017-05-20 17:13:14 -07:00
|
|
|
.then(() => Promise.resolve(`mcp chip config: reg $ { chip_config.cmd } byte $ { byte } written `))
|
2017-01-27 21:05:43 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 20:05:20 -08:00
|
|
|
// pin configurations should already be set before calling
|
|
|
|
writePinsCfg() {
|
2017-05-20 17:13:14 -07:00
|
|
|
// console.log(`writing mcp pins config ${this.id}`)
|
2017-01-12 20:05:20 -08:00
|
|
|
let jobs = [];
|
|
|
|
for (let port in this.ports) {
|
|
|
|
for (let setting in registers.pin_config) {
|
|
|
|
let reg = registers.pin_config[setting]
|
2017-05-15 11:01:29 -07:00
|
|
|
// TODO 0x10 should be based on chip config
|
2017-01-12 20:05:20 -08:00
|
|
|
let byte = 0;
|
|
|
|
let pins = this.ports[port].allPins
|
|
|
|
for (let i = 0; i < 8; i++) {
|
|
|
|
let pin = pins[i]
|
|
|
|
byte += pin.address.toFmt('DEC') * pin.cfg[setting]
|
|
|
|
}
|
2017-05-20 17:13:14 -07:00
|
|
|
//console.log(`port $ { port } - setting $ { setting } - reg $ { reg } - byte $ { byte }`)
|
2017-01-12 20:05:20 -08:00
|
|
|
jobs.push(
|
2017-05-20 17:13:14 -07:00
|
|
|
super.write(portReg(reg, port), byte).then(() => Promise.resolve(`
|
|
|
|
config: wrote $ { byte } to register $ { reg } on port $ { port }
|
|
|
|
`))
|
2017-01-12 20:05:20 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 21:41:42 -08:00
|
|
|
return _u.pSeries(jobs)
|
2017-01-12 20:05:20 -08:00
|
|
|
} // end writePinsCfg
|
|
|
|
|
2017-01-15 17:59:50 -08:00
|
|
|
// reads all pins in all ports
|
2017-01-31 23:28:09 -08:00
|
|
|
readPins(cmd = 'gpio', fmt) {
|
2017-01-15 17:59:50 -08:00
|
|
|
let jobs = []
|
|
|
|
let reg = registers.pin_cmd[cmd]
|
|
|
|
for (let port in this.ports) {
|
2017-05-15 11:01:29 -07:00
|
|
|
jobs.push(() => super.read(portReg(reg, port)))
|
2017-01-15 17:59:50 -08:00
|
|
|
}
|
2017-01-16 21:41:42 -08:00
|
|
|
return _u.pSeries(jobs)
|
2017-01-15 17:59:50 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 20:05:20 -08:00
|
|
|
pinRead(id, opts) {
|
2017-01-15 17:59:50 -08:00
|
|
|
let mcpdev = this;
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
let cmd = opts.cmd ? opts.cmd : 'gpio'
|
|
|
|
let fmt = opts.fmt ? opts.fmt : { in: 'PLC', out: 'PLC' }
|
|
|
|
let reg = registers.pin_cmd[cmd]
|
|
|
|
let gpio = mcpdev.pin(id)
|
|
|
|
if (!gpio) {
|
|
|
|
reject('no pin found for given id')
|
|
|
|
}
|
2017-02-21 21:40:09 -08:00
|
|
|
console.log('id of fired pin', id)
|
|
|
|
console.log('port of fired pin', mcpdev.portByPin(id))
|
|
|
|
return mcpdev.read(portReg(reg, mcpdev.portByPin(id))).then(resp => {
|
2017-02-02 01:41:42 -08:00
|
|
|
resp = _u.byteFormat(resp, { in: 'DEC',
|
|
|
|
out: 'ARY'
|
|
|
|
})
|
|
|
|
let addr = gpio.adr.toFmt('ARY')
|
|
|
|
// console.log(addr)
|
|
|
|
// console.log(resp)
|
|
|
|
resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0
|
|
|
|
}).catch(err => reject(err)) // end Promise
|
|
|
|
}) // end promise
|
|
|
|
|
2017-01-15 17:59:50 -08:00
|
|
|
} // end pinRead
|
|
|
|
|
|
|
|
pinWrite(id, opts) {
|
2017-01-12 20:05:20 -08:00
|
|
|
let mcpdev = this;
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
let cmd = opts.cmd ? opts.cmd : 'gpio'
|
|
|
|
let reg = registers.pin_cmd[cmd]
|
|
|
|
let gpio = mcpdev.pin(id, opts.port)
|
|
|
|
if (!gpio) {
|
|
|
|
reject('no pin found for given id')
|
|
|
|
}
|
|
|
|
if (gpio.port === 'B') {
|
|
|
|
reg = reg + 16
|
|
|
|
}
|
|
|
|
// call device class read
|
2017-02-02 01:41:42 -08:00
|
|
|
// console.log('port - reg', gpio.port, reg)
|
2017-01-12 20:05:20 -08:00
|
|
|
return mcpdev.read(reg).then(resp => {
|
|
|
|
resp = _u.byteFormat(resp, { in: 'DEC',
|
|
|
|
out: 'ARY'
|
|
|
|
})
|
|
|
|
let addr = gpio.pin.address.toFmt('ARY')
|
2017-02-02 01:41:42 -08:00
|
|
|
// // console.log(addr)
|
|
|
|
// console.log(resp)
|
2017-01-12 20:05:20 -08:00
|
|
|
resolve(_u.sum(_u.and(addr, resp))) // resolve 1 or 0
|
|
|
|
})
|
|
|
|
})
|
2017-05-20 17:13:14 -07:00
|
|
|
// .then(state => console.log(`pin state $ { state }`))
|
2017-01-12 20:05:20 -08:00
|
|
|
.catch(err => console.log(err)) // end Promise
|
|
|
|
} // end pinRead
|
|
|
|
|
|
|
|
} // end 23008
|
|
|
|
module.exports.MCP23008 = MCP23008
|
|
|
|
|
|
|
|
class MCP23017 extends MCP23008 {
|
|
|
|
constructor(busObj, i2cAddress, opts) {
|
|
|
|
super(busObj, i2cAddress, opts)
|
|
|
|
// add a second port
|
|
|
|
opts.portID = 'B'
|
2017-01-31 23:28:09 -08:00
|
|
|
opts.pids = opts.pidsB
|
2017-01-12 20:05:20 -08:00
|
|
|
this.ports.B = new Port(opts)
|
2017-01-21 12:22:18 -08:00
|
|
|
this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt
|
2017-05-20 17:13:14 -07:00
|
|
|
// if (this.ports.B.interrupt) { this.ports.B.interrupt.hook = 'B' }
|
2017-01-12 20:05:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pin(id, port) {
|
|
|
|
if (!port) {
|
|
|
|
return this.ports.A.pin(id) ? this.ports.A.pin(id) : this.ports.B.pin(id)
|
|
|
|
}
|
|
|
|
return this.ports[port].pin(id)
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:28:09 -08:00
|
|
|
pid(address, port) {
|
|
|
|
if (!port) {
|
2017-02-02 01:41:42 -08:00
|
|
|
return this.ports.A.pid(address)
|
2017-01-31 23:28:09 -08:00
|
|
|
}
|
|
|
|
return this.ports[port].pid(address)
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:40:09 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-12 20:05:20 -08:00
|
|
|
} // end MCP23017 Class
|
|
|
|
module.exports.MCP23017 = MCP23017
|
|
|
|
|
2017-05-20 17:13:14 -07:00
|
|
|
// ==============================
|
|
|
|
|
2017-02-21 21:40:09 -08:00
|
|
|
function portReg(reg, port) {
|
|
|
|
if (port === 'B') { return reg += 0x10 } else { return reg }
|
|
|
|
}
|
|
|
|
|
2017-01-12 20:05:20 -08:00
|
|
|
/* ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
|
|
|
|
* or see MCP23017.pdf and MCP 23008.pdf in /docs
|
|
|
|
* see table 1.5 and details in sections 1.6.x following
|
|
|
|
* !!! for 23017 MUST initialize with bit 7 "BANK" of IOCON = 1 for the addresses below
|
|
|
|
* then for all registers add 16 (0x10) to each reg address to talk to PortB pins
|
|
|
|
* this will make reg addresses be equilvant for 23008 and PortA of 23017
|
|
|
|
* reg addresses in the config objects are all in Hexidecminal
|
|
|
|
*/
|
2017-01-18 20:31:53 -08:00
|
|
|
// Chip Configuration to be used with Register See Page 18 of 23017 doc
|
2017-01-12 20:05:20 -08:00
|
|
|
let chip_config = {
|
|
|
|
// byte: ['NULL','INTPOL','ODR','HAEN','DISSLW','SEQOP','MIRROR','BANK'] // see page 18 of 23017 datasheet for 8 setting details
|
2017-01-18 20:31:53 -08:00
|
|
|
cmd: 0x0A, // IOCON.BANK=0 (msb) at powerup so need to use 0x0A, if set to 1 then use
|
2017-01-12 20:05:20 -08:00
|
|
|
default: {
|
2017-01-31 23:28:09 -08:00
|
|
|
val: '10100010', // Split Banks port A + 0x10 = Port B,(ignored by 23008), Sequential operation disabled, active high=pulldown
|
2017-01-12 20:05:20 -08:00
|
|
|
fmt: 'STR'
|
|
|
|
},
|
2017-01-18 20:31:53 -08:00
|
|
|
oneint: {
|
2017-01-27 21:05:43 -08:00
|
|
|
val: '11100100', // same as default execpt int pins connected
|
2017-01-12 20:05:20 -08:00
|
|
|
fmt: 'STR'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let registers = {
|
|
|
|
pin_config: {
|
|
|
|
dir: 0,
|
|
|
|
ivrt: 1,
|
|
|
|
pullup: 6,
|
|
|
|
intr: 2,
|
|
|
|
usedef: 4,
|
|
|
|
defval: 3,
|
|
|
|
},
|
|
|
|
pin_cmd: {
|
|
|
|
intf: 7, // readonly
|
|
|
|
intcap: 8, // readonly
|
|
|
|
gpio: 9, // read/write
|
|
|
|
olat: 10 // read only
|
|
|
|
}
|
|
|
|
}
|