Skip to content

Commit 04860a5

Browse files
committed
Add long distance sensor
1 parent c404e59 commit 04860a5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/Modulino.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,52 @@ class ModulinoDistance : public Module {
424424
private:
425425
VL53L4CD* tof_sensor = nullptr;
426426
VL53L4CD_Result_t results;
427+
};
428+
429+
#include <vl53l1x_class.h> // from stm32duino
430+
431+
class ModulinoLongDistance : public Module {
432+
public:
433+
bool begin() {
434+
// try scanning for 0x29 since the library contains a while(true) on begin()
435+
getWire()->beginTransmission(0x29);
436+
if (getWire()->endTransmission() != 0) {
437+
return false;
438+
}
439+
tof_sensor = new VL53L1X((TwoWire*)getWire(), -1);
440+
auto ret = tof_sensor->InitSensor(VL53L1X_DEFAULT_DEVICE_ADDRESS);
441+
if (ret == VL53L1X_ERROR_NONE) {
442+
tof_sensor->VL53L1X_SetDistanceMode(2);
443+
tof_sensor->VL53L1X_SetTimingBudgetInMs(10);
444+
tof_sensor->VL53L1X_StartRanging();
445+
return true;
446+
} else {
447+
tof_sensor = nullptr;
448+
return false;
449+
}
450+
}
451+
operator bool() {
452+
return (tof_sensor != nullptr);
453+
}
454+
float get() {
455+
if (tof_sensor == nullptr) {
456+
return NAN;
457+
}
458+
uint8_t NewDataReady = 0;
459+
uint8_t status = tof_sensor->VL53L1X_CheckForDataReady(&NewDataReady);
460+
if (NewDataReady) {
461+
tof_sensor->VL53L1X_ClearInterrupt();
462+
status = tof_sensor->VL53L1X_GetDistance(&_distance);
463+
if (status != VL53L1X_ERROR_NONE) {
464+
return NAN;
465+
}
466+
}
467+
return _distance;
468+
}
469+
bool isValid(float distance) {
470+
return !isnan(distance);
471+
}
472+
private:
473+
VL53L1X* tof_sensor = nullptr;
474+
uint16_t _distance;
427475
};

0 commit comments

Comments
 (0)