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

+ 44
- 34
Marlin/Marlin_main.cpp View File

462
  * ***************************************************************************
462
  * ***************************************************************************
463
  */
463
  */
464
 
464
 
465
+void get_available_commands();
465
 void process_next_command();
466
 void process_next_command();
466
 
467
 
467
 void plan_arc(float target[NUM_AXIS], float* offset, uint8_t clockwise);
468
 void plan_arc(float target[NUM_AXIS], float* offset, uint8_t clockwise);
804
  *  - Call LCD update
805
  *  - Call LCD update
805
  */
806
  */
806
 void loop() {
807
 void loop() {
807
-  if (commands_in_queue < BUFSIZE) get_command();
808
+  if (commands_in_queue < BUFSIZE) get_available_commands();
808
 
809
 
809
   #if ENABLED(SDSUPPORT)
810
   #if ENABLED(SDSUPPORT)
810
     card.checkautostart(false);
811
     card.checkautostart(false);
856
   serial_count = 0;
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
   static char serial_line_buffer[MAX_CMD_SIZE];
861
   static char serial_line_buffer[MAX_CMD_SIZE];
868
   static boolean serial_comment_mode = false;
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
   #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
866
   #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
873
     static millis_t last_command_time = 0;
867
     static millis_t last_command_time = 0;
874
     millis_t ms = millis();
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
       SERIAL_ECHOLNPGM(MSG_WAIT);
870
       SERIAL_ECHOLNPGM(MSG_WAIT);
878
       last_command_time = ms;
871
       last_command_time = ms;
879
     }
872
     }
893
 
886
 
894
       serial_comment_mode = false; // end of line == end of comment
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
       serial_line_buffer[serial_count] = 0; // terminate string
891
       serial_line_buffer[serial_count] = 0; // terminate string
899
       serial_count = 0; //reset buffer
892
       serial_count = 0; //reset buffer
978
       if (MYSERIAL.available() > 0) {
971
       if (MYSERIAL.available() > 0) {
979
         // if we have one more character, copy it over
972
         // if we have one more character, copy it over
980
         serial_char = MYSERIAL.read();
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
       // otherwise do nothing
976
       // otherwise do nothing
984
     }
977
     }
988
     }
981
     }
989
 
982
 
990
   } // queue has space, serial has data
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
     static bool stop_buffering = false,
989
     static bool stop_buffering = false,
995
                 sd_comment_mode = false;
990
                 sd_comment_mode = false;
996
 
991
 
1050
         if (!sd_comment_mode) command_queue[cmd_queue_index_w][sd_count++] = sd_char;
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
 bool code_has_value() {
1070
 bool code_has_value() {
1060
   while (c == ' ') c = seen_pointer[++i];
1073
   while (c == ' ') c = seen_pointer[++i];
1061
   if (c == '-' || c == '+') c = seen_pointer[++i];
1074
   if (c == '-' || c == '+') c = seen_pointer[++i];
1062
   if (c == '.') c = seen_pointer[++i];
1075
   if (c == '.') c = seen_pointer[++i];
1063
-  return (c >= '0' && c <= '9');
1076
+  return NUMERIC(c);
1064
 }
1077
 }
1065
 
1078
 
1066
 float code_value() {
1079
 float code_value() {
6066
   //  - Bypass N[-0-9][0-9]*[ ]*
6079
   //  - Bypass N[-0-9][0-9]*[ ]*
6067
   //  - Overwrite * with nul to mark the end
6080
   //  - Overwrite * with nul to mark the end
6068
   while (*current_command == ' ') ++current_command;
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
     current_command += 2; // skip N[-0-9]
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
     while (*current_command == ' ') ++current_command; // skip [ ]*
6085
     while (*current_command == ' ') ++current_command; // skip [ ]*
6073
   }
6086
   }
6074
   char* starpos = strchr(current_command, '*');  // * should always be the last parameter
6087
   char* starpos = strchr(current_command, '*');  // * should always be the last parameter
6082
   // Skip spaces to get the numeric part
6095
   // Skip spaces to get the numeric part
6083
   while (*cmd_ptr == ' ') cmd_ptr++;
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
   // Get and skip the code number
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
     cmd_ptr++;
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
   while (*cmd_ptr == ' ') cmd_ptr++;
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
   current_command_args = cmd_ptr;
6114
   current_command_args = cmd_ptr;
6105
 
6115
 
6106
   KEEPALIVE_STATE(IN_HANDLER);
6116
   KEEPALIVE_STATE(IN_HANDLER);
6668
     if (*p == 'N') {
6678
     if (*p == 'N') {
6669
       SERIAL_PROTOCOL(' ');
6679
       SERIAL_PROTOCOL(' ');
6670
       SERIAL_ECHO(*p++);
6680
       SERIAL_ECHO(*p++);
6671
-      while ((*p >= '0' && *p <= '9') || *p == '-')
6681
+      while (NUMERIC_SIGNED(*p))
6672
         SERIAL_ECHO(*p++);
6682
         SERIAL_ECHO(*p++);
6673
     }
6683
     }
6674
     SERIAL_PROTOCOLPGM(" P"); SERIAL_PROTOCOL(int(BLOCK_BUFFER_SIZE - movesplanned() - 1));
6684
     SERIAL_PROTOCOLPGM(" P"); SERIAL_PROTOCOL(int(BLOCK_BUFFER_SIZE - movesplanned() - 1));
7365
       filrunout();
7375
       filrunout();
7366
   #endif
7376
   #endif
7367
 
7377
 
7368
-  if (commands_in_queue < BUFSIZE) get_command();
7378
+  if (commands_in_queue < BUFSIZE) get_available_commands();
7369
 
7379
 
7370
   millis_t ms = millis();
7380
   millis_t ms = millis();
7371
 
7381
 

+ 2
- 0
Marlin/macros.h View File

51
 #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
51
 #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
52
 #define DISABLED(b) (!_CAT(SWITCH_ENABLED_, b))
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
 #define COUNT(a) (sizeof(a)/sizeof(*a))
56
 #define COUNT(a) (sizeof(a)/sizeof(*a))
55
 
57
 
56
 #endif //__MACROS_H
58
 #endif //__MACROS_H

Loading…
Cancel
Save