Procházet zdrojové kódy

Immediate commands take precedence

Scott Lahteine před 7 roky
rodič
revize
48c6284c91
2 změnil soubory, kde provedl 72 přidání a 22 odebrání
  1. 71
    21
      Marlin/Marlin_main.cpp
  2. 1
    1
      Marlin/language.h

+ 71
- 21
Marlin/Marlin_main.cpp Zobrazit soubor

@@ -789,8 +789,48 @@ extern "C" {
789 789
   extern void digipot_i2c_init();
790 790
 #endif
791 791
 
792
+inline void echo_command(char * const cmd) {
793
+  SERIAL_ECHO_START;
794
+  SERIAL_ECHOPAIR(MSG_ENQUEUEING, cmd);
795
+  SERIAL_CHAR('"');
796
+  SERIAL_EOL;
797
+}
798
+
799
+/**
800
+ * Shove a command in RAM to the front of the main command queue.
801
+ * Return true if the command is successfully added.
802
+ */
803
+inline bool _shovecommand(const char* cmd, bool say_ok=false) {
804
+  if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
805
+  cmd_queue_index_r = (cmd_queue_index_r + BUFSIZE - 1) % BUFSIZE; // Index of the previous slot
806
+  commands_in_queue++;
807
+  strcpy(command_queue[cmd_queue_index_r], cmd);
808
+  send_ok[cmd_queue_index_r] = say_ok;
809
+  return true;
810
+}
811
+
812
+/**
813
+ * Shove a command to the front of the queue with Serial Echo
814
+ * Return true if the command is successfully added.
815
+ */
816
+bool shove_and_echo_command(const char* cmd, bool say_ok=false) {
817
+  if (_shovecommand(cmd, say_ok)) {
818
+    echo_command(cmd);
819
+    return true;
820
+  }
821
+  return false;
822
+}
823
+
792 824
 /**
793
- * Inject the next "immediate" command, when possible.
825
+ * Shove a command onto the front of the queue,
826
+ * and don't return until successful.
827
+ */
828
+void shove_and_echo_command_now(const char* cmd) {
829
+  while (!shove_and_echo_command(cmd)) idle();
830
+}
831
+
832
+/**
833
+ * Inject the next "immediate" command, when possible, onto the front of the queue.
794 834
  * Return true if any immediate commands remain to inject.
795 835
  */
796 836
 static bool drain_injected_commands_P() {
@@ -801,14 +841,14 @@ static bool drain_injected_commands_P() {
801 841
     cmd[sizeof(cmd) - 1] = '\0';
802 842
     while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
803 843
     cmd[i] = '\0';
804
-    if (enqueue_and_echo_command(cmd)) {   // success?
844
+    if (shove_and_echo_command(cmd)) {     // success?
805 845
       if (c)                               // newline char?
806
-        injected_commands_P += i + 1;        // advance to the next command
846
+        injected_commands_P += i + 1;      // advance to the next command
807 847
       else
808
-        injected_commands_P = NULL;          // nul char? no more commands
848
+        injected_commands_P = NULL;        // nul char? no more commands
809 849
     }
810 850
   }
811
-  return (injected_commands_P != NULL);      // return whether any more remain
851
+  return (injected_commands_P != NULL);    // return whether any more remain
812 852
 }
813 853
 
814 854
 /**
@@ -821,6 +861,9 @@ void enqueue_and_echo_commands_P(const char* pgcode) {
821 861
   drain_injected_commands_P(); // first command executed asap (when possible)
822 862
 }
823 863
 
864
+/**
865
+ * Clear the Marlin command queue
866
+ */
824 867
 void clear_command_queue() {
825 868
   cmd_queue_index_r = cmd_queue_index_w;
826 869
   commands_in_queue = 0;
@@ -836,8 +879,9 @@ inline void _commit_command(bool say_ok) {
836 879
 }
837 880
 
838 881
 /**
839
- * Copy a command directly into the main command buffer, from RAM.
840
- * Returns true if successfully adds the command
882
+ * Copy a command from RAM into the main command buffer.
883
+ * Return true if the command was successfully added.
884
+ * Return false for a full buffer, or if the 'command' is a comment.
841 885
  */
842 886
 inline bool _enqueuecommand(const char* cmd, bool say_ok=false) {
843 887
   if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
@@ -846,24 +890,21 @@ inline bool _enqueuecommand(const char* cmd, bool say_ok=false) {
846 890
   return true;
847 891
 }
848 892
 
849
-void enqueue_and_echo_command_now(const char* cmd) {
850
-  while (!enqueue_and_echo_command(cmd)) idle();
851
-}
852
-
853 893
 /**
854 894
  * Enqueue with Serial Echo
855 895
  */
856 896
 bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) {
857 897
   if (_enqueuecommand(cmd, say_ok)) {
858
-    SERIAL_ECHO_START;
859
-    SERIAL_ECHOPAIR(MSG_Enqueueing, cmd);
860
-    SERIAL_CHAR('"');
861
-    SERIAL_EOL;
898
+    echo_command(cmd);
862 899
     return true;
863 900
   }
864 901
   return false;
865 902
 }
866 903
 
904
+void enqueue_and_echo_command_now(const char* cmd) {
905
+  while (!enqueue_and_echo_command(cmd)) idle();
906
+}
907
+
867 908
 void setup_killpin() {
868 909
   #if HAS_KILL
869 910
     SET_INPUT_PULLUP(KILL_PIN);
@@ -882,7 +923,6 @@ void setup_killpin() {
882 923
 
883 924
 #endif
884 925
 
885
-// Set home pin
886 926
 void setup_homepin(void) {
887 927
   #if HAS_HOME
888 928
     SET_INPUT_PULLUP(HOME_PIN);
@@ -971,6 +1011,11 @@ void gcode_line_error(const char* err, bool doFlush = true) {
971 1011
   serial_count = 0;
972 1012
 }
973 1013
 
1014
+/**
1015
+ * Get all commands waiting on the serial port and queue them.
1016
+ * Exit when the buffer is full or when no more characters are
1017
+ * left on the serial port.
1018
+ */
974 1019
 inline void get_serial_commands() {
975 1020
   static char serial_line_buffer[MAX_CMD_SIZE];
976 1021
   static bool serial_comment_mode = false;
@@ -1108,6 +1153,11 @@ inline void get_serial_commands() {
1108 1153
 
1109 1154
 #if ENABLED(SDSUPPORT)
1110 1155
 
1156
+  /**
1157
+   * Get commands from the SD Card until the command buffer is full
1158
+   * or until the end of the file is reached. The special character '#'
1159
+   * can also interrupt buffering.
1160
+   */
1111 1161
   inline void get_sdcard_commands() {
1112 1162
     static bool stop_buffering = false,
1113 1163
                 sd_comment_mode = false;
@@ -1126,7 +1176,7 @@ inline void get_serial_commands() {
1126 1176
     uint16_t sd_count = 0;
1127 1177
     bool card_eof = card.eof();
1128 1178
     while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
1129
-      int16_t n = card.get();
1179
+      const int16_t n = card.get();
1130 1180
       char sd_char = (char)n;
1131 1181
       card_eof = card.eof();
1132 1182
       if (card_eof || n == -1
@@ -1144,12 +1194,12 @@ inline void get_serial_commands() {
1144 1194
         }
1145 1195
         if (sd_char == '#') stop_buffering = true;
1146 1196
 
1147
-        sd_comment_mode = false; //for new command
1197
+        sd_comment_mode = false; // for new command
1148 1198
 
1149
-        if (!sd_count) continue; //skip empty lines
1199
+        if (!sd_count) continue; // skip empty lines (and comment lines)
1150 1200
 
1151
-        command_queue[cmd_queue_index_w][sd_count] = '\0'; //terminate string
1152
-        sd_count = 0; //clear buffer
1201
+        command_queue[cmd_queue_index_w][sd_count] = '\0'; // terminate string
1202
+        sd_count = 0; // clear sd line buffer
1153 1203
 
1154 1204
         _commit_command(false);
1155 1205
       }

+ 1
- 1
Marlin/language.h Zobrazit soubor

@@ -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"

Loading…
Zrušit
Uložit