aggreate class with event emitter, add bubble up event emitter from interrupts to class

master
David Kebler 2017-05-30 13:22:45 -07:00
parent 095a11064e
commit c83c0914b7
5 changed files with 48 additions and 27 deletions

View File

@ -21,23 +21,33 @@ let pinB = 16
let id = `${CHIP} at I2C bus address of 0x${ADDR.toString(16)}` let id = `${CHIP} at I2C bus address of 0x${ADDR.toString(16)}`
async function ihandler(port) {
let pin = await this.interruptPin(port, 'PLC')
console.log(`handler fired for ${this.id} on port ${port} pin ${pin}`)
await this.interruptReset(port)
}
let mcp = new MCP[CHIP](bus, ADDR, { let mcp = new MCP[CHIP](bus, ADDR, {
pin_cfg_default: 'toggle_switch', pin_cfg_default: 'toggle_switch',
id: id, id: id,
interruptA: new Interrupt(pinA, ihandler), interruptA: new Interrupt(pinA, ihandler),
interruptB: new Interrupt(pinB, ihandler) interruptB: new Interrupt(pinB, ihandler),
}); stateA: 128,
pidsA: ['sw1', 'sw2', 'sw3', 'sw4', 'sw5', 'sw6', 'sw7', 'sw8'],
pidsB: ['sw9', 'sw10', 'sw11', 'sw12', 'sw13', 'sw14', 'sw15', 'sw16']
}); // must have ; for async immediate function following
(async function () { (async function () {
await mcp.init() await mcp.init()
// start will start the
await mcp.start() await mcp.start()
mcp.on('fired', (data) => {
console.log(`bank event 'fired' data => ${data.bank} on port ${data.port} pin# ${data.pin} pid ${data.pid}`)
})
})() })()
//=================================//
async function ihandler(port) {
let pin = await this.interruptPin(port, 'PLC')
await this.interruptReset(port)
let data = { bank: this.id, port: port, pin: pin, pid: this.pid(pin, port) }
// console.log('data in handler', data)
return data
}

View File

@ -21,7 +21,7 @@ let mcp = new MCP[CHIP](bus, ADDR);
await pause(MS) await pause(MS)
let pins = [1, 7, 8] let pins = [1, 7, 8]
await mcp.on(pins, PORT, 'PLC') await mcp.pinsOn(pins, PORT, 'PLC')
let result = await mcp.readPort(PORT, { format: 'PLC' }) let result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(`${pins} on = current state: ${result.sort()}`) console.log(`${pins} on = current state: ${result.sort()}`)
await pause(MS) await pause(MS)
@ -33,7 +33,7 @@ let mcp = new MCP[CHIP](bus, ADDR);
await pause(MS) await pause(MS)
pins = [2, 1, 7] pins = [2, 1, 7]
await mcp.off(pins, PORT, 'PLC') await mcp.pinsOff(pins, PORT, 'PLC')
result = await mcp.readPort(PORT, { format: 'PLC' }) result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(`${pins} off = current state: ${result.sort()}`) console.log(`${pins} off = current state: ${result.sort()}`)
await pause(MS) await pause(MS)

View File

