update deps - improve the examples and make more flexible to try other devices

master
David Kebler 2019-09-09 13:26:02 -07:00
parent 70bd13e9dc
commit 95a8d20649
10 changed files with 100 additions and 180 deletions

View File

@ -8,7 +8,7 @@ import Bus from '../src/bus'
; ;
(async () => { (async () => {
let i2cbus = new Bus({id:'i2c-bus',tcp:true}) let i2cbus = new Bus({id:'i2c-busx',tcp:true})
await i2cbus.init() await i2cbus.init()

31
examples/client.js Normal file
View File

@ -0,0 +1,31 @@
/*
* A tcp customer/client to talk with the i2c bus manipulate an i2c device
*
*/
const DEVICE = (process.env.BUS_DEVICE || 'scan')
const TRANSPORT = (process.env.TRANSPORT || 'tcp')
import Base from '@uci/base'
// import {test, reply } from './relays'
const HOST = (process.env.BUS_HOST || 'sbc')
const PORT = (process.env.BUS_PORT || 1776)
const ADDRESS = 39
let options = {id:'i2c-client', useRootNS:true}
;
(async () => {
// import not supported by eslint, but esm does so ignore parsing error
let {test,reply} = await import(`./${DEVICE}`)
let client = new Base(options)
if (TRANSPORT==='tcp') client.addSocket('tcp','c','t',{host:HOST, port:1776})
else client.addSocket('np','c','n',{path:'i2c-bus'})
client.reply = reply // add reply processor
await client.init()
await test.call(client,ADDRESS)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -1,51 +0,0 @@
/*
* A tcp customer/client to talk with the i2c bus and scan the bus for devices
*
*/
const PATH = 'i2c-bus'
import Base from '@uci/base'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let relays = new Base({id:'pipe-i2c-client', sockets:'uc#c>n', uc:{path:PATH}})
relays.reply = function (packet) {
// console.log(packet.bus)
console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`)
}
await relays.init()
console.log('=============sending============')
let packet = {cmd:'read', args:{address:39 ,cmd: 0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'write', args:{address:39,cmd: 0, byte:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:39 ,cmd:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'write', args:{address:39,cmd: 9, byte:255} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:39 ,cmd:9} }
console.dir(packet)
await relays.send(packet)
await delay(1000)
packet = {cmd:'write', args:{address:39,cmd: 9, byte:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:39 ,cmd:9} }
console.dir(packet)
await relays.send(packet)
await delay(1000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -1,35 +0,0 @@
/*
* i2c bus unix socket and client in one for demo
*
*/
import Base from '@uci/base'
const PATH = 'i2c-bus'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let scanner = new Base({id:'pipe-i2c-client', sockets:'uc#c>n', uc:{path:PATH}})
scanner.reply = function (packet) {
let addresses = packet.response.map(device => {
return device.toString(16)})
// console.log(packet)
console.log('==== device hex addreses on i2cbus ===\n',addresses)
}
await scanner.init()
console.log('=============sending============')
let packet = {cmd:'scan'}
console.dir(packet)
await scanner.send(packet)
await delay(3000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

40
examples/relays.js Normal file
View File

@ -0,0 +1,40 @@
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
export function reply (packet) {
let req = packet._header.request
console.log(`response from relays for ${req.cmd}:`,req.args, `was ${packet.response}`)
}
export async function test (address) {
let packet
console.log('=============sending packets for i2c mcp23008 relay device ============')
console.log('setting ioddir')
packet = {cmd:'write', args:{address:address,cmd: 0, byte:0} }
console.dir(packet)
await this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:0} }
console.dir(packet)
await this.send(packet)
console.log('========= turn on each relay ============')
let byte = 0
for (let i = 0; i < 8 ; i++) {
byte += Math.pow(2,i)
packet = {cmd:'write', args:{address:address,cmd: 9, byte:byte} }
console.log(`==== relay ${i+1} on with byte: ${byte} ===`)
console.dir(packet)
await this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:9} }
console.dir(packet)
await this.send(packet)
await delay(1000)
}
console.log('========= done each relay, clear (off) this ============')
packet = {cmd:'write', args:{address:address,cmd: 9, byte:0} }
console.dir(packet)
await this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:9} }
console.dir(packet)
await this.send(packet)
await delay(1000)
}

16
examples/scan.js Normal file
View File

@ -0,0 +1,16 @@
export function reply (packet) {
let addresses = (radix=16) => { return packet.response.map(device => {
return device.toString(radix)}) }
// console.log(packet)
console.log('==== device decimal addreses on i2cbus ===\n',addresses(10))
console.log('==== device hex addreses on i2cbus ===\n',addresses())
}
export async function test () {
console.log('=============sending scan request ============')
let packet = {cmd:'scan'}
console.dir(packet)
await this.send(packet)
}

View File

@ -1,51 +0,0 @@
/*
* A tcp customer/client to talk with the i2c bus and scan the bus for devices
*
*/
const HOST = (process.env.BUS_HOST || 'sbc')
const ADDRESS = 39
import Base from '@uci/base'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let relays = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{host:HOST, port:1776}})
relays.reply = async function (packet) {
console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`)
}
await relays.init()
console.log('=============sending============')
let packet = {cmd:'read', args:{address:ADDRESS ,cmd: 0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'write', args:{address:ADDRESS,cmd: 0, byte:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:ADDRESS ,cmd:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'write', args:{address:ADDRESS,cmd: 9, byte:255} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:ADDRESS ,cmd:9} }
console.dir(packet)
await relays.send(packet)
await delay(1000)
packet = {cmd:'write', args:{address:ADDRESS,cmd: 9, byte:0} }
console.dir(packet)
await relays.send(packet)
packet = {cmd:'read', args:{address:ADDRESS ,cmd:9} }
console.dir(packet)
await relays.send(packet)
await delay(1000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -1,31 +0,0 @@
/*
* A tcp customer/client to talk with the i2c bus and scan the bus for devices
*
*/
import Base from '@uci/base'
const HOST = (process.env.BUS_HOST || 'sbc')
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let scanner = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{host:HOST,port: 1776}})
scanner.reply = function (packet) {
let addresses = packet.response.map(device => {
return device.toString(16)})
// console.log(packet)
console.log('==== device hex addreses on i2cbus ===\n',addresses)
}
await scanner.init()
console.log('=============sending============')
let packet = {cmd:'scan'}
console.dir(packet)
await scanner.send(packet)
await delay(3000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})

View File

@ -4,13 +4,14 @@
"description": "I2c Bus Classes for Communication to I2C bus via socket or direct call", "description": "I2c Bus Classes for Communication to I2C bus via socket or direct call",
"main": "src/bus", "main": "src/bus",
"scripts": { "scripts": {
"nrelay": "node --require esm examples/np-relay", "client": "node --require esm examples/client || true",
"trelay": "node --require esm examples/tcp-relay", "client:pipe": "TRANSPORT=pipe npm run client",
"pcan": "node --require esm examples/np-scan || true", "relays": "BUS_DEVICE=relays npm run client",
"tscan": "node --require esm examples/tcp-scan || true", "relays:pipe": "TRANSPORT=pipe npm run relays",
"bus": "node --require esm examples/bus", "bus": "node --require esm examples/bus",
"busd": "DEBUG=true node --require esm examples/bus", "bus:dev": "UCI_ENV=dev ./node_modules/.bin/nodemon --require esm examples/bus",
"busl": "UCI_LOG=true node --require esm examples/bus" "bus:debug": "UCI_LOG_LEVEL=debug npm run bus:dev",
"busl": "UCI_ENV=pro node --require esm examples/bus"
}, },
"author": "David Kebler", "author": "David Kebler",
"license": "MIT", "license": "MIT",
@ -29,15 +30,16 @@
}, },
"homepage": "https://github.com/uCOMmandIt/i2c#readme", "homepage": "https://github.com/uCOMmandIt/i2c#readme",
"optionalDependencies": { "optionalDependencies": {
"i2c-bus": "^4.0.10" "i2c-bus": "^4.0.11"
}, },
"dependencies": { "dependencies": {
"@uci/base": "^0.1.25",
"@uci-utils/logger": "^0.0.15", "@uci-utils/logger": "^0.0.15",
"@uci/base": "^0.1.27",
"better-try-catch": "^0.6.2", "better-try-catch": "^0.6.2",
"pify": "^4.0.1" "pify": "^4.0.1"
}, },
"devDependencies": { "devDependencies": {
"esm": "^3.2.25" "esm": "^3.2.25",
"nodemon": "^1.19.2"
} }
} }

View File

@ -34,8 +34,7 @@ class I2CBus extends Base {
async init() { async init() {
await super.init() await super.init()
} }
// TODO use the replacement method instead of replacing _packetPorcess
// TODO use the resplacement method instead of replacing _packetPorcess
// or refactor bus_funcs and add to a namespace so default processing can be used // or refactor bus_funcs and add to a namespace so default processing can be used
// which will allow adding more command to the this module // which will allow adding more command to the this module
// can use a packet Hook to do validation. // can use a packet Hook to do validation.