From a04e6440d46bd01cface2f64dc9fa0d795ac0374 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 21 Dec 2020 22:11:41 +0100 Subject: [PATCH] millis() overflow logic reworked --- include/reset.h | 2 +- src/bmesensor.cpp | 4 ++-- src/led.cpp | 2 +- src/ota.cpp | 2 +- src/rcommand.cpp | 4 ++-- src/reset.cpp | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/reset.h b/include/reset.h index 0ce84c78..af9d6c50 100644 --- a/include/reset.h +++ b/include/reset.h @@ -12,6 +12,6 @@ void do_reset(bool warmstart); void do_after_reset(void); void enter_deepsleep(const uint64_t wakeup_sec, const gpio_num_t wakeup_gpio); -uint64_t uptime(void); +unsigned long long uptime(void); #endif // _RESET_H \ No newline at end of file diff --git a/src/bmesensor.cpp b/src/bmesensor.cpp index 4a365d90..56924d4a 100644 --- a/src/bmesensor.cpp +++ b/src/bmesensor.cpp @@ -26,7 +26,6 @@ bsec_virtual_sensor_t sensorList[10] = { }; uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0}; -uint16_t stateUpdateCounter = 0; Bsec iaqSensor; @@ -218,6 +217,7 @@ void loadState(void) { void updateState(void) { bool update = false; + static uint16_t stateUpdateCounter = 0; if (stateUpdateCounter == 0) { // first state update when IAQ accuracy is >= 1 @@ -228,7 +228,7 @@ void updateState(void) { } else { /* Update every STATE_SAVE_PERIOD minutes */ - if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) { + if ((long)(millis() - stateUpdateCounter * STATE_SAVE_PERIOD) >= 0) { update = true; stateUpdateCounter++; } diff --git a/src/led.cpp b/src/led.cpp index e74e92c6..b0b302d6 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -146,7 +146,7 @@ void ledLoop(void *parameter) { // management if (LEDBlinkStarted && LEDBlinkDuration) { // Custom blink is finished, let this order, avoid millis() overflow - if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) { + if ((long)(millis() - LEDBlinkStarted) >= LEDBlinkDuration) { // Led becomes off, and stop blink LEDState = LED_OFF; LEDBlinkStarted = 0; diff --git a/src/ota.cpp b/src/ota.cpp index c256559e..6b94a193 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -180,7 +180,7 @@ int do_ota_update() { unsigned long timeout = millis(); while (client.available() == 0) { - if ((millis() - timeout) > (RESPONSE_TIMEOUT_MS)) { + if ((long)(millis() - timeout) > (RESPONSE_TIMEOUT_MS)) { ESP_LOGI(TAG, "Client timeout"); ota_display(3, " E", "client timeout"); goto abort; diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 0759dc79..afea9eb4 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -298,8 +298,8 @@ void get_config(uint8_t val[]) { void get_status(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: get device status"); payload.reset(); - payload.addStatus(read_voltage(), uptime() / 1000, temperatureRead(), - getFreeRAM(), rtc_get_reset_reason(0), + payload.addStatus(read_voltage(), (uint64_t)(uptime() / 1000ULL), + temperatureRead(), getFreeRAM(), rtc_get_reset_reason(0), rtc_get_reset_reason(1)); SendPayload(STATUSPORT); }; diff --git a/src/reset.cpp b/src/reset.cpp index 0572dabd..18412909 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -11,7 +11,7 @@ static const char TAG[] = __FILE__; // variables keep its values after a wakeup from sleep RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE; RTC_DATA_ATTR struct timeval RTC_sleep_start_time; -RTC_DATA_ATTR unsigned long RTC_millis = 0; +RTC_DATA_ATTR unsigned long long RTC_millis = 0; timeval sleep_stop_time; const char *runmode[5] = {"powercycle", "normal", "wakeup", "update", "sleep"}; @@ -193,4 +193,4 @@ Error: do_reset(true); } -uint64_t uptime() { return (RTC_millis + millis()); } \ No newline at end of file +unsigned long long uptime() { return (RTC_millis + millis()); } \ No newline at end of file