diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..c11a1d5 --- /dev/null +++ b/.eslintrc.js @@ -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" + ] + } +} diff --git a/package.json b/package.json index 449b906..4a378fe 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/bus.js b/src/bus.js index 91ca4ad..53d2242 100644 --- a/src/bus.js +++ b/src/bus.js @@ -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 diff --git a/src/device.js b/src/device.js index 764a251..e2c9425 100644 --- a/src/device.js +++ b/src/device.js @@ -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) }