Przeglądaj źródła

Merge pull request #6074 from thinkyhead/rc_immediate_shove

Immediate commands take precedence
Scott Lahteine 8 lat temu
rodzic
commit
eab7854a73
2 zmienionych plików z 72 dodań i 22 usunięć
  1. 71
    21
      Marlin/Marlin_main.cpp
  2. 1
    1
      Marlin/language.h

+ 71
- 21
Marlin/Marlin_main.cpp Wyświetl plik

@@ -796,8 +796,48 @@ extern "C" {
796 796
   extern void digipot_i2c_init();
797 797
 #endif
798 798
 
799
+inline void echo_command(char * const cmd) {
800
+  SERIAL_ECHO_START;
801
+  SERIAL_ECHOPAIR(MSG_ENQUEUEING, cmd);
802
+  SERIAL_CHAR('"');
803
+  SERIAL_EOL;
804
+}
805
+
806
+/**
807
+ * Shove a command in RAM to the front of the main command queue.
808
+ * Return true if the command is successfully added.
809
+ */
810
+inline bool _shovecommand(const char* cmd, bool say_ok=false) {
811
+  if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
812
+  cmd_queue_index_r = (cmd_queue_index_r + BUFSIZE - 1) % BUFSIZE; // Index of the previous slot
813
+  commands_in_queue++;
814
+  strcpy(command_queue[cmd_queue_index_r], cmd);
815
+  send_ok[cmd_queue_index_r] = say_ok;
816
+  return true;
817
+}
818
+
819
+/**
820
+ * Shove a command to the front of the queue with Serial Echo
821
+ * Return true if the command is successfully added.
822
+ */
823
+bool shove_and_echo_command(const char* cmd, bool say_ok=false) {
824
+  if (_shovecommand(cmd, say_ok)) {
825
+    echo_command(cmd);
826
+    return true;
827
+  }
828
+  return false;
829
+}
830
+
799 831
 /**
800
- * Inject the next "immediate" command, when possible.
832
+ * Shove a command onto the front of the queue,
833
+ * and don't return until successful.
834
+ */
835
+void shove_and_echo_command_now(const char* cmd) {
836
+  while (!shove_and_echo_command(cmd)) idle();
837
+}
838
+
839
+/**
840
+ * Inject the next "immediate" command, when possible, onto the front of the queue.
801 841
  * Return true if any immediate commands remain to inject.
802 842
  */
803 843
 static bool drain_injected_commands_P() {
@@ -808,14 +848,14 @@ static bool drain_injected_commands_P() {
808 848
     cmd[sizeof(cmd) - 1] = '\0';
809 849
     while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
810 850
     cmd[i] = '\0';
811
-    if (enqueue_and_echo_command(cmd)) {   // success?
851
+    if (shove_and_echo_command(cmd)) {     // success?
812 852
       if (c)                               // newline char?
813
-        injected_commands_P += i + 1;        // advance to the next command
853
+        injected_commands_P += i + 1;      // advance to the next command
814 854
       else
815
-        injected_commands_P = NULL;          // nul char? no more commands
855
+        injected_commands_P = NULL;        // nul char? no more commands
816 856
     }
817 857
   }
818
-  return (injected_commands_P != NULL);      // return whether any more remain
858
+  return (injected_commands_P != NULL);    // return whether any more remain
819 859
 }
820 860
 
821 861
 /**
@@ -828,6 +868,9 @@ void enqueue_and_echo_commands_P(const char* pgcode) {
828 868
   drain_injected_commands_P(); // first command executed asap (when possible)
829 869
 }
830 870
 
871
+/**
872
+ * Clear the Marlin command queue
873
+ */
831 874
 void clear_command_queue() {
832 875
   cmd_queue_index_r = cmd_queue_index_w;
833 876
   commands_in_queue = 0;
@@ -843,8 +886,9 @@ inline void _commit_command(bool say_ok) {
843 886
 }
844 887
 
845 888
 /**
846
- * Copy a command directly into the main command buffer, from RAM.
847
- * Returns true if successfully adds the command
889
+ * Copy a command from RAM into the main command buffer.
890
+ * Return true if the command was successfully added.
891
+ * Return false for a full buffer, or if the 'command' is a comment.
848 892
  */
849 893
 inline bool _enqueuecommand(const char* cmd, bool say_ok=false) {
850 894
   if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
@@ -853,24 +897,21 @@ inline bool _enqueuecommand(const char* cmd, bool say_ok=false) {
853 897
   return true;
854 898
 }
855 899
 
856
-void enqueue_and_echo_command_now(const char* cmd) {
857
-  while (!enqueue_and_echo_command(cmd)) idle();
858
-}
859
-
860 900
 /**
861 901
  * Enqueue with Serial Echo
862 902
  */
863 903
 bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) {
864 904
   if (_enqueuecommand(cmd, say_ok)) {
865
-    SERIAL_ECHO_START;
866
-    SERIAL_ECHOPAIR(MSG_Enqueueing, cmd);
867
-    SERIAL_CHAR('"');
868
-    SERIAL_EOL;
905
+    echo_command(cmd);
869 906
     return true;
870 907
   }
871 908
   return false;
872 909
 }
873 910
 
911
+void enqueue_and_echo_command_now(const char* cmd) {
912
+  while (!enqueue_and_echo_command(cmd)) idle();
913
+}
914
+
874 915
 void setup_killpin() {
875 916
   #if HAS_KILL
876 917
     SET_INPUT_PULLUP(KILL_PIN);
@@ -889,7 +930,6 @@ void setup_killpin() {
889 930
 
890 931
 #endif
891 932
 
892
-// Set home pin
893 933
 void setup_homepin(void) {
894 934
   #if HAS_HOME
895 935
     SET_INPUT_PULLUP(HOME_PIN);
@@ -978,6 +1018,11 @@ void gcode_line_error(const char* err, bool doFlush = true) {
978 1018
   serial_count = 0;
979 1019
 }
980 1020
 
1021
+/**
1022
+ * Get all commands waiting on the serial port and queue them.
1023
+ * Exit when the buffer is full or when no more characters are
1024
+ * left on the serial port.
1025
+ */
981 1026
 inline void get_serial_commands() {
982 1027
   static char serial_line_buffer[MAX_CMD_SIZE];
983 1028
   static bool serial_comment_mode = false;
@@ -1115,6 +1160,11 @@ inline void get_serial_commands() {
1115 1160
 
1116 1161
 #if ENABLED(SDSUPPORT)
1117 1162
 
1163
+  /**
1164
+   * Get commands from the SD Card until the command buffer is full
1165
+   * or until the end of the file is reached. The special character '#'
1166
+   * can also interrupt buffering.
1167
+   */
1118 1168
   inline void get_sdcard_commands() {
1119 1169
     static bool stop_buffering = false,
1120 1170
                 sd_comment_mode = false;
@@ -1133,7 +1183,7 @@ inline void get_serial_commands() {
1133 1183
     uint16_t sd_count = 0;
1134 1184
     bool card_eof = card.eof();
1135 1185
     while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
1136
-      int16_t n = card.get();
1186
+      const int16_t n = card.get();
1137 1187
       char sd_char = (char)n;
1138 1188
       card_eof = card.eof();
1139 1189
       if (card_eof || n == -1
@@ -1151,12 +1201,12 @@ inline void get_serial_commands() {
1151 1201
         }
1152 1202
         if (sd_char == '#') stop_buffering = true;
1153 1203
 
1154
-        sd_comment_mode = false; //for new command
1204
+        sd_comment_mode = false; // for new command
1155 1205
 
1156
-        if (!sd_count) continue; //skip empty lines
1206
+        if (!sd_count) continue; // skip empty lines (and comment lines)
1157 1207
 
1158
-        command_queue[cmd_queue_index_w][sd_count] = '\0'; //terminate string
1159
-        sd_count = 0; //clear buffer
1208
+        command_queue[cmd_queue_index_w][sd_count] = '\0'; // terminate string
1209
+        sd_count = 0; // clear sd line buffer
1160 1210
 
1161 1211
         _commit_command(false);
1162 1212
       }

+ 1
- 1
Marlin/language.h Wyświetl plik

@@ -108,7 +108,7 @@
108 108
 
109 109
 // Serial Console Messages (do not translate those!)
110 110
 
111
-#define MSG_Enqueueing                      "enqueueing \""
111
+#define MSG_ENQUEUEING                      "enqueueing \""
112 112
 #define MSG_POWERUP                         "PowerUp"
113 113
 #define MSG_EXTERNAL_RESET                  " External Reset"
114 114
 #define MSG_BROWNOUT_RESET                  " Brown out Reset"

Ładowanie…
Anuluj
Zapisz