diff --git a/package.json b/package.json index 6cdc9cb..47d819b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@uci/mcp", "main": "src", - "version": "0.1.36", + "version": "0.1.37", "description": "Classes and Helper Functions for using the MCP chip on I2C Bus", "scripts": { "relays": "node -r esm examples/relays", diff --git a/src/mcp230xx.js b/src/mcp230xx.js index e924f1d..50a56a7 100644 --- a/src/mcp230xx.js +++ b/src/mcp230xx.js @@ -16,21 +16,31 @@ class MCP230XX extends Device { }) this.chip17 = opts.chip17 this.chipCfg = opts.chipCfg || 'default' + // this._configured = false this.commands = this.bindFuncs(commands) this.addNamespace('commands', 's') // allow access to commands via socket/server // this._pin = this.commands.pin // add a simplier reference for local access // this._chipcfg = this.commands.chip.cfg // add a simplier reference for local access + this.once('ready:i2c', () =>{ + this.configure() + if (this._ready) this._ready.call(this) + }) } - async init() { - await super.init() + async registerReadyFunc(func) { + this._ready = func + } + + async configure() { let res = await this.commands.chip.cfg({cfg:this.chipCfg}) if (res.error) { - log.fatal({method:'init', line:82, msg:'unable to configure mcp chip', error:res.error, cfg:this.chipCfg, address:this.address}) - throw `${res.error} at address ${this.address}/${this.address.toString(16)}` + let err={level:'fatal', msg:'unable to configure mcp chip', error:res.error, cfg:this.chipCfg, address:this.address} + log.fatal(err) + this.emit('status', err) + } else { + this.emit('status', {level:'info', msg:'mcp chip was properly configured', cfg:this.chipCfg}) + this.emit('ready:mcp') } - await this.commands.pin.cfg({pins:'all'}) //pins are outputs by default - if (this.chip17) await this.commands.pin.cfg({pins:'all', port:'B'}) } } // end of MCP230XX Class