Skip to content

Commit

Permalink
Migrate MessageOutput to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent 77779a1 commit ad1f1b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions include/MessageOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
#include <AsyncWebSocket.h>
#include <HardwareSerial.h>
#include <Stream.h>
#include <TaskSchedulerDeclarations.h>
#include <mutex>

#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;
Expand Down
8 changes: 8 additions & 0 deletions src/MessageOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void setup()
while (!Serial)
yield();
#endif
MessageOutput.init(&scheduler);
MessageOutput.println();
MessageOutput.println("Starting OpenDTU");

Expand Down Expand Up @@ -157,6 +158,4 @@ void loop()
yield();
Display.loop();
yield();
MessageOutput.loop();
yield();
}

0 comments on commit ad1f1b6

Please sign in to comment.