Browse Source

More parser comments, optimize code_seen, save with goto

Scott Lahteine 9 years ago
parent
commit
adc8fcb77f
1 changed files with 13 additions and 8 deletions
  1. 13
    8
      Marlin/Marlin_main.cpp

+ 13
- 8
Marlin/Marlin_main.cpp View File

@@ -938,7 +938,7 @@ long code_value_long() { return strtol(seen_pointer + 1, NULL, 10); }
938 938
 int16_t code_value_short() { return (int16_t)strtol(seen_pointer + 1, NULL, 10); }
939 939
 
940 940
 bool code_seen(char code) {
941
-  seen_pointer = strchr(current_command, code);
941
+  seen_pointer = strchr(current_command + 3, code); // +3 since "G0 " is the shortest prefix
942 942
   return (seen_pointer != NULL);  //Return True if a character was found
943 943
 }
944 944
 
@@ -5184,16 +5184,18 @@ void process_next_command() {
5184 5184
   // Get the command code, which must be G, M, or T
5185 5185
   char command_code = *current_command;
5186 5186
 
5187
-  bool code_is_good = code_has_value();
5187
+  // The code must have a numeric value
5188
+  bool code_is_good = (current_command[1] >= '0' && current_command[1] <= '9');
5188 5189
 
5189
-  if (!code_is_good) {
5190
-    unknown_command_error();
5191
-    ok_to_send();
5192
-    return;
5193
-  }
5190
+  int codenum; // define ahead of goto
5194 5191
 
5195
-  int codenum = code_value_short();
5192
+  // Bail early if there's no code
5193
+  if (!code_is_good) goto ExitUnknownCommand;
5196 5194
 
5195
+  // Interpret the code int
5196
+  codenum = code_value_short();
5197
+
5198
+  // Handle a known G, M, or T
5197 5199
   switch(command_code) {
5198 5200
     case 'G': switch (codenum) {
5199 5201
 
@@ -5700,6 +5702,9 @@ void process_next_command() {
5700 5702
     break;
5701 5703
   }
5702 5704
 
5705
+ExitUnknownCommand:
5706
+
5707
+  // Still unknown command? Throw an error
5703 5708
   if (!code_is_good) unknown_command_error();
5704 5709
 
5705 5710
   ok_to_send();

Loading…
Cancel
Save