79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
// Solely a promise wrapper module for the basic async functions of the fivdi/i2c-bus javascript bindings C API
|
|
// https://github.com/fivdi/i2c-bus
|
|
|
|
'use strict'
|
|
|
|
const i2c = require('i2c-bus'),
|
|
Mux = require('/opt/lighting-dev/lib/uci-mux/src/tca9546A'),
|
|
pify = require('pify')
|
|
|
|
class Bus {
|
|
|
|
constructor(busnum, opts={}) {
|
|
|
|
// dealing with missing default busnum when options are provided
|
|
if (!busnum) { this.busnum = 1 }
|
|
else {
|
|
if (typeof(busnum) === Number) {
|
|
this.busnum = busnum
|
|
this.opts = opts
|
|
}
|
|
this.opts = busnum.opts
|
|
}
|
|
|
|
this.bus = i2c.open(this.busnum, () => {})
|
|
this.mux = {} // initialze mux in init method if address provided in options
|
|
}
|
|
|
|
// see https://github.com/fivdi/i2c-bus#busi2cfuncscb for list of functions that can be promisified
|
|
|
|
scan() { return pify(this.bus.scan).bind(this.bus)() }
|
|
close() { return pify(this.bus.close).bind(this.bus)() }
|
|
|
|
readRaw(address, length, buffer) {
|
|
return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer)
|
|
}
|
|
|
|
writeRaw(address, length, buffer) {
|
|
return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
|
|
}
|
|
|
|
read(address, cmd) {
|
|
console.log('read: address, cmd', address, cmd)
|
|
return pify(this.bus.readByte).bind(this.bus)(address, cmd)
|
|
}
|
|
|
|
write(address, cmd, byte) {
|
|
console.log('write: address, cmd, byte', address, cmd, byte)
|
|
return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte)
|
|
}
|
|
|
|
read2(address, cmd) {
|
|
return pify(this.bus.readWord.bind(this.bus))(address, cmd)
|
|
}
|
|
|
|
write2(address, cmd, bytes) {
|
|
return pify(this.bus.writeWord.bind(this.bus))(address, cmd, bytes)
|
|
}
|
|
|
|
receive(address) {
|
|
return pify(this.bus.receiveByte.bind(this.bus))(address)
|
|
}
|
|
|
|
send(address, byte) {
|
|
return pify(this.bus.sendByte.bind(this.bus))(address, byte)
|
|
}
|
|
|
|
async init() {
|
|
if (this.opts.muxAddress) {
|
|
this.mux = new Mux(this.bus, this.opts.muxAddress, { init: this.opts.muxInit })
|
|
return await this.bus.mux.init()
|
|
}
|
|
}
|
|
|
|
} // end of Bus Class
|
|
|
|
module.exports = {
|
|
Bus
|
|
}
|