new version with mux

master
David Kebler 2017-10-13 12:26:36 -07:00
parent 0f9a730bdf
commit a5d8eef3fe
2 changed files with 115 additions and 59 deletions

View File

@ -4,46 +4,75 @@
'use strict' 'use strict'
const i2c = require('i2c-bus'), const i2c = require('i2c-bus'),
pify = require('pify') Mux = require('/opt/lighting-dev/lib/uci-mux/src/tca9546A'),
pify = require('pify')
class Bus { class Bus {
constructor(busnum = 1) { constructor(busnum, opts={}) {
this.busnum = busnum
this.bus = i2c.open(this.busnum, () => {})
}
scan() { return pify(this.bus.scan).bind(this.bus)() } // dealing with missing default busnum when options are provided
close() { return pify(this.bus.close).bind(this.bus)() } if (!busnum) { this.busnum = 1 }
else {
if (typeof(busnum) === Number) {
this.busnum = busnum
this.opts = opts
}
this.opts = busnum.opts
}
readRaw(address, length, buffer) { this.bus = i2c.open(this.busnum, () => {})
return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer) this.mux = {} // initialze mux in init method if address provided in options
} }
writeRaw(address, length, buffer) { // see https://github.com/fivdi/i2c-bus#busi2cfuncscb for list of functions that can be promisified
return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
}
read(address, cmd) { scan() { return pify(this.bus.scan).bind(this.bus)() }
// console.log("read: address, cmd", address, cmd) close() { return pify(this.bus.close).bind(this.bus)() }
return pify(this.bus.readByte).bind(this.bus)(address, cmd)
}
write(address, cmd, byte) { readRaw(address, length, buffer) {
// console.log('write: address, cmd, byte', address, cmd, byte) return pify(this.bus.i2cRead).bind(this.bus)(address, length, buffer)
return pify(this.bus.writeByte.bind(this.bus))(address, cmd, byte) }
}
read2(address, cmd) { writeRaw(address, length, buffer) {
return pify(this.bus.readWord.bind(this.bus))(address, cmd) return pify(this.bus.i2cWrite).bind(this.bus)(address, length, buffer)
} }
write2(address, cmd, bytes) { read(address, cmd) {
return pify(this.bus.writeWord.bind(this.bus))(address, cmd, bytes) 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 } // end of Bus Class
module.exports = { module.exports = {
Bus Bus
} }

View File

@ -3,45 +3,72 @@
// ********************************** // **********************************
class Device { class Device {
// bus is i2c-bus bus object // bus is i2c-bus bus object
constructor(bus, address, opts) { constructor(bus, address, opts) {
this.bus = bus this.bus = bus
this.address = address this.address = address
if (opts) { if (opts) {
this.id = opts.id // must be unique within a bus this.id = opts.id // must be unique within a bus
this.desc = opts.desc 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) { async _setChannel() {
return this.bus.writeRaw(this.address, length, buffer) 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 // for devices that need just a simple send of a byte without a register command
read(cmd) { async receive() {
return this.bus.read(this.address, cmd) await this._setChannel()
} return this.bus.receive(this.address)
}
write(cmd, byte) { async send(cmd, byte) {
return this.bus.write(this.address, cmd, byte) await this._setChannel()
} return this.bus.send(this.address, cmd, byte)
}
// for I2C devices that use a word length packackage // for devices needing a buffer/stream
read2(cmd) { async readRaw(length, buffer) {
return this.bus.read2(this.address, cmd) await this._setChannel()
} return this.bus.readRaw(this.address, length, buffer)
}
write2(cmd, bytes) { async writeRaw(length, buffer) {
return this.bus.write2(this.address, cmd, bytes) 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 = { module.exports = {
Device Device
} }