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