From a4fa6ad38907f8a94bab40f0dd4c42be95ca6191 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Wed, 19 Jan 2022 00:18:06 +0100 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B1=D1=83=D1=84=D1=84=D0=B5=D1=80=20char?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Main.h | 1 + include/classes/CommandBuf.h | 28 +++++++++++++++++++++++ src/Main.cpp | 12 ++++++++++ src/classes/CommandBuf.cpp | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 include/classes/CommandBuf.h create mode 100644 src/classes/CommandBuf.cpp diff --git a/include/Main.h b/include/Main.h index 222a55be..67d2dbb8 100644 --- a/include/Main.h +++ b/include/Main.h @@ -9,3 +9,4 @@ #include "classes/NotAsync.h" #include "ESPConfiguration.h" #include "MqttClient.h" +#include "classes/CommandBuf.h" diff --git a/include/classes/CommandBuf.h b/include/classes/CommandBuf.h new file mode 100644 index 00000000..d2439a67 --- /dev/null +++ b/include/classes/CommandBuf.h @@ -0,0 +1,28 @@ +#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/src/Main.cpp b/src/Main.cpp index 3394cf54..5ceef3e7 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -41,6 +41,18 @@ 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(); + configure("/config.json"); //выводим остаток оперативной памяти после старта diff --git a/src/classes/CommandBuf.cpp b/src/classes/CommandBuf.cpp new file mode 100644 index 00000000..50b16e5d --- /dev/null +++ b/src/classes/CommandBuf.cpp @@ -0,0 +1,43 @@ +#include "classes/CommandBuf.h" + +CommandBuf::CommandBuf() { + commandList = NULL; + commandCount = 0; +} +CommandBuf::~CommandBuf() {} + +//добавление команды в буфер +void CommandBuf::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)); + commandCount++; +} + +//распечатаем все добавленные команды +void CommandBuf::printCommands() { + if (commandCount > 0 && commandList != NULL) { + for (int i = 0; i < commandCount; i++) { + Serial.println(commandList[i].command); + } + } +} + +//заберем последнюю из положенных в буфер команд +String CommandBuf::getLastCommand() { + String ret = "empty"; + if (commandList != NULL) { + int cnt = commandCount - 1; + ret = commandList[cnt].command; + if (cnt > 0) { + delete commandList[cnt].command; + } else if (cnt == 0) { + commandList = NULL; + } + Serial.println("command deleted: " + ret + " " + String(cnt)); + commandCount--; + } + return ret; +} + +CommandBuf* myBuf;