2017-05-28 00:00:07 -07:00
|
|
|
const Bus = require('@uci/i2c').Bus,
|
2017-05-28 21:44:35 -07:00
|
|
|
MCP = require('../src/mcp23008-17'),
|
2017-05-28 00:00:07 -07:00
|
|
|
// use this require when using your own code
|
|
|
|
// MCP = require('@uci/mcp')
|
|
|
|
pause = require('@uci/utils').pPause
|
|
|
|
|
|
|
|
let bus = new Bus()
|
|
|
|
|
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 light otherwise set to zero
|
2017-05-28 21:44:35 -07:00
|
|
|
let PORT = 'B'
|
2017-05-28 00:00:07 -07:00
|
|
|
|
|
|
|
let mcp = new MCP[CHIP](bus, ADDR);
|
|
|
|
|
|
|
|
(async function () {
|
|
|
|
|
|
|
|
await mcp.init()
|
|
|
|
await mcp.allOff(PORT) // start clean
|
|
|
|
console.log('all pins off')
|
|
|
|
await pause(MS)
|
|
|
|
|
|
|
|
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 result = await mcp.readPort(PORT, { format: 'PLC' })
|
|
|
|
console.log(`${pins} on = current state: ${result.sort()}`)
|
|
|
|
await pause(MS)
|
|
|
|
|
|
|
|
pins = [2, 1, 6]
|
|
|
|
await mcp.toggle(pins, PORT, 'PLC')
|
|
|
|
result = await mcp.readPort(PORT, { format: 'PLC' })
|
|
|
|
console.log(`${pins} toggle = current state: ${result.sort()}`)
|
|
|
|
await pause(MS)
|
|
|
|
|
|
|
|
pins = [2, 1, 7]
|
2017-05-30 13:22:45 -07:00
|
|
|
await mcp.pinsOff(pins, PORT, 'PLC')
|
2017-05-28 00:00:07 -07:00
|
|
|
result = await mcp.readPort(PORT, { format: 'PLC' })
|
|
|
|
console.log(`${pins} off = current state: ${result.sort()}`)
|
|
|
|
await pause(MS)
|
|
|
|
|
|
|
|
let pin = 7
|
2017-05-28 21:44:35 -07:00
|
|
|
result = await mcp.readPin(pin, PORT) ? 'on' : 'off'
|
2017-05-28 00:00:07 -07:00
|
|
|
console.log(`Pin ${pin} is ${result}`)
|
|
|
|
|
|
|
|
pin = 6
|
2017-05-28 21:44:35 -07:00
|
|
|
result = await mcp.readPin(pin, PORT) ? 'on' : 'off'
|
2017-05-28 00:00:07 -07:00
|
|
|
console.log(`Pin ${pin} is ${result}`)
|
|
|
|
|
|
|
|
console.log('all pins off')
|
|
|
|
await mcp.allOff(PORT) // clear port after each test
|
|
|
|
|
|
|
|
})()
|