From d5b791a26a974a1ca134f4b91c5af1a042ad89aa Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Wed, 22 Apr 2020 22:02:31 -0500
Subject: [PATCH] Fix Malyan ExtUI parse_lcd_byte

See https://community.platformio.org/t/marlin-compile-fail-board-board-h-no-such-file-or-directory/11843/43
---
 Marlin/src/lcd/extui_malyan_lcd.cpp | 48 +++++++++++++++++------------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/Marlin/src/lcd/extui_malyan_lcd.cpp b/Marlin/src/lcd/extui_malyan_lcd.cpp
index ddb8b846cb..73de849087 100644
--- a/Marlin/src/lcd/extui_malyan_lcd.cpp
+++ b/Marlin/src/lcd/extui_malyan_lcd.cpp
@@ -71,7 +71,7 @@
 #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
 
 // Track incoming command bytes from the LCD
-int inbound_count;
+uint16_t inbound_count;
 
 // For sending print completion messages
 bool last_printing_status = false;
@@ -361,29 +361,38 @@ void process_lcd_command(const char* command) {
     DEBUG_ECHOLNPAIR("UNKNOWN COMMAND FORMAT ", command);
 }
 
+//
 // Parse LCD commands mixed with G-Code
-void parse_lcd_byte(byte b) {
-  static bool parsing_lcd_cmd = false;
+//
+void parse_lcd_byte(const byte b) {
   static char inbound_buffer[MAX_CURLY_COMMAND];
 
-  if (!parsing_lcd_cmd) {
-    if (b == '{' || b == '\n' || b == '\r') {   // A line-ending or opening brace
-      parsing_lcd_cmd = b == '{';               // Brace opens an LCD command
-      if (inbound_count) {                      // Looks like a G-code is in the buffer
-        inbound_buffer[inbound_count] = '\0';   // Reset before processing
-        inbound_count = 0;
+  static uint8_t parsing = 0;                   // Parsing state
+  static bool prevcr = false;                   // Was the last c a CR?
+
+  const char c = b & 0x7F;
+
+  if (parsing) {
+    const bool is_lcd = parsing == 1;           // 1 for LCD
+    if ( ( is_lcd && c == '}')                  // Closing brace on LCD command
+      || (!is_lcd && c == '\n')                 // LF on a G-code command
+    ) {
+      inbound_buffer[inbound_count] = '\0';     // Reset before processing
+      parsing = 0;                              // Unflag and...
+      inbound_count = 0;                        // Reset buffer index
+      if (parsing == 1)
+        process_lcd_command(inbound_buffer);    // Handle the LCD command
+      else
         queue.enqueue_one_now(inbound_buffer);  // Handle the G-code command
-      }
     }
+    else if (inbound_count < MAX_CURLY_COMMAND - 2)
+      inbound_buffer[inbound_count++] = is_lcd ? c : b; // Buffer while space remains
   }
-  else if (b == '}') {                          // Closing brace on an LCD command
-    parsing_lcd_cmd = false;                    // Unflag and...
-    inbound_buffer[inbound_count] = '\0';       // reset before processing
-    inbound_count = 0;
-    process_lcd_command(inbound_buffer);        // Handle the LCD command
+  else {
+         if (c == '{')            parsing = 1;  // Brace opens an LCD command
+    else if (prevcr && c == '\n') parsing = 2;  // CRLF indicates G-code
+    prevcr = (c == '\r');                       // Remember if it was a CR
   }
-  else if (inbound_count < MAX_CURLY_COMMAND - 2)
-    inbound_buffer[inbound_count++] = b;        // Buffer only if space remains
 }
 
 /**
@@ -433,9 +442,8 @@ namespace ExtUI {
     update_usb_status(false);
 
     // now drain commands...
-    while (LCD_SERIAL.available()) {
-      parse_lcd_byte((byte)LCD_SERIAL.read() & 0x7F);
-    }
+    while (LCD_SERIAL.available())
+      parse_lcd_byte((byte)LCD_SERIAL.read());
 
     #if ENABLED(SDSUPPORT)
       // The way last printing status works is simple: