54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
import Base from '@uci/base'
|
|
import commands from './bus-device-commands'
|
|
import logger from '@uci-utils/logger'
|
|
let log = {}
|
|
|
|
class I2CDevice extends Base {
|
|
constructor(opts) {
|
|
opts.ndevice = opts.ndevice || {}
|
|
opts.tdevice = opts.tdevice || {}
|
|
opts.ndevice.path = opts.path || 'i2c-device'
|
|
opts.tdevice.port = opts.port || 1777
|
|
opts.bus = opts.bus || {}
|
|
if (opts.bus.host) opts.bus.port = opts.bus.port || 1776
|
|
else opts.bus.path = opts.bus.path || 'i2c-bus'
|
|
super(opts)
|
|
this.addSocket('bus','c',opts.bus.host ? 't':'n',opts.bus)
|
|
this.addSocket('device:tcp','s','t',opts.tdevice)
|
|
this.addSocket('device:np','s','n',opts.ndevice)
|
|
|
|
log = logger({
|
|
file: 'src/device-packet.js',
|
|
class: 'Device',
|
|
name: 'i2c-device',
|
|
id: this.id
|
|
})
|
|
if (!opts.address) log.fatal({ opts: opts, method:'constructor', line:26, msg:'no i2c bus address supplied'})
|
|
this.address = +opts.address // make sure any passed number is number not a string
|
|
this.channel = +opts.channel // if using TAC9546A channel number on which device is attached
|
|
this.bus = this.bindFuncs(commands)
|
|
this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack
|
|
}
|
|
|
|
async init() {
|
|
let res = await super.init()
|
|
if (res.errors) return {error: res.errors, msg:'socket initialization', }
|
|
res = await this.bus.ack()
|
|
let socket = this.getSocket('bus').opts
|
|
let connection = socket.path || socket.host + ':' + socket.port
|
|
if (!res.ack) {
|
|
let err ={error:'ack', res:res, socket:socket, method:'init', line:38, connection:connection, address:this.address, msg:`no bus or device on bus at address ${this.address}=0x${this.address.toString(16)}`}
|
|
log.error(err)
|
|
return err
|
|
}
|
|
return res
|
|
}
|
|
|
|
// TODO enable mux board channel set
|
|
async _setChannel() {
|
|
}
|
|
|
|
} // end of i2c Device Class
|
|
|
|
export default I2CDevice
|