0.1.23 remove init method, Initialize sockets via base class methods

listen for connection to bus then do a ack.
if all ok emit a ready:i2c event that can be used by extended classes/instances
master
David Kebler 2019-12-05 15:16:59 -08:00
parent 6011e3e37a
commit d29b9295fc
2 changed files with 14 additions and 16 deletions

View File

@ -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": {

View File

@ -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