Skip to content

Commit b68d6a7

Browse files
committed
ModulinoDistance: get() returns wrapper class Distance
1 parent 95ec7f6 commit b68d6a7

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Modulino.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,22 @@ class ModulinoLight : public Module {
380380

381381
};
382382

383+
class Distance {
384+
public:
385+
Distance(int f) : internal(f) {}
386+
operator int() {
387+
return internal;
388+
}
389+
operator bool() {
390+
return internal > 0;
391+
}
392+
bool isValid() {
393+
return *this;
394+
}
395+
private:
396+
int internal;
397+
};
398+
383399
class ModulinoDistance : public Module {
384400
public:
385401
bool begin() {
@@ -402,9 +418,9 @@ class ModulinoDistance : public Module {
402418
operator bool() {
403419
return (tof_sensor != nullptr);
404420
}
405-
float get() {
421+
Distance get() {
406422
if (tof_sensor == nullptr) {
407-
return NAN;
423+
return Distance(-1);
408424
}
409425
uint8_t NewDataReady = 0;
410426
uint8_t status = tof_sensor->VL53L4CD_CheckForDataReady(&NewDataReady);
@@ -413,14 +429,11 @@ class ModulinoDistance : public Module {
413429
tof_sensor->VL53L4CD_GetResult(&results);
414430
}
415431
if (results.range_status == 0) {
416-
return results.distance_mm;
432+
return Distance(results.distance_mm);
417433
} else {
418-
return NAN;
434+
return Distance(-1);
419435
}
420436
}
421-
bool isValid(float distance) {
422-
return !isnan(distance);
423-
}
424437
private:
425438
VL53L4CD* tof_sensor = nullptr;
426439
VL53L4CD_Result_t results;

0 commit comments

Comments
 (0)