103 lines
3.6 KiB
JavaScript
103 lines
3.6 KiB
JavaScript
|
import MCP230XX from './mcp230xx'
|
||
|
import { byteFormat } from '@uci/utils/src/byte'
|
||
|
|
||
|
import logger from '@uci/logger'
|
||
|
let log = {}
|
||
|
|
||
|
// sockets:'inter#s>t', inter:{port:INTERRUPT_PORT}
|
||
|
|
||
|
export default 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#s>t'
|
||
|
opts.inter = {port:opts.iport}
|
||
|
}
|
||
|
if (opts.ipath) {
|
||
|
opts.inter = { path: opts.ipath || 'interrupt'}
|
||
|
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'inter#s>n'
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
pins.forEach( pin => {
|
||
|
let ipin = 'i'+pin
|
||
|
opts[ipin] = 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.sockets = (opts.sockets ? (opts.sockets + ',') : '') + ipin +'#s>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+'#s>n'
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
super(opts)
|
||
|
pins.forEach(pin => {
|
||
|
this[pin] = opts[pin] || {}
|
||
|
})
|
||
|
this.pins = pins
|
||
|
log = logger({file:'src/mcp230xxi.js',class:'MCP230XXi',name:'mcp',id:this.id})
|
||
|
Object.assign(this.pin, this.bindFuncs(pin)) // add interrupt pin commands to base set in "command.js"
|
||
|
this._interruptProcess = null
|
||
|
|
||
|
}
|
||
|
|
||
|
async init(){
|
||
|
await super.init()
|
||
|
this.pins.forEach(async pin => {
|
||
|
// console.log(this.id,pin,this[pin])
|
||
|
// let cfg = {port:this[pin].mport||'A',pins:this[pin].pins||'all', cfg:this[pin].type ||'toggle_switch'}
|
||
|
// console.log(cfg)
|
||
|
await this.pin.cfg({port:this[pin].mport||'A',pins:this[pin].pins||'all', cfg:this[pin].type ||'toggle_switch'})
|
||
|
this.pin.interrupt.reset(this[pin].mport)
|
||
|
})
|
||
|
|
||
|
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
|
||
|
|
||
|
const pin = { interrupt: {
|
||
|
reset: async function (port) {
|
||
|
console.log('resetting interrupt for port',port || 'A',this.id)
|
||
|
return await this.bus.read(port!=='B' ? 0x08 : 0x18) // 0x08 is intcap interrupt capture register
|
||
|
},
|
||
|
find: async function (inter) {
|
||
|
// console.dir(inter)
|
||
|
let packet = {pins:'all',reg:'intf'}
|
||
|
packet.port = inter.port || this[inter.pin].mport || 'A'
|
||
|
// console.log(`${inter.count}th interrupt fired, pin:${inter.pin}`)
|
||
|
// console.log('packet to read port\n',packet.port)
|
||
|
let res = await this.pin.status(packet)
|
||
|
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' })
|
||
|
// console.log('pin and port that caused the interrupt', pin, packet.port)
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|