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'
|
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
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
|
2019-01-01 19:45:25 -08:00
|
|
|
async init() {
|
2018-02-08 09:19:46 -08:00
|
|
|
await super.init()
|
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) {
|
|
|
|
log.fatal({msg:'unable to configure mcp chip', error:res.error, cfg:this.chipCfg, address:this.address})
|
2019-03-08 09:09:48 -08:00
|
|
|
throw `${res.error} at address ${this.address}/${this.address.toString(16)}`
|
2019-03-11 14:52:22 -07:00
|
|
|
}
|
2019-03-16 16:45:29 -07:00
|
|
|
await this.commands.pin.cfg({pins:'all'}) //pins are outputs by default
|
2019-04-20 16:44:32 -07:00
|
|
|
if (this.chip17) await this.commands.pin.cfg({pins:'all', port:'B'})
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
} // end of MCP230XX Class
|
2019-01-01 19:45:25 -08:00
|
|
|
|
|
|
|
export default MCP230XX
|