33
44#include " Wire.h"
55#include < vl53l4cd_class.h> // from stm32duino
6+ #include < vl53l4ed_class.h> // from stm32duino
67#include " Arduino_LSM6DSOX.h"
78#include < Arduino_LPS22HB.h>
89#include < Arduino_HS300x.h>
@@ -395,6 +396,52 @@ class ModulinoLight : public Module {
395396
396397};
397398
399+ class _distance_api {
400+ public:
401+ _distance_api (VL53L4CD* sensor) : sensor(sensor) {
402+ isVL53L4CD = true ;
403+ };
404+ _distance_api (VL53L4ED* sensor) : sensor(sensor) {};
405+ uint8_t setRangeTiming (uint32_t timing_budget_ms, uint32_t inter_measurement_ms) {
406+ if (isVL53L4CD) {
407+ return ((VL53L4CD*)sensor)->VL53L4CD_SetRangeTiming (timing_budget_ms, inter_measurement_ms);
408+ } else {
409+ return ((VL53L4ED*)sensor)->VL53L4ED_SetRangeTiming (timing_budget_ms, inter_measurement_ms);
410+ }
411+ }
412+ uint8_t startRanging () {
413+ if (isVL53L4CD) {
414+ return ((VL53L4CD*)sensor)->VL53L4CD_StartRanging ();
415+ } else {
416+ return ((VL53L4ED*)sensor)->VL53L4ED_StartRanging ();
417+ }
418+ }
419+ uint8_t checkForDataReady (uint8_t * p_is_data_ready) {
420+ if (isVL53L4CD) {
421+ return ((VL53L4CD*)sensor)->VL53L4CD_CheckForDataReady (p_is_data_ready);
422+ } else {
423+ return ((VL53L4ED*)sensor)->VL53L4ED_CheckForDataReady (p_is_data_ready);
424+ }
425+ }
426+ uint8_t clearInterrupt () {
427+ if (isVL53L4CD) {
428+ return ((VL53L4CD*)sensor)->VL53L4CD_ClearInterrupt ();
429+ } else {
430+ return ((VL53L4ED*)sensor)->VL53L4ED_ClearInterrupt ();
431+ }
432+ }
433+ uint8_t getResult (void * result) {
434+ if (isVL53L4CD) {
435+ return ((VL53L4CD*)sensor)->VL53L4CD_GetResult ((VL53L4CD_Result_t*)result);
436+ } else {
437+ return ((VL53L4ED*)sensor)->VL53L4ED_GetResult ((VL53L4ED_ResultsData_t*)result);
438+ }
439+ }
440+ private:
441+ void * sensor;
442+ bool isVL53L4CD = false ;
443+ };
444+
398445class ModulinoDistance : public Module {
399446public:
400447 bool begin () {
@@ -405,29 +452,40 @@ class ModulinoDistance : public Module {
405452 }
406453 tof_sensor = new VL53L4CD ((TwoWire*)getWire (), -1 );
407454 auto ret = tof_sensor->InitSensor ();
408- __increaseI2CPriority ();
409- if (ret == VL53L4CD_ERROR_NONE) {
410- tof_sensor->VL53L4CD_SetRangeTiming (20 , 0 );
411- tof_sensor->VL53L4CD_StartRanging ();
412- return true ;
413- } else {
455+ if (ret != VL53L4CD_ERROR_NONE) {
456+ delete tof_sensor;
414457 tof_sensor = nullptr ;
415- return false ;
458+ tof_sensor_alt = new VL53L4ED ((TwoWire*)getWire (), -1 );
459+ ret = tof_sensor_alt->InitSensor ();
460+ if (ret == VL53L4ED_ERROR_NONE) {
461+ api = new _distance_api (tof_sensor_alt);
462+ } else {
463+ delete tof_sensor_alt;
464+ tof_sensor_alt = nullptr ;
465+ return false ;
466+ }
467+ } else {
468+ api = new _distance_api (tof_sensor);
416469 }
470+
471+ __increaseI2CPriority ();
472+ api->setRangeTiming (20 , 0 );
473+ api->startRanging ();
474+ return true ;
417475 }
418476 operator bool () {
419- return (tof_sensor != nullptr );
477+ return (api != nullptr );
420478 }
421479 bool available () {
422- if (tof_sensor == nullptr ) {
480+ if (api == nullptr ) {
423481 return false ;
424482 }
425483 float ret = internal;
426484 uint8_t NewDataReady = 0 ;
427- tof_sensor-> VL53L4CD_CheckForDataReady (&NewDataReady);
485+ api-> checkForDataReady (&NewDataReady);
428486 if (NewDataReady) {
429- tof_sensor-> VL53L4CD_ClearInterrupt ();
430- tof_sensor-> VL53L4CD_GetResult (&results);
487+ api-> clearInterrupt ();
488+ api-> getResult (&results);
431489 }
432490 if (results.range_status == 0 ) {
433491 internal = results.distance_mm ;
@@ -441,6 +499,9 @@ class ModulinoDistance : public Module {
441499 }
442500private:
443501 VL53L4CD* tof_sensor = nullptr ;
502+ VL53L4ED* tof_sensor_alt = nullptr ;
444503 VL53L4CD_Result_t results;
504+ // VL53L4ED_ResultsData_t results;
445505 float internal = NAN;
506+ _distance_api* api = nullptr ;
446507};
0 commit comments