0.1.37 Added listener for read:i2c which call the chip config function for at least the default configuration

can register custom function to also call
emits ready:mcp if chip is configured correctly
emits log events
master
David Kebler 2019-12-05 14:57:05 -08:00
parent d3f905e86c
commit 9650698683
2 changed files with 17 additions and 7 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@uci/mcp", "name": "@uci/mcp",
"main": "src", "main": "src",
"version": "0.1.36", "version": "0.1.37",
"description": "Classes and Helper Functions for using the MCP chip on I2C Bus", "description": "Classes and Helper Functions for using the MCP chip on I2C Bus",
"scripts": { "scripts": {
"relays": "node -r esm examples/relays", "relays": "node -r esm examples/relays",

View File

@ -16,21 +16,31 @@ class MCP230XX extends Device {
}) })
this.chip17 = opts.chip17 this.chip17 = opts.chip17
this.chipCfg = opts.chipCfg || 'default' this.chipCfg = opts.chipCfg || 'default'
// this._configured = false
this.commands = this.bindFuncs(commands) this.commands = this.bindFuncs(commands)
this.addNamespace('commands', 's') // allow access to commands via socket/server this.addNamespace('commands', 's') // allow access to commands via socket/server
// this._pin = this.commands.pin // add a simplier reference for local access // 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._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() { async registerReadyFunc(func) {
await super.init() this._ready = func
}
async configure() {
let res = await this.commands.chip.cfg({cfg:this.chipCfg}) let res = await this.commands.chip.cfg({cfg:this.chipCfg})
if (res.error) { if (res.error) {
log.fatal({method:'init', line:82, msg:'unable to configure mcp chip', error:res.error, cfg:this.chipCfg, address:this.address}) let err={level:'fatal', 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)}` 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 } // end of MCP230XX Class