Browse Source

Pre-sanitize the command before handling

- Use a global pointer for the current sanitized command
- Pre-sanitize the current command to bypass `N` and nullify `*`,
removing the need for handlers to bypass, ignore, or nullify these
parts, and reducing overhead for `code_seen`, etc.
- Pre-skip leading whitespace.
- Only look for G, M, T codes at the start of the command.
- Verify that G, M, T codes are followed by command codes.
Scott Lahteine 9 years ago
parent
commit
3a4c3ab76e
1 changed files with 41 additions and 49 deletions
  1. 41
    49
      Marlin/Marlin_main.cpp

+ 41
- 49
Marlin/Marlin_main.cpp View File

@@ -236,6 +236,7 @@ bool axis_known_position[3] = { false };
236 236
 
237 237
 static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
238 238
 
239
+static char *current_command;
239 240
 static int cmd_queue_index_r = 0;
240 241
 static int cmd_queue_index_w = 0;
241 242
 static int commands_in_queue = 0;
@@ -940,7 +941,7 @@ long code_value_long() { return strtol(strchr_pointer + 1, NULL, 10); }
940 941
 int16_t code_value_short() { return (int16_t)strtol(strchr_pointer + 1, NULL, 10); }
941 942
 
942 943
 bool code_seen(char code) {
943
-  strchr_pointer = strchr(command_queue[cmd_queue_index_r], code);
944
+  strchr_pointer = strchr(current_command, code);
944 945
   return (strchr_pointer != NULL);  //Return True if a character was found
945 946
 }
946 947
 
