new version with mux
parent
0f9a730bdf
commit
a5d8eef3fe
37
src/bus.js
37
src/bus.js
|
@ -4,14 +4,28 @@
|
|||
'use strict'
|
||||
|
||||
const i2c = require('i2c-bus'),
|
||||
Mux = require('/opt/lighting-dev/lib/uci-mux/src/tca9546A'),
|
||||
pify = require('pify')
|
||||
|
||||
class Bus {
|
||||
|
||||
constructor(busnum = 1) {
|
||||
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.bus = i2c.open(this.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)() }
|
||||
|
@ -25,12 +39,12 @@ class Bus {
|
|||
}
|
||||
|
||||
read(address, cmd) {
|
||||
// console.log("read: address, cmd", 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)
|
||||
console.log('write: address, cmd, byte', address, cmd, byte)
|
||||
return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte)
|
||||
}
|
||||
|
||||
|
@ -42,6 +56,21 @@ class Bus {
|
|||
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 = {
|
||||
|
|
|
@ -10,33 +10,60 @@ class Device {
|
|||
if (opts) {
|
||||
this.id = opts.id // must be unique within a bus
|
||||
this.desc = opts.desc
|
||||
this.channel = opts.channel // if using TAC9546A channel number on which device is attached
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async _setChannel() {
|
||||
if (this.channel) {
|
||||
if (!this.bus.mux) { return Promise.reject('Channel set but no mux on bus')}
|
||||
return this.bus.mux.set(this.channel)
|
||||
}
|
||||
return Promise.resolve() // no channel for device either no mux or device is attached to mux bypass
|
||||
}
|
||||
|
||||
// for devices that need just a simple send of a byte without a register command
|
||||
async receive() {
|
||||
await this._setChannel()
|
||||
return this.bus.receive(this.address)
|
||||
}
|
||||
|
||||
async send(cmd, byte) {
|
||||
await this._setChannel()
|
||||
return this.bus.send(this.address, cmd, byte)
|
||||
}
|
||||
|
||||
// for devices needing a buffer/stream
|
||||
readRaw(length, buffer) {
|
||||
async readRaw(length, buffer) {
|
||||
await this._setChannel()
|
||||
return this.bus.readRaw(this.address, length, buffer)
|
||||
}
|
||||
|
||||
writeRaw(length, buffer) {
|
||||
async writeRaw(length, buffer) {
|
||||
await this._setChannel()
|
||||
return this.bus.writeRaw(this.address, length, buffer)
|
||||
}
|
||||
|
||||
// both cmd and byte should be a single byte as a decimal or hex
|
||||
read(cmd) {
|
||||
async read(cmd) {
|
||||
await this._setChannel()
|
||||
return this.bus.read(this.address, cmd)
|
||||
}
|
||||
|
||||
write(cmd, byte) {
|
||||
async write(cmd, byte) {
|
||||
await this._setChannel()
|
||||
return this.bus.write(this.address, cmd, byte)
|
||||
}
|
||||
|
||||
// for I2C devices that use a word length packackage
|
||||
read2(cmd) {
|
||||
async read2(cmd) {
|
||||
await this._setChannel()
|
||||
return this.bus.read2(this.address, cmd)
|
||||
}
|
||||
|
||||
write2(cmd, bytes) {
|
||||
async write2(cmd, bytes) {
|
||||
await this._setChannel()
|
||||
return this.bus.write2(this.address, cmd, bytes)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue