add hook
parent
bba5bb85ca
commit
709fd35adf
16
package.json
16
package.json
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/interrupt",
|
"name": "@uci/interrupt",
|
||||||
"main": "src/interrupt-packet.mjs",
|
"main": "src",
|
||||||
"version": "0.1.1",
|
"version": "0.1.1",
|
||||||
"description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs",
|
"description": "a class for adding interrupt processesing for gpio pins on Raspberry Pi and Similar SBCs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"mock": "sudo SOCKETS_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/mock",
|
"single": "sudo node --require @std/esm examples/single",
|
||||||
"mockl": "sudo DEBUG=true SOCKETS_DIR=/opt/sockets node_modules/.bin/nodemon --require @std/esm examples/mock"
|
"singlelog": "DEBUG=true node_modules/.bin/nodemon --require @std/esm examples/single"
|
||||||
},
|
},
|
||||||
"author": "David Kebler",
|
"author": "David Kebler",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -24,9 +24,14 @@
|
||||||
"url": "https://github.com/uCOMmandIt/uci-interrrupt/issues"
|
"url": "https://github.com/uCOMmandIt/uci-interrrupt/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
|
"homepage": "https://github.com/uCOMmandIt/uci-interrrupt#readme",
|
||||||
"dependencies": {
|
"optionalDependencies": {
|
||||||
"pigpio": "^0.x"
|
"pigpio": "^0.x"
|
||||||
},
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@uci/base": "^0.1.1",
|
||||||
|
"@uci/logger": "0.0.1",
|
||||||
|
"lodash.debounce": "^4.0.8"
|
||||||
|
},
|
||||||
"@std/esm": "cjs",
|
"@std/esm": "cjs",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@std/esm": "^0.22.0",
|
"@std/esm": "^0.22.0",
|
||||||
|
@ -35,6 +40,7 @@
|
||||||
"codecov": "^3.0.0",
|
"codecov": "^3.0.0",
|
||||||
"istanbul": "^0.4.5",
|
"istanbul": "^0.4.5",
|
||||||
"mocha": "^5.0.1",
|
"mocha": "^5.0.1",
|
||||||
"nodemon": "^1.14.3"
|
"nodemon": "^1.14.3",
|
||||||
|
"pigpio-mock": "0.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,8 +42,8 @@ export default class Interrupt extends Base {
|
||||||
log.info({pins:pin, opts:opts},'created interrupt with these opts')
|
log.info({pins:pin, opts:opts},'created interrupt with these opts')
|
||||||
this.pin_num = pin
|
this.pin_num = pin
|
||||||
this.mock = opts.mockG
|
this.mock = opts.mockG
|
||||||
this.wait = opts.wait || 0
|
this.wait = opts.wait || 0 // debounce is off by default
|
||||||
this.dbopts = { maxWait:opts.maxwait || 300, leading: opts.leading || true, trailing:opts.trailing || false}
|
this.dbopts = { maxWait:opts.maxwait || 500, leading: opts.leading || true, trailing:opts.trailing || false}
|
||||||
this.edge = opts.edge || Gpio.RISING_EDGE
|
this.edge = opts.edge || Gpio.RISING_EDGE
|
||||||
this.pin = new Gpio(
|
this.pin = new Gpio(
|
||||||
pin, {
|
pin, {
|
||||||
|
@ -51,6 +51,7 @@ export default class Interrupt extends Base {
|
||||||
pullUpDown: opts.pull || Gpio.PUD_DOWN
|
pullUpDown: opts.pull || Gpio.PUD_DOWN
|
||||||
// do not! set edge here as it will start the emitter -- see pigio js
|
// do not! set edge here as it will start the emitter -- see pigio js
|
||||||
})
|
})
|
||||||
|
this._hook = opts.hook
|
||||||
this.packet = opts.packet || {}
|
this.packet = opts.packet || {}
|
||||||
this.packet.pin = pin
|
this.packet.pin = pin
|
||||||
this.packet.cmd = this.packet.cmd || 'interrupt'
|
this.packet.cmd = this.packet.cmd || 'interrupt'
|
||||||
|
@ -67,20 +68,25 @@ export default class Interrupt extends Base {
|
||||||
this.exit().then((resp) => console.log('\n', resp)) // unexport on cntrl-c
|
this.exit().then((resp) => console.log('\n', resp)) // unexport on cntrl-c
|
||||||
.catch(err => console.log('error:', err))
|
.catch(err => console.log('error:', err))
|
||||||
})
|
})
|
||||||
|
|
||||||
// const pinDebounce = function (processor, wait, options, packet) {
|
// const pinDebounce = function (processor, wait, options, packet) {
|
||||||
// console.log(processor,wait,options,packet)
|
// console.log(processor,wait,options,packet)
|
||||||
// return debounce.bind(this,processor.bind(this,packet),wait, options)
|
// return debounce.bind(this,processor.bind(this,packet),wait, options)
|
||||||
// return debounce.bind(processor.bind(this,packet),wait)
|
// return debounce.bind(processor.bind(this,packet),wait)
|
||||||
// return debounce.bind(null,processor,{ wait:wait})
|
// return debounce.bind(null,processor,{ wait:wait})
|
||||||
// }
|
// }
|
||||||
|
|
||||||
console.log(`starting interrupt on pin ${this.pin_num} with debounce wait/max:${this.wait},${this.maxwait} (0=off)`)
|
|
||||||
let cb = () => {}
|
|
||||||
if (this.wait===0) cb = this.interruptProcess.bind(this,this.packet)
|
|
||||||
// else cb = pinDebounce(this.interruptProcess,this.wait,this.dbopts,this.packet)
|
// else cb = pinDebounce(this.interruptProcess,this.wait,this.dbopts,this.packet)
|
||||||
// else cb = debounce(()=>{console.log('debounced')},this.wait,this.dbopts)
|
|
||||||
else cb = debounce(this.interruptProcess.bind(this,this.packet),this.wait,this.dbopts)
|
let cb = () => {}
|
||||||
|
if (this.wait===0) {
|
||||||
|
cb = this._interruptProcess.bind(this,this.packet)
|
||||||
|
console.log(`starting interrupt on pin ${this.pin_num} without debounce`)
|
||||||
|
log.info({packet:this.packet},`starting interrupt on pin ${this.pin_num} without debounce`)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cb = debounce(this._interruptProcess.bind(this,this.packet),this.wait,this.dbopts)
|
||||||
|
console.log(`starting interrupt on pin ${this.pin_num} with debounce wait:${this.wait} options:${JSON.stringify(this.dbopts)}` )
|
||||||
|
log.info({packet:this.packet, wait:this.wait, options:this.dbopts},`starting interrupt on pin ${this.pin_num} with debounce wait:${this.wait}` )
|
||||||
|
}
|
||||||
this.pin.on('interrupt',cb)
|
this.pin.on('interrupt',cb)
|
||||||
// rock n roll!!, start the pigpio interrupt
|
// rock n roll!!, start the pigpio interrupt
|
||||||
if(!this.mock) this.pin.enableInterrupt(this.edge)
|
if(!this.mock) this.pin.enableInterrupt(this.edge)
|
||||||
|
@ -99,17 +105,29 @@ export default class Interrupt extends Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
// default processor
|
// default processor
|
||||||
interruptProcess (packet) {
|
async _interruptProcess (packet) {
|
||||||
packet.count += 1
|
packet.count += 1
|
||||||
packet.time = new Date().getTime()
|
packet.time = new Date().getTime()
|
||||||
|
if(this._hook) packet = this.hook(packet)
|
||||||
|
console.log('packet from hook',packet)
|
||||||
|
this.send(packet)
|
||||||
|
}
|
||||||
|
|
||||||
|
//sample hook
|
||||||
|
hook (packet) {
|
||||||
|
// new Promise((resolve) => {
|
||||||
console.log('=======================')
|
console.log('=======================')
|
||||||
console.log(`pin ${packet.pin} on sbc gpio bus has thrown an interrupt`)
|
console.log(`pin ${packet.pin} on sbc gpio bus has thrown an interrupt`)
|
||||||
console.log('sending to all connected sockets with default cmd:"interrupt"')
|
console.log('sending to all connected sockets with default cmd:"interrupt"')
|
||||||
console.dir(packet)
|
console.dir(packet)
|
||||||
this.send(packet)
|
console.log('this is the default beforeHook')
|
||||||
console.log('this is the default processor')
|
console.log('add "beforeHook" for your instance or extended class')
|
||||||
console.log('replace "interruptProcess" for your instance or extended class')
|
|
||||||
console.log('=======================')
|
console.log('=======================')
|
||||||
|
return packet
|
||||||
|
// resolve(packet)
|
||||||
|
// })
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // end Class
|
} // end Class
|
||||||
|
|
Loading…
Reference in New Issue