Browse Source

Further optimization of command parser

Scott Lahteine 9 years ago
parent
commit
1116e13f5a
1 changed files with 82 additions and 87 deletions
  1. 82
    87
      Marlin/Marlin_main.cpp

+ 82
- 87
Marlin/Marlin_main.cpp View File

@@ -141,7 +141,7 @@
141 141
  * M112 - Emergency stop
142 142
  * M114 - Output current position to serial port
143 143
  * M115 - Capabilities string
144
- * M117 - display message
144
+ * M117 - Display a message on the controller screen
145 145
  * M119 - Output Endstop status to serial port
146 146
  * M120 - Enable endstop detection
147 147
  * M121 - Disable endstop detection
@@ -266,7 +266,7 @@ static bool relative_mode = false;  //Determines Absolute or Relative Coordinate
266 266
 static char serial_char;
267 267
 static int serial_count = 0;
268 268
 static boolean comment_mode = false;
269
-static char *strchr_pointer; ///< A pointer to find chars in the command string (X, Y, Z, E, etc.)
269
+static char *seen_pointer; ///< A pointer to find chars in the command string (X, Y, Z, E, etc.)
270 270
 const char* queued_commands_P= NULL; /* pointer to the current line in the active sequence of commands, or NULL when none */
271 271
 const int sensitive_pins[] = SENSITIVE_PINS; ///< Sensitive pin list for M42
272 272
 // Inactivity shutdown
