streamline GCodeParser

This commit is contained in:
2019-07-23 16:43:00 +03:00
parent 86770edc66
commit 8d5cf430df
2 changed files with 42 additions and 38 deletions

View File

View File

@@ -1,19 +1,19 @@
#include <Arduino.h> #include <Arduino.h>
#include <unordered_map>
#include "GCodeParser.h" #include "GCodeParser.h"
Command bufcmd; Command bufcmd;
Command parseGCode(String gcode) { Command parseGCode(String gcode) {
char commandStringIn[50];
char commandString[50]; char commandString[50];
std::unordered_map<char, float> argsMap;
gcode.toCharArray(commandStringIn, 50); gcode.toCharArray(commandString, 50);
// Convert command to uppercase // Convert command to uppercase
for (int i = 0; commandStringIn[i] != '\0'; i++) { for (int i = 0; commandString[i] != '\0'; i++) {
commandString[i] = toupper(commandStringIn[i]); commandString[i] = toupper(commandString[i]);
commandString[i + 1] = '\0';
} }
char command[4]; char command[4];
@@ -23,8 +23,7 @@ Command parseGCode(String gcode) {
char args[45]; char args[45];
strncpy(args, &commandString[4], 45); strncpy(args, &commandString[4], 45);
if (strcmp(command, "G01") == 0 || strcmp(command, "G00") == 0) { char split_args[6][20];
char split_args[3][40];
memset(split_args, 0, sizeof(split_args)); memset(split_args, 0, sizeof(split_args));
char* arg; char* arg;
int argc = 0; int argc = 0;
@@ -40,25 +39,30 @@ Command parseGCode(String gcode) {
// Iterate through arguments // Iterate through arguments
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
arg = split_args[i]; arg = split_args[i];
char axis[2]; char axis;
char value[10]; char value[10];
strncpy(axis, arg, 1); axis = arg[0];
axis[1] = '\0';
strncpy(value, &arg[1], 10); strncpy(value, &arg[1], 10);
float floatValue; float floatValue;
floatValue = atof(value); floatValue = atof(value);
if (strcmp(axis, "X") == 0) { argsMap.emplace(axis, floatValue);
bufcmd.arg1 = floatValue;
} else if (strcmp(axis, "Y") == 0) {
bufcmd.arg2 = floatValue;
} else if (strcmp(axis, "Z") == 0) {
bufcmd.arg3 = floatValue;
} }
if (strcmp(command, "G01") == 0 || strcmp(command, "G00") == 0) {
if(argsMap.count('X') > 0) {
bufcmd.arg1 = argsMap['X'];
} }
if (argsMap.count('Y') > 0) {
bufcmd.arg2 = argsMap['Y'];
}
if (argsMap.count('Z') > 0) {
bufcmd.arg3 = argsMap['Z'];
}
if (strcmp(command, "G00") == 0) { if (strcmp(command, "G00") == 0) {
bufcmd.type = CommandType::G00; bufcmd.type = CommandType::G00;
return bufcmd; return bufcmd;