new version with mux
parent
0f9a730bdf
commit
a5d8eef3fe
85
src/bus.js
85
src/bus.js
|
@ -4,46 +4,75 @@
|
|||
'use strict'
|
||||
|
||||
const i2c = require('i2c-bus'),
|
||||
pify = require('pify')
|
||||
Mux = require('/opt/lighting-dev/lib/uci-mux/src/tca9546A'),
|
||||
pify = require('pify')
|
||||
|
||||
class Bus {
|
||||
|
||||
constructor(busnum = 1) {
|
||||
this.busnum = busnum
|
||||
this.bus = i2c.open(this.busnum, () => {})
|
||||
}
|
||||
constructor(busnum, opts={}) {
|
||||
|
||||
scan() { return pify(this.bus.scan).bind(this.bus)() }
|
||||
close() { return pify(this.bus.close).bind(this.bus)() }
|
||||
// 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
|
||||
}
|
||||
|
||||
readRaw(address, length, buffer) {
|
||||
return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer)
|
||||
}
|
||||
this.bus = i2c.open(this.busnum, () => {})
|
||||
this.mux = {} // initialze mux in init method if address provided in options
|
||||
}
|
||||
|
||||
writeRaw(address, length, buffer) {
|
||||
return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
|
||||
}
|
||||
// see https://github.com/fivdi/i2c-bus#busi2cfuncscb for list of functions that can be promisified
|
||||
|
||||
read(address, cmd) {
|
||||
// console.log("read: address, cmd", address, cmd)
|
||||
return pify(this.bus.readByte).bind(this.bus)(address, cmd)
|
||||
}
|
||||
scan() { return pify(this.bus.scan).bind(this.bus)() }
|
||||
close() { return pify(this.bus.close).bind(this.bus)() }
|
||||
|
||||
write(address, cmd, byte) {
|
||||
// console.log('write: address, cmd, byte', address, cmd, byte)
|
||||
return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte)
|
||||
}
|
||||
readRaw(address, length, buffer) {
|
||||
return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer)
|
||||
}
|
||||
|
||||
read2(address, cmd) {
|
||||
return pify(this.bus.readWord.bind(this.bus))(address, cmd)
|
||||
}
|
||||
writeRaw(address, length, buffer) {
|
||||
return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
|
||||
}
|
||||
|
||||
write2(address, cmd, bytes) {
|
||||
return pify(this.bus.writeWord.bind(this.bus))(address, cmd, bytes)
|
||||
}
|
||||
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
|
||||
Bus
|
||||
}
|
||||
|
|
|
@ -3,45 +3,72 @@
|
|||
// **********************************
|
||||
|
||||
class Device {
|
||||
// bus is i2c-bus bus object
|
||||
constructor(bus, address, opts) {
|
||||
this.bus = bus
|
||||
this.address = address
|
||||
if (opts) {
|
||||
this.id = opts.id // must be unique within a bus
|
||||
this.desc = opts.desc
|
||||
}
|
||||
// bus is i2c-bus bus object
|
||||
constructor(bus, address, opts) {
|
||||
this.bus = bus
|
||||
this.address = address
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
// for devices needing a buffer/stream
|
||||
readRaw(length, buffer) {
|
||||
return this.bus.readRaw(this.address, length, buffer)
|
||||
}
|
||||
}
|
||||
|
||||
writeRaw(length, buffer) {
|
||||
return this.bus.writeRaw(this.address, length, buffer)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// both cmd and byte should be a single byte as a decimal or hex
|
||||
read(cmd) {
|
||||
return this.bus.read(this.address, cmd)
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
|
||||
write(cmd, byte) {
|
||||
return this.bus.write(this.address, cmd, byte)
|
||||
}
|
||||
async send(cmd, byte) {
|
||||
await this._setChannel()
|
||||
return this.bus.send(this.address, cmd, byte)
|
||||
}
|
||||
|
||||
// for I2C devices that use a word length packackage
|
||||
read2(cmd) {
|
||||
return this.bus.read2(this.address, cmd)
|
||||
}
|
||||
// for devices needing a buffer/stream
|
||||
async readRaw(length, buffer) {
|
||||
await this._setChannel()
|
||||
return this.bus.readRaw(this.address, length, buffer)
|
||||
}
|
||||
|
||||
write2(cmd, bytes) {
|
||||
return this.bus.write2(this.address, cmd, bytes)
|
||||
}
|
||||
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
|
||||
async read(cmd) {
|
||||
await this._setChannel()
|
||||
return this.bus.read(this.address, cmd)
|
||||
}
|
||||
|
||||
async write(cmd, byte) {
|
||||
await this._setChannel()
|
||||
return this.bus.write(this.address, cmd, byte)
|
||||
}
|
||||
|
||||
// for I2C devices that use a word length packackage
|
||||
async read2(cmd) {
|
||||
await this._setChannel()
|
||||
return this.bus.read2(this.address, cmd)
|
||||
}
|
||||
|
||||
async write2(cmd, bytes) {
|
||||
await this._setChannel()
|
||||
return this.bus.write2(this.address, cmd, bytes)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Device
|
||||
Device
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue