From 8b08e9f6b8912736a625561ef985c1e127cb9c29 Mon Sep 17 00:00:00 2001 From: rneurink Date: Fri, 3 Apr 2020 10:38:53 +0200 Subject: [PATCH 01/31] Fix casting of registeraddress (uint8_t) RegisterAddr>>8 is not correct. This casts the Registeraddress to a uint8_t and shifts it to the right by 8 bits. The cast can be removed as it is unnecessary or put parentheses around the shift like this: (uint8_t) (RegisterAddr>>8) --- src/vl53l1x_class.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index cf7bb7b..cd6cbde 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -1024,8 +1024,8 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr Serial.println(RegisterAddr); #endif uint8_t buffer[2]; - buffer[0]=(uint8_t) RegisterAddr>>8; - buffer[1]=(uint8_t) RegisterAddr&0xFF; + buffer[0]=RegisterAddr>>8; + buffer[1]=RegisterAddr&0xFF; dev_i2c->write(buffer, 2); for (int i = 0 ; i < NumByteToWrite ; i++) dev_i2c->write(pBuffer[i]); @@ -1049,8 +1049,8 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, Serial.println(RegisterAddr); #endif uint8_t buffer[2]; - buffer[0]=(uint8_t) RegisterAddr>>8; - buffer[1]=(uint8_t) RegisterAddr&0xFF; + buffer[0]=RegisterAddr>>8; + buffer[1]=RegisterAddr&0xFF; dev_i2c->write(buffer, 2); status = dev_i2c->endTransmission(false); //Fix for some STM32 boards From 4cb6c8b14d80727c0cbe18c66d62e021cac5f89b Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 17 May 2020 13:50:05 -0500 Subject: [PATCH 02/31] Add ID check to begin(). Fix typo in device ID. * Add checkID to begin to make sure the device is what we expect it is before an init. * This required PR #36 fix (thanks @rneurink!) * It looks like there was a typo in the device ID. All the units I have are 0xEACC, whereas the original code was 0xEEAC. Any idea where 0xEEAC came from? I think we're safe, I just worry a bit that there's a doc with a typo out there (not surprising with this sensor). --- src/SparkFun_VL53L1X.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 2e07a18..39c7fa2 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -42,6 +42,9 @@ bool SFEVL53L1X::init() bool SFEVL53L1X::begin() { + if (checkID() == false) + return (VL53L1_ERROR_PLATFORM_SPECIFIC_START); + return _device->VL53L1X_SensorInit(); } @@ -51,32 +54,31 @@ bool SFEVL53L1X::checkID() { uint16_t sensorId; _device->VL53L1X_GetSensorId(&sensorId); - if (sensorId == 0xEEAC) + if (sensorId == 0xEACC) return true; return false; } - /*Turns the sensor on if the Shutdown pin is connected*/ void SFEVL53L1X::sensorOn() { - if(_shutdownPin >= 0) - { - digitalWrite(_shutdownPin, HIGH); - } - delay(10); + if (_shutdownPin >= 0) + { + digitalWrite(_shutdownPin, HIGH); + } + delay(10); } /*Turns the sensor off if the Shutdown pin is connected*/ void SFEVL53L1X::sensorOff() { - if(_shutdownPin >= 0) - { - digitalWrite(_shutdownPin, LOW); - } - delay(10); + if (_shutdownPin >= 0) + { + digitalWrite(_shutdownPin, LOW); + } + delay(10); } /*Gets the software version number of the current library installed.*/ @@ -118,7 +120,7 @@ void SFEVL53L1X::setInterruptPolarityLow() * This function gets the interrupt polarity\n * 1=active high (default), 0=active low */ - + uint8_t SFEVL53L1X::getInterruptPolarity() { uint8_t tmp; @@ -145,7 +147,7 @@ bool SFEVL53L1X::checkForDataReady() void SFEVL53L1X::setTimingBudgetInMs(uint16_t timingBudget) { - _device->VL53L1X_SetTimingBudgetInMs(timingBudget); + _device->VL53L1X_SetTimingBudgetInMs(timingBudget); } uint16_t SFEVL53L1X::getTimingBudgetInMs() From 79fd28009108a43e047441d25208f289c9252534 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 17 May 2020 13:56:27 -0500 Subject: [PATCH 03/31] VScode white space changes --- src/vl53l1x_class.cpp | 831 ++++++++++++++++++++---------------------- 1 file changed, 397 insertions(+), 434 deletions(-) diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index cd6cbde..499cf4a 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -40,113 +40,107 @@ #include "Arduino.h" #include "vl53l1x_class.h" - -#define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E -#define MM_CONFIG__INNER_OFFSET_MM 0x0020 -#define MM_CONFIG__OUTER_OFFSET_MM 0x0022 - +#define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E +#define MM_CONFIG__INNER_OFFSET_MM 0x0020 +#define MM_CONFIG__OUTER_OFFSET_MM 0x0022 //#define DEBUG_MODE - const uint8_t VL51L1X_DEFAULT_CONFIGURATION[] = { -0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */ -0x01, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */ -0x01, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */ -0x01, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() */ -0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady() */ -0x00, /* 0x32 : not user-modifiable */ -0x02, /* 0x33 : not user-modifiable */ -0x08, /* 0x34 : not user-modifiable */ -0x00, /* 0x35 : not user-modifiable */ -0x08, /* 0x36 : not user-modifiable */ -0x10, /* 0x37 : not user-modifiable */ -0x01, /* 0x38 : not user-modifiable */ -0x01, /* 0x39 : not user-modifiable */ -0x00, /* 0x3a : not user-modifiable */ -0x00, /* 0x3b : not user-modifiable */ -0x00, /* 0x3c : not user-modifiable */ -0x00, /* 0x3d : not user-modifiable */ -0xff, /* 0x3e : not user-modifiable */ -0x00, /* 0x3f : not user-modifiable */ -0x0F, /* 0x40 : not user-modifiable */ -0x00, /* 0x41 : not user-modifiable */ -0x00, /* 0x42 : not user-modifiable */ -0x00, /* 0x43 : not user-modifiable */ -0x00, /* 0x44 : not user-modifiable */ -0x00, /* 0x45 : not user-modifiable */ -0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ -0x0b, /* 0x47 : not user-modifiable */ -0x00, /* 0x48 : not user-modifiable */ -0x00, /* 0x49 : not user-modifiable */ -0x02, /* 0x4a : not user-modifiable */ -0x0a, /* 0x4b : not user-modifiable */ -0x21, /* 0x4c : not user-modifiable */ -0x00, /* 0x4d : not user-modifiable */ -0x00, /* 0x4e : not user-modifiable */ -0x05, /* 0x4f : not user-modifiable */ -0x00, /* 0x50 : not user-modifiable */ -0x00, /* 0x51 : not user-modifiable */ -0x00, /* 0x52 : not user-modifiable */ -0x00, /* 0x53 : not user-modifiable */ -0xc8, /* 0x54 : not user-modifiable */ -0x00, /* 0x55 : not user-modifiable */ -0x00, /* 0x56 : not user-modifiable */ -0x38, /* 0x57 : not user-modifiable */ -0xff, /* 0x58 : not user-modifiable */ -0x01, /* 0x59 : not user-modifiable */ -0x00, /* 0x5a : not user-modifiable */ -0x08, /* 0x5b : not user-modifiable */ -0x00, /* 0x5c : not user-modifiable */ -0x00, /* 0x5d : not user-modifiable */ -0x01, /* 0x5e : not user-modifiable */ -0xdb, /* 0x5f : not user-modifiable */ -0x0f, /* 0x60 : not user-modifiable */ -0x01, /* 0x61 : not user-modifiable */ -0xf1, /* 0x62 : not user-modifiable */ -0x0d, /* 0x63 : not user-modifiable */ -0x01, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm */ -0x68, /* 0x65 : Sigma threshold LSB */ -0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold() */ -0x80, /* 0x67 : Min count Rate LSB */ -0x08, /* 0x68 : not user-modifiable */ -0xb8, /* 0x69 : not user-modifiable */ -0x00, /* 0x6a : not user-modifiable */ -0x00, /* 0x6b : not user-modifiable */ -0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs() */ -0x00, /* 0x6d : Intermeasurement period */ -0x0f, /* 0x6e : Intermeasurement period */ -0x89, /* 0x6f : Intermeasurement period LSB */ -0x00, /* 0x70 : not user-modifiable */ -0x00, /* 0x71 : not user-modifiable */ -0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold() */ -0x00, /* 0x73 : distance threshold high LSB */ -0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold() */ -0x00, /* 0x75 : distance threshold low LSB */ -0x00, /* 0x76 : not user-modifiable */ -0x01, /* 0x77 : not user-modifiable */ -0x0f, /* 0x78 : not user-modifiable */ -0x0d, /* 0x79 : not user-modifiable */ -0x0e, /* 0x7a : not user-modifiable */ -0x0e, /* 0x7b : not user-modifiable */ -0x00, /* 0x7c : not user-modifiable */ -0x00, /* 0x7d : not user-modifiable */ -0x02, /* 0x7e : not user-modifiable */ -0xc7, /* 0x7f : ROI center, use SetROI() */ -0xff, /* 0x80 : XY ROI (X=Width, Y=Height), use SetROI() */ -0x9B, /* 0x81 : not user-modifiable */ -0x00, /* 0x82 : not user-modifiable */ -0x00, /* 0x83 : not user-modifiable */ -0x00, /* 0x84 : not user-modifiable */ -0x01, /* 0x85 : not user-modifiable */ -0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ -0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */ + 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch */ + 0x01, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */ + 0x01, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */ + 0x01, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity() */ + 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady() */ + 0x00, /* 0x32 : not user-modifiable */ + 0x02, /* 0x33 : not user-modifiable */ + 0x08, /* 0x34 : not user-modifiable */ + 0x00, /* 0x35 : not user-modifiable */ + 0x08, /* 0x36 : not user-modifiable */ + 0x10, /* 0x37 : not user-modifiable */ + 0x01, /* 0x38 : not user-modifiable */ + 0x01, /* 0x39 : not user-modifiable */ + 0x00, /* 0x3a : not user-modifiable */ + 0x00, /* 0x3b : not user-modifiable */ + 0x00, /* 0x3c : not user-modifiable */ + 0x00, /* 0x3d : not user-modifiable */ + 0xff, /* 0x3e : not user-modifiable */ + 0x00, /* 0x3f : not user-modifiable */ + 0x0F, /* 0x40 : not user-modifiable */ + 0x00, /* 0x41 : not user-modifiable */ + 0x00, /* 0x42 : not user-modifiable */ + 0x00, /* 0x43 : not user-modifiable */ + 0x00, /* 0x44 : not user-modifiable */ + 0x00, /* 0x45 : not user-modifiable */ + 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ + 0x0b, /* 0x47 : not user-modifiable */ + 0x00, /* 0x48 : not user-modifiable */ + 0x00, /* 0x49 : not user-modifiable */ + 0x02, /* 0x4a : not user-modifiable */ + 0x0a, /* 0x4b : not user-modifiable */ + 0x21, /* 0x4c : not user-modifiable */ + 0x00, /* 0x4d : not user-modifiable */ + 0x00, /* 0x4e : not user-modifiable */ + 0x05, /* 0x4f : not user-modifiable */ + 0x00, /* 0x50 : not user-modifiable */ + 0x00, /* 0x51 : not user-modifiable */ + 0x00, /* 0x52 : not user-modifiable */ + 0x00, /* 0x53 : not user-modifiable */ + 0xc8, /* 0x54 : not user-modifiable */ + 0x00, /* 0x55 : not user-modifiable */ + 0x00, /* 0x56 : not user-modifiable */ + 0x38, /* 0x57 : not user-modifiable */ + 0xff, /* 0x58 : not user-modifiable */ + 0x01, /* 0x59 : not user-modifiable */ + 0x00, /* 0x5a : not user-modifiable */ + 0x08, /* 0x5b : not user-modifiable */ + 0x00, /* 0x5c : not user-modifiable */ + 0x00, /* 0x5d : not user-modifiable */ + 0x01, /* 0x5e : not user-modifiable */ + 0xdb, /* 0x5f : not user-modifiable */ + 0x0f, /* 0x60 : not user-modifiable */ + 0x01, /* 0x61 : not user-modifiable */ + 0xf1, /* 0x62 : not user-modifiable */ + 0x0d, /* 0x63 : not user-modifiable */ + 0x01, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm */ + 0x68, /* 0x65 : Sigma threshold LSB */ + 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold() */ + 0x80, /* 0x67 : Min count Rate LSB */ + 0x08, /* 0x68 : not user-modifiable */ + 0xb8, /* 0x69 : not user-modifiable */ + 0x00, /* 0x6a : not user-modifiable */ + 0x00, /* 0x6b : not user-modifiable */ + 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs() */ + 0x00, /* 0x6d : Intermeasurement period */ + 0x0f, /* 0x6e : Intermeasurement period */ + 0x89, /* 0x6f : Intermeasurement period LSB */ + 0x00, /* 0x70 : not user-modifiable */ + 0x00, /* 0x71 : not user-modifiable */ + 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold() */ + 0x00, /* 0x73 : distance threshold high LSB */ + 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold() */ + 0x00, /* 0x75 : distance threshold low LSB */ + 0x00, /* 0x76 : not user-modifiable */ + 0x01, /* 0x77 : not user-modifiable */ + 0x0f, /* 0x78 : not user-modifiable */ + 0x0d, /* 0x79 : not user-modifiable */ + 0x0e, /* 0x7a : not user-modifiable */ + 0x0e, /* 0x7b : not user-modifiable */ + 0x00, /* 0x7c : not user-modifiable */ + 0x00, /* 0x7d : not user-modifiable */ + 0x02, /* 0x7e : not user-modifiable */ + 0xc7, /* 0x7f : ROI center, use SetROI() */ + 0xff, /* 0x80 : XY ROI (X=Width, Y=Height), use SetROI() */ + 0x9B, /* 0x81 : not user-modifiable */ + 0x00, /* 0x82 : not user-modifiable */ + 0x00, /* 0x83 : not user-modifiable */ + 0x00, /* 0x84 : not user-modifiable */ + 0x01, /* 0x85 : not user-modifiable */ + 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ + 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after VL53L1X_init() call, put 0x40 in location 0x87 */ }; - - - /* VL53L1X_api.h functions */ - +/* VL53L1X_api.h functions */ VL53L1X_ERROR VL53L1X::VL53L1X_GetSWVersion(VL53L1X_Version_t *pVersion) { @@ -166,20 +160,21 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetI2CAddress(uint8_t new_address) status = VL53L1_WrByte(Device, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1); Device->I2cDevAddr = new_address; - return status; } VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit() { VL53L1X_ERROR status = 0; - uint8_t Addr = 0x00, tmp=0, timeout = 0; + uint8_t Addr = 0x00, tmp = 0, timeout = 0; - for (Addr = 0x2D; Addr <= 0x87; Addr++){ + for (Addr = 0x2D; Addr <= 0x87; Addr++) + { status = VL53L1_WrByte(Device, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]); } status = VL53L1X_StartRanging(); - while(tmp==0){ + while (tmp == 0) + { status = VL53L1X_CheckForDataReady(&tmp); timeout++; if (timeout > 50) @@ -188,15 +183,14 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit() return status; } } - tmp = 0; + tmp = 0; status = VL53L1X_ClearInterrupt(); status = VL53L1X_StopRanging(); status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */ - status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */ + status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */ return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_ClearInterrupt() { VL53L1X_ERROR status = 0; @@ -205,7 +199,6 @@ VL53L1X_ERROR VL53L1X::VL53L1X_ClearInterrupt() return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_SetInterruptPolarity(uint8_t NewPolarity) { uint8_t Temp; @@ -217,8 +210,6 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetInterruptPolarity(uint8_t NewPolarity) return status; } - - VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity) { uint8_t Temp; @@ -226,17 +217,15 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity) status = VL53L1_RdByte(Device, GPIO_HV_MUX__CTRL, &Temp); Temp = Temp & 0x10; - *pInterruptPolarity = !(Temp>>4); + *pInterruptPolarity = !(Temp >> 4); return status; } - - VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging() { VL53L1X_ERROR status = 0; - status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */ + status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */ return status; } @@ -244,12 +233,10 @@ VL53L1X_ERROR VL53L1X::VL53L1X_StopRanging() { VL53L1X_ERROR status = 0; - status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */ + status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */ return status; } - - VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady) { uint8_t Temp; @@ -259,7 +246,8 @@ VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady) status = VL53L1X_GetInterruptPolarity(&IntPol); status = VL53L1_RdByte(Device, GPIO__TIO_HV_STATUS, &Temp); /* Read in the register to check if a new value is available */ - if (status == 0){ + if (status == 0) + { if ((Temp & 1) == IntPol) *isDataReady = 1; else @@ -268,100 +256,104 @@ VL53L1X_ERROR VL53L1X::VL53L1X_CheckForDataReady(uint8_t *isDataReady) return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_SetTimingBudgetInMs(uint16_t TimingBudgetInMs) { uint16_t DM; - VL53L1X_ERROR status=0; + VL53L1X_ERROR status = 0; status = VL53L1X_GetDistanceMode(&DM); if (DM == 0) return 1; - else if (DM == 1) { /* Short DistanceMode */ - switch (TimingBudgetInMs) { + else if (DM == 1) + { /* Short DistanceMode */ + switch (TimingBudgetInMs) + { case 15: /* only available in short distance mode */ VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x01D); + 0x01D); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x0027); + 0x0027); break; case 20: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x0051); + 0x0051); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x006E); + 0x006E); break; case 33: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x00D6); + 0x00D6); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x006E); + 0x006E); break; case 50: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x1AE); + 0x1AE); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x01E8); + 0x01E8); break; case 100: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x02E1); + 0x02E1); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x0388); + 0x0388); break; case 200: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x03E1); + 0x03E1); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x0496); + 0x0496); break; case 500: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x0591); + 0x0591); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x05C1); + 0x05C1); break; default: status = 1; break; } - } else { - switch (TimingBudgetInMs) { + } + else + { + switch (TimingBudgetInMs) + { case 20: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x001E); + 0x001E); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x0022); + 0x0022); break; case 33: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x0060); + 0x0060); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x006E); + 0x006E); break; case 50: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x00AD); + 0x00AD); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x00C6); + 0x00C6); break; case 100: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x01CC); + 0x01CC); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x01EA); + 0x01EA); break; case 200: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x02D9); + 0x02D9); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x02F8); + 0x02F8); break; case 500: VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, - 0x048F); + 0x048F); VL53L1_WrWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI, - 0x04A4); + 0x04A4); break; default: status = 1; @@ -377,49 +369,50 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetTimingBudgetInMs(uint16_t *pTimingBudget) VL53L1X_ERROR status = 0; status = VL53L1_RdWord(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, &Temp); - switch (Temp) { - case 0x001D : - *pTimingBudget = 15; - break; - case 0x0051 : - case 0x001E : - *pTimingBudget = 20; - break; - case 0x00D6 : - case 0x0060 : - *pTimingBudget = 33; - break; - case 0x1AE : - case 0x00AD : - *pTimingBudget = 50; - break; - case 0x02E1 : - case 0x01CC : - *pTimingBudget = 100; - break; - case 0x03E1 : - case 0x02D9 : - *pTimingBudget = 200; - break; - case 0x0591 : - case 0x048F : - *pTimingBudget = 500; - break; - default: - *pTimingBudget = 0; - break; + switch (Temp) + { + case 0x001D: + *pTimingBudget = 15; + break; + case 0x0051: + case 0x001E: + *pTimingBudget = 20; + break; + case 0x00D6: + case 0x0060: + *pTimingBudget = 33; + break; + case 0x1AE: + case 0x00AD: + *pTimingBudget = 50; + break; + case 0x02E1: + case 0x01CC: + *pTimingBudget = 100; + break; + case 0x03E1: + case 0x02D9: + *pTimingBudget = 200; + break; + case 0x0591: + case 0x048F: + *pTimingBudget = 500; + break; + default: + *pTimingBudget = 0; + break; } return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceMode(uint16_t DM) { uint16_t TB; VL53L1X_ERROR status = 0; status = VL53L1X_GetTimingBudgetInMs(&TB); - switch (DM) { + switch (DM) + { case 1: status = VL53L1_WrByte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x14); status = VL53L1_WrByte(Device, RANGE_CONFIG__VCSEL_PERIOD_A, 0x07); @@ -443,63 +436,54 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceMode(uint16_t DM) return status; } - - - VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceMode(uint16_t *DM) { - uint8_t TempDM, status=0; + uint8_t TempDM, status = 0; - status = VL53L1_RdByte(Device,PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM); + status = VL53L1_RdByte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM); if (TempDM == 0x14) - *DM=1; - if(TempDM == 0x0A) - *DM=2; + *DM = 1; + if (TempDM == 0x0A) + *DM = 2; return status; } - - VL53L1X_ERROR VL53L1X::VL53L1X_SetInterMeasurementInMs(uint16_t InterMeasMs) { uint16_t ClockPLL; VL53L1X_ERROR status = 0; status = VL53L1_RdWord(Device, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL); - ClockPLL = ClockPLL&0x3FF; + ClockPLL = ClockPLL & 0x3FF; VL53L1_WrDWord(Device, VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD, - (uint32_t)(ClockPLL * InterMeasMs * 1.075)); + (uint32_t)(ClockPLL * InterMeasMs * 1.075)); return status; - } - VL53L1X_ERROR VL53L1X::VL53L1X_GetInterMeasurementInMs(uint16_t *pIM) { uint16_t ClockPLL; VL53L1X_ERROR status = 0; uint32_t tmp; - status = VL53L1_RdDWord(Device,VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD, &tmp); + status = VL53L1_RdDWord(Device, VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD, &tmp); *pIM = (uint16_t)tmp; status = VL53L1_RdWord(Device, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL); - ClockPLL = ClockPLL&0x3FF; - *pIM= (uint16_t)(*pIM/(ClockPLL*1.065)); + ClockPLL = ClockPLL & 0x3FF; + *pIM = (uint16_t)(*pIM / (ClockPLL * 1.065)); return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_BootState(uint8_t *state) { VL53L1X_ERROR status = 0; uint8_t tmp = 0; - status = VL53L1_RdByte(Device,VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp); + status = VL53L1_RdByte(Device, VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp); *state = tmp; return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetSensorId(uint16_t *sensorId) { VL53L1X_ERROR status = 0; @@ -510,14 +494,13 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetSensorId(uint16_t *sensorId) return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetDistance(uint16_t *distance) { VL53L1X_ERROR status = 0; uint16_t tmp; status = (VL53L1_RdWord(Device, - VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, &tmp)); + VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, &tmp)); *distance = tmp; return status; } @@ -525,72 +508,68 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetDistance(uint16_t *distance) VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalPerSpad(uint16_t *signalRate) { VL53L1X_ERROR status = 0; - uint16_t SpNb=1, signal; + uint16_t SpNb = 1, signal; status = VL53L1_RdWord(Device, - VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &signal); + VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &signal); status = VL53L1_RdWord(Device, - VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb); - *signalRate = (uint16_t) (2000.0*signal/SpNb); + VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb); + *signalRate = (uint16_t)(2000.0 * signal / SpNb); return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetAmbientPerSpad(uint16_t *ambPerSp) { - VL53L1X_ERROR status=0; - uint16_t AmbientRate, SpNb=1; + VL53L1X_ERROR status = 0; + uint16_t AmbientRate, SpNb = 1; status = VL53L1_RdWord(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &AmbientRate); status = VL53L1_RdWord(Device, VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb); - *ambPerSp=(uint16_t) (2000.0 * AmbientRate / SpNb); + *ambPerSp = (uint16_t)(2000.0 * AmbientRate / SpNb); return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalRate(uint16_t *signal) { VL53L1X_ERROR status = 0; uint16_t tmp; status = VL53L1_RdWord(Device, - VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &tmp); - *signal = tmp*8; + VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &tmp); + *signal = tmp * 8; return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetSpadNb(uint16_t *spNb) { VL53L1X_ERROR status = 0; uint16_t tmp; status = VL53L1_RdWord(Device, - VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &tmp); + VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &tmp); *spNb = tmp >> 8; return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetAmbientRate(uint16_t *ambRate) { VL53L1X_ERROR status = 0; uint16_t tmp; status = VL53L1_RdWord(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &tmp); - *ambRate = tmp*8; + *ambRate = tmp * 8; return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetRangeStatus(uint8_t *rangeStatus) { VL53L1X_ERROR status = 0; uint8_t RgSt; status = VL53L1_RdByte(Device, VL53L1_RESULT__RANGE_STATUS, &RgSt); - RgSt = RgSt&0x1F; - switch (RgSt) { + RgSt = RgSt & 0x1F; + switch (RgSt) + { case 9: RgSt = 0; break; @@ -638,98 +617,95 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetRangeStatus(uint8_t *rangeStatus) return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_SetOffset(int16_t OffsetValue) { VL53L1X_ERROR status = 0; int16_t Temp; - Temp = (OffsetValue*4); + Temp = (OffsetValue * 4); VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, - (uint16_t)Temp); + (uint16_t)Temp); VL53L1_WrWord(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0); VL53L1_WrWord(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0); return status; } - -VL53L1X_ERROR VL53L1X::VL53L1X_GetOffset(int16_t *offset) +VL53L1X_ERROR VL53L1X::VL53L1X_GetOffset(int16_t *offset) { VL53L1X_ERROR status = 0; uint16_t Temp; - status = VL53L1_RdWord(Device,ALGO__PART_TO_PART_RANGE_OFFSET_MM, &Temp); - Temp = Temp<<3; - Temp = Temp >>5; + status = VL53L1_RdWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, &Temp); + Temp = Temp << 3; + Temp = Temp >> 5; *offset = (int16_t)(Temp); return status; } VL53L1X_ERROR VL53L1X::VL53L1X_SetXtalk(uint16_t XtalkValue) { -/* XTalkValue in count per second to avoid float type */ + /* XTalkValue in count per second to avoid float type */ VL53L1X_ERROR status = 0; status = VL53L1_WrWord(Device, - ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS, - 0x0000); + ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS, + 0x0000); status = VL53L1_WrWord(Device, ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS, - 0x0000); + 0x0000); status = VL53L1_WrWord(Device, ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, - (XtalkValue<<9)/1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */ + (XtalkValue << 9) / 1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */ return status; } - -VL53L1X_ERROR VL53L1X::VL53L1X_GetXtalk(uint16_t *xtalk ) +VL53L1X_ERROR VL53L1X::VL53L1X_GetXtalk(uint16_t *xtalk) { VL53L1X_ERROR status = 0; uint16_t tmp; - status = VL53L1_RdWord(Device,ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, &tmp); - *xtalk = (tmp*1000)>>9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */ + status = VL53L1_RdWord(Device, ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, &tmp); + *xtalk = (tmp * 1000) >> 9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */ return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_SetDistanceThreshold(uint16_t ThreshLow, - uint16_t ThreshHigh, uint8_t Window, - uint8_t IntOnNoTarget) + uint16_t ThreshHigh, uint8_t Window, + uint8_t IntOnNoTarget) { VL53L1X_ERROR status = 0; uint8_t Temp = 0; status = VL53L1_RdByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, &Temp); Temp = Temp & 0x47; - if (IntOnNoTarget == 0) { + if (IntOnNoTarget == 0) + { status = VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, - (Temp | (Window & 0x07))); - } else { + (Temp | (Window & 0x07))); + } + else + { status = VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, - ((Temp | (Window & 0x07)) | 0x40)); + ((Temp | (Window & 0x07)) | 0x40)); } status = VL53L1_WrWord(Device, SYSTEM__THRESH_HIGH, ThreshHigh); status = VL53L1_WrWord(Device, SYSTEM__THRESH_LOW, ThreshLow); return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdWindow(uint16_t *window) { VL53L1X_ERROR status = 0; uint8_t tmp; - status = VL53L1_RdByte(Device,SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp); + status = VL53L1_RdByte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp); *window = (uint16_t)(tmp & 0x7); return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdLow(uint16_t *low) { VL53L1X_ERROR status = 0; uint16_t tmp; - status = VL53L1_RdWord(Device,SYSTEM__THRESH_LOW, &tmp); + status = VL53L1_RdWord(Device, SYSTEM__THRESH_LOW, &tmp); *low = tmp; return status; } @@ -739,7 +715,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetDistanceThresholdHigh(uint16_t *high) VL53L1X_ERROR status = 0; uint16_t tmp; - status = VL53L1_RdWord(Device,SYSTEM__THRESH_HIGH, &tmp); + status = VL53L1_RdWord(Device, SYSTEM__THRESH_HIGH, &tmp); *high = tmp; return status; } @@ -752,12 +728,13 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetROI(uint8_t X, uint8_t Y, uint8_t opticalCente X = 16; if (Y > 16) Y = 16; - if (X > 10 || Y > 10){ + if (X > 10 || Y > 10) + { opticalCenter = 199; } status = VL53L1_WrByte(Device, ROI_CONFIG__USER_ROI_CENTRE_SPAD, opticalCenter); status = VL53L1_WrByte(Device, ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, - (Y - 1) << 4 | (X - 1)); + (Y - 1) << 4 | (X - 1)); return status; } @@ -766,7 +743,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetROI_XY(uint16_t *ROI_X, uint16_t *ROI_Y) VL53L1X_ERROR status = 0; uint8_t tmp; - status = VL53L1_RdByte(Device,ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, &tmp); + status = VL53L1_RdByte(Device, ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, &tmp); *ROI_X = ((uint16_t)tmp & 0x0F) + 1; *ROI_Y = (((uint16_t)tmp & 0xF0) >> 4) + 1; return status; @@ -776,7 +753,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetSignalThreshold(uint16_t Signal) { VL53L1X_ERROR status = 0; - VL53L1_WrWord(Device,RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS,Signal>>3); + VL53L1_WrWord(Device, RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, Signal >> 3); return status; } @@ -786,21 +763,21 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetSignalThreshold(uint16_t *signal) uint16_t tmp; status = VL53L1_RdWord(Device, - RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); - *signal = tmp <<3; + RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); + *signal = tmp << 3; return status; } - VL53L1X_ERROR VL53L1X::VL53L1X_SetSigmaThreshold(uint16_t Sigma) { VL53L1X_ERROR status = 0; - if(Sigma>(0xFFFF>>2)){ + if (Sigma > (0xFFFF >> 2)) + { return 1; } /* 16 bits register 14.2 format */ - status = VL53L1_WrWord(Device,RANGE_CONFIG__SIGMA_THRESH,Sigma<<2); + status = VL53L1_WrWord(Device, RANGE_CONFIG__SIGMA_THRESH, Sigma << 2); return status; } @@ -809,33 +786,33 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetSigmaThreshold(uint16_t *sigma) VL53L1X_ERROR status = 0; uint16_t tmp; - status = VL53L1_RdWord(Device,RANGE_CONFIG__SIGMA_THRESH, &tmp); + status = VL53L1_RdWord(Device, RANGE_CONFIG__SIGMA_THRESH, &tmp); *sigma = tmp >> 2; return status; - } VL53L1X_ERROR VL53L1X::VL53L1X_StartTemperatureUpdate() { VL53L1X_ERROR status = 0; - uint8_t tmp=0; + uint8_t tmp = 0; - status = VL53L1_WrByte(Device,VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND,0x81); /* full VHV */ - status = VL53L1_WrByte(Device,0x0B,0x92); + status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x81); /* full VHV */ + status = VL53L1_WrByte(Device, 0x0B, 0x92); status = VL53L1X_StartRanging(); - while(tmp==0){ + while (tmp == 0) + { status = VL53L1X_CheckForDataReady(&tmp); } - tmp = 0; + tmp = 0; status = VL53L1X_ClearInterrupt(); status = VL53L1X_StopRanging(); status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */ - status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */ + status = VL53L1_WrByte(Device, 0x0B, 0); /* start VHV from the previous temperature */ return status; } - /* VL53L1X_calibration.h functions */ - +/* VL53L1X_calibration.h functions */ + int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset) { uint8_t i = 0, tmp; @@ -846,9 +823,11 @@ int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset status = VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, 0x0); status = VL53L1_WrWord(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0); status = VL53L1_WrWord(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0); - status = VL53L1X_StartRanging(); /* Enable VL53L1X sensor */ - for (i = 0; i < 50; i++) { - while (tmp == 0){ + status = VL53L1X_StartRanging(); /* Enable VL53L1X sensor */ + for (i = 0; i < 50; i++) + { + while (tmp == 0) + { status = VL53L1X_CheckForDataReady(&tmp); } tmp = 0; @@ -859,14 +838,13 @@ int8_t VL53L1X::VL53L1X_CalibrateOffset(uint16_t TargetDistInMm, int16_t *offset status = VL53L1X_StopRanging(); AverageDistance = AverageDistance / 50; *offset = TargetDistInMm - AverageDistance; - status = VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, *offset*4); + status = VL53L1_WrWord(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, *offset * 4); return status; } - int8_t VL53L1X::VL53L1X_CalibrateXtalk(uint16_t TargetDistInMm, uint16_t *xtalk) { - uint8_t i, tmp= 0; + uint8_t i, tmp = 0; float AverageSignalRate = 0; float AverageDistance = 0; float AverageSpadNb = 0; @@ -874,216 +852,211 @@ int8_t VL53L1X::VL53L1X_CalibrateXtalk(uint16_t TargetDistInMm, uint16_t *xtalk) uint16_t sr; VL53L1X_ERROR status = 0; - status = VL53L1_WrWord(Device, 0x0016,0); + status = VL53L1_WrWord(Device, 0x0016, 0); status = VL53L1X_StartRanging(); - for (i = 0; i < 50; i++) { - while (tmp == 0){ + for (i = 0; i < 50; i++) + { + while (tmp == 0) + { status = VL53L1X_CheckForDataReady(&tmp); } - tmp=0; - status= VL53L1X_GetSignalRate(&sr); - status= VL53L1X_GetDistance(&distance); + tmp = 0; + status = VL53L1X_GetSignalRate(&sr); + status = VL53L1X_GetDistance(&distance); status = VL53L1X_ClearInterrupt(); AverageDistance = AverageDistance + distance; status = VL53L1X_GetSpadNb(&spadNum); AverageSpadNb = AverageSpadNb + spadNum; AverageSignalRate = - AverageSignalRate + sr; + AverageSignalRate + sr; } status = VL53L1X_StopRanging(); AverageDistance = AverageDistance / 50; AverageSpadNb = AverageSpadNb / 50; AverageSignalRate = AverageSignalRate / 50; /* Calculate Xtalk value */ - *xtalk = (uint16_t)(512*(AverageSignalRate*(1-(AverageDistance/TargetDistInMm)))/AverageSpadNb); + *xtalk = (uint16_t)(512 * (AverageSignalRate * (1 - (AverageDistance / TargetDistInMm))) / AverageSpadNb); status = VL53L1_WrWord(Device, 0x0016, *xtalk); return status; } - - - - /* Write and read functions from I2C */ - +/* Write and read functions from I2C */ VL53L1X_ERROR VL53L1X::VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) { - int status; + int status; - status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, pdata, (uint16_t)count); - return status; + status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, pdata, (uint16_t)count); + return status; } VL53L1X_ERROR VL53L1X::VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count) { - int status; + int status; - status = VL53L1_I2CRead(Dev->I2cDevAddr, index, pdata, (uint16_t)count); + status = VL53L1_I2CRead(Dev->I2cDevAddr, index, pdata, (uint16_t)count); - return status; + return status; } - VL53L1X_ERROR VL53L1X::VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) { - int status; + int status; - status=VL53L1_I2CWrite(Dev->I2cDevAddr, index, &data, 1); - return status; + status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, &data, 1); + return status; } VL53L1X_ERROR VL53L1X::VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) { - int status; - uint8_t buffer[2]; + int status; + uint8_t buffer[2]; - buffer[0] = data >> 8; - buffer[1] = data & 0x00FF; - status=VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 2); - return status; + buffer[0] = data >> 8; + buffer[1] = data & 0x00FF; + status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 2); + return status; } VL53L1X_ERROR VL53L1X::VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) { - int status; - uint8_t buffer[4]; - - buffer[0] = (data >> 24) & 0xFF; - buffer[1] = (data >> 16) & 0xFF; - buffer[2] = (data >> 8) & 0xFF; - buffer[3] = (data >> 0) & 0xFF; - status=VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4); - return status; + int status; + uint8_t buffer[4]; + + buffer[0] = (data >> 24) & 0xFF; + buffer[1] = (data >> 16) & 0xFF; + buffer[2] = (data >> 8) & 0xFF; + buffer[3] = (data >> 0) & 0xFF; + status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4); + return status; } - VL53L1X_ERROR VL53L1X::VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) { - int status; + int status; - status = VL53L1_I2CRead(Dev->I2cDevAddr, index, data, 1); + status = VL53L1_I2CRead(Dev->I2cDevAddr, index, data, 1); - if(status) - return -1; + if (status) + return -1; - return 0; + return 0; } VL53L1X_ERROR VL53L1X::VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) { - int status; - uint8_t buffer[2] = {0,0}; - - status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 2); - if (!status) - { - *data = (buffer[0] << 8) + buffer[1]; - } - return status; + int status; + uint8_t buffer[2] = {0, 0}; + status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 2); + if (!status) + { + *data = (buffer[0] << 8) + buffer[1]; + } + return status; } VL53L1X_ERROR VL53L1X::VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) { - int status; - uint8_t buffer[4] = {0,0,0,0}; - - status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 4); - if(!status) - { - *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; - } - return status; + int status; + uint8_t buffer[4] = {0, 0, 0, 0}; + status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 4); + if (!status) + { + *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; + } + return status; } VL53L1X_ERROR VL53L1X::VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData) { - int status; - uint8_t buffer = 0; - - /* read data direct onto buffer */ - status = VL53L1_I2CRead(Dev->I2cDevAddr, index, &buffer,1); - if (!status) - { - buffer = (buffer & AndData) | OrData; - status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, &buffer, (uint16_t)1); - } - return status; + int status; + uint8_t buffer = 0; + + /* read data direct onto buffer */ + status = VL53L1_I2CRead(Dev->I2cDevAddr, index, &buffer, 1); + if (!status) + { + buffer = (buffer & AndData) | OrData; + status = VL53L1_I2CWrite(Dev->I2cDevAddr, index, &buffer, (uint16_t)1); + } + return status; } -VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite) +VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToWrite) { #ifdef DEBUG_MODE - Serial.print("Beginning transmission to "); - Serial.println(((DeviceAddr) >> 1) & 0x7F); + Serial.print("Beginning transmission to "); + Serial.println(((DeviceAddr) >> 1) & 0x7F); #endif - dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); + dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); #ifdef DEBUG_MODE - Serial.print("Writing port number "); - Serial.println(RegisterAddr); + Serial.print("Writing port number "); + Serial.println(RegisterAddr); #endif - uint8_t buffer[2]; - buffer[0]=RegisterAddr>>8; - buffer[1]=RegisterAddr&0xFF; - dev_i2c->write(buffer, 2); - for (int i = 0 ; i < NumByteToWrite ; i++) - dev_i2c->write(pBuffer[i]); - - dev_i2c->endTransmission(true); - return 0; + uint8_t buffer[2]; + buffer[0] = RegisterAddr >> 8; + buffer[1] = RegisterAddr & 0xFF; + dev_i2c->write(buffer, 2); + for (int i = 0; i < NumByteToWrite; i++) + dev_i2c->write(pBuffer[i]); + + dev_i2c->endTransmission(true); + return 0; } -VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead) +VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead) { - int status = 0; -//Loop until the port is transmitted correctly - do { + int status = 0; + //Loop until the port is transmitted correctly + do + { #ifdef DEBUG_MODE - Serial.print("Beginning transmission to "); - Serial.println(((DeviceAddr) >> 1) & 0x7F); + Serial.print("Beginning transmission to "); + Serial.println(((DeviceAddr) >> 1) & 0x7F); #endif - dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); + dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); #ifdef DEBUG_MODE - Serial.print("Writing port number "); - Serial.println(RegisterAddr); + Serial.print("Writing port number "); + Serial.println(RegisterAddr); #endif - uint8_t buffer[2]; - buffer[0]=RegisterAddr>>8; - buffer[1]=RegisterAddr&0xFF; - dev_i2c->write(buffer, 2); - status = dev_i2c->endTransmission(false); + uint8_t buffer[2]; + buffer[0] = RegisterAddr >> 8; + buffer[1] = RegisterAddr & 0xFF; + dev_i2c->write(buffer, 2); + status = dev_i2c->endTransmission(false); //Fix for some STM32 boards //Reinitialize th i2c bus with the default parameters #ifdef ARDUINO_ARCH_STM32 - if (status){ - dev_i2c->end(); - dev_i2c->begin(); + if (status) + { + dev_i2c->end(); + dev_i2c->begin(); } -#endif -//End of fix - } while(status != 0); +#endif + //End of fix + } while (status != 0); - dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte) NumByteToRead); + dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte)NumByteToRead); - int i=0; - while (dev_i2c->available()) - { - pBuffer[i] = dev_i2c->read(); - i++; - } + int i = 0; + while (dev_i2c->available()) + { + pBuffer[i] = dev_i2c->read(); + i++; + } - return 0; + return 0; } - VL53L1X_ERROR VL53L1X::VL53L1_GetTickCount( uint32_t *ptick_count_ms) { - /* Returns current tick count in [ms] */ + /* Returns current tick count in [ms] */ - VL53L1X_ERROR status = VL53L1_ERROR_NONE; + VL53L1X_ERROR status = VL53L1_ERROR_NONE; //*ptick_count_ms = timeGetTime(); *ptick_count_ms = 0; @@ -1091,29 +1064,27 @@ VL53L1X_ERROR VL53L1X::VL53L1_GetTickCount( return status; } - - -VL53L1X_ERROR VL53L1X::VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us){ +VL53L1X_ERROR VL53L1X::VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us) +{ (void)pdev; - delay(wait_us/1000); - return VL53L1_ERROR_NONE; + delay(wait_us / 1000); + return VL53L1_ERROR_NONE; } - -VL53L1X_ERROR VL53L1X::VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms){ +VL53L1X_ERROR VL53L1X::VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms) +{ (void)pdev; delay(wait_ms); - return VL53L1_ERROR_NONE; + return VL53L1_ERROR_NONE; } - VL53L1X_ERROR VL53L1X::VL53L1_WaitValueMaskEx( VL53L1_Dev_t *pdev, - uint32_t timeout_ms, - uint16_t index, - uint8_t value, - uint8_t mask, - uint32_t poll_delay_ms) + uint32_t timeout_ms, + uint16_t index, + uint8_t value, + uint8_t mask, + uint32_t poll_delay_ms) { /* @@ -1127,60 +1098,52 @@ VL53L1X_ERROR VL53L1X::VL53L1_WaitValueMaskEx( * poll_delay_ms); */ - VL53L1_Error status = VL53L1_ERROR_NONE; - uint32_t start_time_ms = 0; - uint32_t current_time_ms = 0; - uint32_t polling_time_ms = 0; - uint8_t byte_value = 0; - uint8_t found = 0; - - + VL53L1_Error status = VL53L1_ERROR_NONE; + uint32_t start_time_ms = 0; + uint32_t current_time_ms = 0; + uint32_t polling_time_ms = 0; + uint8_t byte_value = 0; + uint8_t found = 0; /* calculate time limit in absolute time */ - VL53L1_GetTickCount(&start_time_ms); + VL53L1_GetTickCount(&start_time_ms); /* remember current trace functions and temporarily disable * function logging */ - /* wait until value is found, timeout reached on error occurred */ while ((status == VL53L1_ERROR_NONE) && (polling_time_ms < timeout_ms) && - (found == 0)) { + (found == 0)) + { if (status == VL53L1_ERROR_NONE) status = VL53L1_RdByte( - pdev, - index, - &byte_value); + pdev, + index, + &byte_value); if ((byte_value & mask) == value) found = 1; - if (status == VL53L1_ERROR_NONE && + if (status == VL53L1_ERROR_NONE && found == 0 && poll_delay_ms > 0) status = VL53L1_WaitMs( - pdev, - poll_delay_ms); + pdev, + poll_delay_ms); /* Update polling time (Compare difference rather than absolute to negate 32bit wrap around issue) */ VL53L1_GetTickCount(¤t_time_ms); polling_time_ms = current_time_ms - start_time_ms; - } - if (found == 0 && status == VL53L1_ERROR_NONE) status = VL53L1_ERROR_TIME_OUT; return status; } - - - - From 50bfe6282e28e2d20c10ef9fa3ad6c0f1edf19e5 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 17 May 2020 14:09:02 -0500 Subject: [PATCH 04/31] Remove infinite loop when read fails It looks like ST put in some fix for their boards. This caused an infinite loop in our sketches if a sensor was not connected. This fixes it but it may break ST board support (who cares?). --- src/vl53l1x_class.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index 499cf4a..ac7b78f 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -1009,8 +1009,10 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead) { int status = 0; + //Loop until the port is transmitted correctly - do + uint8_t maxAttempts = 5; + for (uint8_t x = 0; x < maxAttempts; x++) { #ifdef DEBUG_MODE Serial.print("Beginning transmission to "); @@ -1026,6 +1028,10 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, buffer[1] = RegisterAddr & 0xFF; dev_i2c->write(buffer, 2); status = dev_i2c->endTransmission(false); + + if (status == 0) + break; + //Fix for some STM32 boards //Reinitialize th i2c bus with the default parameters #ifdef ARDUINO_ARCH_STM32 @@ -1036,7 +1042,7 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, } #endif //End of fix - } while (status != 0); + } dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte)NumByteToRead); From 3d7569973f581f7de2c3a23a69a3eaa007dcc07d Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 17 May 2020 14:25:15 -0500 Subject: [PATCH 05/31] Wait one intermeasurement period at POR before attempting init. Fix for issue #37. --- src/vl53l1x_class.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index ac7b78f..1bcb8cb 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -173,6 +173,9 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit() status = VL53L1_WrByte(Device, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]); } status = VL53L1X_StartRanging(); + + delay(103); //Wait the default intermeasurement period of 103ms before checking for dataready + while (tmp == 0) { status = VL53L1X_CheckForDataReady(&tmp); @@ -182,6 +185,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit() status = VL53L1_ERROR_TIME_OUT; return status; } + delay(1); } tmp = 0; status = VL53L1X_ClearInterrupt(); From 668078c3c5f5626ed78a23bbedb0f6c7c9207d30 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 17 May 2020 14:38:04 -0500 Subject: [PATCH 06/31] Update examples to freeze on failure Previous examples would only print success, but then run regardless. This stops on failure and says so. Also added link to lib and changed to 115200bps. --- .../Example1_ReadDistance.ino | 15 +- .../Example2_SetDistanceMode.ino | 19 ++- .../Example3_StatusAndRate.ino | 58 ++++--- .../Example4_SetIntermeasurementPeriod.ino | 17 +- .../Example5_LCDDemo/Example5_LCDDemo.ino | 41 +++-- .../Example6_ArduinoPlotterOutput.ino | 132 +++++++-------- .../Example7_Calibration.ino | 158 ++++++++++-------- 7 files changed, 237 insertions(+), 203 deletions(-) diff --git a/examples/Example1_ReadDistance/Example1_ReadDistance.ino b/examples/Example1_ReadDistance/Example1_ReadDistance.ino index 65cd86a..2af66ad 100644 --- a/examples/Example1_ReadDistance/Example1_ReadDistance.ino +++ b/examples/Example1_ReadDistance/Example1_ReadDistance.ino @@ -20,7 +20,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X //Optional interrupt and shutdown pins. #define SHUTDOWN_PIN 2 @@ -34,19 +34,23 @@ void setup(void) { Wire.begin(); - Serial.begin(9600); + Serial.begin(115200); Serial.println("VL53L1X Qwiic Test"); - if (distanceSensor.begin() == 0) //Begin returns 0 on a good init + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init { - Serial.println("Sensor online!"); + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; } + Serial.println("Sensor online!"); } void loop(void) { distanceSensor.startRanging(); //Write configuration bytes to initiate measurement - while (!distanceSensor.checkForDataReady()) { + while (!distanceSensor.checkForDataReady()) + { delay(1); } int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor @@ -64,4 +68,3 @@ void loop(void) Serial.println(); } - diff --git a/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino b/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino index addff07..212fada 100644 --- a/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino +++ b/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino @@ -15,7 +15,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X //Optional interrupt and shutdown pins. #define SHUTDOWN_PIN 2 @@ -29,14 +29,17 @@ void setup(void) { Wire.begin(); - Serial.begin(9600); + Serial.begin(115200); Serial.println("VL53L1X Qwiic Test"); - if (distanceSensor.begin() == 0) //Begin returns 0 on a good init + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init { - Serial.println("Sensor online!"); + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; } - + Serial.println("Sensor online!"); + distanceSensor.setDistanceModeShort(); //distanceSensor.setDistanceModeLong(); } @@ -45,12 +48,13 @@ void loop(void) { distanceSensor.startRanging(); //Write configuration bytes to initiate measurement - while (!distanceSensor.checkForDataReady()) { + while (!distanceSensor.checkForDataReady()) + { delay(1); } int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor distanceSensor.clearInterrupt(); - + distanceSensor.stopRanging(); Serial.print("Distance(mm): "); @@ -64,4 +68,3 @@ void loop(void) Serial.println(); } - diff --git a/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino b/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino index 56e4e79..edbe1bd 100644 --- a/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino +++ b/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino @@ -14,7 +14,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X //Optional interrupt and shutdown pins. #define SHUTDOWN_PIN 2 @@ -33,13 +33,18 @@ void setup(void) { Wire.begin(); - Serial.begin(9600); + Serial.begin(115200); Serial.println("VL53L1X Qwiic Test"); - if (distanceSensor.begin() == 0) //Begin returns 0 on a good init - Serial.println("Sensor online!"); + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); - for (int x = 0 ; x < HISTORY_SIZE ; x++) + for (int x = 0; x < HISTORY_SIZE; x++) history[x] = 0; } @@ -47,22 +52,24 @@ void loop(void) { long startTime = millis(); distanceSensor.startRanging(); //Write configuration block of 135 bytes to setup a measurement - while (!distanceSensor.checkForDataReady()) { + while (!distanceSensor.checkForDataReady()) + { delay(1); } int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor distanceSensor.clearInterrupt(); distanceSensor.stopRanging(); long endTime = millis(); - + Serial.print("Distance(mm): "); Serial.print(distance); history[historySpot] = distance; - if (++historySpot == HISTORY_SIZE) historySpot = 0; + if (++historySpot == HISTORY_SIZE) + historySpot = 0; long avgDistance = 0; - for (int x = 0 ; x < HISTORY_SIZE ; x++) + for (int x = 0; x < HISTORY_SIZE; x++) avgDistance += history[x]; avgDistance /= HISTORY_SIZE; @@ -85,22 +92,22 @@ void loop(void) //Make it human readable switch (rangeStatus) { - case 0: - Serial.print("Good"); - break; - case 1: - Serial.print("Sigma fail"); - break; - case 2: - Serial.print("Signal fail"); - break; - case 7: - Serial.print("Wrapped target fail"); - break; - default: - Serial.print("Unknown: "); - Serial.print(rangeStatus); - break; + case 0: + Serial.print("Good"); + break; + case 1: + Serial.print("Sigma fail"); + break; + case 2: + Serial.print("Signal fail"); + break; + case 7: + Serial.print("Wrapped target fail"); + break; + default: + Serial.print("Unknown: "); + Serial.print(rangeStatus); + break; } Serial.print("\tHz: "); @@ -108,4 +115,3 @@ void loop(void) Serial.println(); } - diff --git a/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino b/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino index 8647864..7791f6f 100644 --- a/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino +++ b/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino @@ -14,7 +14,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X //Optional interrupt and shutdown pins. #define SHUTDOWN_PIN 2 @@ -28,12 +28,17 @@ void setup(void) { Wire.begin(); - Serial.begin(9600); + Serial.begin(115200); Serial.println("VL53L1X Qwiic Test"); - if (distanceSensor.begin() == 0) //Begin returns 0 on a good init + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } Serial.println("Sensor online!"); - + // Intermeasurement period must be >= timing budget. Default = 100 ms. distanceSensor.setIntermeasurementPeriod(200); Serial.println(distanceSensor.getIntermeasurementPeriod()); @@ -42,7 +47,8 @@ void setup(void) void loop(void) { - while (!distanceSensor.checkForDataReady()) { + while (!distanceSensor.checkForDataReady()) + { delay(1); } int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor @@ -59,4 +65,3 @@ void loop(void) Serial.println(); } - diff --git a/examples/Example5_LCDDemo/Example5_LCDDemo.ino b/examples/Example5_LCDDemo/Example5_LCDDemo.ino index e9d1645..d98d5b7 100644 --- a/examples/Example5_LCDDemo/Example5_LCDDemo.ino +++ b/examples/Example5_LCDDemo/Example5_LCDDemo.ino @@ -15,7 +15,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X #include //Optional interrupt and shutdown pins. @@ -50,7 +50,7 @@ byte deltaSpot = 0; //Keeps track of where we are within the deltas array //Too quickly and it gets twitchy. Too slow and it doesn't seem like it's responding. #define LOOPTIME 50 -int maxMPH = 0; //Keeps track of what the latest fastest speed is +int maxMPH = 0; //Keeps track of what the latest fastest speed is long maxMPH_timeout = 0; //Forget the max speed after some length of time #define maxMPH_remember 3000 //After this number of ms the system will forget the max speed @@ -73,12 +73,15 @@ void setup(void) lcd.print("Distance: 3426 "); lcd.print("12 mph "); - if (distanceSensor.begin() == 0) //Begin returns 0 on a good init + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init { - Serial.println("Sensor online!"); + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; } + Serial.println("Sensor online!"); - for (int x = 0 ; x < HISTORY_SIZE ; x++) + for (int x = 0; x < HISTORY_SIZE; x++) history[x] = 0; } @@ -87,25 +90,26 @@ void loop(void) //Write configuration block of 135 bytes to setup a measurement distanceSensor.startRanging(); - while (!distanceSensor.checkForDataReady()) { + while (!distanceSensor.checkForDataReady()) + { delay(1); } int distanceMM = distanceSensor.getDistance(); distanceSensor.clearInterrupt(); distanceSensor.stopRanging(); - + lastReading = millis(); history[historySpot] = distanceMM; - if (historySpot++ == HISTORY_SIZE) historySpot = 0; + if (historySpot++ == HISTORY_SIZE) + historySpot = 0; long avgDistance = 0; - for (int x = 0 ; x < HISTORY_SIZE ; x++) + for (int x = 0; x < HISTORY_SIZE; x++) avgDistance += history[x]; avgDistance /= HISTORY_SIZE; - //Every loop let's get a reading newDistance = distanceMM / 10; //Go get distance in cm @@ -114,24 +118,26 @@ void loop(void) //Scan delta array to see if this new delta is sane or not boolean safeDelta = true; - for (int x = 0 ; x < numberOfDeltas ; x++) + for (int x = 0; x < numberOfDeltas; x++) { //We don't want to register jumps greater than 30cm in 50ms //But if we're less than 1000cm then maybe //30 works well - if ( abs(deltaDistance - deltas[x]) > 40) safeDelta = false; + if (abs(deltaDistance - deltas[x]) > 40) + safeDelta = false; } //Insert this new delta into the array if (safeDelta) { deltas[deltaSpot++] = deltaDistance; - if (deltaSpot > numberOfDeltas) deltaSpot = 0; //Wrap this variable + if (deltaSpot > numberOfDeltas) + deltaSpot = 0; //Wrap this variable } //Get average of the current deltas array float avgDeltas = 0.0; - for (byte x = 0 ; x < numberOfDeltas ; x++) + for (byte x = 0; x < numberOfDeltas; x++) avgDeltas += (float)deltas[x]; avgDeltas /= numberOfDeltas; @@ -162,10 +168,12 @@ void loop(void) else { validCount++; - if (avgDistance > maxDistance) maxDistance = avgDistance; + if (avgDistance > maxDistance) + maxDistance = avgDistance; } - if (validCount > 10) readingValid = true; + if (validCount > 10) + readingValid = true; if (readingValid == false) { @@ -195,4 +203,3 @@ void loop(void) delay(25); } - diff --git a/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino b/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino index df060fc..d086e6a 100644 --- a/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino +++ b/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino @@ -1,11 +1,3 @@ -#include - -#include -#include -#include -#include -#include - /* Reading distance from the laser based VL53L1X By: Armin Joachimsmeyer @@ -22,7 +14,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X //Optional interrupt and shutdown pins. #define SHUTDOWN_PIN 2 @@ -33,80 +25,88 @@ SFEVL53L1X distanceSensor; //Uncomment the following line to use the optional shutdown and interrupt pins. //SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); -void setup(void) { - Wire.begin(); - - // initialize the digital pin as an output. - pinMode(LED_BUILTIN, OUTPUT); - Serial.begin(9600); - while (!Serial) - ; //delay for Leonardo - // Just to know which program is running on my Arduino - Serial.println(F("START " __FILE__)); - -#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__) - // test beep for the connected speaker - tone(TONE_PIN,1000,100); - delay(200); +void setup(void) +{ + Wire.begin(); + + // initialize the digital pin as an output. + pinMode(LED_BUILTIN, OUTPUT); + Serial.begin(115200); + while (!Serial) + ; //delay for Leonardo + // Just to know which program is running on my Arduino + Serial.println(F("START " __FILE__)); + +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + // test beep for the connected speaker + tone(TONE_PIN, 1000, 100); + delay(200); #endif - if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init - Serial.println("Sensor online!"); - } + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); - // Short mode max distance is limited to 1.3 m but has a better ambient immunity. - // Above 1.3 meter error 4 is thrown (wrap around). - distanceSensor.setDistanceModeShort(); - //distanceSensor.setDistanceModeLong(); // default + // Short mode max distance is limited to 1.3 m but has a better ambient immunity. + // Above 1.3 meter error 4 is thrown (wrap around). + distanceSensor.setDistanceModeShort(); + //distanceSensor.setDistanceModeLong(); // default - // Print Legend - Serial.println("Distance Signal-Rate/100 Ambient-Rate/100"); + // Print Legend + Serial.println("Distance Signal-Rate/100 Ambient-Rate/100"); - /* + /* * The minimum timing budget is 20 ms for the short distance mode and 33 ms for the medium and long distance modes. * Predefined values = 15, 20, 33, 50, 100(default), 200, 500. * This function must be called after SetDistanceMode. */ - distanceSensor.setTimingBudgetInMs(50); - - // measure periodically. Intermeasurement period must be >/= timing budget. - distanceSensor.setIntermeasurementPeriod(100); - distanceSensor.startRanging(); // Start once + distanceSensor.setTimingBudgetInMs(50); + // measure periodically. Intermeasurement period must be >/= timing budget. + distanceSensor.setIntermeasurementPeriod(100); + distanceSensor.startRanging(); // Start once } -void loop(void) { - while (!distanceSensor.checkForDataReady()) { - delay(1); - } - byte rangeStatus = distanceSensor.getRangeStatus(); - unsigned int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor - distanceSensor.clearInterrupt(); - - /* +void loop(void) +{ + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + byte rangeStatus = distanceSensor.getRangeStatus(); + unsigned int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + + /* * With signed int we get overflow at short distances. * With unsigned we get an overflow below around 2.5 cm. */ - unsigned int tSignalRate = distanceSensor.getSignalRate(); - unsigned int tAmbientRate = distanceSensor.getAmbientRate(); + unsigned int tSignalRate = distanceSensor.getSignalRate(); + unsigned int tAmbientRate = distanceSensor.getAmbientRate(); - if (rangeStatus == 0) { -#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__) - tone(11, distance + 500); + if (rangeStatus == 0) + { +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(11, distance + 500); #endif - } else { - // if tAmbientRate > tSignalRate we likely get a signal fail error condition - // in Distance mode short we get error 4 (out of bounds) or 7 (wrap around) if the distance is greater than 1.3 meter. - distance = rangeStatus; -#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__) - noTone(11); + } + else + { + // if tAmbientRate > tSignalRate we likely get a signal fail error condition + // in Distance mode short we get error 4 (out of bounds) or 7 (wrap around) if the distance is greater than 1.3 meter. + distance = rangeStatus; +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + noTone(11); #endif - } + } - Serial.print(distance); - Serial.print(' '); - Serial.print(tSignalRate / 100); - Serial.print(' '); - Serial.println(tAmbientRate / 100); + Serial.print(distance); + Serial.print(' '); + Serial.print(tSignalRate / 100); + Serial.print(' '); + Serial.println(tAmbientRate / 100); } - diff --git a/examples/Example7_Calibration/Example7_Calibration.ino b/examples/Example7_Calibration/Example7_Calibration.ino index 4fbe130..c06f220 100644 --- a/examples/Example7_Calibration/Example7_Calibration.ino +++ b/examples/Example7_Calibration/Example7_Calibration.ino @@ -22,7 +22,7 @@ */ #include -#include "SparkFun_VL53L1X.h" +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X #define VERSION_EXAMPLE "1.0" @@ -37,108 +37,118 @@ SFEVL53L1X distanceSensor; //Uncomment the following line to use the optional shutdown and interrupt pins. //SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); -void setup(void) { - Wire.begin(); +void setup(void) +{ + Wire.begin(); - // initialize the digital pin as an output. - pinMode(LED_BUILTIN, OUTPUT); - Serial.begin(9600); + // initialize the digital pin as an output. + pinMode(LED_BUILTIN, OUTPUT); + Serial.begin(115200); #if defined(__AVR_ATmega32U4__) - while (!Serial); //delay for Leonardo, but this loops forever for Maple Serial + while (!Serial) + ; //delay for Leonardo, but this loops forever for Maple Serial #endif #if defined(SERIAL_USB) - delay(2000); // To be able to connect Serial monitor after reset and before first printout + delay(2000); // To be able to connect Serial monitor after reset and before first printout #endif - // Just to know which program is running on my Arduino - Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__)); + // Just to know which program is running on my Arduino + Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__)); -#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__) - tone(TONE_PIN, 1000, 100); - delay(200); +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(TONE_PIN, 1000, 100); + delay(200); #endif - Serial.println(); - Serial.println("*****************************************************************************************************"); - Serial.println(" Offset calibration"); - Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor."); - Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second."); - Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin()."); - Serial.println("*****************************************************************************************************"); - Serial.println(); + Serial.println(); + Serial.println("*****************************************************************************************************"); + Serial.println(" Offset calibration"); + Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor."); + Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second."); + Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin()."); + Serial.println("*****************************************************************************************************"); + Serial.println(); #ifdef AVR - Serial.print("Old offset value read from EEPROM="); - Serial.print((int16_t) eeprom_read_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET)); - Serial.println(" mm"); + Serial.print("Old offset value read from EEPROM="); + Serial.print((int16_t)eeprom_read_word((uint16_t *)EEPROM_ADDRESS_FOR_OFFSET)); + Serial.println(" mm"); #endif - if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init - Serial.println("Sensor online!"); + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + // Short mode max distance is limited to 1.3 m but has a better ambient immunity. + // Above 1.3 meter error 4 is thrown (wrap around). + distanceSensor.setDistanceModeShort(); + //distanceSensor.setDistanceModeLong(); // default + distanceSensor.setTimingBudgetInMs(50); + + int tLowDistanceCount = 0; + while (tLowDistanceCount < 20) + { + while (!distanceSensor.checkForDataReady()) + { + delay(1); } - - // Short mode max distance is limited to 1.3 m but has a better ambient immunity. - // Above 1.3 meter error 4 is thrown (wrap around). - distanceSensor.setDistanceModeShort(); - //distanceSensor.setDistanceModeLong(); // default - distanceSensor.setTimingBudgetInMs(50); - - int tLowDistanceCount = 0; - while (tLowDistanceCount < 20) { - while (!distanceSensor.checkForDataReady()) { - delay(1); - } - if (distanceSensor.getDistance() < 100) { - tLowDistanceCount++; - } else { - tLowDistanceCount = 0; - } - distanceSensor.clearInterrupt(); + if (distanceSensor.getDistance() < 100) + { + tLowDistanceCount++; } - - Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds"); -#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__) - tone(TONE_PIN, 1000, 100); - delay(1000); - tone(TONE_PIN, 1000, 100); - delay(1000); - tone(TONE_PIN, 1000, 100); - delay(1000); - tone(TONE_PIN, 1000, 100); - delay(1000); - tone(TONE_PIN, 1000, 900); - delay(1000); + else + { + tLowDistanceCount = 0; + } + distanceSensor.clearInterrupt(); + } + + Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds"); +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 900); + delay(1000); #else - delay(5000); + delay(5000); #endif - /* + /* * Place a target, 17 % gray, at a distance of 140 mm from the sensor and call the VL53L1X_CalibrateOffset (dev, 140, &offset) function. * The calibration may take a few seconds. The offset correction is applied to the sensor at the end of calibration. * * The calibration function takes 50 measurements and then takes the difference between the target distance * and the average distance and then calls setOffset() with this value. Thats all. No magic. */ - distanceSensor.calibrateOffset(140); - Serial.print("Result of offset calibration. RealDistance - MeasuredDistance="); - Serial.print(distanceSensor.getOffset()); - Serial.print(" mm"); - Serial.println(); + distanceSensor.calibrateOffset(140); + Serial.print("Result of offset calibration. RealDistance - MeasuredDistance="); + Serial.print(distanceSensor.getOffset()); + Serial.print(" mm"); + Serial.println(); #ifdef AVR - eeprom_write_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset()); - Serial.println("Offset value written to end of EEPROM."); + eeprom_write_word((uint16_t *)EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset()); + Serial.println("Offset value written to end of EEPROM."); #endif - // measure periodically. Intermeasurement period must be >/= timing budget. - distanceSensor.setIntermeasurementPeriod(100); - distanceSensor.startRanging(); // Start once + // measure periodically. Intermeasurement period must be >/= timing budget. + distanceSensor.setIntermeasurementPeriod(100); + distanceSensor.startRanging(); // Start once -#if ! defined (ESP32) && ! defined(ARDUINO_SAM_DUE) && ! defined(__SAM3X8E__) - tone(TONE_PIN, 1000, 900); +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(TONE_PIN, 1000, 900); #endif } -void loop(void) { - +void loop(void) +{ } - From 6c56d99efdb111ceb1ee8c97c5299bb7b814190f Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 17 May 2020 14:38:48 -0500 Subject: [PATCH 07/31] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 81ed7b9..c39b21e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.6 +version=1.2.7 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X From b709bd872bad1133d161dfa87f862488e21ac714 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 20 May 2020 17:26:00 +0200 Subject: [PATCH 08/31] Delete arduino-cli.yaml --- arduino-cli.yaml | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 arduino-cli.yaml diff --git a/arduino-cli.yaml b/arduino-cli.yaml deleted file mode 100644 index 98981f2..0000000 --- a/arduino-cli.yaml +++ /dev/null @@ -1,7 +0,0 @@ -board_manager: - additional_urls: - - http://digistump.com/package_digistump_index.json - - http://drazzy.com/package_drazzy.com_index.json - - http://arduino.esp8266.com/stable/package_esp8266com_index.json - - https://dl.espressif.com/dl/package_esp32_index.json - - https://github.com/stm32duino/BoardManagerFiles/raw/dev/STM32/package_stm_index.json From 88bf5da9cf68e17510c30496e7308e1580f70853 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 20 May 2020 17:33:19 +0200 Subject: [PATCH 09/31] Use action for compile --- .github/workflows/LibraryBuild.yml | 114 ++++++----------------------- 1 file changed, 23 insertions(+), 91 deletions(-) diff --git a/.github/workflows/LibraryBuild.yml b/.github/workflows/LibraryBuild.yml index 743b2d3..ff2ebc9 100644 --- a/.github/workflows/LibraryBuild.yml +++ b/.github/workflows/LibraryBuild.yml @@ -10,26 +10,26 @@ # This is the name of the workflow, visible on GitHub UI. name: LibraryBuild -on: [push, pull_request] # see: https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request +on: + push: # see: https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request + paths: + - '**.ino' + - '**.cpp' + - '**.h' + - '**LibraryBuild.yml' + pull_request: jobs: build: name: ${{ matrix.arduino-boards-fqbn }} - test compiling examples - runs-on: ubuntu-latest # I picked Ubuntu to use shell scripts. + runs-on: ubuntu-18.04 # I picked Ubuntu to use shell scripts. env: - # Space separated list without double quotes around the list. - # If you need a library with a space in its name, like Adafruit NeoPixel or Adafruit INA219, you must use double quotes - # around the name and have at least 2 entries, where the first must be without double quotes! You may use Servo as dummy entry. + # Comma separated list without double quotes around the list. + PLATFORM_DEFAULT_URL: https://raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json REQUIRED_LIBRARIES: EspSoftwareSerial - # Global color definitions for output colors - RED: '\033[0;31m' - GREEN: '\033[0;32m' - YELLOW: '\033[1;33m' - BLUE: '\033[0;34m' - strategy: matrix: # The matrix will produce one job for each configuration parameter of type `arduino-boards-fqbn` @@ -47,6 +47,7 @@ jobs: - arduino:avr:leonardo - arduino:avr:mega - arduino:sam:arduino_due_x + - SparkFun:apollo3:amap3redboard - esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 - esp32:esp32:featheresp32:FlashFreq=80 - STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 @@ -57,31 +58,17 @@ jobs: # You may exclude specific examples for a board with examples-exclude: Use a space separated list. ############################################################################################################# include: - - arduino-boards-fqbn: arduino:avr:uno - platform: arduino:avr - - - arduino-boards-fqbn: arduino:avr:leonardo - platform: arduino:avr - - - arduino-boards-fqbn: arduino:avr:mega - platform: arduino:avr - - arduino-boards-fqbn: arduino:sam:arduino_due_x - platform: arduino:sam examples-exclude: Example5_LCDDemo # No SoftwareSerial available. Space separated list of (unique substrings of) example names to exclude in build - arduino-boards-fqbn: esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 - platform: esp8266:esp8266 + platform-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json - arduino-boards-fqbn: esp32:esp32:featheresp32:FlashFreq=80 - platform: esp32:esp32 + platform-url: https://dl.espressif.com/dl/package_esp32_index.json - arduino-boards-fqbn: STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 - platform: STM32:stm32 - -###################################################### -# End of configuration, start of fixed script section -###################################################### + platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/dev/STM32/package_stm_index.json # Do not cancel all jobs / architectures if one job fails fail-fast: false @@ -91,68 +78,13 @@ jobs: # First of all, we clone the repo using the `checkout` action. - name: Checkout - uses: actions/checkout@master - - # We use the `arduino/setup-arduino-cli` action to install and - # configure the Arduino CLI on the system. - - name: Setup Arduino CLI - uses: arduino/setup-arduino-cli@v1.0.0 - - - name: Link this repository as Arduino library - run: | - mkdir -p $HOME/Arduino/libraries - ln -s $PWD $HOME/Arduino/libraries/. - - - name: Install platform from build matrix - env: - FQBN: ${{ matrix.arduino-boards-fqbn }} - run: | - arduino-cli core update-index - if [ "${{ matrix.platform }}" == "" ]; then echo -e ""$RED"ERROR: platform missing for board ${FQBN%|*}. Check your matrix.includes entries"; exit 1; fi - if [[ ${{ matrix.platform }} != *"arduino"* && ! -f ./arduino-cli.yaml ]]; then echo -e ""$RED"Non Arduino platform ${{ matrix.platform }} requested, but file arduino-cli.yaml is missing."; exit 1; fi - arduino-cli core install ${{ matrix.platform }} # for each job / board one platform is installed - arduino-cli board listall - if [ ${{ matrix.platform }} == "esp32:esp32" ]; then pip install pyserial; fi - - - name: List installed boards with their FQBN - run: | - arduino-cli board listall - # ls -l $HOME/.arduino15/packages/ # I see only arduino and one of the Attiny cores but not all 3 together - # echo -e HOME=\"$HOME\" # /home/runner - # echo PWD=$PWD # /home/runner/work/Github-Actions-Test/Github-Actions-Test - # which arduino-cli # /opt/hostedtoolcache/arduino-cli/0.9.0/x64/arduino-cli - - - name: Install libraries - run: if [[ "$REQUIRED_LIBRARIES" != "" ]]; then arduino-cli lib install ${{ env.REQUIRED_LIBRARIES }}; fi + uses: actions/checkout@v2 - # Finally, we compile the sketch, using the FQBN that was set in the build matrix. - name: Compile all examples - env: - FQBN: ${{ matrix.arduino-boards-fqbn }} - BUILD_PROPERTIES: ${{ toJson(matrix.examples-build-properties) }} - run: | - BUILD_PROPERTIES=${BUILD_PROPERTIES#\{} # remove "{" - # if matrix.examples-build-properties are specified, create an associative shell array - if [[ $BUILD_PROPERTIES != "null" ]]; then declare -A PROP_MAP="( $(echo $BUILD_PROPERTIES | sed -E 's/"(\w*)": *([^,}]*)[,}]/\[\1\]=\2/g' ) )"; fi - echo -e "Compiling examples for board ${{ matrix.arduino-boards-fqbn }} \n" - EXAMPLES=($(find . -name "*.ino")) - for example in "${EXAMPLES[@]}"; do # Loop over all example directories - EXAMPLE_NAME=$(basename $(dirname $example)) - if [[ "${{ matrix.examples-exclude }}" == *"$EXAMPLE_NAME"* ]]; then - echo -e "Skipping $EXAMPLE_NAME \xe2\x9e\x9e" # Right arrow - else - # check if there is an entry in the associative array and create a compile parameter - echo -n "Compiling $EXAMPLE_NAME " - if [[ "${PROP_MAP[$EXAMPLE_NAME]}" != "" ]]; then echo -n "with ${PROP_MAP[$EXAMPLE_NAME]} "; fi - build_stdout=$(arduino-cli compile --verbose --warnings all --fqbn ${FQBN%|*} --build-properties compiler.cpp.extra_flags="${PROP_MAP[$EXAMPLE_NAME]}" $(dirname $example) 2>&1); - if [ $? -ne 0 ]; then - echo -e ""$RED"\xe2\x9c\x96" # If ok output a green checkmark else a red X and the command output. - exit_code=1 - echo -e "$build_stdout \n" - else - echo -e ""$GREEN"\xe2\x9c\x93" - fi - fi - done - exit $exit_code - shell: bash {0} # Needed to avoid an exit at first error + uses: ArminJo/arduino-test-compile@v2 + with: + required-libraries: ${{ env.REQUIRED_LIBRARIES }} + arduino-board-fqbn: ${{ matrix.arduino-boards-fqbn }} + platform-default-url: ${{ env.PLATFORM_DEFAULT_URL }} + platform-url: ${{ matrix.platform-url }} + examples-exclude: ${{ matrix.examples-exclude }} From ef55ab360123b7d2c5c003c7e2af774aa5d97341 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 20 May 2020 19:46:50 +0200 Subject: [PATCH 10/31] added all apollo boards --- .github/workflows/LibraryBuild.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/LibraryBuild.yml b/.github/workflows/LibraryBuild.yml index ff2ebc9..1586da5 100644 --- a/.github/workflows/LibraryBuild.yml +++ b/.github/workflows/LibraryBuild.yml @@ -47,7 +47,13 @@ jobs: - arduino:avr:leonardo - arduino:avr:mega - arduino:sam:arduino_due_x + - SparkFun:apollo3:artemis - SparkFun:apollo3:amap3redboard + - SparkFun:apollo3:amap3nano + - SparkFun:apollo3:amap3atp + - SparkFun:apollo3:amap3thing + - SparkFun:apollo3:edge + - SparkFun:apollo3:edge2 - esp8266:esp8266:huzzah:eesz=4M3M,xtal=80 - esp32:esp32:featheresp32:FlashFreq=80 - STM32:stm32:GenF1:pnum=BLUEPILL_F103C8 From 0cf67496bbb55fda52516e3e8aaa6b5e209e0f47 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 20 May 2020 20:46:54 +0200 Subject: [PATCH 11/31] Try other pins for 2. serial --- examples/Example5_LCDDemo/Example5_LCDDemo.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/Example5_LCDDemo/Example5_LCDDemo.ino b/examples/Example5_LCDDemo/Example5_LCDDemo.ino index d98d5b7..6642303 100644 --- a/examples/Example5_LCDDemo/Example5_LCDDemo.ino +++ b/examples/Example5_LCDDemo/Example5_LCDDemo.ino @@ -28,6 +28,8 @@ SFEVL53L1X distanceSensor; #if defined(ESP8266) SoftwareSerial lcd(10, 9); // RX, TX +#elif defined(ARDUINO_ARCH_APOLLO3) +SoftwareSerial lcd(14, 15); // use RX1, TX1 of Apollo boards #else SoftwareSerial lcd(10, A3); // RX, TX #endif From 7b705e0b5d61b1b2a914820ef66c0497c934092f Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 20 May 2020 23:04:26 +0200 Subject: [PATCH 12/31] Bump version to 1.2.8 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index c39b21e..0bac1db 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.7 +version=1.2.8 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X From 065e29261bd5aaabd5306d4adfb7712cd054c1e2 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 24 May 2020 09:24:50 -0500 Subject: [PATCH 13/31] Reduce hard delay. Improve startup time for units that are already measuring. --- library.properties | 2 +- src/vl53l1x_class.cpp | 19 +++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/library.properties b/library.properties index 0bac1db..8d1786d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.8 +version=1.2.9 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index 1bcb8cb..4b05cef 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -166,7 +166,7 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SetI2CAddress(uint8_t new_address) VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit() { VL53L1X_ERROR status = 0; - uint8_t Addr = 0x00, tmp = 0, timeout = 0; + uint8_t Addr = 0x00, dataReady = 0, timeout = 0; for (Addr = 0x2D; Addr <= 0x87; Addr++) { @@ -174,20 +174,15 @@ VL53L1X_ERROR VL53L1X::VL53L1X_SensorInit() } status = VL53L1X_StartRanging(); - delay(103); //Wait the default intermeasurement period of 103ms before checking for dataready - - while (tmp == 0) + //We need to wait at least the default intermeasurement period of 103ms before dataready will occur + //But if a unit has already been powered and polling, it may happen much faster + while (dataReady == 0) { - status = VL53L1X_CheckForDataReady(&tmp); - timeout++; - if (timeout > 50) - { - status = VL53L1_ERROR_TIME_OUT; - return status; - } + status = VL53L1X_CheckForDataReady(&dataReady); + if (timeout++ > 150) + return VL53L1_ERROR_TIME_OUT; delay(1); } - tmp = 0; status = VL53L1X_ClearInterrupt(); status = VL53L1X_StopRanging(); status = VL53L1_WrByte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */ From 28ae87e2cdc1c06027fbf56c7dc66838bc01c8ca Mon Sep 17 00:00:00 2001 From: Joseph Duchesne Date: Sun, 11 Oct 2020 12:12:35 -0400 Subject: [PATCH 14/31] Added One-Shot Ranging trigger --- src/SparkFun_VL53L1X.cpp | 4 ++++ src/SparkFun_VL53L1X.h | 1 + src/vl53l1x_class.cpp | 10 +++++++++- src/vl53l1x_class.h | 5 +++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 39c7fa2..8b24de0 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -133,6 +133,10 @@ void SFEVL53L1X::startRanging() _device->VL53L1X_StartRanging(); } +void SFEVL53L1X::startOneshotRanging() { + _device->VL53L1X_StartOneshotRanging(); +} + void SFEVL53L1X::stopRanging() { _device->VL53L1X_StopRanging(); diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 712233d..423fdf9 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -47,6 +47,7 @@ class SFEVL53L1X void setInterruptPolarityLow(); //Set the polarity of an active interrupt to Low uint8_t getInterruptPolarity(); //get the current interrupt polarity void startRanging(); //Begins taking measurements + void startOneshotRanging(); // Start one-shot ranging void stopRanging(); //Stops taking measurements bool checkForDataReady(); //Checks the to see if data is ready void setTimingBudgetInMs(uint16_t timingBudget); //Set the timing budget for a measurement diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index 4b05cef..338d7b5 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -223,11 +223,19 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity) VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging() { VL53L1X_ERROR status = 0; - + // Consider: maybe also VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */ status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */ return status; } +VL53L1X_ERROR VL53L1X::VL53L1X_StartOneshotRanging() +{ + VL53L1X_ERROR status = 0; + VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */ + status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x10); /* Enable VL53L1X one-shot ranging */ + return status; +} + VL53L1X_ERROR VL53L1X::VL53L1X_StopRanging() { VL53L1X_ERROR status = 0; diff --git a/src/vl53l1x_class.h b/src/vl53l1x_class.h index 606aabf..458014b 100644 --- a/src/vl53l1x_class.h +++ b/src/vl53l1x_class.h @@ -304,6 +304,11 @@ class VL53L1X : public RangeSensor */ VL53L1X_ERROR VL53L1X_StartRanging(); + /** + * @brief This function starts a one-shot ranging distance operation\n + */ + VL53L1X_ERROR VL53L1X_StartOneshotRanging(); + /** * @brief This function stops the ranging. */ From 159c19e4855efd7ec4c88dfbe8a89264d83f9921 Mon Sep 17 00:00:00 2001 From: Elias Santistevan Date: Thu, 20 Jan 2022 09:45:12 -0700 Subject: [PATCH 15/31] Adds sensorID for an alternate part. --- src/SparkFun_VL53L1X.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 39c7fa2..85b6344 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -54,7 +54,7 @@ bool SFEVL53L1X::checkID() { uint16_t sensorId; _device->VL53L1X_GetSensorId(&sensorId); - if (sensorId == 0xEACC) + if (sensorId == 0xEACC | sensorId == 0xEBAA) return true; return false; } @@ -359,4 +359,4 @@ void SFEVL53L1X::calibrateXTalk(uint16_t targetDistanceInMm) { uint16_t xTalk = getXTalk(); _device->VL53L1X_CalibrateXtalk(targetDistanceInMm, &xTalk); -}; \ No newline at end of file +}; From 2ae2e9db73bcc9d897f3be656e8402d4b9668fcb Mon Sep 17 00:00:00 2001 From: Elias Santistevan Date: Thu, 20 Jan 2022 09:47:23 -0700 Subject: [PATCH 16/31] Rolls version --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 8d1786d..4042f76 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.9 +version=1.2.10 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X From 71314d15e76ccca91842c57b7c9c0f5f0e36c0e6 Mon Sep 17 00:00:00 2001 From: Elias Santistevan Date: Thu, 20 Jan 2022 10:03:14 -0700 Subject: [PATCH 17/31] Fixes bitwise OR to logical OR operator in sensorID check * rolls version --- library.properties | 2 +- src/SparkFun_VL53L1X.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library.properties b/library.properties index 4042f76..c3e0e97 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.10 +version=1.2.11 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 85b6344..52b1093 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -54,7 +54,7 @@ bool SFEVL53L1X::checkID() { uint16_t sensorId; _device->VL53L1X_GetSensorId(&sensorId); - if (sensorId == 0xEACC | sensorId == 0xEBAA) + if ( (sensorId == 0xEACC) || (sensorId == 0xEBAA) ) return true; return false; } From ce448e7b535b705098928c9ebd683f3f9b03f08c Mon Sep 17 00:00:00 2001 From: Ricardo Ramos <45825126+PPVJM@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:07:27 -0300 Subject: [PATCH 18/31] Added get and set detection threshold functionality. --- .../Example8_SetGetDetectionThresholds.ino | 126 ++++++++++++++++++ src/SparkFun_VL53L1X.cpp | 42 +++++- src/SparkFun_VL53L1X.h | 19 +++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino diff --git a/examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino b/examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino new file mode 100644 index 0000000..16bfbfb --- /dev/null +++ b/examples/Example8_SetGetDetectionThresholds/Example8_SetGetDetectionThresholds.ino @@ -0,0 +1,126 @@ +/* + Reading distance from the laser based VL53L1X + By: Nathan Seidle + Revised by: Andy England and Ricardo Ramos + SparkFun Electronics + Date: January 21st, 2022 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example sets high and low thresholds for detection. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. +*/ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + DetectionConfig dc; // struct instance which holds the detection configuration + dc.IntOnNoTarget = 1; // No longer used - just use 1 per ST + dc.distanceMode = DISTANCE_SHORT; // short distance mode + dc.thresholdHigh = 300; // high threshold of 300 mm + dc.thresholdLow = 70; // low threshold of 70 mm + dc.windowMode = WINDOW_IN; // will measure and trigger interrrupt when measurement fall between 70 and 300 mm + + if(distanceSensor.setThresholdConfig(&dc) == true) + { + Serial.println("Thresholds programmed. Reading configuration back..."); + dc = {}; + if(distanceSensor.getThresholdConfig(&dc) == true) + { + Serial.print("IntOnNoTarget: "); + Serial.println(dc.IntOnNoTarget); + Serial.print("Distance Mode: "); + + if(dc.distanceMode == DISTANCE_SHORT) + Serial.println("DISTANCE_SHORT (0)"); + else + Serial.println("DISTANCE_LONG (1)"); + + Serial.print("Threshold High: "); + Serial.println(dc.thresholdHigh); + Serial.print("Threshold Low: "); + Serial.println(dc.thresholdLow); + Serial.print("Window mode: "); + switch (dc.windowMode) + { + case WINDOW_BELOW: + Serial.println("WINDOW_BELOW (0)"); + break; + + case WINDOW_ABOVE: + Serial.println("WINDOW_ABOVE (1)"); + break; + + case WINDOW_OUT: + Serial.println("WINDOW_OUT (2)"); + break; + + case WINDOW_IN: + Serial.println("WINDOW_IN (3)"); + break; + + default: + break; + } + } + } + else + { + Serial.println("Could not set threshold configuration."); + } + Serial.println("Starting measurements in 2 seconds."); + delay(2000); + Serial.println("Measurements started!"); + +} + +void loop(void) +{ + distanceSensor.startRanging(); //Write configuration bytes to initiate measurement + + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + + distanceSensor.stopRanging(); + + Serial.print("Distance(mm): "); + Serial.print(distance); + + float distanceInches = distance * 0.0393701; + float distanceFeet = distanceInches / 12.0; + + Serial.print("\tDistance(ft): "); + Serial.print(distanceFeet, 2); + + Serial.println(); +} diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 52b1093..f03d4f3 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -54,7 +54,7 @@ bool SFEVL53L1X::checkID() { uint16_t sensorId; _device->VL53L1X_GetSensorId(&sensorId); - if ( (sensorId == 0xEACC) || (sensorId == 0xEBAA) ) + if ((sensorId == 0xEACC) || (sensorId == 0xEBAA)) return true; return false; } @@ -360,3 +360,43 @@ void SFEVL53L1X::calibrateXTalk(uint16_t targetDistanceInMm) uint16_t xTalk = getXTalk(); _device->VL53L1X_CalibrateXtalk(targetDistanceInMm, &xTalk); }; + +bool SFEVL53L1X::setThresholdConfig(DetectionConfig *config) +{ + VL53L1X_ERROR error = _device->VL53L1X_SetDistanceThreshold(config->thresholdLow, config->thresholdHigh, + (uint8_t)config->windowMode, (uint8_t)config->IntOnNoTarget); + return (error == VL53L1_ERROR_NONE); +} + +bool SFEVL53L1X::getThresholdConfig(DetectionConfig *config) +{ + uint16_t temp16 = 0; + + VL53L1X_ERROR error = _device->VL53L1X_GetDistanceMode(&temp16); + if (error != 0) + return false; + else + config->distanceMode = temp16; + + error = _device->VL53L1X_GetDistanceThresholdWindow(&temp16); + if (error != 0) + return false; + else + config->windowMode = temp16; + + config->IntOnNoTarget = 1; + + error = _device->VL53L1X_GetDistanceThresholdLow(&temp16); + if (error != 0) + return false; + else + config->thresholdLow = temp16; + + error = _device->VL53L1X_GetDistanceThresholdHigh(&temp16); + if (error != 0) + return false; + else + config->thresholdHigh = temp16; + + return true; +} \ No newline at end of file diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 712233d..4334533 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -30,6 +30,22 @@ #include "vl53l1_error_codes.h" #include "vl53l1x_class.h" +#define DISTANCE_SHORT 0 +#define DISTANCE_LONG 1 +#define WINDOW_BELOW 0 +#define WINDOW_ABOVE 1 +#define WINDOW_OUT 2 +#define WINDOW_IN 3 + +struct DetectionConfig +{ + uint16_t distanceMode = DISTANCE_SHORT; // distance mode : DISTANCE_SHORT (0) or DISTANCE_LONG (1) + uint16_t windowMode = WINDOW_IN; // window mode : WINDOW_BELOW (0), WINDOW_ABOVE (1), WINDOW_OUT (2), WINDOW_IN (3) + uint8_t IntOnNoTarget = 1; // = 1 (No longer used - just use 1) + uint16_t thresholdHigh = 0; // (in mm) : the threshold above which one the device raises an interrupt if Window = 1 + uint16_t thresholdLow = 0; // (in mm) : the threshold under which one the device raises an interrupt if Window = 0 +}; + class SFEVL53L1X { public: @@ -108,6 +124,9 @@ class SFEVL53L1X void startTemperatureUpdate(); //Recalibrates the sensor for temperature changes. Run this any time the temperature has changed by more than 8°C void calibrateOffset(uint16_t targetDistanceInMm); //Autocalibrate the offset by placing a target a known distance away from the sensor and passing this known distance into the function. void calibrateXTalk(uint16_t targetDistanceInMm); //Autocalibrate the crosstalk by placing a target a known distance away from the sensor and passing this known distance into the function. + bool setThresholdConfig(DetectionConfig* config); //Sets threshold configuration struct + bool getThresholdConfig(DetectionConfig* config); //Gets current threshold settings. Returns false on error, true otherwise. Stores results in pointer to struct argument. + private: TwoWire *_i2cPort; int _shutdownPin; From 83195ac5d7f43c76c494e3fab6ec7d9e9f6782d7 Mon Sep 17 00:00:00 2001 From: Ricardo Ramos <45825126+PPVJM@users.noreply.github.com> Date: Mon, 31 Jan 2022 18:16:49 -0300 Subject: [PATCH 19/31] Merged pull request and added One Shot example to library. --- .../Example9_OneShotMeasurement.ino | 66 +++++++++++++++++++ src/SparkFun_VL53L1X.cpp | 5 ++ src/SparkFun_VL53L1X.h | 1 + src/vl53l1x_class.cpp | 16 ++++- src/vl53l1x_class.h | 5 ++ 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino diff --git a/examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino b/examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino new file mode 100644 index 0000000..37ae7e1 --- /dev/null +++ b/examples/Example9_OneShotMeasurement/Example9_OneShotMeasurement.ino @@ -0,0 +1,66 @@ +/* + Reading distance from the laser based VL53L1X using a one-shot measurement. + By: Nathan Seidle + Revised by: Andy England, Ricardo Ramos + Pull request by: Joseph Duchesne + SparkFun Electronics + Date: January 31st, 2022 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example makes a one shot measurement. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. +*/ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + +} + +void loop(void) +{ + distanceSensor.startOneshotRanging(); //Write configuration bytes to initiate measurement + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + + Serial.print("Distance(mm): "); + Serial.print(distance); + + float distanceInches = distance * 0.0393701; + float distanceFeet = distanceInches / 12.0; + + Serial.print("\tDistance(ft): "); + Serial.print(distanceFeet, 2); + + Serial.println(); +} diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index f03d4f3..9cdaa04 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -133,6 +133,11 @@ void SFEVL53L1X::startRanging() _device->VL53L1X_StartRanging(); } +void SFEVL53L1X::startOneshotRanging() +{ + _device->VL53L1X_StartOneshotRanging(); +} + void SFEVL53L1X::stopRanging() { _device->VL53L1X_StopRanging(); diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 4334533..b0a4834 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -63,6 +63,7 @@ class SFEVL53L1X void setInterruptPolarityLow(); //Set the polarity of an active interrupt to Low uint8_t getInterruptPolarity(); //get the current interrupt polarity void startRanging(); //Begins taking measurements + void startOneshotRanging(); // Start one-shot ranging void stopRanging(); //Stops taking measurements bool checkForDataReady(); //Checks the to see if data is ready void setTimingBudgetInMs(uint16_t timingBudget); //Set the timing budget for a measurement diff --git a/src/vl53l1x_class.cpp b/src/vl53l1x_class.cpp index 4b05cef..4d161aa 100644 --- a/src/vl53l1x_class.cpp +++ b/src/vl53l1x_class.cpp @@ -223,11 +223,21 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity) VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging() { VL53L1X_ERROR status = 0; - + VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */ status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */ return status; } + +VL53L1X_ERROR VL53L1X::VL53L1X_StartOneshotRanging() +{ + VL53L1X_ERROR status = 0; + VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */ + status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x10); /* Enable VL53L1X one-shot ranging */ + return status; +} + + VL53L1X_ERROR VL53L1X::VL53L1X_StopRanging() { VL53L1X_ERROR status = 0; @@ -963,7 +973,7 @@ VL53L1X_ERROR VL53L1X::VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t * status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 4); if (!status) { - *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; + *data = (buffer[0] << 24) + (buffer[1] << 16U) + (buffer[2] << 8) + buffer[3]; } return status; } @@ -998,7 +1008,7 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr buffer[0] = RegisterAddr >> 8; buffer[1] = RegisterAddr & 0xFF; dev_i2c->write(buffer, 2); - for (int i = 0; i < NumByteToWrite; i++) + for (uint16_t i = 0; i < NumByteToWrite; i++) dev_i2c->write(pBuffer[i]); dev_i2c->endTransmission(true); diff --git a/src/vl53l1x_class.h b/src/vl53l1x_class.h index 606aabf..458014b 100644 --- a/src/vl53l1x_class.h +++ b/src/vl53l1x_class.h @@ -304,6 +304,11 @@ class VL53L1X : public RangeSensor */ VL53L1X_ERROR VL53L1X_StartRanging(); + /** + * @brief This function starts a one-shot ranging distance operation\n + */ + VL53L1X_ERROR VL53L1X_StartOneshotRanging(); + /** * @brief This function stops the ranging. */ From 5481a09ce552d0b0e3a4af9d1e2638c07cd72a25 Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Tue, 1 Feb 2022 10:50:59 +1000 Subject: [PATCH 20/31] Fix setThresholdConfig and incorrect distance mode definitions --- src/SparkFun_VL53L1X.cpp | 10 +++++----- src/SparkFun_VL53L1X.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 9cdaa04..459602d 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -164,12 +164,12 @@ uint16_t SFEVL53L1X::getTimingBudgetInMs() void SFEVL53L1X::setDistanceModeLong() { - _device->VL53L1X_SetDistanceMode(2); + _device->VL53L1X_SetDistanceMode(DISTANCE_LONG); } void SFEVL53L1X::setDistanceModeShort() { - _device->VL53L1X_SetDistanceMode(1); + _device->VL53L1X_SetDistanceMode(DISTANCE_SHORT); } uint8_t SFEVL53L1X::getDistanceMode() @@ -368,9 +368,9 @@ void SFEVL53L1X::calibrateXTalk(uint16_t targetDistanceInMm) bool SFEVL53L1X::setThresholdConfig(DetectionConfig *config) { - VL53L1X_ERROR error = _device->VL53L1X_SetDistanceThreshold(config->thresholdLow, config->thresholdHigh, - (uint8_t)config->windowMode, (uint8_t)config->IntOnNoTarget); - return (error == VL53L1_ERROR_NONE); + return _device->VL53L1X_SetDistanceMode(config->distanceMode) == VL53L1_ERROR_NONE && + _device->VL53L1X_SetDistanceThreshold(config->thresholdLow, config->thresholdHigh, + (uint8_t)config->windowMode, (uint8_t)config->IntOnNoTarget) == VL53L1_ERROR_NONE; } bool SFEVL53L1X::getThresholdConfig(DetectionConfig *config) diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index b0a4834..08b6ae6 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -30,8 +30,8 @@ #include "vl53l1_error_codes.h" #include "vl53l1x_class.h" -#define DISTANCE_SHORT 0 -#define DISTANCE_LONG 1 +#define DISTANCE_SHORT 1 +#define DISTANCE_LONG 2 #define WINDOW_BELOW 0 #define WINDOW_ABOVE 1 #define WINDOW_OUT 2 From ebe6a227d220691084faedeccf5a1a293487c9fd Mon Sep 17 00:00:00 2001 From: "Ho Yun \"Bobby" Date: Tue, 15 Feb 2022 19:44:34 -0700 Subject: [PATCH 21/31] Update README.md --- README.md | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0c524f8..41ad203 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,30 @@ SparkFun Qwiic 4m Distance Sensor with VL53L1X ======================================== [![Build Status](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/workflows/LibraryBuild/badge.svg)](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/actions) + -![SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic)](https://cdn.sparkfun.com//assets/parts/1/2/9/4/8/14722-SparkFun_Distance_Sensor_Breakout-_4_Meter__VL53L1X__Qwiic_-01.jpg) + + + + + + + + + +
SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic)SparkFun Distance Sensor - 1.3 Meter, VL53L4CD (Qwiic)
SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic) [SEN-14722]SparkFun Distance Sensor - 1.3 Meter, VL53L4CD (Qwiic)[SEN-18993]
-[*SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic)(SEN-14722)*](https://www.sparkfun.com/products/14722) +The VL53L1X is a Time Of Flight (ToF) sensor that use a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser and time the reflection to the target. What does all this mean? Using the VL53L1X, you can measure the distance to an object up to 4 meters away with millimeter resolution! That’s pretty incredible. -The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser and time the reflection to the target. What does all this mean? You can measure the distance to an object up to 4 meters away with millimeter resolution! That’s pretty incredible. +We’re far from done: The VL53L1X is a highly complex sensor with a multitude of options and configurations. We’ve written example sketches that allow you to read the distance, signal rate, and range status. Because STMicroelectronics has chosen not to release a complete datasheet we are forced to reverse engineer the interface from their example code and I2C data stream captures. If you’re into puzzles we could use your help to make the library better! -We’re far from done: The VL53L1X is a highly complex sensor with a multitude of options and configurations. We’ve written example sketches that allow you to read the distance, signal rate, and range status. Because ST has chosen not to release a complete datasheet we are forced to reverse engineer the interface from their example code and I2C data stream captures. If you’re into puzzles we could use your help to make the library better! - -We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm. +We’ve found the precision of VL53L1X sensor to be 1mm but the accuracy is around +/-5mm. SparkFun labored with love to create this code. Feel like supporting open source hardware? Buy a [breakout board](https://www.sparkfun.com/products/14722) from SparkFun! +*Note: The VL53L1X is the cousin of VL53L1X. Overall, the sensor functions the same except for a few differences in the specifications. We've also found the precision of VL53L4CD sensor to be 1mm but the accuracy is around +/-7mm (white target: 88%, indoor, no infrared).* + Repository Contents ------------------- @@ -25,12 +35,15 @@ Repository Contents Documentation -------------- -* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide)** +* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. +* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide)**- Basic tutorial for the VL53L1X and VL53L4CD -Product Versions +Products that use this Library -------------- -* **[SEN-14722](https://www.sparkfun.com/products/14722)** - SparkFun red version -* **[SPX-14667](https://www.sparkfun.com/products/14667)** - SparkX Version + +* **[SEN-18993](https://www.sparkfun.com/products/18993)** - SparkFun red version for VL53L4CD +* **[SEN-14722](https://www.sparkfun.com/products/14722)** - SparkFun red version for VL53L1X +* **[SPX-14667](https://www.sparkfun.com/products/14667)** - SparkX Version for VL53L1X License Information ------------------- From da0023513b5dbd7b3045ed3ff200010f1102d966 Mon Sep 17 00:00:00 2001 From: "Ho Yun \"Bobby" Date: Tue, 15 Feb 2022 19:45:02 -0700 Subject: [PATCH 22/31] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 41ad203..254fa83 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ SparkFun Qwiic 4m Distance Sensor with VL53L1X ======================================== [![Build Status](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/workflows/LibraryBuild/badge.svg)](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/actions) - @@ -24,7 +23,7 @@ We’ve found the precision of VL53L1X sensor to be 1mm but the accuracy is arou SparkFun labored with love to create this code. Feel like supporting open source hardware? Buy a [breakout board](https://www.sparkfun.com/products/14722) from SparkFun! -*Note: The VL53L1X is the cousin of VL53L1X. Overall, the sensor functions the same except for a few differences in the specifications. We've also found the precision of VL53L4CD sensor to be 1mm but the accuracy is around +/-7mm (white target: 88%, indoor, no infrared).* +*Note: The VL53L4CD is the cousin of VL53L1X. Overall, the sensor functions the same except for a few differences in the specifications. We've also found the precision of VL53L4CD sensor to be 1mm but the accuracy is around +/-7mm (white target: 88%, indoor, no infrared).* Repository Contents ------------------- From 736ce4b1a138795b02dfd221e04280ae0f10cb59 Mon Sep 17 00:00:00 2001 From: Elias Santistevan Date: Tue, 1 Mar 2022 10:43:36 -0700 Subject: [PATCH 23/31] Updates SparkFun written source files to include MIT license header --- src/SparkFun_VL53L1X.cpp | 22 +++++++++++++++------- src/SparkFun_VL53L1X.h | 22 +++++++++++++++------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 459602d..efaeb35 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -13,13 +13,21 @@ Development environment specifics: Arduino IDE 1.8.1 - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + ==== MIT License ==== + Copyright © 2022 SparkFun Electronics - You should have received a copy of the GNU General Public License - along with this program. If not, see . + Permission is hereby granted, free of charge, to any person obtaining a copy of this + software and associated documentation files (the “Software”), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS + OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ===================== */ #include @@ -404,4 +412,4 @@ bool SFEVL53L1X::getThresholdConfig(DetectionConfig *config) config->thresholdHigh = temp16; return true; -} \ No newline at end of file +} diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 08b6ae6..aa219c4 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -13,13 +13,21 @@ Development environment specifics: Arduino IDE 1.8.1 - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + ==== MIT License ==== + Copyright © 2022 SparkFun Electronics - You should have received a copy of the GNU General Public License - along with this program. If not, see . + Permission is hereby granted, free of charge, to any person obtaining a copy of this + software and associated documentation files (the “Software”), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS + OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ===================== */ #pragma once @@ -134,4 +142,4 @@ class SFEVL53L1X int _interruptPin; int _i2cAddress = 0x52; VL53L1X* _device; -}; \ No newline at end of file +}; From 1624364a7f42e4ad87c711f6edc2bbb916bc1e5c Mon Sep 17 00:00:00 2001 From: "Ho Yun \"Bobby" Date: Tue, 1 Mar 2022 10:58:10 -0700 Subject: [PATCH 24/31] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 254fa83..ed3f5c2 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Repository Contents Documentation -------------- * **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. -* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide)**- Basic tutorial for the VL53L1X and VL53L4CD +* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-vl53l4cd-hookup-guide)**- Basic tutorial for the VL53L1X and VL53L4CD Products that use this Library -------------- From 87f8575f32a130b38366f9a2cc982f957d2035bb Mon Sep 17 00:00:00 2001 From: Elias Santistevan Date: Tue, 1 Mar 2022 13:41:46 -0700 Subject: [PATCH 25/31] Moves all of STMicroelectronics source files to their own subfolder * Updated source file pathing in the source files that needed to be changed * Updates examples that were unnecessarily including files that should probably just be included in SparkFun's source files * Moves BSD_3 License to the "st_src" subfolder to reflect the correct licensing for those source files * Adds MIT license to the main repository directory to reflect SparkFun's software contribution with the correct license * Updates the README to reflect the licensing updates --- LICENSE.md | 31 ++++++------------- README.md | 4 ++- .../Example1_ReadDistance.ino | 6 ---- .../Example7_Calibration.ino | 8 ----- src/SparkFun_VL53L1X.cpp | 2 -- src/SparkFun_VL53L1X.h | 8 +++-- src/{ => st_src}/ComponentObject.h | 0 src/st_src/LICENSE.md | 23 ++++++++++++++ src/{ => st_src}/RangeSensor.h | 2 +- src/{ => st_src}/vl53l1_error_codes.h | 0 src/{ => st_src}/vl53l1x_class.cpp | 0 src/{ => st_src}/vl53l1x_class.h | 0 12 files changed, 42 insertions(+), 42 deletions(-) rename src/{ => st_src}/ComponentObject.h (100%) create mode 100644 src/st_src/LICENSE.md rename src/{ => st_src}/RangeSensor.h (98%) rename src/{ => st_src}/vl53l1_error_codes.h (100%) rename src/{ => st_src}/vl53l1x_class.cpp (100%) rename src/{ => st_src}/vl53l1x_class.h (100%) diff --git a/LICENSE.md b/LICENSE.md index 5c9ff4a..1105cff 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,23 +1,12 @@ -COPYRIGHT(c) 2018 STMicroelectronics +Copyright © 2022 SparkFun Electronics -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - 3. Neither the name of STMicroelectronics nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), +to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/README.md b/README.md index ed3f5c2..2721f22 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Products that use this Library License Information ------------------- -This product is _**open source**_! +SparkFun's source files are _**open source**_! Please review the LICENSE.md file for license information. @@ -57,4 +57,6 @@ Please use, reuse, and modify these files as you see fit. Please maintain attrib Distributed as-is; no warranty is given. +The source files included in the subfolder **st_src** are licensed differently. They are licensed under the BSD-3 license, check the License.md in +that subfolder for specifics. - Your friends at SparkFun. diff --git a/examples/Example1_ReadDistance/Example1_ReadDistance.ino b/examples/Example1_ReadDistance/Example1_ReadDistance.ino index 2af66ad..24a6c17 100644 --- a/examples/Example1_ReadDistance/Example1_ReadDistance.ino +++ b/examples/Example1_ReadDistance/Example1_ReadDistance.ino @@ -1,9 +1,3 @@ -#include -#include -#include -#include -#include - /* Reading distance from the laser based VL53L1X By: Nathan Seidle diff --git a/examples/Example7_Calibration/Example7_Calibration.ino b/examples/Example7_Calibration/Example7_Calibration.ino index c06f220..d960a5d 100644 --- a/examples/Example7_Calibration/Example7_Calibration.ino +++ b/examples/Example7_Calibration/Example7_Calibration.ino @@ -1,11 +1,3 @@ -#include - -#include -#include -#include -#include -#include - /* Calling distance offset calibration for the laser based VL53L1X By: Armin Joachimsmeyer diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index efaeb35..b540436 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -31,9 +31,7 @@ */ #include -#include "Arduino.h" #include "SparkFun_VL53L1X.h" -#include "vl53l1x_class.h" SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) { diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index aa219c4..20fb682 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -34,9 +34,11 @@ #include "Arduino.h" #include "Wire.h" -#include "RangeSensor.h" -#include "vl53l1_error_codes.h" -#include "vl53l1x_class.h" +#include "st_src/RangeSensor.h" +#include "st_src/vl53l1_error_codes.h" +#include "st_src/vl53l1x_class.h" +#include "st_src/ComponentObject.h" +#include "st_src/RangeSensor.h" #define DISTANCE_SHORT 1 #define DISTANCE_LONG 2 diff --git a/src/ComponentObject.h b/src/st_src/ComponentObject.h similarity index 100% rename from src/ComponentObject.h rename to src/st_src/ComponentObject.h diff --git a/src/st_src/LICENSE.md b/src/st_src/LICENSE.md new file mode 100644 index 0000000..5c9ff4a --- /dev/null +++ b/src/st_src/LICENSE.md @@ -0,0 +1,23 @@ +COPYRIGHT(c) 2018 STMicroelectronics + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + 3. Neither the name of STMicroelectronics nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/RangeSensor.h b/src/st_src/RangeSensor.h similarity index 98% rename from src/RangeSensor.h rename to src/st_src/RangeSensor.h index 119112e..0643268 100644 --- a/src/RangeSensor.h +++ b/src/st_src/RangeSensor.h @@ -41,7 +41,7 @@ #define __RANGE_SENSOR_CLASS_H /* Includes ------------------------------------------------------------------*/ -#include +#include /* Classes ------------------------------------------------------------------*/ /** An abstract class for range sensors diff --git a/src/vl53l1_error_codes.h b/src/st_src/vl53l1_error_codes.h similarity index 100% rename from src/vl53l1_error_codes.h rename to src/st_src/vl53l1_error_codes.h diff --git a/src/vl53l1x_class.cpp b/src/st_src/vl53l1x_class.cpp similarity index 100% rename from src/vl53l1x_class.cpp rename to src/st_src/vl53l1x_class.cpp diff --git a/src/vl53l1x_class.h b/src/st_src/vl53l1x_class.h similarity index 100% rename from src/vl53l1x_class.h rename to src/st_src/vl53l1x_class.h From 94d0162af3de230dbcf354376d7cee68c53f32e0 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Sun, 15 May 2022 17:23:08 +0100 Subject: [PATCH 26/31] Add alternate sensor ID 0xEAAA --- src/SparkFun_VL53L1X.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index b540436..34eaa65 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -60,7 +60,7 @@ bool SFEVL53L1X::checkID() { uint16_t sensorId; _device->VL53L1X_GetSensorId(&sensorId); - if ((sensorId == 0xEACC) || (sensorId == 0xEBAA)) + if ((sensorId == 0xEACC) || (sensorId == 0xEBAA) || (sensorId == 0xEAAA)) return true; return false; } From c4982b892e0a9d27fae0c69cc7d1ca953e1af586 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Sun, 15 May 2022 17:38:03 +0100 Subject: [PATCH 27/31] Revert "Add alternate sensor ID 0xEAAA" This reverts commit 94d0162af3de230dbcf354376d7cee68c53f32e0. --- src/SparkFun_VL53L1X.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 34eaa65..b540436 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -60,7 +60,7 @@ bool SFEVL53L1X::checkID() { uint16_t sensorId; _device->VL53L1X_GetSensorId(&sensorId); - if ((sensorId == 0xEACC) || (sensorId == 0xEBAA) || (sensorId == 0xEAAA)) + if ((sensorId == 0xEACC) || (sensorId == 0xEBAA)) return true; return false; } From 2088a8909648bad374f29dccfc64bbf8016561e5 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 12 Oct 2022 14:25:56 +0100 Subject: [PATCH 28/31] Add begin(TwoWire &i2cPort) - requires VL53L1X::dev_i2c to be public --- library.properties | 2 +- src/SparkFun_VL53L1X.cpp | 40 ++++++++++++++++++++++++++++++++++++++ src/SparkFun_VL53L1X.h | 7 ++++++- src/st_src/vl53l1x_class.h | 5 ++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index c3e0e97..36652fc 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.11 +version=1.2.12 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index b540436..38bb6d2 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -33,6 +33,27 @@ #include #include "SparkFun_VL53L1X.h" +SFEVL53L1X::SFEVL53L1X() +{ + _i2cPort = &Wire; + _shutdownPin = -1; + _interruptPin = -1; + _device = new VL53L1X(&Wire, -1, -1); +} +SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort) +{ + _i2cPort = &i2cPort; + _shutdownPin = -1; + _interruptPin = -1; + _device = new VL53L1X(&i2cPort, -1, -1); +} +SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin) +{ + _i2cPort = &i2cPort; + _shutdownPin = shutdownPin; + _interruptPin = -1; + _device = new VL53L1X(&i2cPort, shutdownPin, -1); +} SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) { _i2cPort = &i2cPort; @@ -54,6 +75,25 @@ bool SFEVL53L1X::begin() return _device->VL53L1X_SensorInit(); } +bool SFEVL53L1X::begin(TwoWire &i2cPort) +{ + _i2cPort = &i2cPort; + _device->dev_i2c = &i2cPort; + + if (checkID() == false) + { + Serial.println("Check ID failed..."); + return (VL53L1_ERROR_PLATFORM_SPECIFIC_START); + } + + bool result = _device->VL53L1X_SensorInit() == 0; + + if (!result) + Serial.println("Init failed..."); + + return result; +} + /*Checks the ID of the device, returns true if ID is correct*/ bool SFEVL53L1X::checkID() diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 20fb682..7ae3323 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -59,9 +59,14 @@ struct DetectionConfig class SFEVL53L1X { public: - SFEVL53L1X(TwoWire &i2cPort = Wire, int shutdownPin = -1, int interruptPin = -1); //Constructs our Distance sensor without an interrupt or shutdown pin + //Constructs our Distance sensor + SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined). + SFEVL53L1X(TwoWire &i2cPort); // Set both pins to -1 (undefined). + SFEVL53L1X(TwoWire &i2cPort, int shutdownPin); // Set interrupt pin to -1 (undefined). + SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin); bool init(); //Deprecated version of begin bool begin(); //Initialization of sensor + bool begin(TwoWire &i2cPort); //Initialization of sensor bool checkID(); //Check the ID of the sensor, returns true if ID is correct void sensorOn(); //Toggles shutdown pin to turn sensor on and off void sensorOff(); //Toggles shutdown pin to turn sensor on and off diff --git a/src/st_src/vl53l1x_class.h b/src/st_src/vl53l1x_class.h index 458014b..0af5eea 100644 --- a/src/st_src/vl53l1x_class.h +++ b/src/st_src/vl53l1x_class.h @@ -553,10 +553,13 @@ class VL53L1X : public RangeSensor - protected: + public: /* IO Device */ TwoWire *dev_i2c; + + protected: + /* Digital out pin */ int gpio0; int gpio1Int; From 2a4660a4f6a37c2889c5fd4580f34f2f6b57869e Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 12 Oct 2022 14:52:49 +0100 Subject: [PATCH 29/31] Remove unnecessary overloads --- src/SparkFun_VL53L1X.cpp | 14 -------------- src/SparkFun_VL53L1X.h | 4 +--- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 38bb6d2..980d771 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -40,20 +40,6 @@ SFEVL53L1X::SFEVL53L1X() _interruptPin = -1; _device = new VL53L1X(&Wire, -1, -1); } -SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort) -{ - _i2cPort = &i2cPort; - _shutdownPin = -1; - _interruptPin = -1; - _device = new VL53L1X(&i2cPort, -1, -1); -} -SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin) -{ - _i2cPort = &i2cPort; - _shutdownPin = shutdownPin; - _interruptPin = -1; - _device = new VL53L1X(&i2cPort, shutdownPin, -1); -} SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) { _i2cPort = &i2cPort; diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 7ae3323..ef2dcc6 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -61,9 +61,7 @@ class SFEVL53L1X public: //Constructs our Distance sensor SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined). - SFEVL53L1X(TwoWire &i2cPort); // Set both pins to -1 (undefined). - SFEVL53L1X(TwoWire &i2cPort, int shutdownPin); // Set interrupt pin to -1 (undefined). - SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin); + SFEVL53L1X(TwoWire &i2cPort, int shutdownPin = -1, int interruptPin = -1); bool init(); //Deprecated version of begin bool begin(); //Initialization of sensor bool begin(TwoWire &i2cPort); //Initialization of sensor From 832c0b0467cbde249a74d1d839424d919194f185 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 12 Oct 2022 16:46:00 +0100 Subject: [PATCH 30/31] Add destructor. Correct begin overload --- src/SparkFun_VL53L1X.cpp | 17 +++++------------ src/SparkFun_VL53L1X.h | 1 + 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 980d771..4ea1cb4 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -47,6 +47,10 @@ SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) _interruptPin = interruptPin; _device = new VL53L1X(&i2cPort, shutdownPin, interruptPin); } +SFEVL53L1X::~SFEVL53L1X() +{ + delete (VL53L1X *)_device; +} bool SFEVL53L1X::init() { @@ -66,18 +70,7 @@ bool SFEVL53L1X::begin(TwoWire &i2cPort) _i2cPort = &i2cPort; _device->dev_i2c = &i2cPort; - if (checkID() == false) - { - Serial.println("Check ID failed..."); - return (VL53L1_ERROR_PLATFORM_SPECIFIC_START); - } - - bool result = _device->VL53L1X_SensorInit() == 0; - - if (!result) - Serial.println("Init failed..."); - - return result; + return begin(); } /*Checks the ID of the device, returns true if ID is correct*/ diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index ef2dcc6..fbb940a 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -62,6 +62,7 @@ class SFEVL53L1X //Constructs our Distance sensor SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined). SFEVL53L1X(TwoWire &i2cPort, int shutdownPin = -1, int interruptPin = -1); + ~SFEVL53L1X(); bool init(); //Deprecated version of begin bool begin(); //Initialization of sensor bool begin(TwoWire &i2cPort); //Initialization of sensor From e88f55d6d82a4a8c519ec90a2660e6ec799b4579 Mon Sep 17 00:00:00 2001 From: SFEMark Date: Thu, 16 Nov 2023 15:59:12 -0700 Subject: [PATCH 31/31] Create add_issue_to_project.yml Adding .yml file for issue tracking in GH projects. --- .github/workflows/add_issue_to_project.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/add_issue_to_project.yml diff --git a/.github/workflows/add_issue_to_project.yml b/.github/workflows/add_issue_to_project.yml new file mode 100644 index 0000000..6b60cc3 --- /dev/null +++ b/.github/workflows/add_issue_to_project.yml @@ -0,0 +1,18 @@ +name: Add new issue to our main project + +on: + issues: + types: + - opened + +jobs: + add-to-project: + name: Add issue to project + runs-on: ubuntu-latest + steps: + - uses: actions/add-to-project@main + with: + # You can target a project in a different organization + # to the issue + project-url: https://github.com/orgs/sparkfun/projects/19 + github-token: ${{ secrets.DEFECT_ADD_TO_PROJECT }}