uci-mcp/test/mcp.test.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

//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
2017-01-14 00:04:34 -08:00
let bus = new Bus()
2017-01-12 20:05:20 -08:00
let ADDR = 0x27
let CHIP = 'MCP23017'
let MS = 300 // so humans can watch the lights on boards with leds otherwise set to zero
let PORT = 'B'
2017-01-12 20:05:20 -08:00
let id = `${CHIP} at I2C bus address of 0x${ADDR.toString(16)}`
2017-01-12 20:05:20 -08:00
let mcp = new MCP[CHIP](bus, ADDR, {
pin_cfg_default: 'output',
id: id
});
2017-01-12 20:05:20 -08:00
describe(
`Testing Chip ${id}, port: ${PORT}`,
function () {
hooks()
eachpin()
somepins()
})
2017-01-12 20:05:20 -08:00
//****************** TESTS **********************
function eachpin() {
it('==> test each pin', async function () {
2017-01-12 20:05:20 -08:00
for (let pin of mcp.ports[PORT].pins) {
await mcp.pinsOn(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.pinsOff(pin.address, PORT)
await pause(MS)
}
})
}
2017-01-12 20:05:20 -08:00
function somepins() {
it('==> on, off toggle some pins', async function () {
let pins = [1, 7, 8]
await mcp.pinsOn(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.pinsOn(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.pinsOff(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)
})
}
function hooks() {
before(async() => {
await mcp.init()
})
beforeEach(async() => {
await mcp.allOff(PORT) // start clean
})
after(async() => {
await mcp.allOff(PORT) // start clean
})
}