56 lines
2.2 KiB
JavaScript
56 lines
2.2 KiB
JavaScript
import { Base, map, changed, isPlainObject, to, merge } from '@uci/base'
|
|
import commands from './bus-device-commands'
|
|
import logger from '@uci-utils/logger'
|
|
let log = {}
|
|
|
|
class I2CDevice extends Base {
|
|
constructor(opts) {
|
|
|
|
log = logger({
|
|
file: 'src/device-packet.js',
|
|
class: 'Device',
|
|
name: 'i2c-device',
|
|
id: opts.id
|
|
})
|
|
|
|
if (!opts.address) {
|
|
log.fatal({ opts: opts, method:'constructor', line:26, msg:'no i2c bus address supplied'})
|
|
throw new Error(`no i2c bus address supplied for device ${opts.id}:${opts.name}`)
|
|
}
|
|
opts.i2cbus = opts.i2cbus || { name:opts.name||'i2c-bus', type:'c', transport:'n', options:{name:opts.name ||'i2c-device', path:opts.path||'i2c-bus'}}
|
|
if ((opts.i2cbus.options || {}).host) opts.i2cbus.options.port = opts.i2cbus.options.port || 1776
|
|
else opts.i2cbus.options.path = opts.i2cbus.options.path || 'i2c-bus'
|
|
// console.log('+++++++++++++++++++++++BUS OPTIONS FOR CONSUMER SOCKET+++++++++++++++++')
|
|
// console.dir(opts.i2cbus)
|
|
super(opts)
|
|
this.registerSocket(opts.i2cbus)
|
|
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.i2cbusName = opts.i2cbus.name || 'bus'
|
|
this.bus = this.bindFuncs(commands)
|
|
// todo don't override ack this way in case _s moves
|
|
this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack
|
|
|
|
this.ready.addObserver(`${opts.i2cbus.name}:process`) // will emit on received <bus>:process ready packet
|
|
let busobs = this.ready.combineObservers(`${opts.i2cbus.name}`,[this.getSocket(opts.i2cbus.name).obsName,`${opts.i2cbus.name}:process`])
|
|
// make device ack by extending bus observer
|
|
this.ready.addObserver('i2c:device:ack',busobs
|
|
.pipe(
|
|
map(async ready => {
|
|
if (ready) return (await this.bus.ack()).ack
|
|
return false
|
|
})
|
|
)
|
|
)
|
|
|
|
} // end contructor
|
|
|
|
// TODO enable mux board channel set
|
|
async _setChannel() {
|
|
}
|
|
|
|
} // end of i2c Device Class
|
|
|
|
export default I2CDevice
|
|
export {I2CDevice, map, changed, isPlainObject, to, merge}
|