Skip to content

Commit 9716042

Browse files
committed
Cleanup return and operator bool()
1 parent 2f2ddc9 commit 9716042

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/Modulino.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class ModulinoClass {
2525
_wire->setClock(100000);
2626
}
2727
friend class Module;
28-
friend class Distance;
2928
protected:
3029
HardwareI2C* _wire;
3130
};
@@ -40,6 +39,7 @@ class Module : public Printable {
4039
if (address == 0xFF) {
4140
address = discover() / 2; // divide by 2 to match address in fw main.c
4241
}
42+
return (address != 0xFF);
4343
}
4444
virtual uint8_t discover() {
4545
return 0xFF;
@@ -183,9 +183,6 @@ class ModulinoPixels : public Module {
183183
: Module(address, "LEDS") {
184184
memset((uint8_t*)data, 0xE0, NUMLEDS * 4);
185185
}
186-
bool begin() {
187-
Module::begin();
188-
}
189186
void set(int idx, ModulinoColor rgb, uint8_t brightness = 25) {
190187
if (idx < NUMLEDS) {
191188
uint8_t _brightness = map(brightness, 0, 100, 0, 0x1F);
@@ -270,6 +267,9 @@ class ModulinoMovement : public Module {
270267
initialized = _imu->begin();
271268
return initialized != 0;
272269
}
270+
operator bool() {
271+
return (initialized != 0);
272+
}
273273
int update() {
274274
if (initialized) {
275275
return _imu->readAcceleration(x, y, z);
@@ -300,6 +300,9 @@ class ModulinoThermo: public Module {
300300
initialized = _sensor->begin();
301301
return initialized;
302302
}
303+
operator bool() {
304+
return (initialized != 0);
305+
}
303306
float getHumidity() {
304307
if (initialized) {
305308
return _sensor->readHumidity();
@@ -330,6 +333,9 @@ class ModulinoPressure : public Module {
330333
}
331334
return initialized != 0;
332335
}
336+
operator bool() {
337+
return (initialized != 0);
338+
}
333339
float getPressure() {
334340
if (initialized) {
335341
return _barometer->readPressure();
@@ -355,11 +361,23 @@ class ModulinoDistance : public Module {
355361
public:
356362
bool begin() {
357363
tof_sensor = new VL53L4CD((TwoWire*)getWire(), -1);
358-
tof_sensor->InitSensor();
359-
tof_sensor->VL53L4CD_SetRangeTiming(10, 0);
360-
tof_sensor->VL53L4CD_StartRanging();
364+
auto ret = tof_sensor->InitSensor();
365+
if (ret == VL53L4CD_ERROR_NONE) {
366+
tof_sensor->VL53L4CD_SetRangeTiming(10, 0);
367+
tof_sensor->VL53L4CD_StartRanging();
368+
return true;
369+
} else {
370+
tof_sensor = nullptr;
371+
return false;
372+
}
373+
}
374+
operator bool() {
375+
return (tof_sensor != nullptr);
361376
}
362377
float get() {
378+
if (tof_sensor == nullptr) {
379+
return NAN;
380+
}
363381
VL53L4CD_Result_t results;
364382
uint8_t NewDataReady = 0;
365383
uint8_t status;
@@ -371,5 +389,5 @@ class ModulinoDistance : public Module {
371389
return results.distance_mm;
372390
}
373391
private:
374-
VL53L4CD* tof_sensor; ;
392+
VL53L4CD* tof_sensor = nullptr;
375393
};

0 commit comments

Comments
 (0)