forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
Release v1.3.4.2 - fix isEscape bug, power-loss resilience, EEPROM fix, SD mutex, code cleanup
This commit is contained in:
81
releases/v1.3.4.2/src/Watchdog.cpp
Normal file
81
releases/v1.3.4.2/src/Watchdog.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "Watchdog.h"
|
||||
|
||||
// Wykrywanie środowiska
|
||||
#if defined(ESP_PLATFORM) || defined(ESP32)
|
||||
#include <esp_task_wdt.h>
|
||||
#define WDOG_HAS_ESP 1
|
||||
#else
|
||||
#define WDOG_HAS_ESP 0
|
||||
#endif
|
||||
|
||||
namespace Watchdog {
|
||||
|
||||
static bool s_initialized = false;
|
||||
|
||||
bool init(int timeout_seconds, bool panic_on_trigger) {
|
||||
#if WDOG_HAS_ESP
|
||||
if (s_initialized) return true;
|
||||
esp_err_t err = esp_task_wdt_init(timeout_seconds, panic_on_trigger);
|
||||
if (err == ESP_OK || err == ESP_ERR_INVALID_STATE) {
|
||||
// ESP_ERR_INVALID_STATE: już zainicjalizowany — traktujemy jako OK
|
||||
s_initialized = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#else
|
||||
(void)timeout_seconds; (void)panic_on_trigger;
|
||||
s_initialized = true; // no-op, aby nie blokować wywołań w kodzie
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool addThisTask() {
|
||||
#if WDOG_HAS_ESP
|
||||
if (!s_initialized) return false;
|
||||
esp_err_t err = esp_task_wdt_add(nullptr); // nullptr = bieżący task
|
||||
return (err == ESP_OK || err == ESP_ERR_INVALID_STATE);
|
||||
#else
|
||||
return true; // no-op
|
||||
#endif
|
||||
}
|
||||
|
||||
bool removeThisTask() {
|
||||
#if WDOG_HAS_ESP
|
||||
if (!s_initialized) return false;
|
||||
esp_err_t err = esp_task_wdt_delete(nullptr); // bieżący task
|
||||
return (err == ESP_OK || err == ESP_ERR_INVALID_STATE);
|
||||
#else
|
||||
return true; // no-op
|
||||
#endif
|
||||
}
|
||||
|
||||
void feed() {
|
||||
#if WDOG_HAS_ESP
|
||||
esp_task_wdt_reset();
|
||||
#else
|
||||
// no-op
|
||||
#endif
|
||||
}
|
||||
|
||||
bool setTimeout(int timeout_seconds) {
|
||||
#if WDOG_HAS_ESP
|
||||
if (!s_initialized) {
|
||||
// jeśli ktoś nie zainicjalizował — zrób to teraz
|
||||
return init(timeout_seconds, true);
|
||||
}
|
||||
// W ESP-IDF/Arduino brak prostego API na „live update” — re-init:
|
||||
esp_err_t err = esp_task_wdt_deinit();
|
||||
(void)err; // nie każdy port raportuje OK/INVALID_STATE spójnie
|
||||
s_initialized = false;
|
||||
return init(timeout_seconds, true);
|
||||
#else
|
||||
(void)timeout_seconds;
|
||||
return true; // no-op
|
||||
#endif
|
||||
}
|
||||
|
||||
bool isActive() {
|
||||
return s_initialized;
|
||||
}
|
||||
|
||||
} // namespace Watchdog
|
||||
Reference in New Issue
Block a user