@@ -2843,7 +2844,7 @@ inline void gcode_G92() {
2843 2844
    * M1: // M1 - Conditional stop - Wait for user button press on LCD
2844 2845
    */
2845 2846
   inline void gcode_M0_M1() {
2846
-    char *src = strchr_pointer + 2;
2847
+    char *args = strchr_pointer + 3;
2847 2848
 
2848 2849
     millis_t codenum = 0;
2849 2850
     bool hasP = false, hasS = false;
@@ -2855,11 +2856,9 @@ inline void gcode_G92() {
2855 2856
       codenum = code_value() * 1000; // seconds to wait
2856 2857
       hasS = codenum > 0;
2857 2858
     }
2858
-    char* starpos = strchr(src, '*');
2859
-    if (starpos != NULL) *(starpos) = '\0';
2860
-    while (*src == ' ') ++src;
2861
-    if (!hasP && !hasS && *src != '\0')
2862
-      lcd_setstatus(src, true);
2859
+
2860
+    if (!hasP && !hasS && *args != '\0')
2861
+      lcd_setstatus(args, true);
2863 2862
     else {
2864 2863
       LCD_MESSAGEPGM(MSG_USERWAIT);
2865 2864
       #if defined(LCD_PROGRESS_BAR) && PROGRESS_MSG_EXPIRE > 0
@@ -2932,10 +2931,7 @@ inline void gcode_M17() {
2932 2931
    * M23: Select a file
2933 2932
    */
2934 2933
   inline void gcode_M23() {
2935
-    char* codepos = strchr_pointer + 4;
2936
-    char* starpos = strchr(codepos, '*');
2937
-    if (starpos) *starpos = '\0';
2938
-    card.openFile(codepos, true);
2934
+    card.openFile(strchr_pointer + 4, true);
2939 2935
   }
2940 2936
 
2941 2937
   /**
@@ -2972,14 +2968,7 @@ inline void gcode_M17() {
2972 2968
    * M28: Start SD Write
2973 2969
    */
2974 2970
   inline void gcode_M28() {
2975
-    char* codepos = strchr_pointer + 4;
2976
-    char* starpos = strchr(codepos, '*');
2977
-    if (starpos) {
2978
-      char* npos = strchr(command_queue[cmd_queue_index_r], 'N');
2979
-      strchr_pointer = strchr(npos, ' ') + 1;
2980
-      *(starpos) = '\0';
2981
-    }
2982
-    card.openFile(codepos, false);
2971
+    card.openFile(strchr_pointer + 4, false);
2983 2972
   }
2984 2973
 
2985 2974
   /**
@@ -2996,12 +2985,6 @@ inline void gcode_M17() {
2996 2985
   inline void gcode_M30() {
2997 2986
     if (card.cardOK) {
2998 2987
       card.closefile();
2999
-      char* starpos = strchr(strchr_pointer + 4, '*');
3000
-      if (starpos) {
3001
-        char* npos = strchr(command_queue[cmd_queue_index_r], 'N');
3002
-        strchr_pointer = strchr(npos, ' ') + 1;
3003
-        *(starpos) = '\0';
3004
-      }
3005 2988
       card.removeFile(strchr_pointer + 4);
3006 2989
     }
3007 2990
   }
@@ -3032,17 +3015,14 @@ inline void gcode_M31() {
3032 3015
     if (card.sdprinting)
3033 3016
       st_synchronize();
3034 3017
 
3035
-    char* codepos = strchr_pointer + 4;
3018
+    char* args = strchr_pointer + 4;
3036 3019
 
3037
-    char* namestartpos = strchr(codepos, '!');   //find ! to indicate filename string start.
3038
-    if (! namestartpos)
3039
-      namestartpos = codepos; //default name position, 4 letters after the M
3020
+    char* namestartpos = strchr(args, '!');  // Find ! to indicate filename string start.
3021
+    if (!namestartpos)
3022
+      namestartpos = args; // Default name position, 4 letters after the M
3040 3023
     else
3041 3024
       namestartpos++; //to skip the '!'
3042 3025
 
3043
-    char* starpos = strchr(codepos, '*');
3044
-    if (starpos) *(starpos) = '\0';
3045
-
3046 3026
     bool call_procedure = code_seen('P') && (strchr_pointer < namestartpos);
3047 3027
 
3048 3028
     if (card.cardOK) {
@@ -3061,12 +3041,6 @@ inline void gcode_M31() {
3061 3041
    * M928: Start SD Write
3062 3042
    */
3063 3043
   inline void gcode_M928() {
3064
-    char* starpos = strchr(strchr_pointer + 5, '*');
3065
-    if (starpos) {
3066
-      char* npos = strchr(command_queue[cmd_queue_index_r], 'N');
3067
-      strchr_pointer = strchr(npos, ' ') + 1;
3068
-      *(starpos) = '\0';
3069
-    }
3070 3044
     card.openLogFile(strchr_pointer + 5);
3071 3045
   }
3072 3046
 
@@ -4163,7 +4137,7 @@ inline void gcode_M206() {
4163 4137
         default:
4164 4138
           SERIAL_ECHO_START;
4165 4139
           SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
4166
-          SERIAL_ECHO(command_queue[cmd_queue_index_r]);
4140
+          SERIAL_ECHO(current_command);
4167 4141
           SERIAL_ECHOLNPGM("\"");
4168 4142
           return;
4169 4143
       }
@@ -5084,8 +5058,7 @@ inline void gcode_M999() {
5084 5058
  *
5085 5059
  *   F[mm/min] Set the movement feedrate
5086 5060
  */
5087
-inline void gcode_T() {
5088
-  uint16_t tmp_extruder = code_value_short();
5061
+inline void gcode_T(uint8_t tmp_extruder) {
5089 5062
   if (tmp_extruder >= EXTRUDERS) {
5090 5063
     SERIAL_ECHO_START;
5091 5064
     SERIAL_CHAR('T');
@@ -5188,17 +5161,36 @@ inline void gcode_T() {
5188 5161
 }
5189 5162
 
5190 5163
 /**
5191
- * Process Commands and dispatch them to handlers
5164
+ * Process a single command and dispatch it to its handler
5192 5165
  * This is called from the main loop()
5193 5166
  */
5194 5167
 void process_next_command() {
5168
+  current_command = command_queue[cmd_queue_index_r];
5195 5169
 
5196 5170
   if ((marlin_debug_flags & DEBUG_ECHO)) {
5197 5171
     SERIAL_ECHO_START;
5198
-    SERIAL_ECHOLN(command_queue[cmd_queue_index_r]);
5172
+    SERIAL_ECHOLN(current_command);
5199 5173
   }
5200 5174
 
5201
-  if (code_seen('G')) {
5175
+  // Sanitize the current command:
5176
+  //  - Skip leading spaces
5177
+  //  - Bypass N...
5178
+  //  - Overwrite * with nul to mark the end
5179
+  while (*current_command == ' ') ++current_command;
5180
+  if (*current_command == 'N' && current_command[1] >= '0' && current_command[1] <= '9') {
5181
+    while (*current_command != ' ') ++current_command;
5182
+    while (*current_command == ' ') ++current_command;
5183
+  }
5184
+  char *starpos = strchr(current_command, '*');  // * should always be the last parameter
5185
+  if (starpos) *starpos = '\0';
5186
+
5187
+  // Get the command code as a character
5188
+  char command_code = *current_command;
5189
+
5190
+  // code_value routines look at strchr_pointer + 1
5191
+  strchr_pointer = current_command;
5192
+
5193
+  if (command_code == 'G' && code_has_value()) {
5202 5194
 
5203 5195
     int codenum = code_value_short();
5204 5196
 
@@ -5274,8 +5266,8 @@ void process_next_command() {
5274 5266
     }
5275 5267
   }
5276 5268
 
5277
-  else if (code_seen('M')) {
5278
-    switch(code_value_short()) {
5269
+  else if (command_code == 'M' && code_has_value()) {
5270
+    switch (code_value_short()) {
5279 5271
       #ifdef ULTIPANEL
5280 5272
         case 0: // M0 - Unconditional stop - Wait for user button press on LCD
5281 5273
         case 1: // M1 - Conditional stop - Wait for user button press on LCD
@@ -5704,14 +5696,14 @@ void process_next_command() {
5704 5696
     }
5705 5697
   }
5706 5698
 
5707
-  else if (code_seen('T')) {
5708
-    gcode_T();
5699
+  else if (command_code == 'T' && code_has_value()) {
5700
+    gcode_T(code_value_short());
5709 5701
   }
5710 5702
 
5711 5703
   else {
5712 5704
     SERIAL_ECHO_START;
5713 5705
     SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
5714
-    SERIAL_ECHO(command_queue[cmd_queue_index_r]);
5706
+    SERIAL_ECHO(current_command);
5715 5707
     SERIAL_ECHOLNPGM("\"");
5716 5708
   }
5717 5709
 

Loading…
Cancel
Save