2018-03-04 15:13:55 -08:00
|
|
|
import MCP230XX from './mcp230xx'
|
|
|
|
import { byteFormat } from '@uci/utils/src/byte'
|
|
|
|
|
2018-05-16 07:19:03 -07:00
|
|
|
import logger from '@uci/logger'
|
2018-03-04 15:13:55 -08:00
|
|
|
let log = {}
|
|
|
|
|
2018-07-31 10:29:11 -07:00
|
|
|
// if opts.iport not set then will be generated based on pin number
|
2018-03-04 15:13:55 -08:00
|
|
|
|
|
|
|
export default class MCP230XXi extends MCP230XX {
|
|
|
|
constructor(pins,opts) {
|
|
|
|
if (typeof opts.iport ==='number' || opts.ipath) {
|
|
|
|
if (typeof opts.iport ==='number') {
|
2018-07-31 10:29:11 -07:00
|
|
|
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'inter#c>t'
|
2018-03-04 15:13:55 -08:00
|
|
|
opts.inter = {port:opts.iport}
|
2018-07-31 10:29:11 -07:00
|
|
|
opts.inter.host = opts.ihost || opts.host
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
if (opts.ipath) {
|
|
|
|
opts.inter = { path: opts.ipath || 'interrupt'}
|
2018-07-31 10:29:11 -07:00
|
|
|
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'inter#c>n'
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-07-31 10:29:11 -07:00
|
|
|
pins.forEach( (pin,index) => {
|
2018-03-04 15:13:55 -08:00
|
|
|
let ipin = 'i'+pin
|
|
|
|
opts[ipin] = opts[pin] || {}
|
2018-07-31 10:29:11 -07:00
|
|
|
if (index===1) opts[ipin].mport = opts[ipin].mort || 'B'
|
|
|
|
delete(opts[pin])
|
2018-03-04 15:13:55 -08:00
|
|
|
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
|
2018-07-31 10:29:11 -07:00
|
|
|
opts[ipin].host = opts[ipin].host || opts.ihost || opts.host
|
|
|
|
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + ipin +'#c>t'
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
// 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})
|
2018-07-31 10:29:11 -07:00
|
|
|
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + pin+'#c>n'
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
super(opts)
|
2018-07-31 10:29:11 -07:00
|
|
|
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')
|
2018-03-04 15:13:55 -08:00
|
|
|
pins.forEach(pin => {
|
2018-07-31 10:29:11 -07:00
|
|
|
this[pin] = opts['i'+pin] || {}
|
2018-03-04 15:13:55 -08:00
|
|
|
})
|
|
|
|
this.pins = pins
|
2018-07-31 10:29:11 -07:00
|
|
|
Object.assign(this.commands.pin, this.bindFuncs(ipincommands)) // add interrupt pin commands to base set in "command.js"
|
|
|
|
this.addNamespace('commands','c')
|
2018-03-04 15:13:55 -08:00
|
|
|
this._interruptProcess = null
|
2018-07-31 10:29:11 -07:00
|
|
|
this.ready = false
|
|
|
|
log.info({opts:opts, pins:pins}, 'options for mcp interrupt processor')
|
2018-03-04 15:13:55 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async init(){
|
|
|
|
await super.init()
|
|
|
|
this.pins.forEach(async pin => {
|
2018-07-31 10:29:11 -07:00
|
|
|
let cfg = {port:this[pin].mport||'A',pins:this[pin].pins||'all', cfg:this[pin].type ||'toggle_switch'}
|
|
|
|
await this.pin.cfg(cfg)
|
|
|
|
log.info('resetting mcp port for corresponding gpio pin')
|
|
|
|
await this.pin.interrupt.reset(this[pin].mport)
|
|
|
|
this.ready=true
|
2018-03-04 15:13:55 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
this.on('interrupt', function (details) {
|
|
|
|
details.id = this.id
|
|
|
|
if (!this._interruptProcess) {
|
|
|
|
console.log('----default interrupt processor for mcp instance----')
|
|
|
|
console.log(this.id)
|
|
|
|
console.dir(details)
|
|
|
|
} else this._interruptProcess(details)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
interruptProcessor(func) {this._interruptProcess = func}
|
|
|
|
|
|
|
|
} // end of MCP230XX Class
|
|
|
|
|
2018-07-31 10:29:11 -07:00
|
|
|
// commands to be added to pin command functions
|
|
|
|
const ipincommands= { interrupt: {
|
2018-03-04 15:13:55 -08:00
|
|
|
reset: async function (port) {
|
2018-07-31 10:29:11 -07:00
|
|
|
log.info(`resetting interrupt for port ${port || 'A'},${this.id}`)
|
2018-03-04 15:13:55 -08:00
|
|
|
return await this.bus.read(port!=='B' ? 0x08 : 0x18) // 0x08 is intcap interrupt capture register
|
|
|
|
},
|
2018-07-31 10:29:11 -07:00
|
|
|
// 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 hash packet
|
|
|
|
if(this.ready){ // protects tripped interrupt before it's fully initialized and reset
|
|
|
|
log.info({packet:inter},'finding mcp pin which caused interrupt')
|
|
|
|
let packet = {pins:'all',reg:'intf'}
|
|
|
|
packet.port = inter.port || this[inter.pin].mport || 'A'
|
|
|
|
let res = await this.pin.status(packet)
|
|
|
|
log.info('found pin now resetting mcp port interrupt')
|
|
|
|
await this.pin.interrupt.reset(packet.port)
|
|
|
|
if (!res.status) return {error:'no pin associated with interrupt'}
|
|
|
|
let pin = byteFormat(res.status.port, { in: 'ARY', out: 'PLC' })
|
|
|
|
res.pin = pin[0]
|
|
|
|
packet.pins = pin[0]
|
|
|
|
packet.reg = null
|
|
|
|
res.state = (await this.pin.status(packet)).status.pins[0][1]
|
|
|
|
res.port = packet.port
|
|
|
|
res.count = inter.count
|
|
|
|
res.inter = inter.pin
|
|
|
|
delete res.status
|
|
|
|
this.emit('interrupt',res)
|
|
|
|
return res
|
|
|
|
}
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|