2018-02-17 18:25:27 -08:00
|
|
|
import Device from '@uci/i2c-device'
|
|
|
|
// import Device from '../../uci-i2c-device/src/device-packet'
|
2018-07-31 10:29:11 -07:00
|
|
|
import commands from './commands'
|
2018-02-08 09:19:46 -08:00
|
|
|
|
2019-02-14 14:01:30 -08:00
|
|
|
import logger from '@uci-utils/logger'
|
2018-02-08 09:19:46 -08:00
|
|
|
let log = {}
|
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
class MCP230XX extends Device {
|
2018-02-08 09:19:46 -08:00
|
|
|
constructor(opts) {
|
|
|
|
super(opts)
|
2019-01-01 19:45:25 -08:00
|
|
|
log = logger({
|
|
|
|
file: 'src/mcp230xx.js',
|
|
|
|
class: 'MCP230XX',
|
|
|
|
name: 'mcp',
|
|
|
|
id: this.id
|
|
|
|
})
|
2018-02-13 18:18:03 -08:00
|
|
|
this.chip17 = opts.chip17
|
2019-03-08 09:09:48 -08:00
|
|
|
this.chipCfg = opts.chipCfg || 'default'
|
2019-12-05 14:57:05 -08:00
|
|
|
// this._configured = false
|
2018-07-31 10:29:11 -07:00
|
|
|
this.commands = this.bindFuncs(commands)
|
2019-01-01 19:45:25 -08:00
|
|
|
this.addNamespace('commands', 's') // allow access to commands via socket/server
|
2019-04-20 16:44:32 -07:00
|
|
|
// 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
|
2019-12-05 14:57:05 -08:00
|
|
|
this.once('ready:i2c', () =>{
|
|
|
|
this.configure()
|
|
|
|
if (this._ready) this._ready.call(this)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async registerReadyFunc(func) {
|
|
|
|
this._ready = func
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 14:57:05 -08:00
|
|
|
async configure() {
|
2019-04-20 16:44:32 -07:00
|
|
|
let res = await this.commands.chip.cfg({cfg:this.chipCfg})
|
2019-03-11 14:52:22 -07:00
|
|
|
if (res.error) {
|
2019-12-05 14:57:05 -08:00
|
|
|
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')
|
2019-03-11 14:52:22 -07:00
|
|
|
}
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
} // end of MCP230XX Class
|
2019-01-01 19:45:25 -08:00
|
|
|
|
|
|
|
export default MCP230XX
|