Skip to content

Commit 71aa594

Browse files
committed
Refactor haltdelay to let it hang or reboot
1 parent 340e3f8 commit 71aa594

File tree

4 files changed

+62
-48
lines changed

4 files changed

+62
-48
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -876,22 +876,36 @@ void Wippersnapper_V2::runNetFSMV2() {
876876
The desired error to print to serial.
877877
@param ledStatusColor
878878
The desired color to blink.
879+
@param reboot
880+
If true, the device will reboot after the WDT bites.
881+
If false, the device will not allow the WDT to bite and
882+
instead hang indefinitely, holding the WIPPER drive open
879883
*/
880884
/**************************************************************************/
881-
void Wippersnapper_V2::haltErrorV2(String error,
882-
ws_led_status_t ledStatusColor) {
885+
void Wippersnapper_V2::haltErrorV2(String error, ws_led_status_t ledStatusColor,
886+
bool reboot) {
887+
WS_DEBUG_PRINT("ERROR ");
888+
if (reboot) {
889+
WS_DEBUG_PRINT("[RESET]: ");
890+
} else {
891+
WS_DEBUG_PRINT("[HANG]: ");
892+
}
893+
WS_DEBUG_PRINTLN(error);
894+
statusLEDSolid(ledStatusColor);
883895
for (;;) {
884-
WS_DEBUG_PRINT("ERROR [WDT RESET]: ");
885-
WS_DEBUG_PRINTLN(error);
886-
// let the WDT fail out and reset!
887-
statusLEDSolid(ledStatusColor);
896+
if (!reboot) {
897+
WsV2.feedWDTV2(); // Feed the WDT indefinitely to hold the WIPPER drive
898+
// open
899+
} else {
900+
// Let the WDT fail out and reset!
888901
#ifndef ARDUINO_ARCH_ESP8266
889-
delay(1000);
902+
delay(1000);
890903
#else
891-
// Calls to delay() and yield() feed the ESP8266's
892-
// hardware and software watchdog timers, delayMicroseconds does not.
893-
delayMicroseconds(1000000);
904+
// Calls to delay() and yield() feed the ESP8266's
905+
// hardware and software watchdog timers, delayMicroseconds does not.
906+
delayMicroseconds(1000000);
894907
#endif
908+
}
895909
}
896910
}
897911

src/Wippersnapper_V2.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ class Wippersnapper_V2 {
214214

215215
// Error handling helpers
216216
void
217-
haltErrorV2(String error,
218-
ws_led_status_t ledStatusColor = WS_LED_STATUS_ERROR_RUNTIME);
217+
haltErrorV2(String error, ws_led_status_t ledStatusColor = WS_LED_STATUS_ERROR_RUNTIME, bool reboot = true);
219218
void errorWriteHangV2(String error);
220219

221220
bool _is_offline_mode; ///< Global flag for if the device is in offline mode

src/components/i2c/controller.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,13 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
518518

519519
// Before we do anything on the bus - was the bus initialized correctly?
520520
if (!IsBusStatusOK(use_alt_bus)) {
521-
WS_DEBUG_PRINTLN("[i2c] ERROR: I2C bus is not operational or stuck, please "
522-
"restart device!");
521+
WsV2.haltErrorV2("[i2c] I2C bus is stuck or not operational, reset the board!", WS_LED_STATUS_ERROR_RUNTIME, false);
523522
if (!PublishI2cDeviceAddedorReplaced(device_descriptor, device_status))
524523
return false;
525524
return true;
526525
}
527526

528527
// Mux case #1 - We are creating a mux via I2cDeviceAddorReplace message
529-
// TODO: Refactor
530528
if ((strcmp(device_name, "pca9546") == 0) ||
531529
(strcmp(device_name, "pca9548") == 0)) {
532530
WS_DEBUG_PRINTLN("[i2c] Creating a new MUX driver obj");
@@ -536,15 +534,15 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
536534
_i2c_bus_alt->AddMuxToBus(device_descriptor.i2c_mux_address,
537535
device_name);
538536
} else {
539-
WS_DEBUG_PRINTLN("[i2c] ERROR: Mux specified but not created");
537+
WsV2.haltErrorV2("[i2c] Unable to initialize I2C MUX!", WS_LED_STATUS_ERROR_RUNTIME, false);
540538
}
541539
} else {
542540
if (!_i2c_bus_default->HasMux()) {
543541
WS_DEBUG_PRINT("[i2c] Adding MUX to default bus...");
544542
_i2c_bus_default->AddMuxToBus(device_descriptor.i2c_mux_address,
545543
device_name);
546544
} else {
547-
WS_DEBUG_PRINTLN("[i2c] ERROR: Mux specified but not created");
545+
WsV2.haltErrorV2("[i2c] Unable to initialize I2C MUX!", WS_LED_STATUS_ERROR_RUNTIME, false);
548546
}
549547
}
550548
// TODO: Publish back out to IO instead of blindly returning true
@@ -562,11 +560,10 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
562560
WS_DEBUG_PRINTLN(mux_channel);
563561
did_set_mux_ch = true;
564562
} else {
565-
WS_DEBUG_PRINTLN("[i2c] ERROR: Device requests a MUX but MUX has not "
566-
"been initialized first");
567-
device_status =
568-
wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_INIT;
569-
PublishI2cDeviceAddedorReplaced(device_descriptor, device_status);
563+
WsV2.haltErrorV2("[i2c] Device requires a MUX but MUX not present within config.json!", WS_LED_STATUS_ERROR_RUNTIME, false);
564+
// TODO: Online mode needs the below implemented
565+
// device_status = wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_INIT;
566+
// PublishI2cDeviceAddedorReplaced(device_descriptor, device_status);
570567
return false;
571568
}
572569
} else {
@@ -576,11 +573,10 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
576573
WS_DEBUG_PRINTLN(mux_channel);
577574
did_set_mux_ch = true;
578575
} else {
579-
WS_DEBUG_PRINTLN("[i2c] ERROR: Device requests a MUX but MUX has not "
580-
"been initialized first");
581-
device_status =
582-
wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_INIT;
583-
PublishI2cDeviceAddedorReplaced(device_descriptor, device_status);
576+
WsV2.haltErrorV2("[i2c] Device requires a MUX but MUX not present within config.json!", WS_LED_STATUS_ERROR_RUNTIME, false);
577+
// TODO: Online mode needs the below implemented
578+
// device_status = wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_INIT;
579+
// PublishI2cDeviceAddedorReplaced(device_descriptor, device_status);
584580
return false;
585581
}
586582
}
@@ -636,15 +632,11 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
636632
wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_INIT;
637633

