uci-mcp/src/mcp230xx-packet.mjs

58 lines
1.7 KiB
JavaScript
Raw Normal View History

// import Base from '@uci/base'
import Base from '../../uci-base/src/base'
import { pin, chip } from './commands'
import logger from '../../uci-logger/src/logger'
let log = {}
const LOG_OPTS = {
repo:'uci-mcp',
npm:'@uci/mcp',
file:'src/mcp230xx-packet.mjs',
class:'MCP230XX',
id:this.id,
instance_created:new Date().getTime()
}
export default class MCP230XX extends Base {
constructor(opts) {
log = logger.child(LOG_OPTS)
if (opts.bus) {
if (opts.bus.host) {
opts.bus.socket = 'bus#c>t'
opts.bus.port = opts.bus.port || 1776
}
if (opts.bus.path) opts.bus.socket = 'bus#c>n'
} else {
opts.bus = { path : (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock' }
opts.bus.socket = 'bus#c>n'
}
opts.nmcp = opts.nmcp || {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
opts.sockets = 'nmcp#s>n,tmcp#s>t,'+ opts.bus.socket
console.log(opts)
super(opts)
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
this.address = opts.address
this.chip17 = opts.chip17
this.pin = pin
this.chip = chip
// this.busSend = this.busSend.bind(this)
}
async init(){
await super.init()
let res = await this.chip.ack.bind(this)() // move this to device class
if (!res.ack) throw `no device on this bus at address ${this.address}=0x${this.address.toString(16)}`
res = await this.chip.cfg.bind(this)({})
let cfg = this.chip17 ?'10100010':'00100010'
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
}
// this services the reply from the bus
reply (bus_packet) {
this.emit(bus_packet.id, bus_packet)
}
} // end of MCP230XX Class