From 836f907d633ef55e6631070ea2459f64ab2c0b84 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Tue, 27 Aug 2024 00:01:41 +0900 Subject: [PATCH] remove dependency on Ticker library replace Ticker timer with RTOS implementation --- .gitignore | 3 +++ src/flashz-http.cpp | 54 +++++++++++++++++++++++++++++++++------------ src/flashz-http.hpp | 13 +++++++---- 3 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eec1c26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.pio +.vscode/ +/platformio.ini diff --git a/src/flashz-http.cpp b/src/flashz-http.cpp index 6432124..7561a7a 100644 --- a/src/flashz-http.cpp +++ b/src/flashz-http.cpp @@ -118,10 +118,16 @@ void FlashZhttp::handle_ota_form(AsyncWebServer *srv, const char* url){ request->send(503, PGmimetxt, "Update FAILED"); } else { if (rst_timeout){ - if (!t) - t = new Ticker; - - t->once_ms(rst_timeout, [](){ ESP.restart(); }); + if (!_tmr){ + _tmr = xTimerCreate(TAG, + pdMS_TO_TICKS(rst_timeout), + pdFALSE, // one time timer + static_cast(this), + [](TimerHandle_t h) { ESP.restart(); } + ); + } + + xTimerReset( _tmr, portMAX_DELAY ); } request->send(200, PGmimetxt, "OTA complete, autoreboot in 5 sec..."); } @@ -247,10 +253,16 @@ fz_http_err_t FlashZhttp::_http_get(const char* url, int imgtype){ } if (rst_timeout){ - if (!t) - t = new Ticker; + if (!_tmr){ + _tmr = xTimerCreate(TAG, + pdMS_TO_TICKS(rst_timeout), + pdFALSE, // one time timer + static_cast(this), + [](TimerHandle_t h) { ESP.restart(); } + ); + } - t->once_ms(rst_timeout, [](){ ESP.restart(); }); + xTimerReset( _tmr, portMAX_DELAY ); } return fz_http_err_t::ok; @@ -279,14 +291,20 @@ void FlashZhttp::handle_ota_form(WebServer *server, const char* url){ server->send(500, PGmimetxt, "UPDATE FAILED"); } else { if (rst_timeout){ - if (!t) - t = new Ticker; + if (!_tmr){ + _tmr = xTimerCreate(TAG, + pdMS_TO_TICKS(rst_timeout), + pdFALSE, // one time timer + static_cast(this), + [](TimerHandle_t h) { ESP.restart(); } + ); + } - t->once_ms(rst_timeout, [](){ ESP.restart(); }); + xTimerReset( _tmr, portMAX_DELAY ); } server->client().setNoDelay(true); - server->send(200, PGmimetxt, F("OTA complete, autoreboot in 5 sec...")); + server->send(200, PGmimetxt, "OTA complete, autoreboot in 5 sec..."); server->client().stop(); } } @@ -374,9 +392,17 @@ unsigned FlashZhttp::autoreboot(unsigned t){ #ifndef FZ_NOHTTPCLIENT void FlashZhttp::fetch_async(const char* url, int imgtype, int delay){ if (!cb){ cb = new callback_arg_t(imgtype, url); } - if (!t) t = new Ticker; - // have no idea why, but C3 bootloops here, needs investigation - t->once_ms(delay, FlashZhttp::_fz_http_trigger, this); + if (!_tmr){ + _tmr = xTimerCreate(TAG, + pdMS_TO_TICKS(delay), + pdFALSE, // one time timer + static_cast(this), + [](TimerHandle_t h) { ESP.restart(); } + ); + } + + xTimerReset( _tmr, portMAX_DELAY ); + _err = fz_http_err_t::pending; } #endif //FZ_NOHTTPCLIENT diff --git a/src/flashz-http.hpp b/src/flashz-http.hpp index 3dfe495..251febd 100644 --- a/src/flashz-http.hpp +++ b/src/flashz-http.hpp @@ -29,6 +29,9 @@ #pragma once +#include "freertos/FreeRTOS.h" +#include "freertos/timers.h" + #ifdef FZ_WITH_ASYNCSRV #include #define FZ_NO_WEBSRV @@ -36,8 +39,6 @@ #include #endif // #ifdef FZ_WITH_ASYNCSRV -#include - #define FZ_REBOOT_TIMEOUT 5000 #define FZ_HTTP_CLIENT_DELAY 1000 @@ -67,7 +68,7 @@ enum class fz_http_err_t:int { */ class FlashZhttp { unsigned rst_timeout = FZ_REBOOT_TIMEOUT; - Ticker *t = nullptr; + TimerHandle_t _tmr{nullptr}; #ifndef FZ_NOHTTPCLIENT struct callback_arg_t { @@ -95,7 +96,11 @@ class FlashZhttp { public: ~FlashZhttp(){ - delete t; t = nullptr; + if (_tmr){ + xTimerStop(_tmr, portMAX_DELAY ); + xTimerDelete(_tmr, portMAX_DELAY ); + } + #ifndef FZ_NOHTTPCLIENT delete cb; cb = nullptr;