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

Added non-blocking readRangeNoBlocking function to read the range sen… #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions VL53L0X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ VL53L0X::VL53L0X(void)
: address(ADDRESS_DEFAULT)
, io_timeout(0) // no timeout
, did_timeout(false)
, status_no_bloking(ST_START)
{
}

Expand Down Expand Up @@ -834,6 +835,83 @@ uint16_t VL53L0X::readRangeContinuousMillimeters(void)
return range;
}

// Reads range in non-blocking, returns true when done. If the range
// is 65535, it indicates timeout.
// (readRangeNoBlocking() also calls this function after starting a
// single-shot range measurement)
//
bool VL53L0X::readRangeNoBlocking(uint16_t& range)
{
bool read_end = false;

range = 0;

switch( status_no_bloking )
{
case ST_START:
startTimeout();
writeReg(0x80, 0x01);
writeReg(0xFF, 0x01);
writeReg(0x00, 0x00);
writeReg(0x91, stop_variable);
writeReg(0x00, 0x01);
writeReg(0xFF, 0x00);
writeReg(0x80, 0x00);

writeReg(SYSRANGE_START, 0x01);

status_no_bloking = ST_WAIT_START;
break;

case ST_WAIT_START:
// "Wait until start bit has been cleared"
if (readReg(SYSRANGE_START) & 0x01)
{
if (checkTimeoutExpired())
{
did_timeout = true;
range = 65535;
read_end = true;
status_no_bloking = ST_START;
}
}
else
{
startTimeout();
status_no_bloking = ST_WAIT_RANGE;
}

case ST_WAIT_RANGE:
if ((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0)
{
if (checkTimeoutExpired())
{
did_timeout = true;
range = 65535;
read_end = true;
status_no_bloking = ST_START;
}
}
else
{
// assumptions: Linearity Corrective Gain is 1000 (default);
// fractional ranging is not enabled
range = readReg16Bit(RESULT_RANGE_STATUS + 10);

writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);

status_no_bloking = ST_START;
read_end = true;
}
break;

default:
status_no_bloking = ST_START;
}

return read_end;
}

// Performs a single-shot range measurement and returns the reading in
// millimeters
// based on VL53L0X_PerformSingleRangingMeasurement()
Expand Down
8 changes: 7 additions & 1 deletion VL53L0X.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <Arduino.h>

#define ST_START 0 // Initialize non-blocking reading.
#define ST_WAIT_START 1 // Wait for the sensor to start reading.
#define ST_WAIT_RANGE 2 // Wait for the sensor to send the range read.

class VL53L0X
{
public:
Expand Down Expand Up @@ -126,7 +130,8 @@ class VL53L0X
void stopContinuous(void);
uint16_t readRangeContinuousMillimeters(void);
uint16_t readRangeSingleMillimeters(void);

bool readRangeNoBlocking(uint16_t& range);

inline void setTimeout(uint16_t timeout) { io_timeout = timeout; }
inline uint16_t getTimeout(void) { return io_timeout; }
bool timeoutOccurred(void);
Expand All @@ -152,6 +157,7 @@ class VL53L0X
uint8_t address;
uint16_t io_timeout;
bool did_timeout;
uint8_t status_no_bloking;
uint16_t timeout_start_ms;

uint8_t stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API
Expand Down