瀏覽代碼

Merge pull request #6074 from thinkyhead/rc_immediate_shove

Immediate commands take precedence
Scott Lahteine 8 年之前
父節點
當前提交
eab7854a73
共有 2 個文件被更改,包括 72 次插入22 次删除
  1. 71
    21
      Marlin/Marlin_main.cpp
  2. 1
    1
      Marlin/language.h

+ 71
- 21
Marlin/Marlin_main.cpp 查看文件

796
   extern void digipot_i2c_init();
796
   extern void digipot_i2c_init();
797
 #endif
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
  * Return true if any immediate commands remain to inject.
841
  * Return true if any immediate commands remain to inject.
802
  */
842
  */
803
 static bool drain_injected_commands_P() {
843
 static bool drain_injected_commands_P() {
808
     cmd[sizeof(cmd) - 1] = '\0';
848
     cmd[sizeof(cmd) - 1] = '\0';
809
     while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
849
     while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
810
     cmd[i] = '\0';
850
     cmd[i] = '\0';
811
-    if (enqueue_and_echo_command(cmd)) {   // success?
851
+    if (shove_and_echo_command(cmd)) {     // success?
812
       if (c)                               // newline char?
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
       else
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
   drain_injected_commands_P(); // first command executed asap (when possible)
868
   drain_injected_commands_P(); // first command executed asap (when possible)
829
 }
869
 }
830
 
870
 
871
+/**
872
+ * Clear the Marlin command queue
873
+ */
831
 void clear_command_queue() {
874
 void clear_command_queue() {
832
   cmd_queue_index_r = cmd_queue_index_w;
875
   cmd_queue_index_r = cmd_queue_index_w;
833
   commands_in_queue = 0;
876
   commands_in_queue = 0;
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
 inline bool _enqueuecommand(const char* cmd, bool say_ok=false) {
893
 inline bool _enqueuecommand(const char* cmd, bool say_ok=false) {
850
   if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
894
   if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
853
   return true;
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
  * Enqueue with Serial Echo
901
  * Enqueue with Serial Echo
862
  */
902
  */
863
 bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) {
903
 bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) {
864
   if (_enqueuecommand(cmd, say_ok)) {
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
     return true;
906
     return true;
870
   }
907
   }
871
   return false;
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
 void setup_killpin() {
915
 void setup_killpin() {
875
   #if HAS_KILL
916
   #if HAS_KILL
876
     SET_INPUT_PULLUP(KILL_PIN);
917
     SET_INPUT_PULLUP(KILL_PIN);
889
 
930
 
890
 #endif
931
 #endif
891
 
932
 
892
-// Set home pin
893
 void setup_homepin(void) {
933
 void setup_homepin(void) {
894
   #if HAS_HOME
934
   #if HAS_HOME
895
     SET_INPUT_PULLUP(HOME_PIN);
935
     SET_INPUT_PULLUP(HOME_PIN);
978
   serial_count = 0;
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
 inline void get_serial_commands() {
1026
 inline void get_serial_commands() {
982
   static char serial_line_buffer[MAX_CMD_SIZE];
1027
   static char serial_line_buffer[MAX_CMD_SIZE];
983
   static bool serial_comment_mode = false;
1028
   static bool serial_comment_mode = false;
1115
 
1160
 
1116
 #if ENABLED(SDSUPPORT)
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
   inline void get_sdcard_commands() {
1168
   inline void get_sdcard_commands() {
1119
     static bool stop_buffering = false,
1169
     static bool stop_buffering = false,
1120
                 sd_comment_mode = false;
1170
                 sd_comment_mode = false;
1133
     uint16_t sd_count = 0;
1183
     uint16_t sd_count = 0;
1134
     bool card_eof = card.eof();
1184
     bool card_eof = card.eof();
1135
     while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
1185
     while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
1136
-      int16_t n = card.get();
1186
+      const int16_t n = card.get();
1137
       char sd_char = (char)n;
1187
       char sd_char = (char)n;
1138
       card_eof = card.eof();
1188
       card_eof = card.eof();
1139
       if (card_eof || n == -1
1189
       if (card_eof || n == -1
1151
         }
1201
         }
1152
         if (sd_char == '#') stop_buffering = true;
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
         _commit_command(false);
1211
         _commit_command(false);
1162
       }
1212
       }

+ 1
- 1
Marlin/language.h 查看文件

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

Loading…
取消
儲存