2018-03-04 15:13:55 -08:00
|
|
|
import MCP230XX from './mcp230xx'
|
2019-02-14 14:01:30 -08:00
|
|
|
import { byteFormat } from '@uci-utils/byte'
|
2018-03-04 15:13:55 -08:00
|
|
|
|
2019-02-14 14:01:30 -08:00
|
|
|
import logger from '@uci-utils/logger'
|
2018-03-04 15:13:55 -08:00
|
|
|
let log = {}
|
|
|
|
|
2019-03-08 09:09:48 -08:00
|
|
|
log = logger({
|
|
|
|
file: 'src/mcp230xxi.js',
|
|
|
|
class: 'MCP230XXi',
|
|
|
|
name: 'mcp-interrupt',
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
class MCP230XXi extends MCP230XX {
|
2019-03-07 07:48:16 -08:00
|
|
|
constructor(pins, options={}) {
|
|
|
|
if (!Array.isArray(pins)) { options = pins; pins = options.interrupt.pins} // if pins sent via .interrupt.pins
|
2019-03-08 09:09:48 -08:00
|
|
|
let opts = Object.assign({},options) // don't allow passed options to mutate
|
|
|
|
if (opts.interrupt) delete opts.interrupt.pins // if .interrupt was passed then .pins must be removed
|
|
|
|
delete opts.sockets // .sockets is used by uci base so clear it if was used by enduser and sent by mistake
|
|
|
|
log.debug({msg:'passed options before setting',options:opts})
|
2018-03-04 15:13:55 -08:00
|
|
|
super(opts)
|
2019-04-11 21:15:26 -07:00
|
|
|
this.opts = opts
|
2019-03-08 09:09:48 -08:00
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
log = logger({
|
|
|
|
file: 'src/mcp230xxi.js',
|
|
|
|
class: 'MCP230XXi',
|
2019-03-08 09:09:48 -08:00
|
|
|
name: 'mcp-interrupt',
|
2019-01-01 19:45:25 -08:00
|
|
|
id: this.id
|
|
|
|
})
|
2019-03-08 09:09:48 -08:00
|
|
|
|
|
|
|
log.info({ opts: opts, msg:'mcp interrupt options after calling super() on base'})
|
|
|
|
|
2018-03-04 15:13:55 -08:00
|
|
|
pins.forEach(pin => {
|
2019-01-01 19:45:25 -08:00
|
|
|
this[pin] = opts['i' + pin] || {}
|
2018-03-04 15:13:55 -08:00
|
|
|
})
|
|
|
|
this.pins = pins
|
2019-04-11 21:15:26 -07:00
|
|
|
this.commands.interrupt = this.bindFuncs(icommands) // add interrupt pin commands to base set in "command.js"
|
|
|
|
this.addNamespace('commands', 'c') // add access via push to same commands
|
|
|
|
this._interruptProcess = process
|
2018-07-31 10:29:11 -07:00
|
|
|
this.ready = false
|
2019-01-01 19:45:25 -08:00
|
|
|
log.info({ opts: opts, pins: pins }, 'options for mcp interrupt processor')
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
async init() {
|
2019-04-11 21:15:26 -07:00
|
|
|
if (this.ipath) await this.addSocket('inter-n','s','n',{path:this.ipath})
|
|
|
|
if (this.iport) await this.addSocket('inter-t','s','t',{port:this.iport})
|
2018-03-04 15:13:55 -08:00
|
|
|
await super.init()
|
2019-04-11 21:15:26 -07:00
|
|
|
// this will set default type to internal pullup, only need to to change indivial pins to external if desired
|
2019-04-20 16:44:32 -07:00
|
|
|
await this.commands.pin.cfg({port:'A',pins:'all',cfg:'input_interrupt'})
|
2019-04-11 21:15:26 -07:00
|
|
|
let status = await this._resetInterrupt('A')
|
|
|
|
log.debug('configure all port A pins as interrupts. Reset port A interrupt', status)
|
|
|
|
if (this.chip17) {
|
2019-04-20 16:44:32 -07:00
|
|
|
await this.commands.pin.cfg({port:'B',pins:'all',cfg:'input_interrupt'})
|
2019-04-11 21:15:26 -07:00
|
|
|
let status = await this._resetInterrupt('B')
|
|
|
|
log.debug('configure all port B pins as interrupts. Reset port B interrupt', status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async _readyInterrupt(port) {
|
|
|
|
let istate = await this.bus.read(port !== 'B' ? 0x07 : 0x17)
|
|
|
|
if (istate.response) {
|
|
|
|
this.ready = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
else {
|
2019-01-01 19:45:25 -08:00
|
|
|
this.ready = true
|
2019-04-11 21:15:26 -07:00
|
|
|
return true
|
2019-03-08 09:09:48 -08:00
|
|
|
}
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:15:26 -07:00
|
|
|
async _resetInterrupt(port) {
|
2019-03-08 09:09:48 -08:00
|
|
|
log.debug(`resetting interrupt for port ${port || 'A'},${this.id} arg ${port !== 'B' ? 0x08 : 0x18}`)
|
2019-04-11 21:15:26 -07:00
|
|
|
await this.bus.read(port !== 'B' ? 0x08 : 0x18) // 0x08 is intcap interrupt capture register
|
2019-04-20 16:44:32 -07:00
|
|
|
console.log('reset interrupt==============')
|
2019-04-11 21:15:26 -07:00
|
|
|
return(await this._readyInterrupt(port))
|
|
|
|
}
|
|
|
|
|
|
|
|
_getPortByPin(pin) {
|
|
|
|
let port = this[pin] ? this[pin].mport : null
|
|
|
|
let index = this.pins.map(pin => pin.toString()).indexOf(pin.toString())
|
|
|
|
port = port || index <= 0 ? 'A' : 'B'
|
|
|
|
return port
|
2018-07-31 17:11:43 -07:00
|
|
|
}
|
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
interruptProcessor(func) {
|
|
|
|
this._interruptProcess = func
|
|
|
|
}
|
2019-04-11 21:15:26 -07:00
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
} // end of MCP230XXi Class
|
2018-03-04 15:13:55 -08:00
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
export default MCP230XXi
|
2018-03-04 15:13:55 -08:00
|
|
|
|
2019-04-11 21:15:26 -07:00
|
|
|
|
|
|
|
// default processor
|
|
|
|
function process(details) {
|
|
|
|
details.id = this.id
|
|
|
|
console.log('----default interrupt processor for mcp instance----')
|
|
|
|
console.log('here is where you could either locally take some action for send/push on a message to another process')
|
|
|
|
console.log('create your own function and register it with .interruptProcesser(function)')
|
|
|
|
console.log(this.id)
|
|
|
|
console.dir(details)
|
|
|
|
}
|
|
|
|
|
2018-07-31 17:11:43 -07:00
|
|
|
// commands to be added to pin packet command functions
|
2019-04-11 21:15:26 -07:00
|
|
|
const icommands = {
|
|
|
|
status: async function(packet) {
|
|
|
|
// pin is interrupt pin on sbc for port
|
|
|
|
let port = packet.port || this._getPortByPin(packet.pin)
|
|
|
|
let res = await this._readyInterrupt(port)
|
|
|
|
return {cmd:'reply', request:'interrupt.status', port:port, ipin:packet.pin, ready:res}
|
|
|
|
},
|
|
|
|
reset: async function(packet) {
|
|
|
|
// pin is interrupt pin on sbc for port
|
|
|
|
let port = packet.port || this._getPortByPin(packet.pin)
|
|
|
|
log.info({ packet: packet, port:port, msg:`remote interrupt reset for port ${port}`})
|
|
|
|
await this._resetInterrupt(port)
|
|
|
|
let res = await this._readyInterrupt(port)
|
|
|
|
return {cmd:'reply', request:'interrupt.reset', port:port, ipin:packet.pin, ready:res}
|
|
|
|
},
|
|
|
|
// given a gpio interrupt then push a packet with cmd: 'pin.interrupt.find' and pin: the gpio pin number
|
|
|
|
find: async function(inter) {
|
2019-04-20 16:44:32 -07:00
|
|
|
console.log('interrupt find calledxxxxx', inter, this.ready)
|
2019-04-11 21:15:26 -07:00
|
|
|
if (this.ready) {
|
|
|
|
// protects tripped interrupt before it's fully initialized, or interrupt requests arriving before porcessing is complete
|
|
|
|
this.ready = false
|
|
|
|
log.debug({msg:'raw packet from interrupt, finding pin that caused interrupt', inter:inter})
|
|
|
|
let packet = { pins: 'all', reg: 'intf' }
|
|
|
|
packet.port = inter.port || this._getPortByPin(inter.pin)
|
2019-04-20 16:44:32 -07:00
|
|
|
let res = await this.commands.pin.status(packet) // read port interrupt status
|
2019-04-11 21:15:26 -07:00
|
|
|
let status = await this._resetInterrupt(packet.port)
|
2019-04-20 16:44:32 -07:00
|
|
|
// console.log('interrupt reset ready?', status)
|
2019-04-11 21:15:26 -07:00
|
|
|
log.debug({interrupt:res, reset:status, msg:'interrupt read and reset'})
|
|
|
|
this.ready = true
|
|
|
|
if (!res.status) {
|
|
|
|
log.warn({inter:inter, msg:'no pin associated with interrupt'})
|
|
|
|
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
|
|
|
|
if (packet.pins) { // avoid bad interrupt (i.e. no pin caused interrupt)
|
|
|
|
res.ipin = inter.pin
|
|
|
|
res.port = packet.port
|
|
|
|
delete inter.cmd; delete inter._header; delete inter.pin
|
|
|
|
Object.assign(res,inter)
|
|
|
|
delete res.status
|
2019-04-20 16:44:32 -07:00
|
|
|
res.interrupt_ready = status || false
|
|
|
|
res.state = (await this.commands.pin.status(packet)).status.pins[0][1]
|
|
|
|
// console.log('emitting local on interrupt')
|
|
|
|
this.emit('interrupt',Object.assign({},res)) // emit for end user purposes, don't allow mutation
|
|
|
|
// this.emit('interrupt') // emit for end user purposes, don't allow mutation
|
|
|
|
// console.log('============emitted now calling processor=======================================')
|
2019-04-11 21:15:26 -07:00
|
|
|
this._interruptProcess(res)
|
2019-01-01 19:45:25 -08:00
|
|
|
return res
|
2019-04-11 21:15:26 -07:00
|
|
|
} else {
|
|
|
|
log.warn({inter:inter, msg:'no pin associated with interrupt'})
|
|
|
|
return { error: 'no pin associated with interrupt' }
|
2018-07-31 17:11:43 -07:00
|
|
|
}
|
2018-07-31 10:29:11 -07:00
|
|
|
}
|
2018-03-04 15:13:55 -08:00
|
|
|
}
|
|
|
|
}
|