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", "name": "@uci/i2c-device",
"version": "0.1.21", "version": "0.1.23",
"description": "Device Classes for I2C Interfacing", "description": "Device Classes for I2C Interfacing",
"main": "src/device", "main": "src/device",
"scripts": { "scripts": {
@ -26,7 +26,7 @@
}, },
"homepage": "https://github.com/uCOMmandIt/i2c#readme", "homepage": "https://github.com/uCOMmandIt/i2c#readme",
"dependencies": { "dependencies": {
"@uci/base": "^0.1.30", "@uci/base": "^0.1.31",
"@uci-utils/logger": "^0.0.15" "@uci-utils/logger": "^0.0.15"
}, },
"devDependencies": { "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.channel = +opts.channel // if using TAC9546A channel number on which device is attached
this.bus = this.bindFuncs(commands) this.bus = this.bindFuncs(commands)
this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack
} this.getSocket('bus').on('connection:socket',async ev=>{
if (ev.state==='connected') {
async init() { let res = await this.bus.ack()
let res = await super.init() // console.log('bus address ack was', res.ack)
if (res.errors) return {error: res.errors, msg:'socket initialization', } let socket = this.getSocket('bus').opts
res = await this.bus.ack() let connection = socket.path || socket.host + ':' + socket.port
let socket = this.getSocket('bus').opts if (!res.ack) {
let connection = socket.path || socket.host + ':' + socket.port 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)}`}
if (!res.ack) { this.emit('log', err)
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)}`} } else this.emit('ready:i2c',{bus:socket, connection:connection})
log.error(err) }
return err })
}
return res
} }
// TODO enable mux board channel set // TODO enable mux board channel set