uci-mcp/demo/sample.js

53 lines
1.3 KiB
JavaScript

const Bus = require('@uci/i2c').Bus,
MCP = require('../src/mcp23008-17'),
// use this require when using your own code
// MCP = require('@uci/mcp')
pause = require('@uci/utils').pPause
let bus = new Bus()
let ADDR = 0x27
let CHIP = 'MCP23017'
let MS = 300 // so humans can watch the light otherwise set to zero
let PORT = 'B'
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]
await mcp.pinsOn(pins, PORT, 'PLC')
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]
await mcp.pinsOff(pins, PORT, 'PLC')
result = await mcp.readPort(PORT, { format: 'PLC' })
console.log(`${pins} off = current state: ${result.sort()}`)
await pause(MS)
let pin = 7
result = await mcp.readPin(pin, PORT) ? 'on' : 'off'
console.log(`Pin ${pin} is ${result}`)
pin = 6
result = await mcp.readPin(pin, PORT) ? 'on' : 'off'
console.log(`Pin ${pin} is ${result}`)
console.log('all pins off')
await mcp.allOff(PORT) // clear port after each test
})()