Browse Source

Merge pull request #3267 from thinkyhead/rc_command_args_null_oooops

Cleanup, simplification of command dispatcher code
Scott Lahteine 8 years ago
parent
commit
e0ab06cfae
3 changed files with 46 additions and 37 deletions
  1. 0
    3
      Marlin/Marlin.h
  2. 44
    34
      Marlin/Marlin_main.cpp
  3. 2
    0
      Marlin/macros.h

+ 0
- 3
Marlin/Marlin.h View File

@@ -109,7 +109,6 @@ void serial_echopair_P(const char* s_P, float v);
109 109
 void serial_echopair_P(const char* s_P, double v);
110 110
 void serial_echopair_P(const char* s_P, unsigned long v);
111 111
 
112
-
113 112
 // Things to write to serial from Program memory. Saves 400 to 2k of RAM.
114 113
 FORCE_INLINE void serialprintPGM(const char* str) {
115 114
   char ch;
@@ -119,8 +118,6 @@ FORCE_INLINE void serialprintPGM(const char* str) {
119 118
   }
120 119
 }
121 120
 
122
-void get_command();
123
-
124 121
 void idle(
125 122
   #if ENABLED(FILAMENTCHANGEENABLE)
126 123
     bool no_stepper_sleep=false  // pass true to keep steppers from disabling on timeout

+ 44
- 34
Marlin/Marlin_main.cpp View File

@@ -462,6 +462,7 @@ static bool send_ok[BUFSIZE];
462 462
  * ***************************************************************************
463 463
  */
464 464
 
465
+void get_available_commands();
465 466
 void process_next_command();
466 467
 
467 468
 void plan_arc(float target[NUM_AXIS], float* offset, uint8_t clockwise);
@@ -804,7 +805,7 @@ void setup() {
804 805
  *  - Call LCD update
805 806
  */
806 807
 void loop() {
807
-  if (commands_in_queue < BUFSIZE) get_command();
808
+  if (commands_in_queue < BUFSIZE) get_available_commands();
808 809
 
809 810
   #if ENABLED(SDSUPPORT)
810 811
     card.checkautostart(false);
@@ -856,24 +857,16 @@ void gcode_line_error(const char* err, bool doFlush = true) {
856 857
   serial_count = 0;
857 858
 }
858 859
 
859
-/**
860
- * Add to the circular command queue the next command from:
861
- *  - The command-injection queue (queued_commands_P)
862
- *  - The active serial input (usually USB)
863
- *  - The SD card file being actively printed
864
- */
865
-void get_command() {
866
-
860
+inline void get_serial_commands() {
867 861
   static char serial_line_buffer[MAX_CMD_SIZE];
868 862
   static boolean serial_comment_mode = false;
869 863
 
870
-  if (drain_queued_commands_P()) return; // priority is given to non-serial commands
871
-
864
+  // If the command buffer is empty for too long,
865
+  // send "wait" to indicate Marlin is still waiting.
872 866
   #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
873 867
     static millis_t last_command_time = 0;
874 868
     millis_t ms = millis();
875
-
876
-    if (!MYSERIAL.available() && commands_in_queue == 0 && ms - last_command_time > NO_TIMEOUTS) {
869
+    if (commands_in_queue == 0 && !MYSERIAL.available() && ms > last_command_time + NO_TIMEOUTS) {
877 870
       SERIAL_ECHOLNPGM(MSG_WAIT);
878 871
       last_command_time = ms;
879 872
     }
@@ -893,7 +886,7 @@ void get_command() {
893 886
 
894 887
       serial_comment_mode = false; // end of line == end of comment
895 888
 
896
-      if (!serial_count) return; // empty lines just exit
889
+      if (!serial_count) continue; // skip empty lines
897 890
 
898 891
       serial_line_buffer[serial_count] = 0; // terminate string
899 892
       serial_count = 0; //reset buffer
@@ -978,7 +971,7 @@ void get_command() {
978 971
       if (MYSERIAL.available() > 0) {
979 972
         // if we have one more character, copy it over
980 973
         serial_char = MYSERIAL.read();
981
-        serial_line_buffer[serial_count++] = serial_char;
974
+        if (!serial_comment_mode) serial_line_buffer[serial_count++] = serial_char;
982 975
       }
983 976
       // otherwise do nothing
984 977
     }
@@ -988,9 +981,11 @@ void get_command() {
988 981
     }
989 982
 
990 983
   } // queue has space, serial has data
984
+}
991 985
 
992
-  #if ENABLED(SDSUPPORT)
986
+#if ENABLED(SDSUPPORT)
993 987
 
988
+  inline void get_sdcard_commands() {
994 989
     static bool stop_buffering = false,
995 990
                 sd_comment_mode = false;
996 991
 
@@ -1050,8 +1045,26 @@ void get_command() {
1050 1045
         if (!sd_comment_mode) command_queue[cmd_queue_index_w][sd_count++] = sd_char;
1051 1046
       }
1052 1047
     }
1048
+  }
1049
+
1050
+#endif // SDSUPPORT
1051
+
1052
+/**
1053
+ * Add to the circular command queue the next command from:
1054
+ *  - The command-injection queue (queued_commands_P)
1055
+ *  - The active serial input (usually USB)
1056
+ *  - The SD card file being actively printed
1057
+ */
1058
+void get_available_commands() {
1059
+
1060
+  // if any immediate commands remain, don't get other commands yet
1061
+  if (drain_queued_commands_P()) return;
1062
+
1063
+  get_serial_commands();
1053 1064
 
1054
-  #endif // SDSUPPORT
1065
+  #if ENABLED(SDSUPPORT)
1066
+    get_sdcard_commands();
1067
+  #endif
1055 1068
 }
1056 1069
 
1057 1070
 bool code_has_value() {
@@ -1060,7 +1073,7 @@ bool code_has_value() {
1060 1073
   while (c == ' ') c = seen_pointer[++i];
1061 1074
   if (c == '-' || c == '+') c = seen_pointer[++i];
1062 1075
   if (c == '.') c = seen_pointer[++i];
1063
-  return (c >= '0' && c <= '9');
1076
+  return NUMERIC(c);
1064 1077
 }
1065 1078
 
1066 1079
 float code_value() {
@@ -6066,9 +6079,9 @@ void process_next_command() {
6066 6079
   //  - Bypass N[-0-9][0-9]*[ ]*
6067 6080
   //  - Overwrite * with nul to mark the end
6068 6081
   while (*current_command == ' ') ++current_command;
6069
-  if (*current_command == 'N' && ((current_command[1] >= '0' && current_command[1] <= '9') || current_command[1] == '-')) {
6082
+  if (*current_command == 'N' && NUMERIC_SIGNED(current_command[1])) {
6070 6083
     current_command += 2; // skip N[-0-9]
6071
-    while (*current_command >= '0' && *current_command <= '9') ++current_command; // skip [0-9]*
6084
+    while (NUMERIC(*current_command)) ++current_command; // skip [0-9]*
6072 6085
     while (*current_command == ' ') ++current_command; // skip [ ]*
6073 6086
   }
6074 6087
   char* starpos = strchr(current_command, '*');  // * should always be the last parameter
@@ -6082,25 +6095,22 @@ void process_next_command() {
6082 6095
   // Skip spaces to get the numeric part
6083 6096
   while (*cmd_ptr == ' ') cmd_ptr++;
6084 6097
 
6085
-  // The code must have a numeric value
6086
-  bool code_is_good = false;
6098
+  uint16_t codenum = 0; // define ahead of goto
6087 6099
 
6088
-  int codenum = 0; // define ahead of goto
6100
+  // Bail early if there's no code
6101
+  bool code_is_good = NUMERIC(*cmd_ptr);
6102
+  if (!code_is_good) goto ExitUnknownCommand;
6089 6103
 
6090 6104
   // Get and skip the code number
6091
-  while (*cmd_ptr >= '0' && *cmd_ptr <= '9') {
6092
-    code_is_good = true;
6093
-    codenum = codenum * 10 + *cmd_ptr - '0';
6105
+  do {
6106
+    codenum = (codenum * 10) + (*cmd_ptr - '0');
6094 6107
     cmd_ptr++;
6095
-  }
6096
-
6097
-  // Bail early if there's no code
6098
-  if (!code_is_good) goto ExitUnknownCommand;
6108
+  } while (NUMERIC(*cmd_ptr));
6099 6109
 
6100
-  // Skip all spaces to get to the first argument
6110
+  // Skip all spaces to get to the first argument, or nul
6101 6111
   while (*cmd_ptr == ' ') cmd_ptr++;
6102 6112
 
6103
-  // The command's arguments start here, for sure!
6113
+  // The command's arguments (if any) start here, for sure!
6104 6114
   current_command_args = cmd_ptr;
6105 6115
 
6106 6116
   KEEPALIVE_STATE(IN_HANDLER);
@@ -6668,7 +6678,7 @@ void ok_to_send() {
6668 6678
     if (*p == 'N') {
6669 6679
       SERIAL_PROTOCOL(' ');
6670 6680
       SERIAL_ECHO(*p++);
6671
-      while ((*p >= '0' && *p <= '9') || *p == '-')
6681
+      while (NUMERIC_SIGNED(*p))
6672 6682
         SERIAL_ECHO(*p++);
6673 6683
     }
6674 6684
     SERIAL_PROTOCOLPGM(" P"); SERIAL_PROTOCOL(int(BLOCK_BUFFER_SIZE - movesplanned() - 1));
@@ -7365,7 +7375,7 @@ void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
7365 7375
       filrunout();
7366 7376
   #endif
7367 7377
 
7368
-  if (commands_in_queue < BUFSIZE) get_command();
7378
+  if (commands_in_queue < BUFSIZE) get_available_commands();
7369 7379
 
7370 7380
   millis_t ms = millis();
7371 7381
 

+ 2
- 0
Marlin/macros.h View File

@@ -51,6 +51,8 @@
51 51
 #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
52 52
 #define DISABLED(b) (!_CAT(SWITCH_ENABLED_, b))
53 53
 
54
+#define NUMERIC(a) ((a) >= '0' && '9' >= (a))
55
+#define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-')
54 56
 #define COUNT(a) (sizeof(a)/sizeof(*a))
55 57
 
56 58
 #endif //__MACROS_H

Loading…
Cancel
Save