uci-i2c-bus/examples/tcp-relay.mjs

53 lines
1.6 KiB
JavaScript
Raw Normal View History

/*
* A tcp customer/client to talk with the i2c bus and scan the bus for devices
*
*/
const HOST = 'sbc'
import Base from '../../uci-base/src/base'
// 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', host:HOST, log:true})
relays.reply = function (packet) {
// console.log(packet.bus)
console.log('response from relays',packet.bus.func, packet.bus.args.cmd, packet.bus.response)
}
await relays.init()
console.log('=============sending============')
let packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd: 0}} }
console.dir(packet.bus)
await relays.send(packet)
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 0, byte:0}} }
console.dir(packet.bus)
await relays.send(packet)
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:0}} }
console.dir(packet.bus)
await relays.send(packet)
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:255}} }
console.dir(packet.bus)
await relays.send(packet)
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} }
console.dir(packet.bus)
await relays.send(packet)
await delay(1000)
packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:0}} }
console.dir(packet.bus)
await relays.send(packet)
packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} }
console.dir(packet.bus)
await relays.send(packet)
await delay(1000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})