106 lines
3.2 KiB
JavaScript
106 lines
3.2 KiB
JavaScript
|
|
import bus, { Gpio as gpio } from 'pigpio'
|
|
// import btc from 'better-try-catch'
|
|
// import Base from '@uci/base'
|
|
import Base from '@uci/base'
|
|
|
|
import logger from '@uci/logger'
|
|
let log = {}
|
|
const LOG_OPTS = (id) => {
|
|
return {
|
|
repo:'uci-mcp',
|
|
npm:'@uci/mcp',
|
|
file:'src/mcp230xx-packet.mjs',
|
|
class:'MCP230XX',
|
|
id:id,
|
|
instance_created:new Date().getTime()
|
|
}}
|
|
|
|
export default class Interrupt extends Base {
|
|
constructor(pins,opts={}) {
|
|
|
|
opts.sockets = opts.sockets ? (opts.sockets + ',') : ''
|
|
if (opts.path || opts.itrn) {
|
|
opts.itrn = opts.itrn || {}
|
|
opts.itrn.path = opts.path || opts.itrn.path || (process.env.SOCKETS_DIR || __dirname) + '/interrupt.sock'
|
|
opts.sockets = opts.sockets + 'itrn#c>n,'
|
|
}
|
|
if (opts.itrt || opts.host) {
|
|
opts.itrt = opts.itrt || {}
|
|
opts.itrt.host = opts.host || opts.itrt.host
|
|
opts.itrt.port = opts.itrt.port || opts.port || 1777
|
|
opts.sockets = opts.sockets + 'itrt#c>t'
|
|
}
|
|
if (opts.sockets==='') throw ('must have at least one socket client')
|
|
console.dir(opts)
|
|
super(opts)
|
|
log = logger.child(LOG_OPTS(this.id))
|
|
log.info({pins:pins, opts:opts},'create interrupts with these opts')
|
|
this.mock = opts.mock
|
|
this.mode = gpio.INPUT
|
|
this.pull = opts.pull || gpio.PUD_DOWN
|
|
this.edge = opts.edge || gpio.RISING_EDGE
|
|
this.interrupts = {}
|
|
for(let pin of pins) {
|
|
if (typeof pin==='number') {pin={num:pin}}
|
|
this.interrupts[pin.num] = {
|
|
pin: new gpio(
|
|
pin.num,
|
|
{
|
|
mode: pin.mode || this.mode,
|
|
pullUpDown: pin.pull || this.pull
|
|
// do not! set edge here as it will start the emitter -- see pigio js
|
|
}),
|
|
edge: pin.edge || this.edge,
|
|
socket: pin.socket,
|
|
cmd: pin.cmd
|
|
}
|
|
}
|
|
|
|
} // end constructor
|
|
|
|
|
|
async init(){
|
|
await super.init()
|
|
|
|
// for cntrl-c exit of interrupt
|
|
process.on('SIGINT', () => {
|
|
this.exit().then((resp) => console.log('\n', resp)) // unexport on cntrl-c
|
|
.catch(err => console.log('error:', err))
|
|
})
|
|
for(const pin_num in this.interrupts) {
|
|
let pin = this.interrupts[pin_num].pin
|
|
let edge = this.interrupts[pin_num].edge
|
|
console.log(`starting interrupt on pin ${pin_num}`)
|
|
pin.on('interrupt', this.interruptProcess.bind(this,pin_num))
|
|
// rock n roll!!, start the pigpio interrupt
|
|
console.log('edge=',edge)
|
|
if(!this.mock) pin.enableInterrupt(edge)
|
|
}
|
|
|
|
} // end constructor
|
|
|
|
// manual firing for testing
|
|
fire(pin_num) {
|
|
console.log('manually firing interrupt for pin', pin_num)
|
|
this.interrupts[pin_num].pin.emit('interrupt',null)
|
|
}
|
|
|
|
exit() {
|
|
bus.terminate()
|
|
return Promise.reject('keyboard termination...terminating interrupts')
|
|
}
|
|
|
|
interruptProcess (pin) {
|
|
console.log('=======================')
|
|
console.log(`pin ${pin} on rpi bus has thrown an interrupt`)
|
|
console.log('this is the default processor')
|
|
console.log('replace "interruptProcess" for your instance or extended class')
|
|
console.log('=======================')
|
|
}
|
|
|
|
} // end Class
|
|
|
|
// default handler just logs the pin number fired
|
|
// overwrite with actual socket send with appropriate packet
|