uci-mcp/test/read-write.js

69 lines
1.7 KiB
JavaScript

//to run this test you MUST install @uci/i2c which is not a dependency of @uci/mcp
// npm install @uci/i2c
const Bus = require('@uci/i2c').Bus,
MCP = require('../lib/mcp23008-17'),
expect = require('chai').expect,
pause = require('@uci/utils').pPause
let bus = new Bus()
let ADDR = 0x24
let CHIP = 'MCP23017'
let MS = 500 // so humans can watch the light otherwise set to zero
let PORT = 'B'
let mcp = new MCP[CHIP](bus, ADDR, {
pin_cfg_default: 'output',
});
(async function () {
await mcp.init()
await mcp.allOff(PORT) // start clean
await eachpin()
await somepins()
})()
//****************** TEST SUITES **********************
async function eachpin() {
let result
for (let pin of mcp.ports[PORT].pins) {
await mcp.on(pin.address, PORT)
result = await mcp.readPort(PORT)
console.log(pin.address, result)
await mcp.off(pin.address, PORT)
await pause(MS)
}
}
async function somepins() {
let pins = [1, 7, 8]
await mcp.on(pins, PORT, 'PLC')
let shouldbe = [1, 7, 8]
let result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(pins, shouldbe, result)
await pause(MS)
pins = [2, 1, 7]
await mcp.on(pins, PORT, 'PLC')
shouldbe = [2, 1, 7, 8]
result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(pins, shouldbe, result)
await pause(MS)
pins = [2, 1, 6]
await mcp.toggle(pins, PORT, 'PLC')
shouldbe = [6, 7, 8]
result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(pins, shouldbe, result)
await pause(MS)
pins = [2, 1, 7]
await mcp.off(pins, PORT, 'PLC')
shouldbe = [6, 8]
result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(pins, shouldbe, result)
await pause(MS)
await mcp.allOff(PORT) // clear port after each test
}