From a5d8eef3fe8488a2e1aed6d3f7c3f2ccb5f7e79c Mon Sep 17 00:00:00 2001 From: David Kebler Date: Fri, 13 Oct 2017 12:26:36 -0700 Subject: [PATCH] new version with mux --- src/bus.js | 85 ++++++++++++++++++++++++++++++++---------------- src/device.js | 89 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 115 insertions(+), 59 deletions(-) diff --git a/src/bus.js b/src/bus.js index d7887e7..91ca4ad 100644 --- a/src/bus.js +++ b/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 } diff --git a/src/device.js b/src/device.js index ccc99e4..764a251 100644 --- a/src/device.js +++ b/src/device.js @@ -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 }