uci-mcp/test/read-write.test.js

87 lines
2.3 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 = 0x21
let CHIP = 'MCP23017'
let MS = 0 // so humans can watch the light otherwise set to zero
let PORT = 'B'
let mcp = new MCP[CHIP](bus, ADDR, {
pin_cfg_default: 'output',
});
describe(
`Testing Chip ${CHIP}, Port ${PORT} at I2C bus address of 0x${ADDR.toString(16)}`,
function () {
init()
eachpin()
somepins()
})
//****************** TEST SUITES **********************
function init() {
it('==> Initialize Chip', async function () {
await mcp.init()
await mcp.allOff(PORT) // start clean
})
}
function eachpin() {
it('==> test each pin', async function () {
await mcp.init()
await mcp.allOff(PORT) // start clean
for (let pin of mcp.ports[PORT].pins) {
await mcp.on(pin.address, PORT)
let result = await mcp.readPort(PORT)
expect(result, `pin ${pin.id} at address ${pin.address} write/read failed`).to.equal(pin.address)
await mcp.off(pin.address, PORT)
await pause(MS)
}
await mcp.allOff() // clear port after each test
})
}
function somepins() {
it('==> on, off toggle some pins', async function () {
let pins = [1, 7, 8]
await mcp.on(pins, PORT, 'PLC')
let shouldbe = [1, 7, 8]
let result = await mcp.readPort(PORT, { format: 'PLC' })
expect(result.sort(), 'pin write/read failed on').to.deep.equal(shouldbe.sort())
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' })
expect(result.sort(), 'pin write/read failed on 2').to.deep.equal(shouldbe.sort())
await pause(MS)
pins = [2, 1, 6]
await mcp.toggle(pins, PORT, 'PLC')
shouldbe = [6, 7, 8]
result = await mcp.readPort(PORT, { format: 'PLC' })
expect(result.sort(), 'pin write/read failed toggle').to.deep.equal(shouldbe.sort())
await pause(MS)
pins = [2, 1, 6, 7]
await mcp.off(pins, PORT, 'PLC')
shouldbe = [8]
result = await mcp.readPort(PORT, { format: 'PLC' })
expect(result.sort(), 'pin write/read failed off').to.deep.equal(shouldbe.sort())
await pause(MS)
await mcp.allOff(PORT) // clear port after each test
})
}