57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
import Base from '@uci/base'
|
|
// import Base from '../../uci-base/src/base'
|
|
import commands from './bus-device-commands'
|
|
// import logger from '../../uci-logger/src/logger'
|
|
import logger from '@uci-utils/logger'
|
|
let log = {}
|
|
|
|
class I2CDevice extends Base {
|
|
constructor(opts) {
|
|
opts.bus = opts.bus || {}
|
|
if (opts.bus.host || opts.host) {
|
|
(opts.bus.host = opts.host || opts.bus.host),
|
|
(opts.bus.port = opts.bus.port || opts.port || 1776)
|
|
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'bus#c>t'
|
|
} else {
|
|
opts.bus.path = opts.bus.path || opts.path || 'i2c-bus'
|
|
opts.sockets = (opts.sockets ? opts.sockets + ',' : '') + 'bus#c>n'
|
|
}
|
|
super(opts)
|
|
log = logger({
|
|
file: 'src/device-packet.js',
|
|
class: 'Device',
|
|
name: 'i2c-device',
|
|
id: this.id
|
|
})
|
|
if (!opts.address) log.fatal({ opts: opts }, '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)
|
|
}
|
|
|
|
async init() {
|
|
await super.init()
|
|
let res = await this.bus.ack()
|
|
let socket = this.socket.bus.opts
|
|
let connection = socket.path || socket.host + ':' + socket.port
|
|
if (!res.ack) {
|
|
let err ={error:'ack', res:res, socket:socket, connection:connection, address:this.address, msg:`no bus or device on bus at address ${this.address}=0x${this.address.toString(16)}`}
|
|
log.warn(err)
|
|
return err
|
|
}
|
|
return res
|
|
}
|
|
|
|
async _setChannel() {
|
|
// TODO convert to package - uci-mux
|
|
// console.log('before set',this.address,this.id,this._channel, await this.socket.bus.send(getState)
|
|
// if (this._channel) {
|
|
// if (!await this.socket.bus.send(address) { return Promise.reject('Channel set but no mux on bus')}
|
|
// return await this.socket.bus.send(set(this._channel)
|
|
// }
|
|
return Promise.resolve() // no channel for device either no mux or device is attached to mux bypass
|
|
}
|
|
} // end of i2c Device Class
|
|
|
|
export default I2CDevice
|