uci-i2c-bus/examples/relays.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

const delay = time => new Promise(res=>setTimeout(()=>res(),time))
export function reply (packet) {
let req = packet._header.request
console.log(`response from relays for ${req.cmd}:`,req.args, `was ${packet.response}`)
}
export async function test (address) {
let packet
console.log('=============sending packets for i2c mcp23008 relay device ============')
console.log('setting ioddir')
packet = {cmd:'write', args:{address:address,cmd: 0, byte: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)
await this.send(packet)
packet = {cmd:'read', args:{address:address ,cmd:9} }
console.dir(packet)
await this.send(packet)
await delay(1000)
}
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)
}