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

35 lines
833 B
JavaScript

/*
* i2c bus unix socket and client in one for demo
*
*/
import Base from '../../uci-base/src/base'
const PATH = '/opt/sockets/i2c-bus.sock'
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)
})