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", "name": "@uci/i2c-device",
"version": "0.1.23", "version": "0.1.25",
"description": "Device Classes for I2C Interfacing", "description": "Device Classes for I2C Interfacing",
"main": "src/device", "main": "src/device",
"scripts": { "scripts": {
@ -26,7 +26,7 @@
}, },
"homepage": "https://github.com/uCOMmandIt/i2c#readme", "homepage": "https://github.com/uCOMmandIt/i2c#readme",
"dependencies": { "dependencies": {
"@uci/base": "^0.1.31", "@uci/base": "^0.1.32",
"@uci-utils/logger": "^0.0.15" "@uci-utils/logger": "^0.0.15"
}, },
"devDependencies": { "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 commands from './bus-device-commands'
import logger from '@uci-utils/logger' import logger from '@uci-utils/logger'
let log = {} let log = {}
class I2CDevice extends Base { class I2CDevice extends Base {
constructor(opts) { constructor(opts) {
opts.ndevice = opts.ndevice || {} // opts.ndevice = opts.ndevice || {}
opts.tdevice = opts.tdevice || {} // opts.tdevice = opts.tdevice || {}
opts.ndevice.path = opts.ndevice.path || opts.path || 'i2c-device' // opts.ndevice.path = opts.ndevice.path || opts.path || 'i2c-device'
opts.tdevice.port = opts.tdevice.port || opts.port || 1777 // opts.tdevice.port = opts.tdevice.port || opts.port || 1777
opts.bus = opts.bus || {} 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 if (opts.bus.host) opts.bus.port = opts.bus.port || 1776
else opts.bus.path = opts.bus.path || 'i2c-bus' else opts.bus.path = opts.bus.path || 'i2c-bus'
super(opts) super(opts)
this.addSocket('bus','c',opts.bus.host ? 't':'n',opts.bus) 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.tdevice.name ||'device:tcp','s','t',opts.tdevice)
this.addSocket(opts.ndevice.name ||'device:np','s','n',opts.ndevice) // this.addSocket(opts.ndevice.name ||'device:np','s','n',opts.ndevice)
log = logger({ log = logger({
file: 'src/device-packet.js', 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.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.channel = +opts.channel // if using TAC9546A channel number on which device is attached
this.bus = this.bindFuncs(commands) 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._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=>{ // emit on connected and available
if (ev.state==='connected') {
let res = await this.bus.ack() this.ready.addObserver('i2c',this.ready.getObserver('bus').pipe(
// console.log('bus address ack was', res.ack) map(async ready => {
let socket = this.getSocket('bus').opts if (ready) return (await this.bus.ack()).ack
let connection = socket.path || socket.host + ':' + socket.port return false
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) // map(() => {return false})
} else this.emit('ready:i2c',{bus:socket, connection:connection}) ))
}
}) } // end contructor
}
// TODO enable mux board channel set // TODO enable mux board channel set
async _setChannel() { async _setChannel() {
@ -49,3 +49,4 @@ class I2CDevice extends Base {
} // end of i2c Device Class } // end of i2c Device Class
export default I2CDevice export default I2CDevice
export {I2CDevice, map, changed, isPlainObject, to, merge}