Scott Lahteine 5 years ago
parent
commit
d5b791a26a
1 changed files with 28 additions and 20 deletions
  1. 28
    20
      Marlin/src/lcd/extui_malyan_lcd.cpp

+ 28
- 20
Marlin/src/lcd/extui_malyan_lcd.cpp View File

@@ -71,7 +71,7 @@
71 71
 #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
72 72
 
73 73
 // Track incoming command bytes from the LCD
74
-int inbound_count;
74
+uint16_t inbound_count;
75 75
 
76 76
 // For sending print completion messages
77 77
 bool last_printing_status = false;
@@ -361,29 +361,38 @@ void process_lcd_command(const char* command) {
361 361
     DEBUG_ECHOLNPAIR("UNKNOWN COMMAND FORMAT ", command);
362 362
 }
363 363
 
364
+//
364 365
 // Parse LCD commands mixed with G-Code
365
-void parse_lcd_byte(byte b) {
366
-  static bool parsing_lcd_cmd = false;
366
+//
367
+void parse_lcd_byte(const byte b) {
367 368
   static char inbound_buffer[MAX_CURLY_COMMAND];
368 369
 
369
-  if (!parsing_lcd_cmd) {
370
-    if (b == '{' || b == '\n' || b == '\r') {   // A line-ending or opening brace
371
-      parsing_lcd_cmd = b == '{';               // Brace opens an LCD command
372
-      if (inbound_count) {                      // Looks like a G-code is in the buffer
373
-        inbound_buffer[inbound_count] = '\0';   // Reset before processing
374
-        inbound_count = 0;
370
+  static uint8_t parsing = 0;                   // Parsing state
371
+  static bool prevcr = false;                   // Was the last c a CR?
372
+
373
+  const char c = b & 0x7F;
374
+
375
+  if (parsing) {
376
+    const bool is_lcd = parsing == 1;           // 1 for LCD
377
+    if ( ( is_lcd && c == '}')                  // Closing brace on LCD command
378
+      || (!is_lcd && c == '\n')                 // LF on a G-code command
379
+    ) {
380
+      inbound_buffer[inbound_count] = '\0';     // Reset before processing
381
+      parsing = 0;                              // Unflag and...
382
+      inbound_count = 0;                        // Reset buffer index
383
+      if (parsing == 1)
384
+        process_lcd_command(inbound_buffer);    // Handle the LCD command
385
+      else
375 386
         queue.enqueue_one_now(inbound_buffer);  // Handle the G-code command
376
-      }
377 387
     }
388
+    else if (inbound_count < MAX_CURLY_COMMAND - 2)
389
+      inbound_buffer[inbound_count++] = is_lcd ? c : b; // Buffer while space remains
378 390
   }
379
-  else if (b == '}') {                          // Closing brace on an LCD command
380
-    parsing_lcd_cmd = false;                    // Unflag and...
381
-    inbound_buffer[inbound_count] = '\0';       // reset before processing
382
-    inbound_count = 0;
383
-    process_lcd_command(inbound_buffer);        // Handle the LCD command
391
+  else {
392
+         if (c == '{')            parsing = 1;  // Brace opens an LCD command
393
+    else if (prevcr && c == '\n') parsing = 2;  // CRLF indicates G-code
394
+    prevcr = (c == '\r');                       // Remember if it was a CR
384 395
   }
385
-  else if (inbound_count < MAX_CURLY_COMMAND - 2)
386
-    inbound_buffer[inbound_count++] = b;        // Buffer only if space remains
387 396
 }
388 397
 
389 398
 /**
@@ -433,9 +442,8 @@ namespace ExtUI {
433 442
     update_usb_status(false);
434 443
 
435 444
     // now drain commands...
436
-    while (LCD_SERIAL.available()) {
437
-      parse_lcd_byte((byte)LCD_SERIAL.read() & 0x7F);
438
-    }
445
+    while (LCD_SERIAL.available())
446
+      parse_lcd_byte((byte)LCD_SERIAL.read());
439 447
 
440 448
     #if ENABLED(SDSUPPORT)
441 449
       // The way last printing status works is simple:

Loading…
Cancel
Save