mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 11:59:12 +03:00
кольцевой буфер
This commit is contained in:
7
.vscode/extensions.json
vendored
7
.vscode/extensions.json
vendored
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
|
||||||
// for the documentation about the extensions.json format
|
|
||||||
"recommendations": [
|
|
||||||
"platformio.platformio-ide"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
106
include/Class/CircularBuffer.h
Normal file
106
include/Class/CircularBuffer.h
Normal 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;
|
||||||
@@ -66,6 +66,10 @@ extern int mqttConnectAttempts;
|
|||||||
extern bool changeBroker;
|
extern bool changeBroker;
|
||||||
extern int currentBroker;
|
extern int currentBroker;
|
||||||
|
|
||||||
|
// web sockets
|
||||||
|
extern int wsAttempts;
|
||||||
|
//extern char* wsBufChar;
|
||||||
|
|
||||||
// orders and events
|
// orders and events
|
||||||
extern String orderBuf;
|
extern String orderBuf;
|
||||||
extern String wsBuf;
|
extern String wsBuf;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
extern void testsPerform();
|
extern void testsPerform();
|
||||||
|
extern void testLoop();
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Class/CircularBuffer.h"
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
void wsInit();
|
void wsInit();
|
||||||
void wsSendSetup();
|
void wsSendSetup();
|
||||||
|
|||||||
3
src/Class/CircularBuffer.cpp
Normal file
3
src/Class/CircularBuffer.cpp
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include "Class/CircularBuffer.h"
|
||||||
|
|
||||||
|
CircularBuffer<char *, 20480> *myWsBuffer;
|
||||||
@@ -44,6 +44,10 @@ int mqttConnectAttempts = 0;
|
|||||||
bool changeBroker = false;
|
bool changeBroker = false;
|
||||||
int currentBroker = 1;
|
int currentBroker = 1;
|
||||||
|
|
||||||
|
// web sockets
|
||||||
|
int wsAttempts = 100;
|
||||||
|
//char* wsBufChar = "";
|
||||||
|
|
||||||
// orders and events
|
// orders and events
|
||||||
String orderBuf = "";
|
String orderBuf = "";
|
||||||
String wsBuf = "";
|
String wsBuf = "";
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "Tests.h"
|
#include "Tests.h"
|
||||||
|
|
||||||
#include "BufferExecute.h"
|
#include "BufferExecute.h"
|
||||||
|
#include "Class/CircularBuffer.h"
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "ItemsList.h"
|
#include "ItemsList.h"
|
||||||
#include "Macro.h"
|
#include "Macro.h"
|
||||||
@@ -8,31 +9,52 @@
|
|||||||
#include "Utils/StringUtils.h"
|
#include "Utils/StringUtils.h"
|
||||||
|
|
||||||
void testsPerform() {
|
void testsPerform() {
|
||||||
//Serial.println("====some tests section====");
|
Serial.println("====some tests section====");
|
||||||
////===========================================================================
|
//ТЕСТ КОЛЬЦЕВОГО БУФЕРА=============================================================================
|
||||||
//String str = "0;1;2;3;4";
|
// CircularBuffer<char*, 2048>* myCircularBuffer;
|
||||||
//char* mychar = new char[str.length() + 1];
|
// myCircularBuffer = new CircularBuffer<char*, 1024>;
|
||||||
//strcpy(mychar, str.c_str());
|
//
|
||||||
//test(mychar);
|
// char* buf = "text";
|
||||||
////===========================================================================
|
//
|
||||||
//String myJson;
|
// for (int i = 1; i <= 5; i++) {
|
||||||
//const int capacity = JSON_ARRAY_SIZE(2) + 3 * JSON_OBJECT_SIZE(3);
|
// myCircularBuffer->push(buf);
|
||||||
//StaticJsonBuffer<capacity> jb;
|
//}
|
||||||
//JsonArray& arr = jb.createArray();
|
//
|
||||||
//JsonObject& obj1 = jb.createObject();
|
// char* item;
|
||||||
//obj1["test1"] = 1;
|
//
|
||||||
//obj1["test2"] = 2;
|
// while (myCircularBuffer->pop(item)) {
|
||||||
//obj1["test3"] = 3;
|
// Serial.println(item);
|
||||||
//arr.add(obj1);
|
//}
|
||||||
//arr.printTo(myJson);
|
//===================================================================================================
|
||||||
//Serial.println(myJson);
|
// 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;
|
||||||
|
// JsonArray& arr = jb.createArray();
|
||||||
|
// JsonObject& obj1 = jb.createObject();
|
||||||
|
// obj1["test1"] = 1;
|
||||||
|
// obj1["test2"] = 2;
|
||||||
|
// obj1["test3"] = 3;
|
||||||
|
// arr.add(obj1);
|
||||||
|
// arr.printTo(myJson);
|
||||||
|
// Serial.println(myJson);
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
////===========================================================================
|
//===========================================================================
|
||||||
////Serial.println(isDigitDotCommaStr("-12552.5555"));
|
// Serial.println(isDigitDotCommaStr("-12552.5555"));
|
||||||
////String str = "Geeks for Geeks ";
|
// String str = "Geeks for Geeks ";
|
||||||
////Serial.println(itemsCount2(str, " "));
|
// Serial.println(itemsCount2(str, " "));
|
||||||
//
|
//
|
||||||
//Serial.println("==========end============");
|
Serial.println("==========end============");
|
||||||
|
}
|
||||||
|
|
||||||
|
void testLoop() {
|
||||||
|
// char* item;
|
||||||
|
// myCircularBuffer->pop(item);
|
||||||
|
// Serial.println(item);
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "WebSocket.h"
|
#include "WebSocket.h"
|
||||||
|
|
||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
|
#include "Class/CircularBuffer.h"
|
||||||
#include "Class/NotAsync.h"
|
#include "Class/NotAsync.h"
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
|
|
||||||
@@ -22,11 +23,10 @@ void wsPublishData(String topic, String data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//отправка setup массива в sockets способом через string
|
//отправка setup массива в sockets способом через буфер string, рабочий способ но буфер стринг - плохой метод
|
||||||
void wsSendSetup() {
|
void wsSendSetup() {
|
||||||
File file = seekFile("/setup.json");
|
File file = seekFile("/setup.json");
|
||||||
DynamicJsonDocument doc(2048);
|
DynamicJsonDocument doc(2048);
|
||||||
AsyncWebSocketMessageBuffer(20480);
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
file.find("[");
|
file.find("[");
|
||||||
SerialPrint("I", F("WS"), F("start send config"));
|
SerialPrint("I", F("WS"), F("start send config"));
|
||||||
@@ -41,15 +41,52 @@ void wsSendSetup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loopWsExecute() {
|
void loopWsExecute() {
|
||||||
|
static int attampts = wsAttempts;
|
||||||
if (wsBuf.length()) {
|
if (wsBuf.length()) {
|
||||||
|
if (attampts > 0) {
|
||||||
if (ws.availableForWriteAll()) {
|
if (ws.availableForWriteAll()) {
|
||||||
String tmp = selectToMarker(wsBuf, "\n");
|
String tmp = selectToMarker(wsBuf, "\n");
|
||||||
wsPublishData("config", tmp);
|
wsPublishData("config", tmp);
|
||||||
wsBuf = deleteBeforeDelimiter(wsBuf, "\n");
|
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
|
//отправка setup массива в sockets способом прямой загрузки в ws buffer
|
||||||
void wsSendSetupBuffer() {
|
void wsSendSetupBuffer() {
|
||||||
File file = seekFile("/setup.json");
|
File file = seekFile("/setup.json");
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ void setup() {
|
|||||||
|
|
||||||
getFSInfo();
|
getFSInfo();
|
||||||
|
|
||||||
// testsPerform();
|
testsPerform();
|
||||||
|
|
||||||
just_load = false;
|
just_load = false;
|
||||||
initialized = true;
|
initialized = true;
|
||||||
@@ -109,6 +109,8 @@ void loop() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testLoop();
|
||||||
|
|
||||||
if (wsSetupFlag) {
|
if (wsSetupFlag) {
|
||||||
wsSetupFlag = false;
|
wsSetupFlag = false;
|
||||||
wsSendSetup();
|
wsSendSetup();
|
||||||
|
|||||||
Reference in New Issue
Block a user