millis() overflow logic reworked

This commit is contained in:
cyberman54 2020-12-21 22:11:41 +01:00
parent 069c5ec3ec
commit a04e6440d4
6 changed files with 9 additions and 9 deletions

View File

@ -12,6 +12,6 @@
void do_reset(bool warmstart); void do_reset(bool warmstart);
void do_after_reset(void); void do_after_reset(void);
void enter_deepsleep(const uint64_t wakeup_sec, const gpio_num_t wakeup_gpio); 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 #endif // _RESET_H

View File

@ -26,7 +26,6 @@ bsec_virtual_sensor_t sensorList[10] = {
}; };
uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0}; uint8_t bsecstate_buffer[BSEC_MAX_STATE_BLOB_SIZE] = {0};
uint16_t stateUpdateCounter = 0;
Bsec iaqSensor; Bsec iaqSensor;
@ -218,6 +217,7 @@ void loadState(void) {
void updateState(void) { void updateState(void) {
bool update = false; bool update = false;
static uint16_t stateUpdateCounter = 0;
if (stateUpdateCounter == 0) { if (stateUpdateCounter == 0) {
// first state update when IAQ accuracy is >= 1 // first state update when IAQ accuracy is >= 1
@ -228,7 +228,7 @@ void updateState(void) {
} else { } else {
/* Update every STATE_SAVE_PERIOD minutes */ /* Update every STATE_SAVE_PERIOD minutes */
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) { if ((long)(millis() - stateUpdateCounter * STATE_SAVE_PERIOD) >= 0) {
update = true; update = true;
stateUpdateCounter++; stateUpdateCounter++;
} }

View File

@ -146,7 +146,7 @@ void ledLoop(void *parameter) {
// management // management
if (LEDBlinkStarted && LEDBlinkDuration) { if (LEDBlinkStarted && LEDBlinkDuration) {
// Custom blink is finished, let this order, avoid millis() overflow // 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 // Led becomes off, and stop blink
LEDState = LED_OFF; LEDState = LED_OFF;
LEDBlinkStarted = 0; LEDBlinkStarted = 0;

View File

@ -180,7 +180,7 @@ int do_ota_update() {
unsigned long timeout = millis(); unsigned long timeout = millis();
while (client.available() == 0) { while (client.available() == 0) {
if ((millis() - timeout) > (RESPONSE_TIMEOUT_MS)) { if ((long)(millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
ESP_LOGI(TAG, "Client timeout"); ESP_LOGI(TAG, "Client timeout");
ota_display(3, " E", "client timeout"); ota_display(3, " E", "client timeout");
goto abort; goto abort;

View File

@ -298,8 +298,8 @@ void get_config(uint8_t val[]) {
void get_status(uint8_t val[]) { void get_status(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get device status"); ESP_LOGI(TAG, "Remote command: get device status");
payload.reset(); payload.reset();
payload.addStatus(read_voltage(), uptime() / 1000, temperatureRead(), payload.addStatus(read_voltage(), (uint64_t)(uptime() / 1000ULL),
getFreeRAM(), rtc_get_reset_reason(0), temperatureRead(), getFreeRAM(), rtc_get_reset_reason(0),
rtc_get_reset_reason(1)); rtc_get_reset_reason(1));
SendPayload(STATUSPORT); SendPayload(STATUSPORT);
}; };

View File

@ -11,7 +11,7 @@ static const char TAG[] = __FILE__;
// variables keep its values after a wakeup from sleep // variables keep its values after a wakeup from sleep
RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE; RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE;
RTC_DATA_ATTR struct timeval RTC_sleep_start_time; 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; timeval sleep_stop_time;
const char *runmode[5] = {"powercycle", "normal", "wakeup", "update", "sleep"}; const char *runmode[5] = {"powercycle", "normal", "wakeup", "update", "sleep"};
@ -193,4 +193,4 @@ Error:
do_reset(true); do_reset(true);
} }
uint64_t uptime() { return (RTC_millis + millis()); } unsigned long long uptime() { return (RTC_millis + millis()); }