uci-i2c-device/examples/example.js

39 lines
957 B
JavaScript

/*
*
*/
import Device from '../src/device'
const ADDRESS = process.env.ADDRESS || 0x27
const TIMEOUT = 15
;
(async () => {
let device = new Device({id:'i2c-device', useRootNS:true, initTimeout:TIMEOUT, address:ADDRESS, bus:{host:process.env.HOST, port:process.env.PORT}})
device.on('status',err => {
console.log('STATUS EMITTED\n', err)
})
device.reply = function (packet) {
console.log('=====bus reply response====')
console.log('to request:',packet._header.request)
console.log('was:',packet.response)
console.log('===============')
}
let res = await device.init()
if (res.error) {
console.log('errors during init')
process.kill(process.pid, 'SIGTERM')
}
console.log('all connected good proceed with some commands to bus')
await device.bus.write(9,255)
device.bus.write(9,0)
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})