forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageOutput.cpp
54 lines (46 loc) · 1.17 KB
/
MessageOutput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
*/
#include "MessageOutput.h"
#include <Arduino.h>
MessageOutputClass MessageOutput;
#define MSG_LOCK() xSemaphoreTake(_lock, portMAX_DELAY)
#define MSG_UNLOCK() xSemaphoreGive(_lock)
MessageOutputClass::MessageOutputClass()
{
_lock = xSemaphoreCreateMutex();
MSG_UNLOCK();
}
void MessageOutputClass::register_ws_output(AsyncWebSocket* output)
{
_ws = output;
}
size_t MessageOutputClass::write(uint8_t c)
{
if (_buff_pos < BUFFER_SIZE) {
MSG_LOCK();
_buffer[_buff_pos] = c;
_buff_pos++;
MSG_UNLOCK();
} else {
_forceSend = true;
}
return Serial.write(c);
}
void MessageOutputClass::loop()
{
// Send data via websocket if either time is over or buffer is full
if (_forceSend || (millis() - _lastSend > 1000)) {
MSG_LOCK();
if (_ws && _buff_pos > 0) {
_ws->textAll(_buffer, _buff_pos);
_buff_pos = 0;
}
if(_forceSend) {
_buff_pos = 0;
}
MSG_UNLOCK();
_forceSend = false;
}
}