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

32 lines
849 B
JavaScript

/*
* 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 () => {
let scanner = new Base({id:'tcp-i2c-client', sockets:'tc#c>t', tc:{port: 1776, host:HOST}})
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)
})