0.2.25 refactor makefunc in multiple to properly combine results from single interrupt calls

master
David Kebler 2020-01-23 22:53:14 -08:00
parent 48b6680585
commit 610aa53a93
3 changed files with 11 additions and 12 deletions

View File

@ -1,7 +1,7 @@
{
"name": "@uci/interrupt",
"main": "src",
"version": "0.2.24",
"version": "0.2.25",
"description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs",
"scripts": {
"single": "node -r esm examples/single",

View File

@ -65,6 +65,8 @@ class Interrupt extends Base {
this.emit('log',{level:'info', msg:`new interrupt pin ${this.pin_num} created and watching`, state:await this.status(), edge:this.edge,debounce:this.wait})
super.init()
} // end init
// manual firing for testing
@ -90,17 +92,13 @@ class Interrupt extends Base {
}
async intervalReset(packet) {
// console.log('intervalReset ---- passed argument',packet)
let interval = typeof packet === 'number'? packet : (packet || {}).interval
console.log('----------intervalReset--------', this.pin_num, interval)
if (!interval || interval<=0) {
clearInterval(this._intervalReset)
this._intervalReset = null
console.log('---------disabled--------')
}
else {
this._intervalReset = setInterval(this.reset.bind(this), interval*1000)
console.log('----------enabled----------')
}
return this._intervalReset ? true : false
}

View File

@ -3,7 +3,6 @@ import Base from '@uci/base'
import logger from '@uci-utils/logger'
let log = {}
// will more easily create a group of sbc pin interrupts
class Interrupts extends Base {
@ -70,7 +69,7 @@ class Interrupts extends Base {
interrupt(pin) { return this._interrupts.get(Number(pin)) } // get a handle to single interrupt
async init() {
super.init()
return Promise.all(
Array.from(this._interrupts).map(inter => {
return inter[1].init()
@ -78,7 +77,6 @@ class Interrupts extends Base {
)
}
// only adds consumer sockets to each interrupt to same socket/server
// alternatively use listen handler and single socket
async addInterSocket(name,type) {
@ -99,14 +97,17 @@ class Interrupts extends Base {
export default Interrupts
async function makefunc(fn, packet={}) {
if (!packet.pin || packet.pin==='all') {
let res = {}
let rpacket = {pins:[]}
for (let inter of this._interrupts.entries()) {
res[inter[0]] = await inter[1][fn](packet)
let ipacket = Object.assign({},packet)
delete(ipacket.cmd);delete(ipacket._header)
let res = await inter[1][fn](ipacket)
res.pin = inter[0]
rpacket.pins.push(res)
}
return Object.assign(packet,res)
return rpacket
}
let pin = isNaN(Number(packet)) ? packet.pin : packet
if (this._interrupts.has(Number(pin))) return await this.interrupt(packet.pin)[fn](packet)