parent
f146f6be9a
commit
fc066e773a
|
@ -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)
|
||||
|
|
|
@ -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 => {
|
||||
|
|
10
package.json
10
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": {
|
||||
|
|
|
@ -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
|
107
src/device.js
107
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
|
||||
|
|
Loading…
Reference in New Issue