uci-i2c-bus/examples/relays.js

57 lines
1.8 KiB
JavaScript

const delay = time => new Promise(res=>setTimeout(()=>res(),time))
export async function error (packet) {
console.log('======== error during socket/server processing')
if(packet.msg) console.log(packet.msg)
if(packet.error) console.log(packet.error)
if(packet._header.request) {
console.log('sent request was')
console.dir(packet._header.request)
}
console.log('==========')
}
export function reply (packet) {
let req = packet._header.request
if (req.cmd !=='busFuncs') console.log(`response from relays for ${req.cmd}:`,req.args, `was ${packet.response}`)
else {
console.log('==========Available Bus Functions=====')
console.dir(packet.functions)
console.log('===============')
}
}
export async function test (address) {
let packet
console.log('=============sending packets for i2c mcp23008 relay device ============')
console.log('setting all pins to outputs')
packet = {cmd:'write', args:{address:address,cmd: 0} }
console.dir(packet)
await this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:0} }
console.dir(packet)
await this.send(packet)
console.log('========= turn on each relay ============')
let byte = 0
for (let i = 0; i < 8 ; i++) {
byte += Math.pow(2,i)
packet = {cmd:'write', args:{address:address,cmd: 9, byte:byte} }
console.log(`==== relay ${i+1} on with byte: ${byte} ===`)
console.dir(packet)
this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:9} }
console.dir(packet)
this.send(packet)
await delay(300)
}
console.log('========= done each relay, clear (off) this ============')
packet = {cmd:'write', args:{address:address,cmd: 9, byte:0} }
console.dir(packet)
await this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:9} }
console.dir(packet)
await this.send(packet)
await delay(1000)
}