Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout hangs device during readRange #30

Open
projectmaestro opened this issue Dec 16, 2023 · 0 comments
Open

Timeout hangs device during readRange #30

projectmaestro opened this issue Dec 16, 2023 · 0 comments

Comments

@projectmaestro
Copy link

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant