Browse Source

Ability to insert G-code in front of queue (#14229)

Robby Candra 6 years ago
parent
commit
5ca8d51e12
4 changed files with 71 additions and 11 deletions
  1. 1
    1
      Marlin/src/Marlin.cpp
  2. 57
    8
      Marlin/src/gcode/queue.cpp
  3. 12
    1
      Marlin/src/gcode/queue.h
  4. 1
    1
      Marlin/src/lcd/ultralcd.cpp

+ 1
- 1
Marlin/src/Marlin.cpp View File

@@ -381,7 +381,7 @@ void disable_all_steppers() {
381 381
     #endif // HOST_ACTION_COMMANDS
382 382
 
383 383
     if (run_runout_script)
384
-      enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
384
+      enqueue_and_echo_commands_front_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
385 385
   }
386 386
 
387 387
 #endif // HAS_FILAMENT_SENSOR

+ 57
- 8
Marlin/src/gcode/queue.cpp View File

@@ -132,6 +132,7 @@ inline bool _enqueuecommand(const char* cmd, bool say_ok=false
132 132
 
133 133
 /**
134 134
  * Enqueue with Serial Echo
135
+ * Return true if the command was consumed
135 136
  */
136 137
 bool enqueue_and_echo_command(const char* cmd) {
137 138
 
@@ -139,10 +140,7 @@ bool enqueue_and_echo_command(const char* cmd) {
139 140
   //SERIAL_ECHO(cmd);
140 141
   //SERIAL_ECHOPGM("\") \n");
141 142
 
142
-  if (*cmd == 0 || *cmd == '\n' || *cmd == '\r') {
143
-    //SERIAL_ECHOLNPGM("Null command found...   Did not queue!");
144
-    return true;
145
-  }
143
+  if (*cmd == 0 || *cmd == '\n' || *cmd == '\r') return true;
146 144
 
147 145
   if (_enqueuecommand(cmd)) {
148 146
     SERIAL_ECHO_START();
@@ -152,30 +150,81 @@ bool enqueue_and_echo_command(const char* cmd) {
152 150
   return false;
153 151
 }
154 152
 
153
+#if HAS_QUEUE_FRONT
154
+
155
+  bool early_cmd; // = false
156
+
157
+  /**
158
+   * Insert a high Priority command from RAM into the main command buffer.
159
+   * Return true if the command was consumed
160
+   * Return false for a full buffer, or if the 'command' is a comment.
161
+   */
162
+  inline bool _enqueuecommand_front(const char* cmd) {
163
+    if (*cmd == 0 || *cmd == '\n' || *cmd == '\r') return true;
164
+    if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
165
+    if (cmd_queue_index_r == 0) cmd_queue_index_r = BUFSIZE;
166
+    --cmd_queue_index_r;
167
+    strcpy(command_queue[cmd_queue_index_r], cmd);
168
+    send_ok[cmd_queue_index_r] = false;
169
+    #if NUM_SERIAL > 1
170
+      command_queue_port[cmd_queue_index_r] = -1;
171
+    #endif
172
+    commands_in_queue++;
173
+    return true;
174
+  }
175
+
176
+  /**
177
+   * Insert in the front of queue, one or many commands to run from program memory.
178
+   * Aborts the current queue, if any.
179
+   * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
180
+   */
181
+  void enqueue_and_echo_commands_front_P(PGM_P const pgcode) {
182
+    early_cmd = true;
183
+    enqueue_and_echo_commands_P(pgcode);
184
+  }
185
+
186
+#endif
187
+
155 188
 /**
156 189
  * Inject the next "immediate" command, when possible, onto the front of the queue.
157 190
  * Return true if any immediate commands remain to inject.
191
+ * Do not inject a comment or use leading space!.
158 192
  */
159 193
 static bool drain_injected_commands_P() {
160
-  if (injected_commands_P != nullptr) {
194
+  while (injected_commands_P != nullptr) {
161 195
     size_t i = 0;
162 196
     char c, cmd[60];
163 197
     strncpy_P(cmd, injected_commands_P, sizeof(cmd) - 1);
164 198
     cmd[sizeof(cmd) - 1] = '\0';
165 199
     while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
166 200
     cmd[i] = '\0';
167
-    if (enqueue_and_echo_command(cmd))     // success?
201
+
202
+    if (
203
+      #if HAS_QUEUE_FRONT
204
+        early_cmd ? _enqueuecommand_front(cmd) :
205
+      #endif
206
+      enqueue_and_echo_command(cmd)
207
+    ) {
168 208
       injected_commands_P = c ? injected_commands_P + i + 1 : nullptr; // next command or done
209
+      #if HAS_QUEUE_FRONT
210
+        if (!c) early_cmd = false;
211
+      #endif
212
+    }
213
+    else
214
+      return true; // buffer is full (or command is comment);
169 215
   }
170
-  return (injected_commands_P != nullptr);    // return whether any more remain
216
+  return false;   // return whether any more remain
171 217
 }
172 218
 
173 219
 /**
174
- * Record one or many commands to run from program memory.
220
+ * Enqueue one or many commands to run from program memory.
175 221
  * Aborts the current queue, if any.
176 222
  * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
177 223
  */
178 224
 void enqueue_and_echo_commands_P(PGM_P const pgcode) {
225
+  #if HAS_QUEUE_FRONT
226
+    early_cmd = false;
227
+  #endif
179 228
   injected_commands_P = pgcode;
180 229
   (void)drain_injected_commands_P(); // first command executed asap (when possible)
181 230
 }

+ 12
- 1
Marlin/src/gcode/queue.h View File

@@ -83,8 +83,17 @@ void flush_and_request_resend();
83 83
  */
84 84
 void ok_to_send();
85 85
 
86
+#if ENABLED(ADVANCED_PAUSE_FEATURE)
87
+  /**
88
+   * Insert in the front of queue, one or many commands to run from program memory.
89
+   * Aborts the current queue, if any.
90
+   * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
91
+   */
92
+  void enqueue_and_echo_commands_front_P(PGM_P const pgcode);
93
+#endif
94
+
86 95
 /**
87
- * Record one or many commands to run from program memory.
96
+ * Enqueue one or many commands to run from program memory.
88 97
  * Aborts the current queue, if any.
89 98
  * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
90 99
  */
@@ -92,11 +101,13 @@ void enqueue_and_echo_commands_P(PGM_P const pgcode);
92 101
 
93 102
 /**
94 103
  * Enqueue with Serial Echo
104
+ * Return true on success
95 105
  */
96 106
 bool enqueue_and_echo_command(const char* cmd);
97 107
 
98 108
 #define HAS_LCD_QUEUE_NOW (ENABLED(MALYAN_LCD) || (HAS_LCD_MENU && ANY(AUTO_BED_LEVELING_UBL, PID_AUTOTUNE_MENU, ADVANCED_PAUSE_FEATURE)))
99 109
 #define HAS_QUEUE_NOW (ENABLED(SDSUPPORT) || HAS_LCD_QUEUE_NOW)
110
+#define HAS_QUEUE_FRONT ENABLED(ADVANCED_PAUSE_FEATURE)
100 111
 
101 112
 #if HAS_QUEUE_NOW
102 113
   /**

+ 1
- 1
Marlin/src/lcd/ultralcd.cpp View File

@@ -1398,7 +1398,7 @@ void MarlinUI::update() {
1398 1398
       #if HAS_SPI_LCD
1399 1399
         lcd_pause_show_message(PAUSE_MESSAGE_PAUSING, PAUSE_MODE_PAUSE_PRINT);  // Show message immediately to let user know about pause in progress
1400 1400
       #endif
1401
-      enqueue_and_echo_commands_P(PSTR("M25 P\nM24"));
1401
+      enqueue_and_echo_commands_front_P(PSTR("M25 P\nM24"));
1402 1402
     #elif ENABLED(SDSUPPORT)
1403 1403
       enqueue_and_echo_commands_P(PSTR("M25"));
1404 1404
     #elif defined(ACTION_ON_PAUSE)

Loading…
Cancel
Save