2018-02-08 09:19:46 -08:00
|
|
|
// import Base from '@uci/base'
|
2018-02-14 19:01:08 -08:00
|
|
|
import Device from '../../uci-i2c-device/src/device-packet'
|
2018-02-10 15:28:51 -08:00
|
|
|
import { pin, chip } from './commands'
|
2018-02-08 09:19:46 -08:00
|
|
|
|
|
|
|
import logger from '../../uci-logger/src/logger'
|
|
|
|
let log = {}
|
2018-02-14 19:01:08 -08:00
|
|
|
const LOG_OPTS = (id) => {
|
|
|
|
return {
|
|
|
|
repo:'uci-mcp',
|
|
|
|
npm:'@uci/mcp',
|
|
|
|
file:'src/mcp230xx-packet.mjs',
|
|
|
|
class:'MCP230XX',
|
|
|
|
id:id,
|
|
|
|
instance_created:new Date().getTime()
|
|
|
|
}}
|
2018-02-08 09:19:46 -08:00
|
|
|
|
2018-02-14 19:01:08 -08:00
|
|
|
export default class MCP230XX extends Device {
|
2018-02-08 09:19:46 -08:00
|
|
|
constructor(opts) {
|
|
|
|
opts.nmcp = opts.nmcp || {path: (process.env.SOCKETS_DIR || __dirname) + '/mcp.sock'}
|
2018-02-14 19:01:08 -08:00
|
|
|
opts.sockets = 'nmcp#s>n,tmcp#s>t,'
|
|
|
|
// console.log(opts)
|
2018-02-08 09:19:46 -08:00
|
|
|
super(opts)
|
2018-02-14 19:01:08 -08:00
|
|
|
log = logger.child(LOG_OPTS(this.id))
|
2018-02-08 09:19:46 -08:00
|
|
|
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
|
|
|
|
this.address = opts.address
|
2018-02-13 18:18:03 -08:00
|
|
|
this.chip17 = opts.chip17
|
2018-02-08 09:19:46 -08:00
|
|
|
this.pin = pin
|
2018-02-10 15:28:51 -08:00
|
|
|
this.chip = chip
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async init(){
|
|
|
|
await super.init()
|
2018-02-14 19:01:08 -08:00
|
|
|
// 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)}`
|
|
|
|
let res = await this.chip.cfg.bind(this)({})
|
2018-02-13 18:18:03 -08:00
|
|
|
let cfg = this.chip17 ?'10100010':'00100010'
|
|
|
|
if (res.response !==cfg ) throw `could not configure mcp chip at ${this.address}=0x${this.address.toString(16)}`
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-14 19:01:08 -08:00
|
|
|
// this overwrites the default cmd 'reply' from the bus
|
|
|
|
reply (packet) {
|
|
|
|
// console.log(packet._header.request, packet.response)
|
2018-02-10 15:28:51 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:19:46 -08:00
|
|
|
} // end of MCP230XX Class
|