kaihenzler / node-mcp23017

Node.js library for the I2C I/O Expander MCP23017 on a Raspberry Pi
MIT License
19 stars 28 forks source link

Alert events #12

Open voidbrain opened 2 years ago

voidbrain commented 2 years ago

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

soundstorm commented 2 years ago

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).