parent
f146f6be9a
commit
fc066e773a
|
@ -2,13 +2,13 @@
|
||||||
* i2c bus with named pipe socket - run on same machine/host as bus
|
* 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 = ''
|
// const PATH = ''
|
||||||
|
|
||||||
;
|
;
|
||||||
(async () => {
|
(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) {
|
device.reply = function (packet) {
|
||||||
console.log('for request ',packet._header)
|
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
|
* 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 = ''
|
// const PATH = ''
|
||||||
|
|
||||||
;
|
;
|
||||||
(async () => {
|
(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) {
|
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('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')
|
process.kill(process.pid, 'SIGTERM')
|
||||||
|
|
||||||
})().catch(err => {
|
})().catch(err => {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/i2c-device",
|
"name": "@uci/i2c-device",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"description": "Device Classes for I2C Interfacing",
|
"description": "Device Classes for I2C Interfacing",
|
||||||
"main": "src/device-packet",
|
"main": "src/device",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"tscan": "node -r esm examples/tcp-scan || true",
|
"tscan": "node -r esm examples/tcp-scan || true",
|
||||||
"iscan": "node -r esm examples/ipc-scan || true",
|
"iscan": "node -r esm examples/ipc-scan || true",
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
|
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uci/base": "^0.1.6",
|
"@uci/base": "^0.1.7",
|
||||||
"@uci/logger": "0.0.3"
|
"@uci/logger": "0.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"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 = {}
|
||||||
|
|
||||||
// **********************************
|
export default class Device extends Base {
|
||||||
|
constructor(opts) {
|
||||||
class Device {
|
opts.bus = opts.bus || {}
|
||||||
// bus is i2c-bus bus object
|
if (opts.bus.host || opts.host) {
|
||||||
constructor(bus, address, opts) {
|
opts.bus.host = opts.host || opts.bus.host,
|
||||||
this.bus = bus
|
opts.bus.port = opts.bus.port || opts.port || 1776
|
||||||
this.address = address
|
opts.sockets = (opts.sockets ? (opts.sockets + ',') : '') + 'bus#c>t'
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
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() {
|
async _setChannel() {
|
||||||
// console.log('before set',this.address,this.id,this.channel, this.bus.getState)
|
// TODO convert to package - uci-mux
|
||||||
if (this.channel) {
|
// console.log('before set',this.address,this.id,this._channel, await this.socket.bus.send(getState)
|
||||||
if (!this.bus.address) { return Promise.reject('Channel set but no mux on bus')}
|
// if (this._channel) {
|
||||||
return this.bus.set(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
|
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) {
|
} // end of i2c Device Class
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue