diff --git a/package.json b/package.json index e0eb1dd..77c7454 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci/i2c-device", - "version": "0.1.21", + "version": "0.1.23", "description": "Device Classes for I2C Interfacing", "main": "src/device", "scripts": { @@ -26,7 +26,7 @@ }, "homepage": "https://github.com/uCOMmandIt/i2c#readme", "dependencies": { - "@uci/base": "^0.1.30", + "@uci/base": "^0.1.31", "@uci-utils/logger": "^0.0.15" }, "devDependencies": { diff --git a/src/device.js b/src/device.js index 587d073..2e2a1b6 100644 --- a/src/device.js +++ b/src/device.js @@ -28,20 +28,18 @@ class I2CDevice extends Base { this.channel = +opts.channel // if using TAC9546A channel number on which device is attached this.bus = this.bindFuncs(commands) this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack - } - - async init() { - let res = await super.init() - if (res.errors) return {error: res.errors, msg:'socket initialization', } - res = await this.bus.ack() - let socket = this.getSocket('bus').opts - let connection = socket.path || socket.host + ':' + socket.port - if (!res.ack) { - let err ={error:'ack', res:res, socket:socket, method:'init', line:38, connection:connection, address:this.address, msg:`no bus or device on bus at address ${this.address}=0x${this.address.toString(16)}`} - log.error(err) - return err - } - return res + this.getSocket('bus').on('connection:socket',async ev=>{ + if (ev.state==='connected') { + let res = await this.bus.ack() + // console.log('bus address ack was', res.ack) + let socket = this.getSocket('bus').opts + let connection = socket.path || socket.host + ':' + socket.port + if (!res.ack) { + let err ={level:'fatal', res:res, socket:socket, method:'i2c-device', line:37, connection:connection, address:this.address, msg:`no bus or device on bus at address ${this.address}=0x${this.address.toString(16)}`} + this.emit('log', err) + } else this.emit('ready:i2c',{bus:socket, connection:connection}) + } + }) } // TODO enable mux board channel set