From fc066e773a74737d16f8d0864ea9a6eb22d51716 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Mon, 28 May 2018 14:47:52 -0700 Subject: [PATCH] upgrade dependencies remove old v1 code after making this master branch --- examples/ipc-scan.js | 4 +- examples/tcp-scan.js | 8 ++-- package.json | 10 ++-- src/device-packet.js | 49 -------------------- src/device.js | 107 ++++++++++++++++--------------------------- 5 files changed, 51 insertions(+), 127 deletions(-) delete mode 100644 src/device-packet.js diff --git a/examples/ipc-scan.js b/examples/ipc-scan.js index dfed98c..1729da6 100644 --- a/examples/ipc-scan.js +++ b/examples/ipc-scan.js @@ -2,13 +2,13 @@ * i2c bus with named pipe socket - run on same machine/host as bus * */ -import Device from '../src/device-packet' +import Device from '../src/device' // const PATH = '' ; (async () => { - let device = new Device({id:'an i2c device', address:0x27}) + let device = new Device({id:'an i2c device', useRootNS:true, address:0x27}) device.reply = function (packet) { console.log('for request ',packet._header) diff --git a/examples/tcp-scan.js b/examples/tcp-scan.js index 1ef3a35..f5bf51f 100644 --- a/examples/tcp-scan.js +++ b/examples/tcp-scan.js @@ -2,20 +2,20 @@ * i2c bus with both unix and tcp socket using defaults. For TCP that is host OS name and port 8080 * */ -import Device from '../src/device-packet' +import Device from '../src/device' // const PATH = '' ; (async () => { - let device = new Device({id:'an i2c device', address:0x27, bus:{host:'sbc'} }) + let device = new Device({id:'an i2c device', useRootNS:true, address:0x27, bus:{host:'sbc'} }) device.reply = function (packet) { - console.log('for request ',packet._header) + console.log('for request ',packet._header.request) console.log('bus response is ',packet.response) } - console.log((await device.init()).scan) + console.log('return from await scan', (await device.init()).scan) process.kill(process.pid, 'SIGTERM') })().catch(err => { diff --git a/package.json b/package.json index f5b5882..aa29d49 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "@uci/i2c-device", - "version": "0.1.3", + "version": "0.1.4", "description": "Device Classes for I2C Interfacing", - "main": "src/device-packet", + "main": "src/device", "scripts": { - "tscan": "node -r esmexamples/tcp-scan || true", - "iscan": "node -r esmexamples/ipc-scan || true", + "tscan": "node -r esm examples/tcp-scan || true", + "iscan": "node -r esm examples/ipc-scan || true", "testw": "./node_modules/.bin/mocha --reporter list --recursive --watch", "test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true" }, @@ -26,7 +26,7 @@ }, "homepage": "https://github.com/uCOMmandIt/i2c#readme", "dependencies": { - "@uci/base": "^0.1.6", + "@uci/base": "^0.1.7", "@uci/logger": "0.0.3" }, "devDependencies": { diff --git a/src/device-packet.js b/src/device-packet.js deleted file mode 100644 index ff6a67f..0000000 --- a/src/device-packet.js +++ /dev/null @@ -1,49 +0,0 @@ -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/logger' -let log = {} - -export default class Device 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) - // console.log('created i2c device with these options\n', 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 - 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) throw `no device operational on bus '${socket.id}' (${connection}) at address ${this.address}=0x${this.address.toString(16)}` - 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 diff --git a/src/device.js b/src/device.js index e2c9425..ff6a67f 100644 --- a/src/device.js +++ b/src/device.js @@ -1,76 +1,49 @@ -'use strict' +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/logger' +let log = {} -// ********************************** - -class Device { - // bus is i2c-bus bus object - constructor(bus, address, opts) { - this.bus = bus - this.address = address - if (opts) { - this.id = opts.id // must be unique within a bus - this.desc = opts.desc - this.channel = opts.channel // if using TAC9546A channel number on which device is attached +export default class Device 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) + // console.log('created i2c device with these options\n', 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 + 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) throw `no device operational on bus '${socket.id}' (${connection}) at address ${this.address}=0x${this.address.toString(16)}` + return res } async _setChannel() { - // console.log('before set',this.address,this.id,this.channel, this.bus.getState) - if (this.channel) { - if (!this.bus.address) { return Promise.reject('Channel set but no mux on bus')} - return this.bus.set(this.channel) - } + // 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 } - // for devices that need just a simple send of a byte without a register command - async receive() { - await this._setChannel() - return this.bus.receive(this.address) - } - async send(cmd, byte) { - await this._setChannel() - return this.bus.send(this.address, cmd, byte) - } - - // for devices needing a buffer/stream - async readRaw(length, buffer) { - await this._setChannel() - return this.bus.readRaw(this.address, length, buffer) - } - - async writeRaw(length, buffer) { - await this._setChannel() - return this.bus.writeRaw(this.address, length, buffer) - } - - // both cmd and byte should be a single byte as a decimal or hex - async read(cmd) { - await this._setChannel() - // console.log('after set before read',this.address,this.id,this.channel,this.bus.getState) - return this.bus.read(this.address, cmd) - } - - async write(cmd, byte) { - await this._setChannel() - // console.log('after set, before write',this.address,this.id,this.channel,this.bus.getState) - return this.bus.write(this.address, cmd, byte) - } - - // for I2C devices that use a word length packackage - async read2(cmd) { - await this._setChannel() - return this.bus.read2(this.address, cmd) - } - - async write2(cmd, bytes) { - await this._setChannel() - return this.bus.write2(this.address, cmd, bytes) - } - -} - -module.exports = { - Device -} +} // end of i2c Device Class