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