node-mcp23017 icon indicating copy to clipboard operation
node-mcp23017 copied to clipboard

Alert events

Open voidbrain opened this issue 2 years ago • 1 comments

Hello people. i've a number of sensors (like HC-SR04), i'm trying put them under a mcp23017 so i can use them in parallel. The original code used pigpio https://github.com/fivdi/pigpio/blob/master/doc/gpio.md#event-alert

Especially i need to use "Gpio.INPUT, alert: true" and the "on alert" event ( .on('alert', (level, tick) => {}) to send a pulse via a channel and see the time to calculate distance. I know node-mcp23017 uses i2c-bus. So, is there a way to substitute the alert event with the bus.receiveByte(addr, cb) function? Thanks

voidbrain avatar Mar 19 '22 21:03 voidbrain

When #13 is merged, you might use interrupts, but that would not be the best way. As the MCP uses I2C there is some delay when setting pins. The interrupt however will only trigger for one port at once, so until you read the register you won't know which sensor triggered. If you trigger one at a time you may get around with it. So eg: Enable interrupt on A0, trigger ultrasonic sensor 1, wait for interrupt, save the elapsed time & calculate distance, disable interrupt, continue with enabling A1 interrupt, trigger sensor 2, ... Parallel triggering however would be hard to implement perfectly. If multiple sensors trigger in a shorter time than reading the registers, you won't know which triggered when exactly. The enabling and disabling of the interrupts as described above might be necessary to avoid false triggers (eg you triggered sensor 1 and sensor 2 receives a pulse, but you only want to watch sensor 1).

soundstorm avatar Apr 03 '22 15:04 soundstorm