50 lines
1.3 KiB
JavaScript
50 lines
1.3 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] = opts[pin] ? opts[pin] : opts.pinOpts
|
|
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() {
|
|
await super.init()
|
|
return Promise.all(
|
|
Object.keys(this.pins).map(pin => {
|
|
return this.pins[pin].init()
|
|
})
|
|
)
|
|
}
|
|
|
|
|
|
// register same hook to all pins
|
|
registerHook(func) {
|
|
this.pins.forEach(async pin => {
|
|
this.pins[pin].registerHook(func)
|
|
})
|
|
}
|
|
|
|
} // end Class
|
|
|
|
export default Gpios
|