2018-02-08 09:19:46 -08:00
|
|
|
// import Base from '@uci/base'
|
|
|
|
import Base from '../../uci-base/src/base'
|
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 = {}
|
|
|
|
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.pin = pin
|
2018-02-10 15:28:51 -08:00
|
|
|
this.chip = chip
|
2018-02-08 09:19:46 -08:00
|
|
|
this.busSend = this.busSend.bind(this)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async init(){
|
|
|
|
await super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-10 15:28:51 -08:00
|
|
|
// this services the reply from the bus
|
|
|
|
reply (bus_packet) {
|
|
|
|
this.emit(bus_packet.id, bus_packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
async busSend(packet) {
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
setTimeout(() => {resolve({error:'no response from bus in 10sec'})},10000)
|
|
|
|
packet.id = Math.random().toString().slice(2)
|
|
|
|
// console.log('sending to bus', packet.id)
|
|
|
|
this.socket.bus.send(packet)
|
|
|
|
this.on(packet.id,function(bus_reply){
|
|
|
|
// console.log('reply emitted',bus_reply)
|
|
|
|
this.removeAllListeners(bus_reply.id)
|
|
|
|
delete bus_reply.id
|
|
|
|
resolve(bus_reply)
|
|
|
|
}) //end listener
|
|
|
|
})
|
2018-02-08 09:19:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// setBusListener(packet) {
|
|
|
|
// // this.on(packet.cmd+Math.random().toString().slice(1))
|
|
|
|
// }
|
|
|
|
|
|
|
|
} // end of MCP230XX Class
|