From 4dda40913140fffc4fca422c3c2e97d3b69154dc Mon Sep 17 00:00:00 2001 From: badVibes-- Date: Thu, 9 Apr 2020 19:15:42 +0200 Subject: [PATCH 1/2] Non-blocking read -Added methods to read the sensor in a non blocking manner. -Fixed interrupt pin settings. -Added option to use GPOI0 for determining when the sensor has new data. -Added option to define a different Wire port for communication. --- .DS_Store | Bin 0 -> 6148 bytes README.md | 18 ++- VL53L0X.cpp | 130 +++++++++++------- VL53L0X.h | 7 +- examples/.DS_Store | Bin 0 -> 6148 bytes .../Continuous_Non_Blocking.ino | 44 ++++++ keywords.txt | 2 + 7 files changed, 149 insertions(+), 52 deletions(-) create mode 100644 .DS_Store create mode 100644 examples/.DS_Store create mode 100644 examples/Continuous_Non_Blocking/Continuous_Non_Blocking.ino diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..65267a919194138c52ad60b7390d4d1bbf7c55b0 GIT binary patch literal 6148 zcmeHKJ8Hu~5S>X>2-3JrxmWNF7U7)07qAnhlTknfQmV?id^ErL5Mm@lnl$DO%)H&% zc`Nh^jYdSY`|P(O8xd*YhVpG;X?AYDu!qbj5RN-OWRQ26`sr<0RIewD+k*2YM>+lG zzmDTy?^wT?St>vUr~nn90#sm40qec6_9>8&3Qz$m@S}iz9}3*CCeDHW=|J!i0N5by zhPBTUz+wqtO`HRffoV{ILDd{FH0a2etgDH0V9-T#_|SZ^=7ggDblhLOT(kx;<&)@uv=0JoYm+ze}{Ab2|ldOOC( f+VQg&MP0Eq&THZv=yc?r4&={(=|ZCd|E<6UiPaUM literal 0 HcmV?d00001 diff --git a/README.md b/README.md index 8c456ad..d0bff81 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,11 @@ This library is intended to provide a quicker and easier way to get started usin * `uint8_t getAddress(void)`
Returns the current I²C address. -* `bool init(bool io_2v8 = true)`
- Iniitializes and configures the sensor. If the optional argument `io_2v8` is true (the default if not specified), the sensor is configured for 2V8 mode (2.8 V I/O); if false, the sensor is left in 1V8 mode. The return value is a boolean indicating whether the initialization completed successfully. +* `bool init(int16_t GPIO0_pin, TwoWire &theWire, bool io_2v8)`
+ Iniitializes and configures the sensor. + If the optional argument `GPIO0_pin` is set, the sensor will use that pin to check if the measurment is finished. This pin should be connected to the sensors GPOI0 pin. This can save a bit of time compared to reading the register. Default is -1. + The optional argument `theWire` lets you specify a diferent I2C port for communication with the sensor. Default is Wire. + If the optional argument `io_2v8` is true (the default if not specified), the sensor is configured for 2V8 mode (2.8 V I/O); if false, the sensor is left in 1V8 mode. The return value is a boolean indicating whether the initialization completed successfully. * `void writeReg(uint8_t reg, uint8_t value)`
Writes an 8-bit sensor register with the given value. @@ -143,10 +146,17 @@ This library is intended to provide a quicker and easier way to get started usin Stops continuous mode. * `uint16_t readRangeContinuousMillimeters(void)`
- Returns a range reading in millimeters when continuous mode is active. + Returns a range reading in millimeters when continuous mode is active. Blocks execution until the measurment is received. * `uint16_t readRangeSingleMillimeters(void)`
- Performs a single-shot ranging measurement and returns the reading in millimeters. + Performs a single-shot ranging measurement and returns the reading in millimeters. Blocks execution until the measurment is received. + +* `uint16_t available(void)`
+ Returns true if the measurment has finished and data is ready to be read. Used when reading the sensor in a non-blocking manner. + +* `uint16_t readRangeMillimeters(void)`
+ Returns a range reading in millimeters when continuous mode is active. Should be used in combination with `available(void)` to read the sensor in a non-blocking manner. + * `void setTimeout(uint16_t timeout)`
Sets a timeout period in milliseconds after which read operations will abort if the sensor is not ready. A value of 0 disables the timeout. diff --git a/VL53L0X.cpp b/VL53L0X.cpp index 25b494f..42b2ca8 100644 --- a/VL53L0X.cpp +++ b/VL53L0X.cpp @@ -57,8 +57,14 @@ void VL53L0X::setAddress(uint8_t new_addr) // enough unless a cover glass is added. // If io_2v8 (optional) is true or not given, the sensor is configured for 2V8 // mode. -bool VL53L0X::init(bool io_2v8) +bool VL53L0X::init(int16_t GPIO0_pin, TwoWire &theWire, bool io_2v8) { + gpio_pin=GPIO0_pin; + if(gpio_pin >- 1) + { + pinMode(gpio_pin,INPUT); + } + wire = &theWire; // check model ID register (value specified in datasheet) if (readReg(IDENTIFICATION_MODEL_ID) != 0xEE) { return false; } @@ -236,7 +242,8 @@ bool VL53L0X::init(bool io_2v8) // -- VL53L0X_SetGpioConfig() begin writeReg(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x04); - writeReg(GPIO_HV_MUX_ACTIVE_HIGH, readReg(GPIO_HV_MUX_ACTIVE_HIGH) & ~0x10); // active low + Serial.println(readReg(GPIO_HV_MUX_ACTIVE_HIGH),HEX); + writeReg(GPIO_HV_MUX_ACTIVE_HIGH, 0x01); // active low writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01); // -- VL53L0X_SetGpioConfig() end @@ -284,32 +291,32 @@ bool VL53L0X::init(bool io_2v8) // Write an 8-bit register void VL53L0X::writeReg(uint8_t reg, uint8_t value) { - Wire.beginTransmission(address); - Wire.write(reg); - Wire.write(value); - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + wire->write(value); + last_status = wire->endTransmission(); } // Write a 16-bit register void VL53L0X::writeReg16Bit(uint8_t reg, uint16_t value) { - Wire.beginTransmission(address); - Wire.write(reg); - Wire.write((value >> 8) & 0xFF); // value high byte - Wire.write( value & 0xFF); // value low byte - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + wire->write((value >> 8) & 0xFF); // value high byte + wire->write( value & 0xFF); // value low byte + last_status = wire->endTransmission(); } // Write a 32-bit register void VL53L0X::writeReg32Bit(uint8_t reg, uint32_t value) { - Wire.beginTransmission(address); - Wire.write(reg); - Wire.write((value >> 24) & 0xFF); // value highest byte - Wire.write((value >> 16) & 0xFF); - Wire.write((value >> 8) & 0xFF); - Wire.write( value & 0xFF); // value lowest byte - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + wire->write((value >> 24) & 0xFF); // value highest byte + wire->write((value >> 16) & 0xFF); + wire->write((value >> 8) & 0xFF); + wire->write( value & 0xFF); // value lowest byte + last_status = wire->endTransmission(); } // Read an 8-bit register @@ -317,12 +324,12 @@ uint8_t VL53L0X::readReg(uint8_t reg) { uint8_t value; - Wire.beginTransmission(address); - Wire.write(reg); - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + last_status = wire->endTransmission(); - Wire.requestFrom(address, (uint8_t)1); - value = Wire.read(); + wire->requestFrom(address, (uint8_t)1); + value = wire->read(); return value; } @@ -332,13 +339,13 @@ uint16_t VL53L0X::readReg16Bit(uint8_t reg) { uint16_t value; - Wire.beginTransmission(address); - Wire.write(reg); - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + last_status = wire->endTransmission(); - Wire.requestFrom(address, (uint8_t)2); - value = (uint16_t)Wire.read() << 8; // value high byte - value |= Wire.read(); // value low byte + wire->requestFrom(address, (uint8_t)2); + value = (uint16_t)wire->read() << 8; // value high byte + value |= wire->read(); // value low byte return value; } @@ -348,15 +355,15 @@ uint32_t VL53L0X::readReg32Bit(uint8_t reg) { uint32_t value; - Wire.beginTransmission(address); - Wire.write(reg); - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + last_status = wire->endTransmission(); - Wire.requestFrom(address, (uint8_t)4); - value = (uint32_t)Wire.read() << 24; // value highest byte - value |= (uint32_t)Wire.read() << 16; - value |= (uint16_t)Wire.read() << 8; - value |= Wire.read(); // value lowest byte + wire->requestFrom(address, (uint8_t)4); + value = (uint32_t)wire->read() << 24; // value highest byte + value |= (uint32_t)wire->read() << 16; + value |= (uint16_t)wire->read() << 8; + value |= wire->read(); // value lowest byte return value; } @@ -365,30 +372,30 @@ uint32_t VL53L0X::readReg32Bit(uint8_t reg) // starting at the given register void VL53L0X::writeMulti(uint8_t reg, uint8_t const * src, uint8_t count) { - Wire.beginTransmission(address); - Wire.write(reg); + wire->beginTransmission(address); + wire->write(reg); while (count-- > 0) { - Wire.write(*(src++)); + wire->write(*(src++)); } - last_status = Wire.endTransmission(); + last_status = wire->endTransmission(); } // Read an arbitrary number of bytes from the sensor, starting at the given // register, into the given array void VL53L0X::readMulti(uint8_t reg, uint8_t * dst, uint8_t count) { - Wire.beginTransmission(address); - Wire.write(reg); - last_status = Wire.endTransmission(); + wire->beginTransmission(address); + wire->write(reg); + last_status = wire->endTransmission(); - Wire.requestFrom(address, count); + wire->requestFrom(address, count); while (count-- > 0) { - *(dst++) = Wire.read(); + *(dst++) = wire->read(); } } @@ -755,6 +762,10 @@ uint8_t VL53L0X::getVcselPulsePeriod(vcselPeriodType type) else { return 255; } } + + + + // Start continuous ranging measurements. If period_ms (optional) is 0 or not // given, continuous back-to-back mode is used (the sensor takes measurements as // often as possible); otherwise, continuous timed mode is used, with the given @@ -816,7 +827,7 @@ void VL53L0X::stopContinuous(void) uint16_t VL53L0X::readRangeContinuousMillimeters(void) { startTimeout(); - while ((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0) + while ((gpio_pin > -1) ? digitalRead(gpio_pin) == HIGH : (readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0) { if (checkTimeoutExpired()) { @@ -863,6 +874,31 @@ uint16_t VL53L0X::readRangeSingleMillimeters(void) return readRangeContinuousMillimeters(); } +//check to see if the sensor finished measuring +//used when reading the sensor continuosly in a non-blocking manner +bool VL53L0X::available(void) +{ + if(gpio_pin > -1) + { + if(digitalRead(gpio_pin) == LOW) + return true; + else + return false; + } + if((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0) + return false; + else return true; +} + +//read the sensor mesurment in millimeters after checking if the measurment is finished +//used when reading the sensor continuosly in a non-blocking manner +uint16_t VL53L0X::readRangeMillimeters(void) +{ + uint16_t range = readReg16Bit(RESULT_RANGE_STATUS + 10); + writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01); + return range; +} + // Did a timeout occur in one of the read functions since the last call to // timeoutOccurred()? bool VL53L0X::timeoutOccurred() diff --git a/VL53L0X.h b/VL53L0X.h index bf32488..6f9ac8a 100644 --- a/VL53L0X.h +++ b/VL53L0X.h @@ -2,6 +2,7 @@ #define VL53L0X_h #include +#include class VL53L0X { @@ -101,7 +102,7 @@ class VL53L0X void setAddress(uint8_t new_addr); inline uint8_t getAddress(void) { return address; } - bool init(bool io_2v8 = true); + bool init(int16_t GPIO0_pin = -1, TwoWire &theWire = Wire, bool io_2v8 = true); void writeReg(uint8_t reg, uint8_t value); void writeReg16Bit(uint8_t reg, uint16_t value); @@ -126,6 +127,8 @@ class VL53L0X void stopContinuous(void); uint16_t readRangeContinuousMillimeters(void); uint16_t readRangeSingleMillimeters(void); + bool available(void); + uint16_t readRangeMillimeters(void); inline void setTimeout(uint16_t timeout) { io_timeout = timeout; } inline uint16_t getTimeout(void) { return io_timeout; } @@ -153,6 +156,8 @@ class VL53L0X uint16_t io_timeout; bool did_timeout; uint16_t timeout_start_ms; + uint16_t gpio_pin; + TwoWire *wire; uint8_t stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API uint32_t measurement_timing_budget_us; diff --git a/examples/.DS_Store b/examples/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 +#include + +VL53L0X sensor; + +void setup() +{ + Serial.begin(9600); + Wire.begin(); + + sensor.setTimeout(500); + + //to check for measurments in a more efficient manner you can + //connect the sensors GPIO0 pin to Arduino, and initialise + //it with "sensor.init(myPinNumber)" + if (!sensor.init()) + { + Serial.println("Failed to detect and initialize sensor!"); + while (1) {} + } + + // Start continuous back-to-back mode (take readings as + // fast as possible). To use continuous timed mode + // instead, provide a desired inter-measurement period in + // ms (e.g. sensor.startContinuous(100)). + sensor.startContinuous(); +} + +void loop() +{ + if(sensor.available()) + { + Serial.print(sensor.readRangeMillimeters()); + if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); } + } + + Serial.println(); +} diff --git a/keywords.txt b/keywords.txt index 437add9..e8ef87d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -21,6 +21,8 @@ startContinuous KEYWORD2 stopContinuous KEYWORD2 readRangeContinuousMillimeters KEYWORD2 readRangeSingleMillimeters KEYWORD2 +available KEYWORD2 +readRangeMillimeters KEYWORD2 setTimeout KEYWORD2 getTimeout KEYWORD2 timeoutOccurred KEYWORD2 From 0497bda58cdc06fca7323c867a5b543737421e4e Mon Sep 17 00:00:00 2001 From: badVibes-- Date: Thu, 9 Apr 2020 20:07:00 +0200 Subject: [PATCH 2/2] Update VL53L0X.cpp Dough --- VL53L0X.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/VL53L0X.cpp b/VL53L0X.cpp index 42b2ca8..3074373 100644 --- a/VL53L0X.cpp +++ b/VL53L0X.cpp @@ -242,7 +242,6 @@ bool VL53L0X::init(int16_t GPIO0_pin, TwoWire &theWire, bool io_2v8) // -- VL53L0X_SetGpioConfig() begin writeReg(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x04); - Serial.println(readReg(GPIO_HV_MUX_ACTIVE_HIGH),HEX); writeReg(GPIO_HV_MUX_ACTIVE_HIGH, 0x01); // active low writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);