Skip to content

distance: allow both VL53L4CD and VL53L4ED #10

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
- source-path: ./
# Install library dependencies.
- name: STM32duino VL53L4CD
- name: STM32duino VL53L4ED
- name: Arduino_LSM6DSOX
- name: Arduino_LPS22HB
- name: Arduino_HS300x
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ category=Communication
url=https://github.com/arduino-libraries/Modulino
architectures=*
includes=Modulino.h
depends=STM32duino VL53L4CD,Arduino_LSM6DSOX,Arduino_LPS22HB,Arduino_HS300x
depends=STM32duino VL53L4CD,STM32duino VL53L4ED,Arduino_LSM6DSOX,Arduino_LPS22HB,Arduino_HS300x
85 changes: 73 additions & 12 deletions src/Modulino.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Wire.h"
#include <vector>
#include <vl53l4cd_class.h> // from stm32duino
#include <vl53l4ed_class.h> // from stm32duino
#include "Arduino_LSM6DSOX.h"
#include <Arduino_LPS22HB.h>
#include <Arduino_HS300x.h>
Expand Down Expand Up @@ -396,6 +397,52 @@ class ModulinoLight : public Module {

};

class _distance_api {
public:
_distance_api(VL53L4CD* sensor) : sensor(sensor) {
isVL53L4CD = true;
};
_distance_api(VL53L4ED* sensor) : sensor(sensor) {};
uint8_t setRangeTiming(uint32_t timing_budget_ms, uint32_t inter_measurement_ms) {
if (isVL53L4CD) {
return ((VL53L4CD*)sensor)->VL53L4CD_SetRangeTiming(timing_budget_ms, inter_measurement_ms);
} else {
return ((VL53L4ED*)sensor)->VL53L4ED_SetRangeTiming(timing_budget_ms, inter_measurement_ms);
}
}
uint8_t startRanging() {
if (isVL53L4CD) {
return ((VL53L4CD*)sensor)->VL53L4CD_StartRanging();
} else {
return ((VL53L4ED*)sensor)->VL53L4ED_StartRanging();
}
}
uint8_t checkForDataReady(uint8_t* p_is_data_ready) {
if (isVL53L4CD) {
return ((VL53L4CD*)sensor)->VL53L4CD_CheckForDataReady(p_is_data_ready);
} else {
return ((VL53L4ED*)sensor)->VL53L4ED_CheckForDataReady(p_is_data_ready);
}
}
uint8_t clearInterrupt() {
if (isVL53L4CD) {
return ((VL53L4CD*)sensor)->VL53L4CD_ClearInterrupt();
} else {
return ((VL53L4ED*)sensor)->VL53L4ED_ClearInterrupt();
}
}
uint8_t getResult(void* result) {
if (isVL53L4CD) {
return ((VL53L4CD*)sensor)->VL53L4CD_GetResult((VL53L4CD_Result_t*)result);
} else {
return ((VL53L4ED*)sensor)->VL53L4ED_GetResult((VL53L4ED_ResultsData_t*)result);
}
}
private:
void* sensor;
bool isVL53L4CD = false;
};

class ModulinoDistance : public Module {
public:
bool begin() {
Expand All @@ -406,29 +453,40 @@ class ModulinoDistance : public Module {
}
tof_sensor = new VL53L4CD((TwoWire*)getWire(), -1);
auto ret = tof_sensor->InitSensor();
__increaseI2CPriority();
if (ret == VL53L4CD_ERROR_NONE) {
tof_sensor->VL53L4CD_SetRangeTiming(20, 0);
tof_sensor->VL53L4CD_StartRanging();
return true;
} else {
if (ret != VL53L4CD_ERROR_NONE) {
delete tof_sensor;
tof_sensor = nullptr;
return false;
tof_sensor_alt = new VL53L4ED((TwoWire*)getWire(), -1);
ret = tof_sensor_alt->InitSensor();
if (ret == VL53L4ED_ERROR_NONE) {
api = new _distance_api(tof_sensor_alt);
} else {
delete tof_sensor_alt;
tof_sensor_alt = nullptr;
return false;
}
} else {
api = new _distance_api(tof_sensor);
}

__increaseI2CPriority();
api->setRangeTiming(20, 0);
api->startRanging();
return true;
}
operator bool() {
return (tof_sensor != nullptr);
return (api != nullptr);
}
bool available() {
if (tof_sensor == nullptr) {
if (api == nullptr) {
return false;
}
float ret = internal;
uint8_t NewDataReady = 0;
tof_sensor->VL53L4CD_CheckForDataReady(&NewDataReady);
api->checkForDataReady(&NewDataReady);
if (NewDataReady) {
tof_sensor->VL53L4CD_ClearInterrupt();
tof_sensor->VL53L4CD_GetResult(&results);
api->clearInterrupt();
api->getResult(&results);
}
if (results.range_status == 0) {
internal = results.distance_mm;
Expand All @@ -442,6 +500,9 @@ class ModulinoDistance : public Module {
}
private:
VL53L4CD* tof_sensor = nullptr;
VL53L4ED* tof_sensor_alt = nullptr;
VL53L4CD_Result_t results;
//VL53L4ED_ResultsData_t results;
float internal = NAN;
_distance_api* api = nullptr;
};