Skip to content

Timeout hangs device during readRange #30

@projectmaestro

Description

@projectmaestro

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions