adafruit / Adafruit_VL53L0X

Arduino library for Adafruit VL53L0X
149 stars 118 forks source link

Added Calls to Support Interrupts and Added Interrupt Example #49

Closed Vgdn1942 closed 2 years ago

Vgdn1942 commented 3 years ago

Updated and clang-format this pull request https://github.com/adafruit/Adafruit_VL53L0X/pull/8

frollard commented 2 years ago

Would love to see this get pulled. Looks good so far. Examples are a bit of a pain because (unless I'm doing it wrong) the library wants to fight with the one from the library manager. Excellent work.

ladyada commented 2 years ago

@frollard did you try this PR out, does the code work ok? :)

caternuson commented 2 years ago

Referring to the VL53L0X state machine flow diagram from the datasheet, and focusing on the steps for making a measurement (rectangle): image

This library currently combines the mode setting (Range Mode) and range starting (Range Meas) into single methods:

The interrupt feature is just a signaling mechanism for the measurement completion (or limits exceeded). There shouldn't be any need for new measurement start/stop methods. I think all that's needed is a new method that exposes VL53L0X_SetGpioConfig() to allow enabling and configuring the interrupt behavior. The rest would be up to user code - to read the interrupt pin, attach it to an ISR, etc. Or am I missing something?

frollard commented 2 years ago

There shouldn't be any need for new measurement start/stop methods.

The only start/stop I can think would be low power/ultra low power instances where the sensor is held in standby most of the time, pinging, then start/stop continuous mode when something is detected to accurately find the 'end' state. I have a hunch the most common diy use case is 'turn on continuous', and ability to switch between the resolution/speed modes. The only example right now is the 'extended' multi which is super overwhelming for someone trying to get a single sensor into continuous mode. @ladyada I got it to compile but am currently switching from arduino ide to pio...and it's truly a nightmare. Don't rely on me for in depth testing in the immediate term.

frollard commented 2 years ago

Did a bunch of testing, wrote a long reply, and it's vanished.

Setting to continuous mode with gpio in 'new measurement ready interrupt' I only get 1 reading back; the interrupt triggers and sets an isr flag, the main loop detects the flag, queries the reading then resets the interrupt mask (which should enable the next trigger)...but it just stops interrupting. If I have the main loop ignore the reading-ready isr flag and just poll the chip on every loop, it comes back with a new reading every 50ms or so (which seems very slow for setting the chip to 'high speed' mode. Will investigate further and come back with something more concrete.

caternuson commented 2 years ago

Going to go ahead and merge this as is. Tested and seems to work. Using new example, which only triggers interrupts when reading are below lower limit threshold of 50mm: Screenshot from 2022-03-15 13-26-24

Tested the other options as well:

  // Second Parameter options are VL53L0X_GPIOFUNCTIONALITY_OFF,
  // VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW,
  // VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH,
  // VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT,
  // VL53L0X_GPIOFUNCTIONALITY_NEW_MEASURE_READY

which all seemed to nominally work.

Thanks for the PR. Sorry it took so long to take a real look at and test.