@@ -786,21 +786,20 @@ void get_command() {
786 786
         fromsd[cmd_queue_index_w] = false;
787 787
       #endif
788 788
 
789
-      if (strchr(command, 'N') != NULL) {
790
-        strchr_pointer = strchr(command, 'N');
791
-        gcode_N = (strtol(strchr_pointer + 1, NULL, 10));
789
+      char *npos = strchr(command, 'N');
790
+      char *apos = strchr(command, '*');
791
+      if (npos) {
792
+        gcode_N = strtol(npos + 1, NULL, 10);
792 793
         if (gcode_N != gcode_LastN + 1 && strstr_P(command, PSTR("M110")) == NULL) {
793 794
           gcode_line_error(PSTR(MSG_ERR_LINE_NO));
794 795
           return;
795 796
         }
796 797
 
797
-        if (strchr(command, '*') != NULL) {
798
-          byte checksum = 0;
799
-          byte count = 0;
798
+        if (apos) {
799
+          byte checksum = 0, count = 0;
800 800
           while (command[count] != '*') checksum ^= command[count++];
801
-          strchr_pointer = strchr(command, '*');
802 801
 
803
-          if (strtol(strchr_pointer + 1, NULL, 10) != checksum) {
802
+          if (strtol(apos + 1, NULL, 10) != checksum) {
804 803
             gcode_line_error(PSTR(MSG_ERR_CHECKSUM_MISMATCH));
805 804
             return;
806 805
           }
@@ -814,28 +813,26 @@ void get_command() {
814 813
         gcode_LastN = gcode_N;
815 814
         // if no errors, continue parsing
816 815
       }
817
-      else {  // if we don't receive 'N' but still see '*'
818
-        if ((strchr(command, '*') != NULL)) {
819
-          gcode_line_error(PSTR(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM), false);
820
-          return;
821
-        }
816
+      else if (apos) { // No '*' without 'N'
817
+        gcode_line_error(PSTR(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM), false);
818
+        return;
822 819
       }
823 820
 
824
-      if (strchr(command, 'G') != NULL) {
825
-        strchr_pointer = strchr(command, 'G');
826
-        switch (strtol(strchr_pointer + 1, NULL, 10)) {
827
-          case 0:
828
-          case 1:
829
-          case 2:
830
-          case 3:
831
-            if (IsStopped()) {
821
+      // Movement commands alert when stopped
822
+      if (IsStopped()) {
823
+        char *gpos = strchr(command, 'G');
824
+        if (gpos) {
825
+          int codenum = strtol(gpos + 1, NULL, 10);
826
+          switch (codenum) {
827
+            case 0:
828
+            case 1:
829
+            case 2:
830
+            case 3:
832 831
               SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
833 832
               LCD_MESSAGEPGM(MSG_STOPPED);
834
-            }
835
-            break;
836
-          default:
837
-            break;
838
-        }
833
+              break;
834
+          }
835
+        }        
839 836
       }
840 837
 
841 838
       // If command was e-stop process now
@@ -917,32 +914,32 @@ void get_command() {
917 914
 
918 915
 bool code_has_value() {
919 916
   int i = 1;
920
-  char c = strchr_pointer[i];
921
-  if (c == '-' || c == '+') c = strchr_pointer[++i];
922
-  if (c == '.') c = strchr_pointer[++i];
917
+  char c = seen_pointer[i];
918
+  if (c == '-' || c == '+') c = seen_pointer[++i];
919
+  if (c == '.') c = seen_pointer[++i];
923 920
   return (c >= '0' && c <= '9');
924 921
 }
925 922
 
926 923
 float code_value() {
927 924
   float ret;
928
-  char *e = strchr(strchr_pointer, 'E');
925
+  char *e = strchr(seen_pointer, 'E');
929 926
   if (e) {
930 927
     *e = 0;
931
-    ret = strtod(strchr_pointer+1, NULL);
928
+    ret = strtod(seen_pointer+1, NULL);
932 929
     *e = 'E';
933 930
   }
934 931
   else
935
-    ret = strtod(strchr_pointer+1, NULL);
932
+    ret = strtod(seen_pointer+1, NULL);
936 933
   return ret;
937 934
 }
938 935
 
939
-long code_value_long() { return strtol(strchr_pointer + 1, NULL, 10); }
936
+long code_value_long() { return strtol(seen_pointer + 1, NULL, 10); }
940 937
 
941
-int16_t code_value_short() { return (int16_t)strtol(strchr_pointer + 1, NULL, 10); }
938
+int16_t code_value_short() { return (int16_t)strtol(seen_pointer + 1, NULL, 10); }
942 939
 
943 940
 bool code_seen(char code) {
944
-  strchr_pointer = strchr(current_command, code);
945
-  return (strchr_pointer != NULL);  //Return True if a character was found
941
+  seen_pointer = strchr(current_command, code);
942
+  return (seen_pointer != NULL);  //Return True if a character was found
946 943
 }
947 944
 
948 945
 #define DEFINE_PGM_READ_ANY(type, reader)       \
@@ -1793,6 +1790,13 @@ void gcode_get_destination() {
1793 1790
   }
1794 1791
 }
1795 1792
 
1793
+void unknown_command_error() {
1794
+  SERIAL_ECHO_START;
1795
+  SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
1796
+  SERIAL_ECHO(current_command);
1797
+  SERIAL_ECHOPGM("\"\n");
1798
+}
1799
+
1796 1800
 /**
1797 1801
  * G0, G1: Coordinated movement of X Y Z E axes
1798 1802
  */
@@ -2844,7 +2848,7 @@ inline void gcode_G92() {
2844 2848
    * M1: // M1 - Conditional stop - Wait for user button press on LCD
2845 2849
    */
2846 2850
   inline void gcode_M0_M1() {
2847
-    char *args = strchr_pointer + 3;
2851
+    char *args = current_command + 3;
2848 2852
 
2849 2853
     millis_t codenum = 0;
2850 2854
     bool hasP = false, hasS = false;
@@ -2931,7 +2935,7 @@ inline void gcode_M17() {
2931 2935
    * M23: Select a file
2932 2936
    */
2933 2937
   inline void gcode_M23() {
2934
-    card.openFile(strchr_pointer + 4, true);
2938
+    card.openFile(current_command + 4, true);
2935 2939
   }
2936 2940
 
2937 2941
   /**
@@ -2968,7 +2972,7 @@ inline void gcode_M17() {
2968 2972
    * M28: Start SD Write
2969 2973
    */
2970 2974
   inline void gcode_M28() {
2971
-    card.openFile(strchr_pointer + 4, false);
2975
+    card.openFile(current_command + 4, false);
2972 2976
   }
2973 2977
 
2974 2978
   /**
@@ -2985,7 +2989,7 @@ inline void gcode_M17() {
2985 2989
   inline void gcode_M30() {
2986 2990
     if (card.cardOK) {
2987 2991
       card.closefile();
2988
-      card.removeFile(strchr_pointer + 4);
2992
+      card.removeFile(current_command + 4);
2989 2993
     }
2990 2994
   }
2991 2995
 
@@ -3015,7 +3019,7 @@ inline void gcode_M31() {
3015 3019
     if (card.sdprinting)
3016 3020
       st_synchronize();
3017 3021
 
3018
-    char* args = strchr_pointer + 4;
3022
+    char* args = current_command + 4;
3019 3023
 
3020 3024
     char* namestartpos = strchr(args, '!');  // Find ! to indicate filename string start.
3021 3025
     if (!namestartpos)
@@ -3023,12 +3027,12 @@ inline void gcode_M31() {
3023 3027
     else
3024 3028
       namestartpos++; //to skip the '!'
3025 3029
 
3026
-    bool call_procedure = code_seen('P') && (strchr_pointer < namestartpos);
3030
+    bool call_procedure = code_seen('P') && (seen_pointer < namestartpos);
3027 3031
 
3028 3032
     if (card.cardOK) {
3029 3033
       card.openFile(namestartpos, true, !call_procedure);
3030 3034
 
3031
-      if (code_seen('S') && strchr_pointer < namestartpos) // "S" (must occur _before_ the filename!)
3035
+      if (code_seen('S') && seen_pointer < namestartpos) // "S" (must occur _before_ the filename!)
3032 3036
         card.setIndex(code_value_short());
3033 3037
 
3034 3038
       card.startFileprint();
@@ -3041,7 +3045,7 @@ inline void gcode_M31() {
3041 3045
    * M928: Start SD Write
3042 3046
    */
3043 3047
   inline void gcode_M928() {
3044
-    card.openLogFile(strchr_pointer + 5);
3048
+    card.openLogFile(current_command + 5);
3045 3049
   }
3046 3050
 
3047 3051
 #endif // SDSUPPORT
@@ -3838,16 +3842,12 @@ inline void gcode_M115() {
3838 3842
   SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
3839 3843
 }
3840 3844
 
3841
-#ifdef ULTIPANEL
3842
-
3843
-  /**
3844
-   * M117: Set LCD Status Message
3845
-   */
3846
-  inline void gcode_M117() {
3847
-    lcd_setstatus(strchr_pointer + 5);
3848
-  }
3849
-
3850
-#endif
3845
+/**
3846
+ * M117: Set LCD Status Message
3847
+ */
3848
+inline void gcode_M117() {
3849
+  lcd_setstatus(current_command + 5);
3850
+}
3851 3851
 
3852 3852
 /**
3853 3853
  * M119: Output endstop states to serial output
@@ -4135,10 +4135,7 @@ inline void gcode_M206() {
4135 4135
           autoretract_enabled = true;
4136 4136
           break;
4137 4137
         default:
4138
-          SERIAL_ECHO_START;
4139
-          SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
4140
-          SERIAL_ECHO(current_command);
4141
-          SERIAL_ECHOLNPGM("\"");
4138
+          unknown_command_error();
4142 4139
           return;
4143 4140
       }
4144 4141
       for (int i=0; i<EXTRUDERS; i++) retracted[i] = false;
@@ -5184,17 +5181,21 @@ void process_next_command() {
5184 5181
   char *starpos = strchr(current_command, '*');  // * should always be the last parameter
5185 5182
   if (starpos) *starpos = '\0';
5186 5183
 
5187
-  // Get the command code as a character
5184
+  // Get the command code, which must be G, M, or T
5188 5185
   char command_code = *current_command;
5189 5186
 
5190
-  // code_value routines look at strchr_pointer + 1
5191
-  strchr_pointer = current_command;
5187
+  bool code_is_good = code_has_value();
5192 5188
 
5193
-  if (command_code == 'G' && code_has_value()) {
5189
+  if (!code_is_good) {
5190
+    unknown_command_error();
5191
+    ok_to_send();
5192
+    return;
5193
+  }
5194 5194
 
5195
-    int codenum = code_value_short();
5195
+  int codenum = code_value_short();
5196 5196
 
5197
-    switch (codenum) {
5197
+  switch(command_code) {
5198
+    case 'G': switch (codenum) {
5198 5199
 
5199 5200
     // G0, G1
5200 5201
     case 0:
@@ -5263,11 +5264,12 @@ void process_next_command() {
5263 5264
     case 92: // G92
5264 5265
       gcode_G92();
5265 5266
       break;
5267
+
5268
+    default: code_is_good = false;
5266 5269
     }
5267
-  }
5270
+    break;
5268 5271
 
5269
-  else if (command_code == 'M' && code_has_value()) {
5270
-    switch (code_value_short()) {
5272
+    case 'M': switch (codenum) {
5271 5273
       #ifdef ULTIPANEL
5272 5274
         case 0: // M0 - Unconditional stop - Wait for user button press on LCD
5273 5275
         case 1: // M1 - Conditional stop - Wait for user button press on LCD
@@ -5342,8 +5344,7 @@ void process_next_command() {
5342 5344
 
5343 5345
       case 105: // M105: Read current temperature
5344 5346
         gcode_M105();
5345
-        return;
5346
-        break;
5347
+        return; // "ok" already printed
5347 5348
 
5348 5349
       case 109: // M109: Wait for temperature
5349 5350
         gcode_M109();
@@ -5417,13 +5418,9 @@ void process_next_command() {
5417 5418
       case 115: // M115: Report capabilities
5418 5419
         gcode_M115();
5419 5420
         break;
5420
-
5421
-      #ifdef ULTIPANEL
5422
-        case 117: // M117: Set LCD message text
5423
-          gcode_M117();
5424
-          break;
5425
-      #endif
5426
-
5421
+      case 117: // M117: Set LCD message text, if possible
5422
+        gcode_M117();
5423
+        break;
5427 5424
       case 114: // M114: Report current position
5428 5425
         gcode_M114();
5429 5426
         break;
@@ -5693,19 +5690,17 @@ void process_next_command() {
5693 5690
       case 999: // M999: Restart after being Stopped
5694 5691
         gcode_M999();
5695 5692
         break;
5693
+
5694
+      default: code_is_good = false;
5696 5695
     }
5697
-  }
5696
+    break;
5698 5697
 
5699
-  else if (command_code == 'T' && code_has_value()) {
5700
-    gcode_T(code_value_short());
5698
+    case 'T':
5699
+      gcode_T(code_value_short());
5700
+    break;
5701 5701
   }
5702 5702
 
5703
-  else {
5704
-    SERIAL_ECHO_START;
5705
-    SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
5706
-    SERIAL_ECHO(current_command);
5707
-    SERIAL_ECHOLNPGM("\"");
5708
-  }
5703
+  if (!code_is_good) unknown_command_error();
5709 5704
 
5710 5705
   ok_to_send();
5711 5706
 }

Loading…
Cancel
Save