uci-gpio/src/gpios.js

50 lines
1.4 KiB
JavaScript

import Base from '@uci/base'
import logger from '@uci-utils/logger'
import Gpio from './gpio'
import commands from './commands'
let log = {}
// pins is array of gpio pin numbers, options is object where pin number is a key for that pin's options (if needed)
// by default a pin is a simple output
// for single pin just use the Gpio Class itself
class Gpios extends Base {
constructor(pins, opts={}) {
super(opts)
this.id = this.id || 'gpios'
this.pins = {}
log = logger({ name: 'gpios', id: this.id, package:'@uci/gpios', file:'src/gpios.js'})
pins.forEach(pin => {
if (typeof pin !=='number') pin = parseInt(pin)
opts[pin] = Object.assign({},opts.pinOpts || {}, opts[pin] || {})
log.debug({ opts: opts[pin], line:27, msg:`pin options for pin ${pin}`})
this.pins[pin] = new Gpio(pin, opts[pin])
})
this.commands = this.bindFuncs(commands)
this.addNamespace('commands', 's') // give access to these commands above if a socket/server is created
}
async init() {
console.log('init')
for (let pin of Object.keys(this.pins)) {
log.debug({msg:`initializing pin ${pin} with ${this.pins[pin].value}`})
await this.pins[pin].init()
}
await super.init()
return 'ready'
}
// register same hook to all pins
registerHook(func) {
this.pins.forEach(async pin => {
this.pins[pin].registerHook(func)
})
}
} // end Class
export default Gpios