add mux board to bus class if address is set and init called. Device uses mux if available

master
David Kebler 2017-10-17 09:36:38 -07:00
parent a5d8eef3fe
commit c737a29368
4 changed files with 39 additions and 27 deletions

27
.eslintrc.js Normal file
View File

@ -0,0 +1,27 @@
module.exports = {
"env": {
"es6": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2017
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"no-console": 0,
"semi": ["error", "never"],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
]
}
}

View File

@ -1,6 +1,6 @@
{
"name": "@uci/i2c",
"version": "0.1.0",
"version": "0.1.1",
"description": "Bus and Device Classes for I2C Interfacing",
"main": "index.js",
"scripts": {

View File

@ -4,25 +4,12 @@
'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
}
constructor(busnum=1) {
this.busnum = busnum
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
@ -39,12 +26,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)
}
@ -57,19 +44,15 @@ class Bus {
}
receive(address) {
// console.log('receivebyte', address)
return pify(this.bus.receiveByte.bind(this.bus))(address)
}
send(address, byte) {
// console.log('sendbyte', 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

View File

@ -12,13 +12,13 @@ class Device {
this.desc = opts.desc
this.channel = opts.channel // if using TAC9546A channel number on which device is attached
}
}
async _setChannel() {
// console.log('before set',this.address,this.id,this.channel, this.bus.getState)
if (this.channel) {
if (!this.bus.mux) { return Promise.reject('Channel set but no mux on bus')}
return this.bus.mux.set(this.channel)
if (!this.bus.address) { return Promise.reject('Channel set but no mux on bus')}
return this.bus.set(this.channel)
}
return Promise.resolve() // no channel for device either no mux or device is attached to mux bypass
}
@ -48,11 +48,13 @@ class Device {
// both cmd and byte should be a single byte as a decimal or hex
async read(cmd) {
await this._setChannel()
// console.log('after set before read',this.address,this.id,this.channel,this.bus.getState)
return this.bus.read(this.address, cmd)
}
async write(cmd, byte) {
await this._setChannel()
// console.log('after set, before write',this.address,this.id,this.channel,this.bus.getState)
return this.bus.write(this.address, cmd, byte)
}