кольцевой буфер

This commit is contained in:
Dmitry Borisenko
2021-12-21 00:30:05 +01:00
parent 329d1cd382
commit 16cecfb353
10 changed files with 211 additions and 39 deletions

View File

@@ -1,7 +0,0 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

View File

@@ -0,0 +1,106 @@
#pragma once
#include <Arduino.h>
template <typename T, size_t BUFFER_SIZE>
class CircularBuffer {
public:
CircularBuffer() : _head{0}, _tail{0}, _full{false} {}
~CircularBuffer() {}
void reset() {
_head = _tail = _full = 0;
}
bool empty() const {
return _head == _tail && !_full;
}
bool full() const {
return _full;
}
/**
* Get the number of elements currently stored in the circular_buffer.
*/
size_t size() const {
size_t res = 0;
if (!_full) {
if (_head < _tail)
res = BUFFER_SIZE + _head - _tail;
else
res = _head - _tail;
} else {
res = BUFFER_SIZE;
}
return res;
}
/** Push the transaction to the buffer. This overwrites the buffer if it's full.
* Загрузить данные в буфер
* @param data item to be pushed to the buffer.
*/
void push(const T &item) {
if (_full) {
_tail++;
if (_tail == BUFFER_SIZE)
_tail = 0;
}
_pool[_head++] = item;
if (_head == BUFFER_SIZE)
_head = 0;
if (_head == _tail)
_full = true;
}
/** Pop from the buffer.
* Забрать данные из буфера
* @param data item to store the data to be popped from the buffer.
* @return True if data popped.
*/
bool pop(T &item) {
bool res = false;
if (!empty()) {
item = _pool[_tail++];
if (_tail == BUFFER_SIZE) _tail = 0;
_full = false;
res = true;
}
return res;
}
bool pop_back(T &item) {
bool res = false;
if (!empty()) {
item = _pool[--_head];
_full = false;
res = true;
}
return res;
}
/** Peek into circular buffer without popping.
*
* @param data item to be peeked from the buffer.
* @return True if the buffer is not empty and data contains a transaction, false otherwise.
*/
bool peek(T &item) const {
bool res = false;
if (!empty()) {
item = _pool[_tail];
res = true;
}
return res;
}
private:
T _pool[BUFFER_SIZE];
size_t _head;
size_t _tail;
bool _full;
};
extern CircularBuffer<char *, 20480> *myWsBuffer;

View File

@@ -66,6 +66,10 @@ extern int mqttConnectAttempts;
extern bool changeBroker;
extern int currentBroker;
// web sockets
extern int wsAttempts;
//extern char* wsBufChar;
// orders and events
extern String orderBuf;
extern String wsBuf;

View File

@@ -1,3 +1,4 @@
#pragma once
extern void testsPerform();
extern void testLoop();

View File

@@ -1,5 +1,5 @@
#pragma once
#include "Class/CircularBuffer.h"
#include "Global.h"
void wsInit();
void wsSendSetup();

View File

@@ -0,0 +1,3 @@
#include "Class/CircularBuffer.h"
CircularBuffer<char *, 20480> *myWsBuffer;

View File

@@ -44,6 +44,10 @@ int mqttConnectAttempts = 0;
bool changeBroker = false;
int currentBroker = 1;
// web sockets
int wsAttempts = 100;
//char* wsBufChar = "";
// orders and events
String orderBuf = "";
String wsBuf = "";

View File

