From ad1f1b690c98c8b6af7c592572e5be6d3a0f1c12 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Mon, 20 Nov 2023 21:57:43 +0100 Subject: [PATCH] Migrate MessageOutput to TaskScheduler --- include/MessageOutput.h | 9 +++++++-- src/MessageOutput.cpp | 8 ++++++++ src/main.cpp | 3 +-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/MessageOutput.h b/include/MessageOutput.h index 7c56a6f47..3a30bff3b 100644 --- a/include/MessageOutput.h +++ b/include/MessageOutput.h @@ -4,18 +4,23 @@ #include #include #include +#include #include #define BUFFER_SIZE 500 class MessageOutputClass : public Print { public: - void loop(); + void init(Scheduler* scheduler); size_t write(uint8_t c) override; - size_t write(const uint8_t *buffer, size_t size) override; + size_t write(const uint8_t* buffer, size_t size) override; void register_ws_output(AsyncWebSocket* output); private: + void loop(); + + Task _loopTask; + AsyncWebSocket* _ws = NULL; char _buffer[BUFFER_SIZE]; uint16_t _buff_pos = 0; diff --git a/src/MessageOutput.cpp b/src/MessageOutput.cpp index 23c644bc9..abe28313b 100644 --- a/src/MessageOutput.cpp +++ b/src/MessageOutput.cpp @@ -8,6 +8,14 @@ MessageOutputClass MessageOutput; +void MessageOutputClass::init(Scheduler* scheduler) +{ + scheduler->addTask(_loopTask); + _loopTask.setCallback(std::bind(&MessageOutputClass::loop, this)); + _loopTask.setIterations(TASK_FOREVER); + _loopTask.enable(); +} + void MessageOutputClass::register_ws_output(AsyncWebSocket* output) { _ws = output; diff --git a/src/main.cpp b/src/main.cpp index eade58424..48c9aa04c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ void setup() while (!Serial) yield(); #endif + MessageOutput.init(&scheduler); MessageOutput.println(); MessageOutput.println("Starting OpenDTU"); @@ -157,6 +158,4 @@ void loop() yield(); Display.loop(); yield(); - MessageOutput.loop(); - yield(); } \ No newline at end of file