From 003c130eef5242802ce487b1833b4a5277285215 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Wed, 19 Jan 2022 21:05:32 +0100 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=82=D1=80=D0=B8=20=D1=82=D0=B8=D0=BF=D0=B0=20=D0=BE=D1=87?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=B9:=20=D0=BE=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D1=8C=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B5=D0=B9,=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D1=8C=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80,=20?= =?UTF-8?q?=D0=B8=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8C=20char?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Main.h | 5 ++- include/classes/CommandBuf.h | 28 ------------- include/classes/QueueFromChar.h | 39 ++++++++++++++++++ .../{QueueBuf.h => QueueFromInstance.h} | 10 ++--- include/classes/QueueFromStruct.h | 41 +++++++++++++++++++ src/Main.cpp | 29 ------------- .../{CommandBuf.cpp => QueueFromChar.cpp} | 14 +++---- .../{QueueBuf.cpp => QueueFromInstance.cpp} | 14 +++---- src/classes/QueueFromStruct.cpp | 27 ++++++++++++ src/classes/QueueInst.cpp | 12 ++++++ 10 files changed, 141 insertions(+), 78 deletions(-) delete mode 100644 include/classes/CommandBuf.h create mode 100644 include/classes/QueueFromChar.h rename include/classes/{QueueBuf.h => QueueFromInstance.h} (66%) create mode 100644 include/classes/QueueFromStruct.h rename src/classes/{CommandBuf.cpp => QueueFromChar.cpp} (81%) rename src/classes/{QueueBuf.cpp => QueueFromInstance.cpp} (61%) create mode 100644 src/classes/QueueFromStruct.cpp diff --git a/include/Main.h b/include/Main.h index e9727402..ded2c075 100644 --- a/include/Main.h +++ b/include/Main.h @@ -9,6 +9,7 @@ #include "classes/NotAsync.h" #include "ESPConfiguration.h" #include "MqttClient.h" -#include "classes/CommandBuf.h" -#include "classes/QueueBuf.h" +#include "classes/QueueFromChar.h" +#include "classes/QueueFromInstance.h" +#include "classes/QueueFromStruct.h" #include "classes/QueueInst.h" diff --git a/include/classes/CommandBuf.h b/include/classes/CommandBuf.h deleted file mode 100644 index d2439a67..00000000 --- a/include/classes/CommandBuf.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "Global.h" - -#define MAX_COMMAND_LENGTH 16 -#define BUFFER 128 - -class CommandBuf; - -class CommandBuf { - public: - CommandBuf(); - ~CommandBuf(); - - void addCommand(const char* command); - - void printCommands(); - - String getLastCommand(); - - private: - struct CharBufferStruct { - char command[MAX_COMMAND_LENGTH + 1]; - }; - CharBufferStruct* commandList; - int commandCount = 0; -}; - -extern CommandBuf* myBuf; diff --git a/include/classes/QueueFromChar.h b/include/classes/QueueFromChar.h new file mode 100644 index 00000000..110c5a26 --- /dev/null +++ b/include/classes/QueueFromChar.h @@ -0,0 +1,39 @@ +#pragma once +#include "Global.h" + +#define MAX_COMMAND_LENGTH 16 +#define BUFFER 128 + +class QueueFromChar; + +class QueueFromChar { + public: + QueueFromChar(); + ~QueueFromChar(); + + void addCommand(const char* command); + + void printCommands(); + + String getLastCommand(); + + private: + struct CharBufferStruct { + char command[MAX_COMMAND_LENGTH + 1]; + }; + CharBufferStruct* commandList; + int commandCount = 0; +}; + +extern QueueFromChar* myBuf; + +//========проверка очереди===================== +// myBuf = new QueueFromChar; +// myBuf->addCommand("zero"); +// myBuf->addCommand("one"); +// myBuf->addCommand("two"); +// myBuf->printCommands(); +// myBuf->getLastCommand(); +// myBuf->getLastCommand(); +// myBuf->getLastCommand(); +// myBuf->printCommands(); diff --git a/include/classes/QueueBuf.h b/include/classes/QueueFromInstance.h similarity index 66% rename from include/classes/QueueBuf.h rename to include/classes/QueueFromInstance.h index 775fc6a0..67102226 100644 --- a/include/classes/QueueBuf.h +++ b/include/classes/QueueFromInstance.h @@ -6,12 +6,12 @@ using namespace std; -class QueueBuf; +class QueueFromInstance; -class QueueBuf { +class QueueFromInstance { public: - QueueBuf(); - ~QueueBuf(); + QueueFromInstance(); + ~QueueFromInstance(); void push(QueueInstance instance); void pop(); @@ -21,4 +21,4 @@ class QueueBuf { queue queue1; }; -extern QueueBuf* myQueue; +extern QueueFromInstance* myQueue; diff --git a/include/classes/QueueFromStruct.h b/include/classes/QueueFromStruct.h new file mode 100644 index 00000000..2ca7623f --- /dev/null +++ b/include/classes/QueueFromStruct.h @@ -0,0 +1,41 @@ +#pragma once +#include "Global.h" +#include +#include + +using namespace std; + +struct QueueItems { + String myword; +}; + +class QueueFromStruct; + +class QueueFromStruct { + public: + QueueFromStruct(); + ~QueueFromStruct(); + + void push(QueueItems word); + void pop(); + QueueItems front(); + + private: + queue queue1; + QueueItems tmpItem; +}; + +extern QueueFromStruct* myQueueStruct; + +//=======проверка очереди из структур================= +// myQueueStruct = new QueueFromStruct; +// QueueItems myItem; +// myItem.myword = "word1"; +// myQueueStruct->push(myItem); +// myItem.myword = "word2"; +// myQueueStruct->push(myItem); +// myItem.myword = "word3"; +// myQueueStruct->push(myItem); +// Serial.println(myQueueStruct->front().myword); +// Serial.println(myQueueStruct->front().myword); +// Serial.println(myQueueStruct->front().myword); diff --git a/src/Main.cpp b/src/Main.cpp index 949a4417..3394cf54 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -41,35 +41,6 @@ void setup() { sendConfigJson = new SendJson; sendWigdetsJson = new SendJson; - // myBuf = new CommandBuf; - // - // myBuf->addCommand("zero"); - // myBuf->addCommand("one"); - // myBuf->addCommand("two"); - // myBuf->printCommands(); - // - // myBuf->getLastCommand(); - // myBuf->getLastCommand(); - // myBuf->getLastCommand(); - // myBuf->printCommands(); - - myQueue = new QueueBuf; - - myQueue->push(*new QueueInstance("text1")); - myQueue->push(*new QueueInstance("text2")); - myQueue->push(*new QueueInstance("text3")); - - Serial.println(myQueue->front().get()); - Serial.println(myQueue->front().get()); - Serial.println(myQueue->front().get()); - - // myQueue->push(20); - // myQueue->push(30); - // - // Serial.println(myQueue->front()); - // Serial.println(myQueue->front()); - // Serial.println(myQueue->front()); - configure("/config.json"); //выводим остаток оперативной памяти после старта diff --git a/src/classes/CommandBuf.cpp b/src/classes/QueueFromChar.cpp similarity index 81% rename from src/classes/CommandBuf.cpp rename to src/classes/QueueFromChar.cpp index 50b16e5d..8f52955e 100644 --- a/src/classes/CommandBuf.cpp +++ b/src/classes/QueueFromChar.cpp @@ -1,13 +1,13 @@ -#include "classes/CommandBuf.h" +#include "classes/QueueFromChar.h" -CommandBuf::CommandBuf() { +QueueFromChar::QueueFromChar() { commandList = NULL; commandCount = 0; } -CommandBuf::~CommandBuf() {} +QueueFromChar::~QueueFromChar() {} //добавление команды в буфер -void CommandBuf::addCommand(const char* command) { +void QueueFromChar::addCommand(const char* command) { commandList = (CharBufferStruct*)realloc(commandList, (commandCount + 1) * sizeof(CharBufferStruct)); strncpy(commandList[commandCount].command, command, MAX_COMMAND_LENGTH); Serial.println("command added: " + String(command) + " " + String(commandCount)); @@ -15,7 +15,7 @@ void CommandBuf::addCommand(const char* command) { } //распечатаем все добавленные команды -void CommandBuf::printCommands() { +void QueueFromChar::printCommands() { if (commandCount > 0 && commandList != NULL) { for (int i = 0; i < commandCount; i++) { Serial.println(commandList[i].command); @@ -24,7 +24,7 @@ void CommandBuf::printCommands() { } //заберем последнюю из положенных в буфер команд -String CommandBuf::getLastCommand() { +String QueueFromChar::getLastCommand() { String ret = "empty"; if (commandList != NULL) { int cnt = commandCount - 1; @@ -40,4 +40,4 @@ String CommandBuf::getLastCommand() { return ret; } -CommandBuf* myBuf; +QueueFromChar* myBuf; diff --git a/src/classes/QueueBuf.cpp b/src/classes/QueueFromInstance.cpp similarity index 61% rename from src/classes/QueueBuf.cpp rename to src/classes/QueueFromInstance.cpp index ceffc4e3..50ff3e4f 100644 --- a/src/classes/QueueBuf.cpp +++ b/src/classes/QueueFromInstance.cpp @@ -1,22 +1,22 @@ -#include "classes/QueueBuf.h" +#include "classes/QueueFromInstance.h" -QueueBuf::QueueBuf() {} -QueueBuf::~QueueBuf() {} +QueueFromInstance::QueueFromInstance() {} +QueueFromInstance::~QueueFromInstance() {} //добавим элемент в конец очереди -void QueueBuf::push(QueueInstance instance) { +void QueueFromInstance::push(QueueInstance instance) { queue1.push(instance); } //удалим элемент из начала очереди -void QueueBuf::pop() { +void QueueFromInstance::pop() { if (!queue1.empty()) { queue1.pop(); } } //вернуть элемент из начала очереди и удалить его -QueueInstance QueueBuf::front() { +QueueInstance QueueFromInstance::front() { QueueInstance instance(""); if (!queue1.empty()) { instance = queue1.front(); @@ -25,4 +25,4 @@ QueueInstance QueueBuf::front() { return instance; } -QueueBuf* myQueue; +QueueFromInstance* myQueue; diff --git a/src/classes/QueueFromStruct.cpp b/src/classes/QueueFromStruct.cpp new file mode 100644 index 00000000..f44567cc --- /dev/null +++ b/src/classes/QueueFromStruct.cpp @@ -0,0 +1,27 @@ +#include "classes/QueueFromStruct.h" + +QueueFromStruct::QueueFromStruct() {} +QueueFromStruct::~QueueFromStruct() {} + +//добавим элемент в конец очереди +void QueueFromStruct::push(QueueItems word) { + queue1.push(word); +} + +//удалим элемент из начала очереди +void QueueFromStruct::pop() { + if (!queue1.empty()) { + queue1.pop(); + } +} + +//вернуть элемент из начала очереди и удалить его +QueueItems QueueFromStruct::front() { + if (!queue1.empty()) { + tmpItem = queue1.front(); + queue1.pop(); + } + return tmpItem; +} + +QueueFromStruct* myQueueStruct; diff --git a/src/classes/QueueInst.cpp b/src/classes/QueueInst.cpp index 99a76f17..f31b0ac5 100644 --- a/src/classes/QueueInst.cpp +++ b/src/classes/QueueInst.cpp @@ -8,3 +8,15 @@ QueueInstance::~QueueInstance() {} String QueueInstance::get() { return _text; } + +//========проверка очереди из экземпляров====== + +// myQueue = new QueueFromInstance; + +// myQueue->push(QueueInstance("text1")); +// myQueue->push(QueueInstance("text2")); +// myQueue->push(QueueInstance("text3")); + +// Serial.println(myQueue->front().get()); +// Serial.println(myQueue->front().get()); +// Serial.println(myQueue->front().get()); \ No newline at end of file