if opts .port/.path/.topic/.wport are base number/name to which pin number is added/appended (default being 9000,9100,'interrupt')
for specific port number pass itrt.port,itrn.path,itrm.topic,itrw.port which override if others are present
master
David Kebler 2019-03-06 13:02:08 -08:00
parent d0d8af7f28
commit d656af443e
4 changed files with 21 additions and 15 deletions

View File

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

View File

@ -1,6 +1,5 @@
import Interrupt from './interrupt'
import Interrupts from './interrupts'
export { Interrupt as Interrupt }
export { Interrupts as Interrupts }
export { Interrupt, Interrupts }
export default Interrupt

View File

@ -1,34 +1,40 @@
// import btc from 'better-try-catch'
let bus = {}
import debounce from 'lodash.debounce'
// import throttle from 'lodash.throttle'
// import debounce from 'debounce-fn'
import Base from '@uci/base'
import logger from '@uci/logger'
import logger from '@uci-utils/logger'
let log = {}
// a pin makes a socket (server/listner) for each pin to which a consumer can be connected
class Interrupt extends Base {
constructor(pin, opts = {}) {
opts.conPacket = (opts.resetCmd && !opts.conPacket) ? opts.conPacket : { cmd: opts.resetCmd, pin: pin } // will use either option
// if opts .port/.path/.topic/.wport are base number/name to which pin number is added/appended (default being 9000,9100,'interrupt')
// for specific port number pass itrt.port,itrn.path,itrm.topic,itrw.port which override it if present
// conPacket is for uci socket. This will send this command on connect, needed to initially reset the interrupt
if (opts.path || opts.itrn) {
opts.itrn = opts.itrn || {}
opts.itrn.path = opts.path || opts.itrn.path || 'interrupt:' + pin
if (opts.path && typeof opts.path !=='boolean') opts.path = opts.path + ':' + pin
if (typeof opts.path ==='boolean') opts.path = ''
opts.itrn.path = opts.itrn.path || opts.path || 'interrupt:' + pin
opts.itrn.conPacket = opts.conPacket
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'itrn#s>n'
}
if (opts.topics || opts.itrm) {
if (opts.topic || opts.itrm) {
opts.itrm = opts.itrm || {}
opts.itrm.topics = opts.topics || 'interrupt/' + pin
opts.itrm.topics = opts.itrm.topic || opts.topic +'/'+ pin || 'interrupt/' + pin
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'itrm#s>m'
}
if (opts.itrw || opts.wport) {
if (opts.itrw || opts.wport || opts.wport===0 ) {
opts.itrw = opts.itrw || {}
if (opts.wport) opts.wport = opts.wport + pin
opts.itrw.port = opts.itrw.port || opts.wport || 9100 + pin
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'itrw#s>w'
}
// default is a tcp socket server at 9000+pin
if (opts.itrt || opts.port || !opts.sockets) {
if (opts.itrt || opts.port || opts.port===0 || !opts.sockets ) {
opts.itrt = opts.itrt || {}
if (opts.port) opts.port = opts.port + pin
opts.itrt.port = opts.itrt.port || opts.port || 9000 + pin
opts.itrt.conPacket = opts.conPacket
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'itrt#s>t'
@ -59,6 +65,8 @@ class Interrupt extends Base {
await super.init()
// for cntrl-c exit of interrupt
// create the pigio pin_num
// TODO check for rpi and pigpio, if not available use onoff
// wrap all needed commands so can call module for pigpio or onoff
if (this.mock) bus = await import('pigpio-mock')
else bus = await import('pigpio')
this.pin = new bus.Gpio(this.pin_num, {
@ -72,7 +80,6 @@ class Interrupt extends Base {
.then(resp => console.log('\n', resp)) // unexport on cntrl-c
.catch(err => console.log('error:', err))
})
let cb = () => {}
if (this.wait === 0) {
cb = this._interruptProcess.bind(this, this.packet)

View File

@ -1,6 +1,6 @@
import Interrupt from './interrupt'
import logger from '@uci/logger'
import logger from '@uci-utils/logger'
let log = {}
class Interrupts {