Implemented class to send debug output to uart as well as to websocket
This commit is contained in:
parent
f689fedf4e
commit
cd5d5edd5f
27
include/MessageOutput.h
Normal file
27
include/MessageOutput.h
Normal file
@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
|
||||
#include <AsyncWebSocket.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <Stream.h>
|
||||
|
||||
#define BUFFER_SIZE 500
|
||||
|
||||
class MessageOutputClass : public Print {
|
||||
public:
|
||||
MessageOutputClass();
|
||||
void loop();
|
||||
size_t write(uint8_t c);
|
||||
void register_ws_output(AsyncWebSocket* output);
|
||||
|
||||
private:
|
||||
AsyncWebSocket* _ws = NULL;
|
||||
char _buffer[BUFFER_SIZE];
|
||||
uint16_t _buff_pos = 0;
|
||||
uint32_t _lastSend = 0;
|
||||
bool _forceSend = false;
|
||||
|
||||
SemaphoreHandle_t _lock;
|
||||
};
|
||||
|
||||
extern MessageOutputClass MessageOutput;
|
||||
54
src/MessageOutput.cpp
Normal file
54
src/MessageOutput.cpp
Normal file
@ -0,0 +1,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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user