@ -29,8 +29,9 @@
}, },
"homepage": "https://github.com/uCOMmandIt/uci-mcp#readme", "homepage": "https://github.com/uCOMmandIt/uci-mcp#readme",
"dependencies": { "dependencies": {
"@uci/i2c": "0.0.1", "@uci/i2c": "^0.1.0",
"@uci/utils": "0.0.1" "@uci/utils": "^0.1.0",
"aggregation": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"chai": "^3.5.0", "chai": "^3.5.0",

View File

@ -2,7 +2,9 @@
const Device = require('@uci/i2c').Device, const Device = require('@uci/i2c').Device,
portpin = require('./port-pin'), // classes for MCP port and pins portpin = require('./port-pin'), // classes for MCP port and pins
_ = require('@uci/utils') EventEmitter = require('events'),
_ = require('@uci/utils'),
aggregate = require("aggregation/es6")
const const
Port = portpin.Port, Port = portpin.Port,
@ -10,7 +12,7 @@ const
pause = _.pPause, pause = _.pPause,
debug = _.debug('mcp:23008-17') debug = _.debug('mcp:23008-17')
class MCP23008 extends Device { class MCP23008 extends aggregate(Device, EventEmitter) {
constructor(busObj, i2cAddress, opts = {}) { constructor(busObj, i2cAddress, opts = {}) {
super(busObj, i2cAddress, opts) super(busObj, i2cAddress, opts)
// opts could include options passed on to ports and pin including custom pin config, pin ids...see gpio.js // opts could include options passed on to ports and pin including custom pin config, pin ids...see gpio.js
@ -50,9 +52,12 @@ class MCP23008 extends Device {
debug.L1(`\n=======\nInitializing ${this.id}`) debug.L1(`\n=======\nInitializing ${this.id}`)
await this.writeChipCfg(this.chip_config) // chip settings await this.writeChipCfg(this.chip_config) // chip settings
await this.writePinsCfg() await this.writePinsCfg()
// write out any loaded state // write out any initial state(s)
for (let port in this.ports) { for (let port in this.ports) {
await this.writePort(this.state(port), 'force', port) if (this.state(port)) {
debug.L1(`writing initial port state ${port} ${this.state(port)}`)
await this.writePort(this.state(port), 'force', port)
}
} }
} }
@ -69,14 +74,18 @@ class MCP23008 extends Device {
async startInterrupt(port) { async startInterrupt(port) {
await this.interruptReset(port) await this.interruptReset(port)
debug.L1(`starting interrupt on port ${ port }`)
await this.inter(port).start() await this.inter(port).start()
let chip = this // scope `this` for use in the listener below
// bind handler to the chip so handler can read/write to chip/bank when interrupt is emitted // bind handler to the chip so handler can read/write to chip/bank when interrupt is emitted
debug.L2('handler', this.inter(port).handler) debug.L3('handler', this.inter(port).handler)
let ihandler = this.inter(port).handler.bind(this) let ihandler = this.inter(port).handler.bind(this)
// inside the listener `this` is the interrupt not the chip/bank // inside the listener `this` is the interrupt not the chip/bank
this.inter(port).on('fired', function () { this.inter(port).on('fired', async function () {
debug.L1(`interrupt from ${this.pin_number}`) debug.L1(`interrupt from ${this.pin_number}`)
ihandler(port) let data = await ihandler(port)
debug.L2(`port interrupt event 'fired' data => ${data.bank} on port ${data.port} pin# ${data.pin} pid ${data.pid}`)
chip.emit('fired', data) // emit up the class chain
}) })
} }
@ -119,6 +128,7 @@ class MCP23008 extends Device {
await this.write(portReg(reg, port), byte) await this.write(portReg(reg, port), byte)
} }
} }
debug.L1(`done writing mcp pins config`)
} // end writePinsCfg } // end writePinsCfg
async readPort(port, opts = {}) { async readPort(port, opts = {}) {
@ -160,7 +170,7 @@ class MCP23008 extends Device {
this.ports[port].state.value = byte this.ports[port].state.value = byte
} }
async on(pins, port, format) { async pinsOn(pins, port, format) {
if ('AB'.indexOf(port) === -1) { if ('AB'.indexOf(port) === -1) {
format = port format = port
port = 'A' port = 'A'
@ -174,7 +184,7 @@ class MCP23008 extends Device {
await this.writePort(pins, 'force', port) await this.writePort(pins, 'force', port)
} }
async off(pins, port, format) { async pinsOff(pins, port, format) {
if ('AB'.indexOf(port) === -1) { if ('AB'.indexOf(port) === -1) {
format = port format = port
port = 'A' port = 'A'

View File

@ -32,10 +32,10 @@ function eachpin() {
it('==> test each pin', async function () { it('==> test each pin', async function () {
for (let pin of mcp.ports[PORT].pins) { for (let pin of mcp.ports[PORT].pins) {
await mcp.on(pin.address, PORT) await mcp.pinsOn(pin.address, PORT)
let result = await mcp.readPort(PORT) let result = await mcp.readPort(PORT)
expect(result, `pin ${pin.id} at address ${pin.address} write/read failed`).to.equal(pin.address) expect(result, `pin ${pin.id} at address ${pin.address} write/read failed`).to.equal(pin.address)
await mcp.off(pin.address, PORT) await mcp.pinsOff(pin.address, PORT)
await pause(MS) await pause(MS)
} }
}) })
@ -45,14 +45,14 @@ function somepins() {
it('==> on, off toggle some pins', async function () { it('==> on, off toggle some pins', async function () {
let pins = [1, 7, 8] let pins = [1, 7, 8]
await mcp.on(pins, PORT, 'PLC') await mcp.pinsOn(pins, PORT, 'PLC')
let shouldbe = [1, 7, 8] let shouldbe = [1, 7, 8]
let result = await mcp.readPort(PORT, { format: 'PLC' }) let result = await mcp.readPort(PORT, { format: 'PLC' })
expect(result.sort(), 'pin write/read failed on').to.deep.equal(shouldbe.sort()) expect(result.sort(), 'pin write/read failed on').to.deep.equal(shouldbe.sort())
await pause(MS) await pause(MS)
pins = [2, 1, 7] pins = [2, 1, 7]
await mcp.on(pins, PORT, 'PLC') await mcp.pinsOn(pins, PORT, 'PLC')
shouldbe = [2, 1, 7, 8] shouldbe = [2, 1, 7, 8]
result = await mcp.readPort(PORT, { format: 'PLC' }) result = await mcp.readPort(PORT, { format: 'PLC' })
expect(result.sort(), 'pin write/read failed on 2').to.deep.equal(shouldbe.sort()) expect(result.sort(), 'pin write/read failed on 2').to.deep.equal(shouldbe.sort())
@ -66,7 +66,7 @@ function somepins() {
await pause(MS) await pause(MS)
pins = [2, 1, 6, 7] pins = [2, 1, 6, 7]
await mcp.off(pins, PORT, 'PLC') await mcp.pinsOff(pins, PORT, 'PLC')
shouldbe = [8] shouldbe = [8]
result = await mcp.readPort(PORT, { format: 'PLC' }) result = await mcp.readPort(PORT, { format: 'PLC' })
expect(result.sort(), 'pin write/read failed off').to.deep.equal(shouldbe.sort()) expect(result.sort(), 'pin write/read failed off').to.deep.equal(shouldbe.sort())