little cleanup

update dependencies
master
David Kebler 2019-01-01 19:45:25 -08:00
parent 16cea2ef02
commit 54a2c237f3
3 changed files with 107 additions and 74 deletions

View File

@ -30,7 +30,7 @@
"homepage": "https://github.com/uCOMmandIt/uci-mcp#readme",
"dependencies": {
"@uci/i2c-device": "^0.1.4",
"@uci/logger": "0.0.4",
"@uci/logger": "0.0.6",
"@uci/utils": "^0.1.1"
},
"devDependencies": {

View File

@ -5,10 +5,15 @@ import commands from './commands'
import logger from '@uci/logger'
let log = {}
export default class MCP230XX extends Device {
class MCP230XX extends Device {
constructor(opts) {
super(opts)
log = logger({file:'src/mcp230xx.js',class:'MCP230XX',name:'mcp',id:this.id})
log = logger({
file: 'src/mcp230xx.js',
class: 'MCP230XX',
name: 'mcp',
id: this.id
})
this.chip17 = opts.chip17
this.commands = this.bindFuncs(commands)
this.addNamespace('commands', 's') // allow access to commands via socket/server
@ -20,7 +25,11 @@ export default class MCP230XX extends Device {
await super.init()
let res = await this.chipcfg({})
let cfg = this.chip17 ? '10100010' : '00100010'
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
if (res.response !== cfg)
throw `could not configure mcp chip at ${
this.address
}=0x${this.address.toString(16)}`
}
} // end of MCP230XX Class
export default MCP230XX

View File

@ -6,42 +6,51 @@ let log = {}
// if opts.iport not set then will be generated based on pin number
export default class MCP230XXi extends MCP230XX {
class MCP230XXi extends MCP230XX {
constructor(pins, opts) {
if (typeof opts.iport === 'number' || opts.ipath) {
if (typeof opts.iport === 'number') {
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'inter#c>t'
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'inter#c>t'
opts.inter = { port: opts.iport }
opts.inter.host = opts.ihost || opts.host
}
if (opts.ipath) {
opts.inter = { path: opts.ipath || 'interrupt' }
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'inter#c>n'
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'inter#c>n'
}
}
else {
} else {
pins.forEach((pin, index) => {
let ipin = 'i' + pin
opts[ipin] = opts[pin] || {}
if (index === 1) opts[ipin].mport = opts[ipin].mort || 'B'
delete(opts[pin])
delete opts[pin]
if (opts[ipin].port || opts.iport !== 'number') {
opts[ipin].port = opts[ipin].port || opts.iport
if (typeof opts[ipin].port !== 'number') opts[ipin].port = 9000 + pin
opts[ipin].host = opts[ipin].host || opts.ihost || opts.host
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + ipin +'#c>t'
opts.sockets =
(opts.sockets ? opts.sockets + ',' : '') + ipin + '#c>t'
}
// either on the same host as bus and interrupt or not - no need for both
else {
opts[pin].path = opts[pin].path || opts.ipath
if(!opts[pin].path) Object.assign(opts[pin],{path:'interrupt:'+pin})
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + pin+'#c>n'
if (!opts[pin].path)
Object.assign(opts[pin], { path: 'interrupt:' + pin })
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + pin + '#c>n'
}
})
}
super(opts)
log = logger({file:'src/mcp230xxi.js',class:'MCP230XXi',name:'mcp',id:this.id})
log.info({opts:opts},'mcp interrupt options after calling super() on base')
log = logger({
file: 'src/mcp230xxi.js',
class: 'MCP230XXi',
name: 'mcp',
id: this.id
})
log.info(
{ opts: opts },
'mcp interrupt options after calling super() on base'
)
pins.forEach(pin => {
this[pin] = opts['i' + pin] || {}
})
@ -51,13 +60,16 @@ export default class MCP230XXi extends MCP230XX {
this._interruptProcess = null
this.ready = false
log.info({ opts: opts, pins: pins }, 'options for mcp interrupt processor')
}
async init() {
await super.init()
this.pins.forEach(async pin => {
let cfg = {port:this[pin].mport||'A',pins:this[pin].pins||'all', cfg:this[pin].type ||'toggle_switch'}
let cfg = {
port: this[pin].mport || 'A',
pins: this[pin].pins || 'all',
cfg: this[pin].type || 'toggle_switch'
}
await this.pin.cfg(cfg)
// shouldn't need this as reset is pushed upon connection to interrupt socket
// log.info('initial resetting of mcp interrupt port for corresponding sbc gpio pin')
@ -73,28 +85,39 @@ export default class MCP230XXi extends MCP230XX {
console.dir(details)
} else this._interruptProcess(details)
})
}
async _reset(port) { // local non-packet hidden command
async _reset(port) {
// local non-packet hidden command
log.info(`resetting interrupt for port ${port || 'A'},${this.id}`)
return await this.bus.read(port !== 'B' ? 0x08 : 0x18) // 0x08 is intcap interrupt capture register
}
interruptProcessor(func) {this._interruptProcess = func}
interruptProcessor(func) {
this._interruptProcess = func
}
} // end of MCP230XXi Class
} // end of MCP230XX Class
export default MCP230XXi
// commands to be added to pin packet command functions
const ipincommands= { interrupt: {
const ipincommands = {
interrupt: {
reset: async function(packet) {
log.info({packet:packet},`'socket has push requested a reset of mcp interrupt port for gpio pin ${packet.pin}`)
log.info(
{ packet: packet },
`'socket has push requested a reset of mcp interrupt port for gpio pin ${
packet.pin
}`
)
let port = this[packet.pin].mport || 'A'
await this._reset(port)
},
// given a gpio interrupt then push a packet with cmd: 'pin.interrupt.find' and pin: the gpio pin number
find: async function (inter) { // inter is a UCI packet
if(this.ready){ // protects tripped interrupt before it's fully initialized, or interrupt requests arriving before porcessing is complete
find: async function(inter) {
// inter is a UCI packet
if (this.ready) {
// protects tripped interrupt before it's fully initialized, or interrupt requests arriving before porcessing is complete
this.ready = false
log.info({ packet: inter }, 'finding mcp pin which caused interrupt')
let packet = { pins: 'all', reg: 'intf' }
@ -106,7 +129,8 @@ const ipincommands= { interrupt: {
res.pin = pin[0]
packet.pins = pin[0]
packet.reg = null
if (packet.pins) { // avoid bad interrupt (i.e. no pin caused interrupt)
if (packet.pins) {
// avoid bad interrupt (i.e. no pin caused interrupt)
res.state = (await this.pin.status(packet)).status.pins[0][1]
res.port = packet.port
res.count = inter.count