@@ -1,6 +1,7 @@
#include "Tests.h"
#include "BufferExecute.h"
#include "Class/CircularBuffer.h"
#include "Global.h"
#include "ItemsList.h"
#include "Macro.h"
@@ -8,13 +9,28 @@
#include "Utils/StringUtils.h"
void testsPerform() {
//Serial.println("====some tests section====");
////===========================================================================
Serial.println("====some tests section====");
//ТЕСТ КОЛЬЦЕВОГО БУФЕРА=============================================================================
// CircularBuffer<char*, 2048>* myCircularBuffer;
// myCircularBuffer = new CircularBuffer<char*, 1024>;
//
// char* buf = "text";
//
// for (int i = 1; i <= 5; i++) {
// myCircularBuffer->push(buf);
//}
//
// char* item;
//
// while (myCircularBuffer->pop(item)) {
// Serial.println(item);
//}
//===================================================================================================
// String str = "0;1;2;3;4";
// char* mychar = new char[str.length() + 1];
// strcpy(mychar, str.c_str());
// test(mychar);
////===========================================================================
//===========================================================================
// String myJson;
// const int capacity = JSON_ARRAY_SIZE(2) + 3 * JSON_OBJECT_SIZE(3);
// StaticJsonBuffer<capacity> jb;
@@ -29,10 +45,16 @@ void testsPerform() {
//
//
//
////===========================================================================
////Serial.println(isDigitDotCommaStr("-12552.5555"));
////String str = "Geeks for Geeks ";
////Serial.println(itemsCount2(str, " "));
//===========================================================================
// Serial.println(isDigitDotCommaStr("-12552.5555"));
// String str = "Geeks for Geeks ";
// Serial.println(itemsCount2(str, " "));
//
//Serial.println("==========end============");
Serial.println("==========end============");
}
void testLoop() {
// char* item;
// myCircularBuffer->pop(item);
// Serial.println(item);
}

View File

@@ -1,6 +1,7 @@
#include "WebSocket.h"
#include "ArduinoJson.h"
#include "Class/CircularBuffer.h"
#include "Class/NotAsync.h"
#include "Global.h"
@@ -22,11 +23,10 @@ void wsPublishData(String topic, String data) {
}
}
//отправка setup массива в sockets способом через string
//отправка setup массива в sockets способом через буфер string, рабочий способ но буфер стринг - плохой метод
void wsSendSetup() {
File file = seekFile("/setup.json");
DynamicJsonDocument doc(2048);
AsyncWebSocketMessageBuffer(20480);
int i = 0;
file.find("[");
SerialPrint("I", F("WS"), F("start send config"));
@@ -41,15 +41,52 @@ void wsSendSetup() {
}
void loopWsExecute() {
static int attampts = wsAttempts;
if (wsBuf.length()) {
if (attampts > 0) {
if (ws.availableForWriteAll()) {
String tmp = selectToMarker(wsBuf, "\n");
wsPublishData("config", tmp);
wsBuf = deleteBeforeDelimiter(wsBuf, "\n");
attampts = wsAttempts;
} else {
attampts--;
SerialPrint("I", F("WS"), String(attampts));
}
} else {
SerialPrint("I", F("WS"), F("socket fatal error"));
attampts = wsAttempts;
}
}
}
//отправка setup массива в sockets способом через кольщевой буфер char
void wsSendSetup2() {
// myWsBuffer = new CircularBuffer<char*, 20480>;
// File file = seekFile("/setup.json");
// DynamicJsonDocument doc(2048);
// int i = 0;
// file.find("[");
// SerialPrint("I", F("WS"), F("start send config"));
// do {
// i++;
// deserializeJson(doc, file);
// char* element = "";
// serializeJson(doc, (char*)element, 1024);
// Serial.println(element);
// myWsBuffer->push(element);
//
//} while (file.findUntil(",", "]"));
// SerialPrint("I", F("WS"), F("completed send config"));
}
void loopWsExecute2() {
// char* item;
// if (myWsBuffer->pop(item)) {
// Serial.println(item);
// }
}
//отправка setup массива в sockets способом прямой загрузки в ws buffer
void wsSendSetupBuffer() {
File file = seekFile("/setup.json");

View File

@@ -85,7 +85,7 @@ void setup() {
getFSInfo();
// testsPerform();
testsPerform();
just_load = false;
initialized = true;
@@ -109,6 +109,8 @@ void loop() {
return;
}
testLoop();
if (wsSetupFlag) {
wsSetupFlag = false;
wsSendSetup();