2020-01-09 20:17:32 -08:00
|
|
|
import { I2CDevice as Device, map, changed, isPlainObject, to, merge} from '@uci/i2c-device'
|
2018-07-31 10:29:11 -07:00
|
|
|
import commands from './commands'
|
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'
|
2020-01-09 20:17:32 -08:00
|
|
|
this.pinsCfg = opts.pinsCfg || this.chip17 ? [{port:'A', pins:'all'},{port:'B', pins:'all'}] : [{pins:'all'}]
|
|
|
|
|
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-12-05 14:57:05 -08:00
|
|
|
|
2020-01-09 20:17:32 -08:00
|
|
|
if (opts.lport) this.registerSocket('mcp-t','s','t',{port:opts.lport})
|
|
|
|
if (opts.lpath) this.registerSocket('mcp-n','s','n',{path: opts.lpath})
|
|
|
|
|
|
|
|
this.ready.addObserver('mcp',this.ready.getObserver('i2c').pipe(
|
|
|
|
map(async ready => {
|
|
|
|
if (ready) {
|
|
|
|
let res = await this.commands.chip.cfg({cfg:this.chipCfg})
|
|
|
|
if (!res.error) {
|
|
|
|
let res = await this.commands.pin.cfgs({configs:this.pinsCfg})
|
|
|
|
return res.error? false :true
|
|
|
|
} return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
))
|
|
|
|
|
|
|
|
} // end contructor
|
2018-02-08 09:19:46 -08:00
|
|
|
|
|
|
|
} // end of MCP230XX Class
|
2019-01-01 19:45:25 -08:00
|
|
|
|
|
|
|
export default MCP230XX
|
2020-01-09 20:17:32 -08:00
|
|
|
export {MCP230XX, map, changed, isPlainObject, to, merge}
|