2025-01-20 15:13:01 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
typedef struct {
|
2025-01-20 20:53:54 +01:00
|
|
|
String command;
|
|
|
|
std::vector<String> params;
|
|
|
|
} GCommand;
|
2025-01-20 15:13:01 +01:00
|
|
|
|
2025-01-20 20:53:54 +01:00
|
|
|
class ZCodeParser // Variant of GCode, but made by Zhen. Lol
|
2025-01-20 15:13:01 +01:00
|
|
|
{
|
|
|
|
private:
|
2025-01-20 20:53:54 +01:00
|
|
|
static std::vector<String> splitString(const String &input, char delimiter);
|
2025-01-20 15:13:01 +01:00
|
|
|
public:
|
2025-01-20 20:53:54 +01:00
|
|
|
static std::vector<GCommand> ParseString(const String inString);
|
2025-01-20 15:13:01 +01:00
|
|
|
};
|
|
|
|
|
2025-01-20 20:53:54 +01:00
|
|
|
std::vector<String> ZCodeParser::splitString(const String &input, char delimiter) {
|
|
|
|
std::vector<String> result;
|
2025-01-20 15:13:01 +01:00
|
|
|
int startIndex = 0;
|
|
|
|
int endIndex;
|
|
|
|
|
2025-01-20 20:53:54 +01:00
|
|
|
while (true) {
|
|
|
|
endIndex = input.indexOf(delimiter, startIndex);
|
|
|
|
if (endIndex == -1) {
|
|
|
|
result.push_back(input.substring(startIndex)); // Get the rest of the string
|
2025-01-20 15:13:01 +01:00
|
|
|
break;
|
|
|
|
}
|
2025-01-20 20:53:54 +01:00
|
|
|
result.push_back(input.substring(startIndex, endIndex));
|
|
|
|
startIndex = endIndex + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<GCommand> ZCodeParser::ParseString(const String inString){
|
|
|
|
// G0 = move, no block
|
|
|
|
// G1 = move, block
|
|
|
|
// M17 = Enable Stepper
|
|
|
|
// M18 = Disable Stepper
|
|
|
|
// ; = comments
|
|
|
|
// \n = new command
|
|
|
|
|
|
|
|
std::vector<String> stringCommands = ZCodeParser::splitString(inString, '\n');
|
|
|
|
|
|
|
|
// Remove comments
|
|
|
|
for (size_t i = 0; i < stringCommands.size(); i++)
|
|
|
|
{
|
|
|
|
int commentIndex = stringCommands[i].indexOf(';');
|
|
|
|
if (commentIndex == -1) continue; // Continue if no comments
|
2025-01-20 15:13:01 +01:00
|
|
|
|
2025-01-20 20:53:54 +01:00
|
|
|
stringCommands[i] = stringCommands[i].substring(0, commentIndex);
|
2025-01-20 15:13:01 +01:00
|
|
|
}
|
|
|
|
|
2025-01-20 20:53:54 +01:00
|
|
|
// Extract function name and params
|
|
|
|
std::vector<GCommand> commnads;
|
2025-01-20 15:13:01 +01:00
|
|
|
for (size_t i = 0; i < stringCommands.size(); i++)
|
|
|
|
{
|
2025-01-20 20:53:54 +01:00
|
|
|
std::vector<String> seperatedStrings = ZCodeParser::splitString(stringCommands[i], ' ');
|
|
|
|
GCommand commnad;
|
|
|
|
commnad.command = seperatedStrings[0];
|
|
|
|
stringCommands.erase(stringCommands.begin());
|
|
|
|
commnad.params = stringCommands;
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("Parsed: ");
|
|
|
|
for (size_t i = 0; i < commnads.size(); i++)
|
|
|
|
{
|
|
|
|
Serial.print(commnads[i].command);
|
2025-01-20 15:13:01 +01:00
|
|
|
Serial.print(", ");
|
|
|
|
}
|
|
|
|
Serial.println("\n");
|
2025-01-20 20:53:54 +01:00
|
|
|
|
|
|
|
// return ;
|
2025-01-20 15:13:01 +01:00
|
|
|
}
|