3
3
4
4
#include " Wire.h"
5
5
#include < vl53l4cd_class.h> // from stm32duino
6
+ #include < vl53l4ed_class.h> // from stm32duino
6
7
#include " Arduino_LSM6DSOX.h"
7
8
#include < Arduino_LPS22HB.h>
8
9
#include < Arduino_HS300x.h>
@@ -395,6 +396,52 @@ class ModulinoLight : public Module {
395
396
396
397
};
397
398
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
+
398
445
class ModulinoDistance : public Module {
399
446
public:
400
447
bool begin () {
@@ -405,29 +452,40 @@ class ModulinoDistance : public Module {
405
452
}
406
453
tof_sensor = new VL53L4CD ((TwoWire*)getWire (), -1 );
407
454
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;
414
457
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);
416
469
}
470
+
471
+ __increaseI2CPriority ();
472
+ api->setRangeTiming (20 , 0 );
473
+ api->startRanging ();
474
+ return true ;
417
475
}
418
476
operator bool () {
419
- return (tof_sensor != nullptr );
477
+ return (api != nullptr );
420
478
}
421
479
bool available () {
422
- if (tof_sensor == nullptr ) {
480
+ if (api == nullptr ) {
423
481
return false ;
424
482
}
425
483
float ret = internal;
426
484
uint8_t NewDataReady = 0 ;
427
- tof_sensor-> VL53L4CD_CheckForDataReady (&NewDataReady);
485
+ api-> checkForDataReady (&NewDataReady);
428
486
if (NewDataReady) {
429
- tof_sensor-> VL53L4CD_ClearInterrupt ();
430
- tof_sensor-> VL53L4CD_GetResult (&results);
487
+ api-> clearInterrupt ();
488
+ api-> getResult (&results);
431
489
}
432
490
if (results.range_status == 0 ) {
433
491
internal = results.distance_mm ;
@@ -441,6 +499,9 @@ class ModulinoDistance : public Module {
441
499
}
442
500
private:
443
501
VL53L4CD* tof_sensor = nullptr ;
502
+ VL53L4ED* tof_sensor_alt = nullptr ;
444
503
VL53L4CD_Result_t results;
504
+ // VL53L4ED_ResultsData_t results;
445
505
float internal = NAN;
506
+ _distance_api* api = nullptr ;
446
507
};
0 commit comments