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;