0.1.20 add default named pipe and tcp socket/server

return init errors
add ack package function to call bus ac
master
David Kebler 2019-08-15 14:05:24 -07:00
parent a60f99ad0a
commit 471ee2df1d
7 changed files with 68 additions and 69 deletions

38
examples/example.js Normal file
View File

@ -0,0 +1,38 @@
/*
*
*/
import Device from '../src/device'
const ADDRESS = process.env.ADDRESS || 0x27
const TIMEOUT = 15
;
(async () => {
let device = new Device({id:'i2c-device', useRootNS:true, initTimeout:TIMEOUT, address:ADDRESS, bus:{host:process.env.HOST, port:process.env.PORT}})
device.on('status',err => {
console.log('STATUS EMITTED\n', err)
})
device.reply = function (packet) {
console.log('=====bus reply response====')
console.log('to request:',packet._header.request)
console.log('was:',packet.response)
console.log('===============')
}
let res = await device.init()
if (res.error) {
console.log('errors during init')
process.kill(process.pid, 'SIGTERM')
}
console.log('all connected good proceed with some commands to bus')
await device.bus.write(9,255)
device.bus.write(9,0)
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})

View File

@ -1,24 +0,0 @@
/*
* i2c bus with named pipe socket - run on same machine/host as bus
*
*/
import Device from '../src/device'
// const PATH = ''
;
(async () => {
let device = new Device({id:'an i2c device', useRootNS:true, 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

@ -1,24 +0,0 @@
/*
* 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'
// const PATH = ''
;
(async () => {
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.request)
console.log('bus response is ',packet.response)
}
console.log('return from await scan', (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')
})

3
nodemon.json Normal file
View File

@ -0,0 +1,3 @@
{
"ignoreRoot": [".git"]
}

4
nodemon.json.sav Normal file
View File

@ -0,0 +1,4 @@
{
"ignoreRoot": [".git"],
"watch": ["node_modules/@uci/*/src","node_modules/@uci-utils/*/src","src","examples"]
}

View File

@ -1,11 +1,11 @@
{ {
"name": "@uci/i2c-device", "name": "@uci/i2c-device",
"version": "0.1.17", "version": "0.1.20",
"description": "Device Classes for I2C Interfacing", "description": "Device Classes for I2C Interfacing",
"main": "src/device", "main": "src/device",
"scripts": { "scripts": {
"tscan": "node -r esm examples/tcp-scan || true", "example": "node -r esm examples/example --preserve-symlinks || true",
"iscan": "node -r esm examples/ipc-scan || true", "example:dev": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm examples/example --preserve-symlinks || true" ,
"testw": "./node_modules/.bin/mocha --reporter list --recursive --watch", "testw": "./node_modules/.bin/mocha --reporter list --recursive --watch",
"test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true" "test": "istanbul cover ./node_modules/.bin/_mocha test/ --report lcovonly -- -R spec --recursive && codecov || true"
}, },
@ -26,15 +26,14 @@
}, },
"homepage": "https://github.com/uCOMmandIt/i2c#readme", "homepage": "https://github.com/uCOMmandIt/i2c#readme",
"dependencies": { "dependencies": {
"@uci/base": "^0.1.21", "@uci/base": "^0.1.30",
"@uci-utils/logger": "0.0.14" "@uci-utils/logger": "0.0.15"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.1.2", "chai": "^4.2.0",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"codecov": "^3.0.2", "esm": "^3.2.25",
"esm": "^3.0.38", "mocha": "^6.2.0",
"istanbul": "^0.4.5", "nodemon": "^1.19.2"
"mocha": "^5.2.0"
} }
} }

View File

@ -5,16 +5,18 @@ let log = {}
class I2CDevice extends Base { class I2CDevice extends Base {
constructor(opts) { constructor(opts) {
opts.ndevice = opts.ndevice || {}
opts.tdevice = opts.tdevice || {}
opts.ndevice.path = opts.path || 'i2c-device'
opts.tdevice.port = opts.port || 1777
opts.bus = opts.bus || {} opts.bus = opts.bus || {}
if (opts.bus.host || opts.host) { if (opts.bus.host) opts.bus.port = opts.bus.port || 1776
(opts.bus.host = opts.host || opts.bus.host), else opts.bus.path = opts.bus.path || 'i2c-bus'
(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) super(opts)
this.addSocket('bus','c',opts.bus.host ? 't':'n',opts.bus)
this.addSocket('device:tcp','s','t',opts.tdevice)
this.addSocket('device:np','s','n',opts.ndevice)
log = logger({ log = logger({
file: 'src/device-packet.js', file: 'src/device-packet.js',
class: 'Device', class: 'Device',
@ -25,12 +27,13 @@ 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)
this.addNamespace('bus','s') // give remote packet access to bus commands. ack will superceed base ack this._s.ack = async (packet) => { return Object.assign(packet,await this.bus.ack()) } // give socket access to bus.ack
} }
async init() { async init() {
await super.init() let res = await super.init()
let res = await this.bus.ack() if (res.errors) return {error: res.errors, msg:'socket initialization', }
res = await this.bus.ack()
let socket = this.getSocket('bus').opts let socket = this.getSocket('bus').opts
let connection = socket.path || socket.host + ':' + socket.port let connection = socket.path || socket.host + ':' + socket.port
if (!res.ack) { if (!res.ack) {