0.1.25 add ready i2c device observer

remove default t and n devices, just keep single bus socket by default
master
David Kebler 2020-01-06 23:28:52 -08:00
parent d29b9295fc
commit 390e4d1854
2 changed files with 25 additions and 24 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@uci/i2c-device",
"version": "0.1.23",
"version": "0.1.25",
"description": "Device Classes for I2C Interfacing",
"main": "src/device",
"scripts": {
@ -26,7 +26,7 @@
},
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
"dependencies": {
"@uci/base": "^0.1.31",
"@uci/base": "^0.1.32",
"@uci-utils/logger": "^0.0.15"
},
"devDependencies": {

View File

@ -1,21 +1,21 @@
import Base from '@uci/base'
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) {
opts.ndevice = opts.ndevice || {}
opts.tdevice = opts.tdevice || {}
opts.ndevice.path = opts.ndevice.path || opts.path || 'i2c-device'
opts.tdevice.port = opts.tdevice.port || opts.port || 1777
opts.bus = opts.bus || {}
// opts.ndevice = opts.ndevice || {}
// opts.tdevice = opts.tdevice || {}
// opts.ndevice.path = opts.ndevice.path || opts.path || 'i2c-device'
// opts.tdevice.port = opts.tdevice.port || opts.port || 1777
opts.bus = opts.bus || Object.assign({},{host:opts.host, port:opts.port, path:opts.path})
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(opts.tdevice.name ||'device:tcp','s','t',opts.tdevice)
this.addSocket(opts.ndevice.name ||'device:np','s','n',opts.ndevice)
this.registerSocket('bus','c',opts.bus.host ? 't':'n',opts.bus)
// this.addSocket(opts.tdevice.name ||'device:tcp','s','t',opts.tdevice)
// this.addSocket(opts.ndevice.name ||'device:np','s','n',opts.ndevice)
log = logger({
file: 'src/device-packet.js',
@ -27,20 +27,20 @@ class I2CDevice extends Base {
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)
// 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.getSocket('bus').on('connection:socket',async ev=>{
if (ev.state==='connected') {
let res = await this.bus.ack()
// console.log('bus address ack was', res.ack)
let socket = this.getSocket('bus').opts
let connection = socket.path || socket.host + ':' + socket.port
if (!res.ack) {
let err ={level:'fatal', res:res, socket:socket, method:'i2c-device', line:37, connection:connection, address:this.address, msg:`no bus or device on bus at address ${this.address}=0x${this.address.toString(16)}`}
this.emit('log', err)
} else this.emit('ready:i2c',{bus:socket, connection:connection})
}
// emit on connected and available
this.ready.addObserver('i2c',this.ready.getObserver('bus').pipe(
map(async ready => {
if (ready) return (await this.bus.ack()).ack
return false
})
}
// ,
// map(() => {return false})
))
} // end contructor
// TODO enable mux board channel set
async _setChannel() {
@ -49,3 +49,4 @@ class I2CDevice extends Base {
} // end of i2c Device Class
export default I2CDevice
export {I2CDevice, map, changed, isPlainObject, to, merge}