mirror of
https://github.com/tbnobody/OpenDTU.git
synced 2026-03-26 10:57:58 +01:00
Fix: Virtual console scrambled output when the output came from different contexts
This commit is contained in:
@@ -8,17 +8,6 @@
|
||||
|
||||
MessageOutputClass MessageOutput;
|
||||
|
||||
#define MSG_LOCK() \
|
||||
do { \
|
||||
} while (xSemaphoreTake(_lock, portMAX_DELAY) != pdPASS)
|
||||
#define MSG_UNLOCK() xSemaphoreGive(_lock)
|
||||
|
||||
MessageOutputClass::MessageOutputClass()
|
||||
{
|
||||
_lock = xSemaphoreCreateMutex();
|
||||
MSG_UNLOCK();
|
||||
}
|
||||
|
||||
void MessageOutputClass::register_ws_output(AsyncWebSocket* output)
|
||||
{
|
||||
_ws = output;
|
||||
@@ -27,10 +16,9 @@ void MessageOutputClass::register_ws_output(AsyncWebSocket* output)
|
||||
size_t MessageOutputClass::write(uint8_t c)
|
||||
{
|
||||
if (_buff_pos < BUFFER_SIZE) {
|
||||
MSG_LOCK();
|
||||
std::lock_guard<std::mutex> lock(_msgLock);
|
||||
_buffer[_buff_pos] = c;
|
||||
_buff_pos++;
|
||||
MSG_UNLOCK();
|
||||
} else {
|
||||
_forceSend = true;
|
||||
}
|
||||
@@ -38,11 +26,23 @@ size_t MessageOutputClass::write(uint8_t c)
|
||||
return Serial.write(c);
|
||||
}
|
||||
|
||||
size_t MessageOutputClass::write(const uint8_t* buffer, size_t size)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_msgLock);
|
||||
if (_buff_pos + size < BUFFER_SIZE) {
|
||||
memcpy(&_buffer[_buff_pos], buffer, size);
|
||||
_buff_pos += size;
|
||||
}
|
||||
_forceSend = true;
|
||||
|
||||
return Serial.write(buffer, size);
|
||||
}
|
||||
|
||||
void MessageOutputClass::loop()
|
||||
{
|
||||
// Send data via websocket if either time is over or buffer is full
|
||||
if (_forceSend || (millis() - _lastSend > 1000)) {
|
||||
MSG_LOCK();
|
||||
std::lock_guard<std::mutex> lock(_msgLock);
|
||||
if (_ws && _buff_pos > 0) {
|
||||
_ws->textAll(_buffer, _buff_pos);
|
||||
_buff_pos = 0;
|
||||
@@ -50,7 +50,6 @@ void MessageOutputClass::loop()
|
||||
if (_forceSend) {
|
||||
_buff_pos = 0;
|
||||
}
|
||||
MSG_UNLOCK();
|
||||
_forceSend = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user