uci-mcp/src/mcp230xx.js

48 lines
1.4 KiB
JavaScript

import Device from '@uci/i2c-device'
// import Device from '../../uci-i2c-device/src/device-packet'
import commands from './commands'
import logger from '@uci-utils/logger'
let log = {}
class MCP230XX extends Device {
constructor(opts) {
super(opts)
log = logger({
file: 'src/mcp230xx.js',
class: 'MCP230XX',
name: 'mcp',
id: this.id
})
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 registerReadyFunc(func) {
this._ready = func
}
async configure() {
let res = await this.commands.chip.cfg({cfg:this.chipCfg})
if (res.error) {
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')
}
}
} // end of MCP230XX Class
export default MCP230XX