ClawMachineCosplay/src/ZCodeParser.h

95 lines
2.6 KiB
C
Raw Normal View History

2025-01-20 15:13:01 +01:00
#include <Arduino.h>
#include <vector>
#include <Shared/ZCommand.h>
2025-01-20 15:13:01 +01:00
class ZCodeParser
2025-01-20 15:13:01 +01:00
{
private:
static void splitString(const char *input, char delimiter, std::vector<String> &result);
2025-01-20 15:13:01 +01:00
public:
static std::vector<ZCommand> ParseString(const String &inString);
2025-01-20 15:13:01 +01:00
};
void ZCodeParser::splitString(const char *input, char delimiter, std::vector<String> &result)
{
// Serial.println("Splitting String: ");
2025-01-20 15:13:01 +01:00
result.clear();
size_t start = 0;
size_t len = strlen(input);
for (size_t i = 0; i <= len; i++)
{
if (input[i] == delimiter || input[i] == '\0')
{
if (i > start)
{
2025-01-22 04:29:06 +01:00
result.push_back(String(input + start).substring(0, i - start)); // Fix applied here
}
start = i + 1;
2025-01-20 15:13:01 +01:00
}
2025-01-20 20:53:54 +01:00
}
// Serial.println("Split Result:");
// for (const auto &s : result)
// {
// Serial.print("[" + s + "] ");
// }
// Serial.println();
2025-01-20 20:53:54 +01:00
}
std::vector<ZCommand> ZCodeParser::ParseString(const String &inString)
{
// Serial.println("\n--- Parsing Started ---");
// Serial.println("Heap Before Parsing: " + String(ESP.getFreeHeap()));
std::vector<String> stringCommands;
splitString(inString.c_str(), '\n', stringCommands);
2025-01-20 20:53:54 +01:00
// Serial.println("After Splitting into Commands:");
// for (size_t i = 0; i < stringCommands.size(); i++)
// {
// Serial.println("Command[" + String(i) + "]: " + stringCommands[i]);
// }
2025-01-20 20:53:54 +01:00
// Remove comments
for (String &command : stringCommands)
2025-01-20 20:53:54 +01:00
{
int commentIndex = command.indexOf(';');
if (commentIndex != -1)
{
command = command.substring(0, commentIndex);
// Serial.println("Comment Removed: " + command);
}
2025-01-20 15:13:01 +01:00
}
std::vector<ZCommand> commands;
for (const String &commandLine : stringCommands)
2025-01-20 15:13:01 +01:00
{
std::vector<String> separatedStrings;
splitString(commandLine.c_str(), ' ', separatedStrings);
if (separatedStrings.empty())
continue;
ZCommand command;
command.command = separatedStrings[0];
// Serial.println("Processing Command: " + command.command);
for (size_t j = 1; j < separatedStrings.size(); j++)
{
2025-01-22 04:29:06 +01:00
command.params.push_back(separatedStrings[j]);
// Serial.println("Added Param: " + command.params.back());
}
2025-01-22 04:29:06 +01:00
commands.push_back(command);
// Serial.println("Stored Command: " + commands.back().command);
2025-01-20 20:53:54 +01:00
}
// Serial.println("Heap After Parsing: " + String(ESP.getFreeHeap()));
// Serial.println("--- Parsing Finished ---\n");
return commands;
}