Migrate MessageOutput to TaskScheduler

This commit is contained in:
Thomas Basler 2023-11-20 21:57:43 +01:00
parent 77779a1ed9
commit ad1f1b690c
3 changed files with 16 additions and 4 deletions

View File

@ -4,18 +4,23 @@
#include <AsyncWebSocket.h> #include <AsyncWebSocket.h>
#include <HardwareSerial.h> #include <HardwareSerial.h>
#include <Stream.h> #include <Stream.h>
#include <TaskSchedulerDeclarations.h>
#include <mutex> #include <mutex>
#define BUFFER_SIZE 500 #define BUFFER_SIZE 500
class MessageOutputClass : public Print { class MessageOutputClass : public Print {
public: public:
void loop(); void init(Scheduler* scheduler);
size_t write(uint8_t c) override; 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); void register_ws_output(AsyncWebSocket* output);
private: private:
void loop();
Task _loopTask;
AsyncWebSocket* _ws = NULL; AsyncWebSocket* _ws = NULL;
char _buffer[BUFFER_SIZE]; char _buffer[BUFFER_SIZE];
uint16_t _buff_pos = 0; uint16_t _buff_pos = 0;

View File

@ -8,6 +8,14 @@
MessageOutputClass MessageOutput; 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) void MessageOutputClass::register_ws_output(AsyncWebSocket* output)
{ {
_ws = output; _ws = output;

View File

@ -36,6 +36,7 @@ void setup()
while (!Serial) while (!Serial)
yield(); yield();
#endif #endif
MessageOutput.init(&scheduler);
MessageOutput.println(); MessageOutput.println();
MessageOutput.println("Starting OpenDTU"); MessageOutput.println("Starting OpenDTU");
@ -157,6 +158,4 @@ void loop()
yield(); yield();
Display.loop(); Display.loop();
yield(); yield();
MessageOutput.loop();
yield();
} }