adding interrupt class for gpio pins on RPi and the like
parent
1c7af6009f
commit
4e4a43a099
|
@ -4,25 +4,29 @@ const Device = require('uci-dev').Device,
|
|||
_u = require('uci-utils')
|
||||
|
||||
class MCP23008 extends Device {
|
||||
constructor(busObj, i2cAddress, opts) {
|
||||
constructor(busObj, i2cAddress, opts = {}) {
|
||||
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'
|
||||
this.ports.A = new Port(opts)
|
||||
// if not specified there RPi is not managing the interrupt
|
||||
// pin number on RPi that is connected to and services the interrupt. 4/17/27/22 = 7/11/13/15
|
||||
if (opts.interPin) {
|
||||
this.ports.A.interPin = opts.interPin.A ? opts.interPin.A : opts.interPin
|
||||
this.chip_config = opts.chip_config
|
||||
if (opts.interrupt) {
|
||||
this.ports.A.interrupt = opts.interruptA ? opts.interruptA : opts.interrupt
|
||||
}
|
||||
} // end constructor
|
||||
|
||||
init() {
|
||||
// console.log('chip configuration', chip_config.cmd, chipSetByte())
|
||||
// console.log(super.write.toString())
|
||||
return super.write(chip_config.cmd, chipSetByte()) // configure chip
|
||||
.then(() => { return this.writePinsCfg() })
|
||||
return super.write(chip_config.cmd, chipSetByte(this.chip_config)) // configure chip
|
||||
.then(() => {
|
||||
for (let port in this.ports) {
|
||||
if (this.ports[port].interrupt) { this.ports[port].interrupt.init() }
|
||||
}
|
||||
return this.writePinsCfg()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
@ -34,7 +38,7 @@ class MCP23008 extends Device {
|
|||
for (let port in this.ports) {
|
||||
for (let setting in registers.pin_config) {
|
||||
let reg = registers.pin_config[setting]
|
||||
if (port === 'B') { reg = reg + 0x10 }
|
||||
if (port === 'B') { reg = reg + 0x10 } // TODO 0x10 should be based on chip config
|
||||
let byte = 0;
|
||||
let pins = this.ports[port].allPins
|
||||
for (let i = 0; i < 8; i++) {
|
||||
|
@ -129,8 +133,8 @@ class MCP23017 extends MCP23008 {
|
|||
// add a second port
|
||||
opts.portID = 'B'
|
||||
this.ports.B = new Port(opts)
|
||||
if (opts.interPin) {
|
||||
this.ports.B.interPin = opts.interPin.B ? opts.interPin.B : opts.interPin
|
||||
if (opts.interrupt) {
|
||||
this.ports.B.interrupt = opts.interruptB ? opts.interruptB : opts.interrupt
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,16 +156,16 @@ module.exports.MCP23017 = MCP23017
|
|||
* this will make reg addresses be equilvant for 23008 and PortA of 23017
|
||||
* reg addresses in the config objects are all in Hexidecminal
|
||||
*/
|
||||
// Chip cConfiguration to be used with Register
|
||||
// Chip Configuration to be used with Register See Page 18 of 23017 doc
|
||||
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
|
||||
cmd: 0x0A, // IOCON.BANK=0 (msb) at powerup so need to use 0x0A, if set to 1 then use
|
||||
default: {
|
||||
val: '10000000', // int pins connected, port A + 0x10 = Port B -- ignored by 23008
|
||||
val: '10000000', // int pins not connected, port A + 0x10 = Port B -- ignored by 23008
|
||||
fmt: 'STR'
|
||||
},
|
||||
twoints: {
|
||||
val: '00000001', // int pins separate, port A + 0x10 = Port B -- ignored by 23008
|
||||
oneint: {
|
||||
val: '11000000', // int pins connected, port A + 0x10 = Port B -- ignored by 23008
|
||||
fmt: 'STR'
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue