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

52 lines
1.5 KiB
JavaScript

/*
* 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)
})