638634
if (WsV2._sdCardV2->isModeOffline()) {
639-
WS_DEBUG_PRINTLN("[i2c] Driver failed to initialize!\n\tDid you set "
635+
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
640636
"the correct value for i2cDeviceName?\n\tDid you set "
641637
"the correct value for"
642-
"i2cDeviceAddress?");
643-
while (
644-
1) { // Keep the WIPPER drive open to allow user to edit config.json
645-
WsV2.feedWDTV2();
646-
delay(500);
647-
}
638+
"i2cDeviceAddress?",
639+
WS_LED_STATUS_ERROR_RUNTIME, false);
648640
}
649641
if (!PublishI2cDeviceAddedorReplaced(device_descriptor, device_status))
650642
return false;
@@ -656,15 +648,11 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
656648
wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_UNSUPPORTED_SENSOR;
657649

658650
if (WsV2._sdCardV2->isModeOffline()) {
659-
WS_DEBUG_PRINTLN("[i2c] Driver failed to initialize!\n\tDid you set "
651+
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
660652
"the correct value for i2cDeviceName?\n\tDid you set "
661653
"the correct value for"
662-
"i2cDeviceAddress?");
663-
while (
664-
1) { // Keep the WIPPER drive open to allow user to edit config.json
665-
WsV2.feedWDTV2();
666-
delay(500);
667-
}
654+
"i2cDeviceAddress?",
655+
WS_LED_STATUS_ERROR_RUNTIME, false);
668656
}
669657

670658
if (!PublishI2cDeviceAddedorReplaced(device_descriptor, device_status))

src/components/i2c/drivers/drvPm25.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef DRV_PM25_H
1717
#define DRV_PM25_H
1818

19+
#include "Wippersnapper_V2.h"
1920
#include "drvBase.h"
2021
#include <Adafruit_PM25AQI.h>
2122
#include <Wire.h>
@@ -41,7 +42,8 @@ class drvPm25 : public drvBase {
4142
The name of the driver.
4243
*/
4344
/*******************************************************************************/
44-
drvPm25(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel, const char* driver_name)
45+
drvPm25(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
46+
const char *driver_name)
4547
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {
4648
_i2c = i2c;
4749
_address = sensorAddress;
@@ -58,8 +60,8 @@ class drvPm25 : public drvBase {
5860
/*******************************************************************************/
5961
bool begin() override {
6062
_pm25 = new Adafruit_PM25AQI();
61-
// Wait one second for sensor to boot up!
62-
delay(1000);
63+
// Wait three seconds for the sensor to boot up!
64+
delay(3000);
6365
return _pm25->begin_I2C(_i2c);
6466
}
6567

@@ -74,10 +76,14 @@ class drvPm25 : public drvBase {
7476
/*******************************************************************************/
7577
bool getEventPM10_STD(sensors_event_t *pm10StdEvent) {
7678
PM25_AQI_Data data;
77-
if (!_pm25->read(&data))
79+
if (!_pm25->read(&data)) {
80+
WS_DEBUG_PRINTLN("Failed to read PM10STD data");
7881
return false; // couldn't read data
82+
}
7983

8084
pm10StdEvent->pm10_std = (float)data.pm10_standard;
85+
WS_DEBUG_PRINT("PM10STD: ");
86+
WS_DEBUG_PRINTLN(pm10StdEvent->pm10_std);
8187
return true;
8288
}
8389

@@ -92,10 +98,13 @@ class drvPm25 : public drvBase {
9298
/*******************************************************************************/
9399
bool getEventPM25_STD(sensors_event_t *pm25StdEvent) {
94100
PM25_AQI_Data data;
95-
if (!_pm25->read(&data))
101+
if (!_pm25->read(&data)) {
102+
WS_DEBUG_PRINTLN("Failed to read PM25STD data");
96103
return false; // couldn't read data
97-
104+
}
98105
pm25StdEvent->pm25_std = (float)data.pm25_standard;
106+
WS_DEBUG_PRINT("PM25STD: ");
107+
WS_DEBUG_PRINTLN(pm25StdEvent->pm25_std);
99108
return true;
100109
}
101110

@@ -110,10 +119,14 @@ class drvPm25 : public drvBase {
110119
/*******************************************************************************/
111120
bool getEventPM100_STD(sensors_event_t *pm100StdEvent) {
112121
PM25_AQI_Data data;
113-
if (!_pm25->read(&data))
122+
if (!_pm25->read(&data)) {
123+
WS_DEBUG_PRINTLN("Failed to read PM100STD data");
114124
return false; // couldn't read data
125+
}
115126

116127
pm100StdEvent->pm100_std = (float)data.pm100_standard;
128+
WS_DEBUG_PRINT("PM100STD: ");
129+
WS_DEBUG_PRINTLN(pm100StdEvent->pm100_std);
117130
return true;
118131
}
119132

0 commit comments

Comments
 (0)