uci-mcp/demo/interrupt.js

54 lines
1.4 KiB
JavaScript

// to try this demo
// npm install @uci/interrupt
// Connect 5V MCP interrupt pin via 3.3 V logic level!!!!! converter to a gpio pin
// Set your SETTINGS below
// npm run inter
const Bus = require('@uci/i2c').Bus,
MCP = require('../src/mcp23008-17'),
// use this require when using your own code
// MCP = require('@uci/mcp')
Interrupt = require('@uci/interrupt')
let bus = new Bus()
// SETTINGS
let ADDR = 0x20
let CHIP = 'MCP23017'
let MS = 300 // so humans can watch the light otherwise set to zero
let pinA = 12
let pinB = 16
let id = `${CHIP} at I2C bus address of 0x${ADDR.toString(16)}`
let mcp = new MCP[CHIP](bus, ADDR, {
pin_cfg_default: 'toggle_switch',
id: id,
interruptA: new Interrupt(pinA, ihandler),
interruptB: new Interrupt(pinB, ihandler),
stateA: 128,
pidsA: ['sw1', 'sw2', 'sw3', 'sw4', 'sw5', 'sw6', 'sw7', 'sw8'],
pidsB: ['sw9', 'sw10', 'sw11', 'sw12', 'sw13', 'sw14', 'sw15', 'sw16']
}); // must have ; for async immediate function following
(async function () {
await mcp.init()
await mcp.start()
mcp.on('fired', (data) => {
console.log(`bank event 'fired' data => ${data.bank} on port ${data.port} pin# ${data.pin} pid ${data.pid}`)
})
})()
//=================================//
async function ihandler(port) {
let pin = await this.interruptPin(port, 'PLC')
await this.interruptReset(port)
let data = { bank: this.id, port: port, pin: pin, pid: this.pid(pin, port) }
// console.log('data in handler', data)
return data
}