adding interrupt class for gpio pins on RPi and the like

master
David Kebler 2017-01-18 20:31:53 -08:00
parent 1c7af6009f
commit 4e4a43a099
1 changed files with 19 additions and 15 deletions

View File

@ -4,25 +4,29 @@ const Device = require('uci-dev').Device,
_u = require('uci-utils') _u = require('uci-utils')
class MCP23008 extends Device { class MCP23008 extends Device {
constructor(busObj, i2cAddress, opts) { constructor(busObj, i2cAddress, opts = {}) {
super(busObj, i2cAddress, opts) super(busObj, i2cAddress, opts)
// opts could pass in array of custom pin config, or single for all, or anything // opts could pass in array of custom pin config, or single for all, or anything
// this.registers = registers // load in register database // this.registers = registers // load in register database
this.ports = {} this.ports = {}
opts.portID = 'A' opts.portID = 'A'
this.ports.A = new Port(opts) this.ports.A = new Port(opts)
// if not specified there RPi is not managing the interrupt this.chip_config = opts.chip_config
// pin number on RPi that is connected to and services the interrupt. 4/17/27/22 = 7/11/13/15 if (opts.interrupt) {
if (opts.interPin) { this.ports.A.interrupt = opts.interruptA ? opts.interruptA : opts.interrupt
this.ports.A.interPin = opts.interPin.A ? opts.interPin.A : opts.interPin
} }
} // end constructor } // end constructor
init() { init() {
// console.log('chip configuration', chip_config.cmd, chipSetByte()) // console.log('chip configuration', chip_config.cmd, chipSetByte())
// console.log(super.write.toString()) // console.log(super.write.toString())
return super.write(chip_config.cmd, chipSetByte()) // configure chip return super.write(chip_config.cmd, chipSetByte(this.chip_config)) // configure chip
.then(() => { return this.writePinsCfg() }) .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 port in this.ports) {
for (let setting in registers.pin_config) { for (let setting in registers.pin_config) {
let reg = registers.pin_config[setting] 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 byte = 0;
let pins = this.ports[port].allPins let pins = this.ports[port].allPins
for (let i = 0; i < 8; i++) { for (let i = 0; i < 8; i++) {
@ -129,8 +133,8 @@ class MCP23017 extends MCP23008 {
// add a second port // add a second port
opts.portID = 'B' opts.portID = 'B'
this.ports.B = new Port(opts) this.ports.B = new Port(opts)
if (opts.interPin) { if (opts.interrupt) {
this.ports.B.interPin = opts.interPin.B ? opts.interPin.B : opts.interPin 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 * this will make reg addresses be equilvant for 23008 and PortA of 23017
* reg addresses in the config objects are all in Hexidecminal * 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 = { let chip_config = {
// byte: ['NULL','INTPOL','ODR','HAEN','DISSLW','SEQOP','MIRROR','BANK'] // see page 18 of 23017 datasheet for 8 setting details // 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: { 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' fmt: 'STR'
}, },
twoints: { oneint: {
val: '00000001', // int pins separate, port A + 0x10 = Port B -- ignored by 23008 val: '11000000', // int pins connected, port A + 0x10 = Port B -- ignored by 23008
fmt: 'STR' fmt: 'STR'
} }
} }