revert back to just opts.bus from settable bus property name, refactor opts.bus processing

master
David Kebler 2018-02-23 10:37:30 -08:00
parent c564ce208d
commit 6f633159a3
5 changed files with 50 additions and 21 deletions

24
examples/ipc-scan.mjs Normal file
View File

@ -0,0 +1,24 @@
/*
* i2c bus with named pipe socket - run on same machine/host as bus
*
*/
import Device from '../src/device-packet'
// const PATH = ''
;
(async () => {
let device = new Device({id:'an i2c device', address:0x27})
device.reply = function (packet) {
console.log('for request ',packet._header)
console.log('bus response is ',packet.response)
}
console.log((await device.init()).scan)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})

View File

@ -4,6 +4,8 @@
"description": "Device Classes for I2C Interfacing",
"main": "src/device-packet.mjs",
"scripts": {
"tscan": "node -r @std/esm examples/tcp-scan || true",
"iscan": "node -r @std/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"
},
@ -23,9 +25,9 @@
"url": "https://github.com/uCOMmandIt/i2c/issues"
},
"homepage": "https://github.com/uCOMmandIt/i2c#readme",
"dependencies": {
"@uci/base": "^0.1.1",
"@uci/logger": "0.0.1"
},
"@std/esm": "cjs",
"devDependencies": {

View File

@ -2,7 +2,7 @@ export default {
ack: async function (){
// TODO if mux channel used check it as well
let bus = await this.send(this._bus_name,{ cmd:'scan'})
let bus = await this.send('bus',{ cmd:'scan'})
if (bus.error) return bus
let res = { cmd:'reply', ack: false, address:this.address, scan:bus.response}
if (bus.response.indexOf(this.address) !== -1) res.ack = true
@ -11,47 +11,47 @@ export default {
receive: async function() {
await this._setChannel()
return await this.send(this._bus_name, { cmd:'receive', args: {address:this.address}})
return await this.send('bus',{ cmd:'receive', args: {address:this.address}})
},
send: async function(byte) {
await this._setChannel()
return await this.send(this._bus_name, { cmd:'send', args: {address:this.address, byte:byte }})
return await this.send('bus',{ cmd:'send', args: {address:this.address, byte:byte }})
},
// for devices needing a buffer/stream
readRaw: async function (length, buffer) {
await this._setChannel()
return await this.send(this._bus_name, { cmd:'readRaw', args: {address:this.address, length:length, buffer:buffer }})
return await this.send('bus',{ cmd:'readRaw', args: {address:this.address, length:length, buffer:buffer }})
},
writeRaw: async function (length, buffer) {
await this._setChannel()
return await this.send(this._bus_name, { cmd:'writeRaw', args: {address:this.address, length:length, buffer:buffer }})
return await this.send('bus',{ cmd:'writeRaw', args: {address:this.address, length:length, buffer:buffer }})
},
// both cmd and byte should be a single byte as a decimal or hex
read: async function (cmd) {
await this._setChannel()
// console.log('after set before read',this.address,this.id,this._channel,await this.socket.bus.send(getState)
return await this.send(this._bus_name, { cmd:'read', args: {address:this.address, cmd:cmd }})
return await this.send('bus',{ cmd:'read', args: {address:this.address, cmd:cmd }})
},
write: async function (cmd, byte) {
await this._setChannel()
// console.log('after set, before write',this.address,this.id,this._channel,await this.socket.bus.send(getState)
return await this.send(this._bus_name, { cmd:'write', args: {address:this.address, cmd:cmd, byte:byte }})
return await this.send('bus',{ cmd:'write', args: {address:this.address, cmd:cmd, byte:byte }})
},
// for I2C devices that use a word length packackage
read2: async function (cmd) {
await this._setChannel()
return await this.send(this._bus_name, { cmd:'read2', args: {address:this.address, cmd:cmd }})
return await this.send('bus',{ cmd:'read2', args: {address:this.address, cmd:cmd }})
},
write2: async function (cmd, bytes) {
await this._setChannel()
return await this.send(this._bus_name, { cmd:'write2', args: {address:this.address, cmd:cmd, byte:bytes }})
return await this.send('bus',{ cmd:'write2', args: {address:this.address, cmd:cmd, byte:bytes }})
}
}

View File

@ -16,18 +16,21 @@ const LOG_OPTS = (id) => {
export default class Device extends Base {
constructor(opts) {
if (!opts.bus) opts.bus ={}
opts.bus.name = opts.bus.name || 'bus' //optional bus socket name
// either device process instance runs on same host (use named pipe) or not (the host of bus must be given)
if (opts[opts.bus.name].host) opts[opts.bus.name].port = opts[opts.bus.name].port || 1776
else opts[opts.bus.name].path = opts[opts.bus.name].path || (process.env.SOCKETS_DIR || __dirname) + '/i2c-bus.sock'
opts.sockets = (opts.sockets ? (opts.sockets+',') : '') + opts.bus.name + '#c>' + ((opts[opts.bus.name].path) ? 'n' :'t')
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({opts:opts},'created i2c device with these options')
console.log('created i2c device with these options\n', opts)
log = logger.child(LOG_OPTS(this.id))
if (!opts.address) log.fatal({opts:opts},'no i2c bus address supplied' )
this.address = opts.address
this._bus_name = opts.bus.name
this._channel = opts.channel // if using TAC9546A channel number on which device is attached
this.bus = this.bindFuncs(commands)
}
@ -35,7 +38,7 @@ export default class Device extends Base {
async init(){
await super.init()
let res = await this.bus.ack()
let socket = this.socket[this._bus_name].opts
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
@ -52,4 +55,4 @@ export default class Device extends Base {
}
} // end of MCP230XX Class
} // end of i2c Device Class