refactored bus packet so packet cmd: is bus function
changed examples accordinglymaster
parent
a2844cc313
commit
a5e52274b0
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"@std/esm": "cjs"
|
||||
}
|
|
@ -12,35 +12,35 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|||
;
|
||||
(async () => {
|
||||
|
||||
let relays = new Base({sockets:'uc', path:PATH, log:true})
|
||||
let relays = new Base({sockets:'uc', path:PATH})
|
||||
|
||||
relays.reply = function (packet) {
|
||||
// console.log(packet.bus)
|
||||
console.log('response from relays',packet.bus.func, packet.bus.args.cmd, packet.bus.response)
|
||||
console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`)
|
||||
}
|
||||
await relays.init()
|
||||
console.log('=============sending============')
|
||||
let packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd: 0}} }
|
||||
console.dir(packet.bus)
|
||||
let packet = {cmd:'read', args:{address:39 ,cmd: 0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 0, byte:0}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'write', args:{address:39,cmd: 0, byte:0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:0}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'read', args:{address:39 ,cmd:0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:255}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'write', args:{address:39,cmd: 9, byte:255} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'read', args:{address:39 ,cmd:9} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
await delay(1000)
|
||||
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:0}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'write', args:{address:39,cmd: 9, byte:0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'read', args:{address:39 ,cmd:9} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
|
||||
await delay(1000)
|
||||
|
|
|
@ -3,25 +3,29 @@
|
|||
*
|
||||
*/
|
||||
|
||||
import Bus from '../src/bus-packet'
|
||||
import Base from '../../uci-base/src/base'
|
||||
|
||||
const PATH = '/opt/uci/uci-base/src/unix.sock'
|
||||
|
||||
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||
;
|
||||
(async () => {
|
||||
|
||||
let i2cbus = new Bus({id:'i2c-bus', sockets:'us,uc'})
|
||||
let scanner = new Base({id:'pipe-i2c-client', sockets:'uc', path:PATH})
|
||||
|
||||
i2cbus.reply = function (packet) {
|
||||
console.log('==== response from i2cbus =>',packet.bus.response)
|
||||
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 i2cbus.init()
|
||||
await scanner.init()
|
||||
console.log('=============sending============')
|
||||
let packet = {cmd:'rw', bus:{func:'scan'} }
|
||||
let packet = {cmd:'scan'}
|
||||
console.dir(packet)
|
||||
await i2cbus.send(packet)
|
||||
await scanner.send(packet)
|
||||
|
||||
await delay(1000)
|
||||
await delay(3000)
|
||||
process.kill(process.pid, 'SIGTERM')
|
||||
|
||||
|
||||
|
|
|
@ -16,31 +16,31 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|||
|
||||
relays.reply = function (packet) {
|
||||
// console.log(packet.bus)
|
||||
console.log('response from relays',packet.bus.func, packet.bus.args.cmd, packet.bus.response)
|
||||
console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`)
|
||||
}
|
||||
await relays.init()
|
||||
console.log('=============sending============')
|
||||
let packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd: 0}} }
|
||||
console.dir(packet.bus)
|
||||
let packet = {cmd:'read', args:{address:39 ,cmd: 0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 0, byte:0}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'write', args:{address:39,cmd: 0, byte:0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:0}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'read', args:{address:39 ,cmd:0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:255}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'write', args:{address:39,cmd: 9, byte:255} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'read', args:{address:39 ,cmd:9} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
await delay(1000)
|
||||
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:0}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'write', args:{address:39,cmd: 9, byte:0} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} }
|
||||
console.dir(packet.bus)
|
||||
packet = {cmd:'read', args:{address:39 ,cmd:9} }
|
||||
console.dir(packet)
|
||||
await relays.send(packet)
|
||||
|
||||
await delay(1000)
|
||||
|
|
|
@ -2,11 +2,8 @@
|
|||
* A tcp customer/client to talk with the i2c bus and scan the bus for devices
|
||||
*
|
||||
*/
|
||||
|
||||
import Base from '../../uci-base/src/base'
|
||||
const HOST = 'sbc'
|
||||
|
||||
|
||||
|
||||
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||
;
|
||||
(async () => {
|
||||
|
@ -14,14 +11,14 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|||
let scanner = new Base({id:'tcp-i2c-client', sockets:'tc', host:HOST})
|
||||
|
||||
scanner.reply = function (packet) {
|
||||
let addresses = packet.bus.response.map(device => {
|
||||
let addresses = packet.response.map(device => {
|
||||
return device.toString(16)})
|
||||
console.log(packet)
|
||||
// console.log(packet)
|
||||
console.log('==== device hex addreses on i2cbus ===\n',addresses)
|
||||
}
|
||||
await scanner.init()
|
||||
console.log('=============sending============')
|
||||
let packet = {cmd:'rw', bus:{func:'scan'} }
|
||||
let packet = {cmd:'scan'}
|
||||
console.dir(packet)
|
||||
await scanner.send(packet)
|
||||
|
||||
|
|
|
@ -11,66 +11,100 @@ export default class Bus extends Base {
|
|||
this.busnum = opts.busnum || 1
|
||||
this.i2cbus = i2c.open(this.busnum, () => {})
|
||||
this.funcs = bus_funcs
|
||||
this.registerPacketProcessor.bind(this)(process)
|
||||
// this.init = this.init.bind(this)
|
||||
}
|
||||
|
||||
async init(){
|
||||
// console.log('init', this.rw)
|
||||
await super.init()
|
||||
}
|
||||
|
||||
async rw (packet){
|
||||
// console.dir('=>',packet.bus.fusncs)
|
||||
if (!packet.bus.func) return {error: 'no i2c bus function in packet', packet: packet.bus }
|
||||
if (this.funcs[packet.bus.func]) {
|
||||
packet.bus.response = await this.funcs[packet.bus.func].bind(this)(packet.bus.args)
|
||||
} // end of Bus Packet Class
|
||||
|
||||
// replace base processor with one for i2c bus functions
|
||||
const process = async function (packet){
|
||||
if (!packet.cmd) return {error: 'no cmd: key in packet', packet: packet }
|
||||
if (this.context.funcs[packet.cmd]) {
|
||||
// let res = validateArgs(packet)
|
||||
// if (res.error) return res.error
|
||||
packet.cmd_sent = packet.cmd
|
||||
packet.response = await this.context.funcs[packet.cmd].bind(this.context)(packet.args)
|
||||
packet.cmd = 'reply'
|
||||
return packet
|
||||
}
|
||||
return {error: 'no i2c bus function available for packet function', packet: packet.bus }
|
||||
} else return {error: 'no i2c bus function available for packet command', packet: packet }
|
||||
}
|
||||
|
||||
} // end of Bus Packet Class
|
||||
const validateArgs = function (packet) {
|
||||
let missing = []
|
||||
console.log(packet)
|
||||
if (packet.cmd==='scan' || packet.cmd ==='close') return {}
|
||||
if (!packet.args.address) missing.push('address')
|
||||
switch (packet.cmd)
|
||||
{
|
||||
case 'readRaw':
|
||||
case 'writeRaw':
|
||||
if (!packet.args.length) missing.push('length')
|
||||
if (!packet.args.buffer) missing.push('buffer')
|
||||
break
|
||||
case 'read':
|
||||
case 'read2':
|
||||
case 'write':
|
||||
case 'write2':
|
||||
if (!packet.args.cmd) missing.push('cmd')
|
||||
}
|
||||
|
||||
switch (packet.cmd)
|
||||
{
|
||||
case 'write':
|
||||
case 'write2':
|
||||
case 'send':
|
||||
if (!packet.args.byte) missing.push('byte')
|
||||
}
|
||||
|
||||
if (missing.length > 0) {
|
||||
return {error: `following arguments are missing ${missing}`, packet: packet }
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
const bus_funcs = {
|
||||
|
||||
scan: function () { return pify(this.i2cbus.scan).bind(this.i2cbus)() },
|
||||
close: function () { return pify(this.i2cbus.close).bind(this.i2cbus)() },
|
||||
|
||||
readRaw: function (arg) {
|
||||
return pify(this.i2cbus.i2cRead).bind(this.i2cbus)(arg.address, arg.length, arg.buffer)
|
||||
readRaw: function (args) {
|
||||
return pify(this.i2cbus.i2cRead).bind(this.i2cbus)(args.address, args.length, args.buffer)
|
||||
},
|
||||
|
||||
writeRaw: function (arg) {
|
||||
return pify(this.i2cbus.i2cWrite).bind(this.i2cbus)(arg.address, arg.length, arg.buffer)
|
||||
writeRaw: function (args) {
|
||||
return pify(this.i2cbus.i2cWrite).bind(this.i2cbus)(args.address, args.length, args.buffer)
|
||||
},
|
||||
|
||||
read: function (arg) {
|
||||
read: function (args) {
|
||||
// console.log('read: address, cmd', address, cmd)
|
||||
return pify(this.i2cbus.readByte).bind(this.i2cbus)(arg.address, arg.cmd)
|
||||
return pify(this.i2cbus.readByte).bind(this.i2cbus)(args.address, args.cmd)
|
||||
},
|
||||
|
||||
write: function (arg) {
|
||||
// console.log('write: address, cmd, byte', arg.address, arg.cmd, arg.byte)
|
||||
return pify(this.i2cbus.writeByte.bind(this.i2cbus))(arg.address, arg.cmd, arg.byte)
|
||||
write: function (args) {
|
||||
// console.log('write: address, cmd, byte', args.address, args.cmd, args.byte)
|
||||
return pify(this.i2cbus.writeByte.bind(this.i2cbus))(args.address, args.cmd, args.byte)
|
||||
},
|
||||
|
||||
read2: function (arg) {
|
||||
return pify(this.i2cbus.readWord.bind(this.i2cbus))(arg.address, arg.cmd)
|
||||
read2: function (args) {
|
||||
return pify(this.i2cbus.readWord.bind(this.i2cbus))(args.address, args.cmd)
|
||||
},
|
||||
|
||||
write2: function (arg) {
|
||||
return pify(this.i2cbus.writeWord.bind(this.i2cbus))(arg.address, arg.cmd, arg.bytes)
|
||||
write2: function (args) {
|
||||
return pify(this.i2cbus.writeWord.bind(this.i2cbus))(args.address, args.cmd, args.byte)
|
||||
},
|
||||
|
||||
receive: function (arg) {
|
||||
receive: function (args) {
|
||||
// console.log('receivebyte', address)
|
||||
return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(arg.address)
|
||||
return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(args.address)
|
||||
},
|
||||
|
||||
send: function (arg) {
|
||||
send: function (args) {
|
||||
// console.log('sendbyte', address,byte)
|
||||
return pify(this.i2cbus.sendByte.bind(this.i2cbus))(arg.address, arg.byte)
|
||||
}
|
||||
|
||||
return pify(this.i2cbus.sendByte.bind(this.i2cbus))(args.address, args.byte)
|
||||
}
|
||||
} //end i2c functions
|
||||
|
|
Loading…
Reference in New Issue