adafruit/Adafruit_VL6180X

Timeout hangs device during readRange

Opened this issue · 0 comments

I've had issues with this library where the I2C failed to communicate and the programme hung. I have forked the library and this is a version of readRange that handles timeouts gracefully. I would imagine you'd want a better approach than just returning zero but it does work.

uint8_t Adafruit_VL6180X::readRange(void) {
unsigned long time = millis();

// wait for device to be ready for range measurement
while (!(read8(VL6180X_REG_RESULT_RANGE_STATUS) & 0x01) ) {
if ( millis() - time > 5 ) { return 0; }
}

// Start a range measurement
write8(VL6180X_REG_SYSRANGE_START, 0x01);

// Poll until bit 2 is set
while (!(read8(VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO) & 0x04)) {
if ( millis() - time > 5 ) { return 0; }
}

// read range in mm
uint8_t range = read8(VL6180X_REG_RESULT_RANGE_VAL);

// clear interrupt
write8(VL6180X_REG_SYSTEM_INTERRUPT_CLEAR, 0x07);

return range;
}