Handler to be used internally by Domapic Modules for controlling a gpio in "in" mode
This package provides a Domapic handler for controlling a gpio in "in" mode using the onoff library internally. Passing to it a Domapic module instance, it will retrieve the module instance configuration defined when started the service, and will configure the gpio based on it.
Just define which are your module "options keys" for configuring the gpio, and the handler will automatically load the configuration. Or you can also set the options with fixed values programatically.
npm i gpio-in-domapic -saveoptions<object>Object containing default values for options. Will apply these values if no configuration keys are provided.debounceTimeout<boolean>Defines the debounce time for emitting gpio events.
configurationKeys<object>Object defining configuration keys from which the options will be loaded.gpio<string>Key defining the configuration property in which the gpio number is defined. Default isgpio.debounceTimeout<string>Key defining the configuration property in which thedebounceTimeoutoption for the gpio is defined.
gpio.init()async method. Initializes the gpio retrieving configuration, etc.gpio.statusgetter. Returns the current gpio status.gpio.eventsgetter. Returns gpio eventEmitter object. Read the events chapter for further info.
eventNames<object>Object containing gpio event names.
Gpio instances emit events through an eventEmitter object exposed in the events getter. Event names are exposed in the Gpio static object eventNames. Available events are:
Gpio.eventNames.ON. Emitted when gpio status change totrueGpio.eventNames.OFF. Emitted when gpio status change tofalseGpio.eventNames.CHANGE. Emitted whenever the gpio status changes. It sends the new status as first argument to subscribed listeners.Gpio.eventNames.ERROR. Emitted whenever the gpio throws an error. It sends the error as first argument to subscribed listeners.
In the next example, the gpio-in-domapic package is used to create a Domapic Module having an state for returning a door status, and emitting an event when the door status changes. It also allow users to decide the debounce time when starting the module.
const path = require('path')
const domapic = require('domapic-service')
const gpioIn = require('gpio-in-domapic')
domapic.createModule({
packagePath: path.resolve(__dirname),
customConfig: {
gpio: {
type: 'number',
describe: 'Set gpio number for the door sensor'
},
debounce: {
type: 'number',
describe: 'Set debounce timeout for the door sensor',
default: 1000
}
}
}).then(async dmpcModule => {
const doorSensor = new gpioIn.Gpio(dmpcModule, {
}, {
debounceTimeout: 'debounce'
})
await dmpcModule.register({
door: {
description: 'Door status',
data: {
type: 'boolean'
},
state: {
description: 'Returns current door status',
handler: () => doorSensor.status
},
event: {
description: 'Door status has changed'
}
}
})
await doorSensor.init()
doorSensor.events.on(gpioIn.Gpio.eventNames.CHANGE, newValue => {
dmpcModule.tracer.debug('Door status has changed', newValue)
dmpcModule.events.emit('door', newValue)
})
return dmpcModule.start()
})Now, the module can be started using the debounce option, which Gpio will use as debounceTimeout:
node server.js --gpio=18 --debounce=1500