uci-mcp/src/mcp230xx.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

import { I2CDevice as Device, map, changed, isPlainObject, to, merge} from '@uci/i2c-device'
import commands from './commands'
import logger from '@uci-utils/logger'
let log = {}
2019-01-01 19:45:25 -08:00
class MCP230XX extends Device {
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
})
this.chip17 = opts.chip17
this.chipCfg = opts.chipCfg || 'default'
this.pinsCfg = opts.pinsCfg || this.chip17 ? [{port:'A', pins:'all'},{port:'B', pins:'all'}] : [{pins:'all'}]
this.commands = this.bindFuncs(commands)
2019-01-01 19:45:25 -08:00
this.addNamespace('commands', 's') // allow access to commands via socket/server
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
} // end of MCP230XX Class
2019-01-01 19:45:25 -08:00
export default MCP230XX
export {MCP230XX, map, changed, isPlainObject, to, merge}