добавил буффер char

This commit is contained in:
Dmitry Borisenko
2022-01-19 00:18:06 +01:00
parent 3abad88fc3
commit a4fa6ad389
4 changed files with 84 additions and 0 deletions

View File

@@ -9,3 +9,4 @@
#include "classes/NotAsync.h"
#include "ESPConfiguration.h"
#include "MqttClient.h"
#include "classes/CommandBuf.h"

View File

@@ -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;

View File

@@ -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");
//выводим остаток оперативной памяти после старта

View File

@@ -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;