From 27c5ede796fc99527a4661254250158e71891121 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Thu, 24 May 2018 05:47:55 -0500
Subject: [PATCH 1/2] Add USE_MARLINSERIAL conditional

---
 Marlin/Conditionals_LCD.h | 2 ++
 Marlin/MarlinConfig.h     | 2 +-
 Marlin/MarlinSerial.cpp   | 6 +++---
 Marlin/MarlinSerial.h     | 6 +++---
 Marlin/Marlin_main.cpp    | 5 ++---
 Marlin/SanityCheck.h      | 4 ++--
 Marlin/serial.h           | 8 ++++----
 7 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/Marlin/Conditionals_LCD.h b/Marlin/Conditionals_LCD.h
index ae487e36ce8..c9d44b1be3e 100644
--- a/Marlin/Conditionals_LCD.h
+++ b/Marlin/Conditionals_LCD.h
@@ -505,4 +505,6 @@
 #define HAS_RESUME_CONTINUE (ENABLED(NEWPANEL) || ENABLED(EMERGENCY_PARSER))
 #define HAS_COLOR_LEDS (ENABLED(BLINKM) || ENABLED(RGB_LED) || ENABLED(RGBW_LED) || ENABLED(PCA9632) || ENABLED(NEOPIXEL_LED))
 
+#define USE_MARLINSERIAL !(defined(__AVR__) && defined(USBCON))
+
 #endif // CONDITIONALS_LCD_H
diff --git a/Marlin/MarlinConfig.h b/Marlin/MarlinConfig.h
index 47e6020d991..f0aa1304434 100644
--- a/Marlin/MarlinConfig.h
+++ b/Marlin/MarlinConfig.h
@@ -30,7 +30,7 @@
 #include "Conditionals_LCD.h"
 #include "Configuration_adv.h"
 
-#if defined(__AVR__) && !defined(USBCON)
+#if USE_MARLINSERIAL
   #define HardwareSerial_h // trick to disable the standard HWserial
 #endif
 
diff --git a/Marlin/MarlinSerial.cpp b/Marlin/MarlinSerial.cpp
index cd4dd03ade7..83bc49edc44 100644
--- a/Marlin/MarlinSerial.cpp
+++ b/Marlin/MarlinSerial.cpp
@@ -34,7 +34,7 @@
 
 #include "MarlinConfig.h"
 
-#if !(defined(__AVR__) && defined(USBCON)) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
+#if USE_MARLINSERIAL && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
 
   #include "MarlinSerial.h"
   #include "Marlin.h"
@@ -561,9 +561,9 @@
   // Preinstantiate
   MarlinSerial customizedSerial;
 
-#endif // !(__AVR__ && USBCON) && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
+#endif // USE_MARLINSERIAL && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
 
 // For AT90USB targets use the UART for BT interfacing
-#if defined(__AVR__) && defined(USBCON) && ENABLED(BLUETOOTH)
+#if !USE_MARLINSERIAL && ENABLED(BLUETOOTH)
   HardwareSerial bluetoothSerial;
 #endif
diff --git a/Marlin/MarlinSerial.h b/Marlin/MarlinSerial.h
index 9060f668ad2..96071f800cb 100644
--- a/Marlin/MarlinSerial.h
+++ b/Marlin/MarlinSerial.h
@@ -85,7 +85,7 @@
   #define TX_BUFFER_SIZE 32
 #endif
 
-#if !(defined(__AVR__) && defined(USBCON))
+#if USE_MARLINSERIAL
 
   #if RX_BUFFER_SIZE > 256
     typedef uint16_t ring_buffer_pos_t;
@@ -159,10 +159,10 @@
 
   extern MarlinSerial customizedSerial;
 
-#endif // !(__AVR__ && USBCON)
+#endif // USE_MARLINSERIAL
 
 // Use the UART for Bluetooth in AT90USB configurations
-#if defined(__AVR__) && defined(USBCON) && ENABLED(BLUETOOTH)
+#if !USE_MARLINSERIAL && ENABLED(BLUETOOTH)
   extern HardwareSerial bluetoothSerial;
 #endif
 
diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index 2211bda88e4..08384b17593 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -14500,15 +14500,14 @@ void loop() {
           card.closefile();
           SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
 
-          #if !(defined(__AVR__) && defined(USBCON))
+          #if USE_MARLINSERIAL
             #if ENABLED(SERIAL_STATS_DROPPED_RX)
               SERIAL_ECHOLNPAIR("Dropped bytes: ", customizedSerial.dropped());
             #endif
-
             #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
               SERIAL_ECHOLNPAIR("Max RX Queue Size: ", customizedSerial.rxMaxEnqueued());
             #endif
-          #endif // !(__AVR__ && USBCON)
+          #endif
 
           ok_to_send();
         }
diff --git a/Marlin/SanityCheck.h b/Marlin/SanityCheck.h
index 6e7b6228628..bf6d449cb76 100644
--- a/Marlin/SanityCheck.h
+++ b/Marlin/SanityCheck.h
@@ -307,7 +307,7 @@
 /**
  * Serial
  */
-#if !(defined(__AVR__) && defined(USBCON))
+#if USE_MARLINSERIAL
   #if ENABLED(SERIAL_XON_XOFF) && RX_BUFFER_SIZE < 1024
     #error "SERIAL_XON_XOFF requires RX_BUFFER_SIZE >= 1024 for reliable transfers without drops."
   #elif RX_BUFFER_SIZE && (RX_BUFFER_SIZE < 2 || !IS_POWER_OF_2(RX_BUFFER_SIZE))
@@ -1274,7 +1274,7 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE && Y_MAX_LENGTH >= Y_BED_SIZE,
 /**
  * emergency-command parser
  */
-#if ENABLED(EMERGENCY_PARSER) && defined(__AVR__) && defined(USBCON)
+#if ENABLED(EMERGENCY_PARSER) && !USE_MARLINSERIAL
   #error "EMERGENCY_PARSER does not work on boards with AT90USB processors (USBCON)."
 #endif
 
diff --git a/Marlin/serial.h b/Marlin/serial.h
index dc1da873535..139f99c1102 100644
--- a/Marlin/serial.h
+++ b/Marlin/serial.h
@@ -25,7 +25,10 @@
 
 #include "MarlinConfig.h"
 
-#if defined(__AVR__) && defined(USBCON)
+#if USE_MARLINSERIAL
+  #include "MarlinSerial.h"
+  #define MYSERIAL0 customizedSerial
+#else
   #include <HardwareSerial.h>
   #if ENABLED(BLUETOOTH)
     extern HardwareSerial bluetoothSerial;
@@ -33,9 +36,6 @@
   #else
     #define MYSERIAL0 Serial
   #endif // BLUETOOTH
-#else
-  #include "MarlinSerial.h"
-  #define MYSERIAL0 customizedSerial
 #endif
 
 extern const char echomagic[] PROGMEM;

From 181516f7cb9c1201a0db228d8865d426ea205460 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Thu, 24 May 2018 04:35:15 -0500
Subject: [PATCH 2/2] Fix Malyan LCD, allow no SD Card

---
 Marlin/malyanlcd.cpp | 174 +++++++++++++++++++++++++------------------
 1 file changed, 103 insertions(+), 71 deletions(-)

diff --git a/Marlin/malyanlcd.cpp b/Marlin/malyanlcd.cpp
index e72d188bbd3..e8d6c251438 100644
--- a/Marlin/malyanlcd.cpp
+++ b/Marlin/malyanlcd.cpp
@@ -45,8 +45,13 @@
 
 #if ENABLED(MALYAN_LCD)
 
-#include "cardreader.h"
-#include "SdFatConfig.h"
+#if ENABLED(SDSUPPORT)
+  #include "cardreader.h"
+  #include "SdFatConfig.h"
+#else
+  #define LONG_FILENAME_LENGTH 0
+#endif
+
 #include "temperature.h"
 #include "planner.h"
 #include "stepper.h"
@@ -57,6 +62,15 @@
 
 #include "Marlin.h"
 
+#if USE_MARLINSERIAL
+  // Make an exception to use HardwareSerial too
+  #undef HardwareSerial_h
+  #include <HardwareSerial.h>
+  #define USB_STATUS true
+#else
+  #define USB_STATUS Serial
+#endif
+
 // On the Malyan M200, this will be Serial1. On a RAMPS board,
 // it might not be.
 #define LCD_SERIAL Serial1
@@ -132,8 +146,6 @@ void process_lcd_c_command(const char* command) {
 void process_lcd_eb_command(const char* command) {
   char elapsed_buffer[10];
   duration_t elapsed;
-  bool has_days;
-  uint8_t len;
   switch (command[0]) {
     case '0': {
       elapsed = print_job_timer.duration();
@@ -144,9 +156,17 @@ void process_lcd_eb_command(const char* command) {
               PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}{TQ:%03i}{TT:%s}"),
               thermalManager.degHotend(0),
               thermalManager.degTargetHotend(0),
-              thermalManager.degBed(),
-              thermalManager.degTargetBed(),
-              card.percentDone(),
+              #if HAS_HEATED_BED
+                thermalManager.degBed(),
+                thermalManager.degTargetBed(),
+              #else
+                0, 0,
+              #endif
+              #if ENABLED(SDSUPPORT)
+                card.percentDone(),
+              #else
+                0,
+              #endif
               elapsed_buffer);
       write_to_lcd(message_buffer);
     } break;
@@ -223,51 +243,55 @@ void process_lcd_p_command(const char* command) {
 
   switch (command[0]) {
     case 'X':
-      // cancel print
-      write_to_lcd_P(PSTR("{SYS:CANCELING}"));
-      card.stopSDPrint(
-        #if SD_RESORT
-          true
+      #if ENABLED(SDSUPPORT)
+        // cancel print
+        write_to_lcd_P(PSTR("{SYS:CANCELING}"));
+        card.stopSDPrint(
+          #if SD_RESORT
+            true
+          #endif
+        );
+        clear_command_queue();
+        quickstop_stepper();
+        print_job_timer.stop();
+        thermalManager.disable_all_heaters();
+        #if FAN_COUNT > 0
+          for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
         #endif
-      );
-      clear_command_queue();
-      quickstop_stepper();
-      print_job_timer.stop();
-      thermalManager.disable_all_heaters();
-      #if FAN_COUNT > 0
-        for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
+        wait_for_heatup = false;
+        write_to_lcd_P(PSTR("{SYS:STARTED}"));
       #endif
-      wait_for_heatup = false;
-      write_to_lcd_P(PSTR("{SYS:STARTED}"));
       break;
     case 'H':
       // Home all axis
       enqueue_and_echo_commands_now_P(PSTR("G28"));
       break;
     default: {
-      // Print file 000 - a three digit number indicating which
-      // file to print in the SD card. If it's a directory,
-      // then switch to the directory.
+      #if ENABLED(SDSUPPORT)
+        // Print file 000 - a three digit number indicating which
+        // file to print in the SD card. If it's a directory,
+        // then switch to the directory.
 
-      // Find the name of the file to print.
-      // It's needed to echo the PRINTFILE option.
-      // The {S:L} command should've ensured the SD card was mounted.
-      card.getfilename(atoi(command));
+        // Find the name of the file to print.
+        // It's needed to echo the PRINTFILE option.
+        // The {S:L} command should've ensured the SD card was mounted.
+        card.getfilename(atoi(command));
 
-      // There may be a difference in how V1 and V2 LCDs handle subdirectory
-      // prints. Investigate more. This matches the V1 motion controller actions
-      // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
-      if (card.filenameIsDir) {
-        card.chdir(card.filename);
-        write_to_lcd_P(PSTR("{SYS:DIR}"));
-      }
-      else {
-        char message_buffer[MAX_CURLY_COMMAND];
-        sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.filename);
-        write_to_lcd(message_buffer);
-        write_to_lcd_P(PSTR("{SYS:BUILD}"));
-        card.openAndPrintFile(card.filename);
-      }
+        // There may be a difference in how V1 and V2 LCDs handle subdirectory
+        // prints. Investigate more. This matches the V1 motion controller actions
+        // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
+        if (card.filenameIsDir) {
+          card.chdir(card.filename);
+          write_to_lcd_P(PSTR("{SYS:DIR}"));
+        }
+        else {
+          char message_buffer[MAX_CURLY_COMMAND];
+          sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.filename);
+          write_to_lcd(message_buffer);
+          write_to_lcd_P(PSTR("{SYS:BUILD}"));
+          card.openAndPrintFile(card.filename);
+        }
+      #endif
     } break; // default
   } // switch
 }
@@ -292,7 +316,11 @@ void process_lcd_s_command(const char* command) {
       char message_buffer[MAX_CURLY_COMMAND];
       sprintf_P(message_buffer, PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}"),
         thermalManager.degHotend(0), thermalManager.degTargetHotend(0),
-        thermalManager.degBed(), thermalManager.degTargetBed()
+        #if HAS_HEATED_BED
+          thermalManager.degBed(), thermalManager.degTargetBed()
+        #else
+          0, 0
+        #endif
       );
       write_to_lcd(message_buffer);
     } break;
@@ -303,23 +331,25 @@ void process_lcd_s_command(const char* command) {
       break;
 
     case 'L': {
-      if (!card.cardOK) card.initsd();
+      #if ENABLED(SDSUPPORT)
+        if (!card.cardOK) card.initsd();
 
-      // A more efficient way to do this would be to
-      // implement a callback in the ls_SerialPrint code, but
-      // that requires changes to the core cardreader class that
-      // would not benefit the majority of users. Since one can't
-      // select a file for printing during a print, there's
-      // little reason not to do it this way.
-      char message_buffer[MAX_CURLY_COMMAND];
-      uint16_t file_count = card.get_num_Files();
-      for (uint16_t i = 0; i < file_count; i++) {
-        card.getfilename(i);
-        sprintf_P(message_buffer, card.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.filename);
-        write_to_lcd(message_buffer);
-      }
+        // A more efficient way to do this would be to
+        // implement a callback in the ls_SerialPrint code, but
+        // that requires changes to the core cardreader class that
+        // would not benefit the majority of users. Since one can't
+        // select a file for printing during a print, there's
+        // little reason not to do it this way.
+        char message_buffer[MAX_CURLY_COMMAND];
+        uint16_t file_count = card.get_num_Files();
+        for (uint16_t i = 0; i < file_count; i++) {
+          card.getfilename(i);
+          sprintf_P(message_buffer, card.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.filename);
+          write_to_lcd(message_buffer);
+        }
 
-      write_to_lcd_P(PSTR("{SYS:OK}"));
+        write_to_lcd_P(PSTR("{SYS:OK}"));
+      #endif
     } break;
 
     default:
@@ -371,15 +401,15 @@ void process_lcd_command(const char* command) {
 /**
  * UC means connected.
  * UD means disconnected
- * The stock firmware considers USB initialied as "connected."
+ * The stock firmware considers USB initialized as "connected."
  */
 void update_usb_status(const bool forceUpdate) {
   static bool last_usb_connected_status = false;
   // This is mildly different than stock, which
   // appears to use the usb discovery status.
   // This is more logical.
-  if (last_usb_connected_status != Serial || forceUpdate) {
-    last_usb_connected_status =  Serial;
+  if (last_usb_connected_status != USB_STATUS || forceUpdate) {
+    last_usb_connected_status = USB_STATUS;
     write_to_lcd_P(last_usb_connected_status ? PSTR("{R:UC}\r\n") : PSTR("{R:UD}\r\n"));
   }
 }
@@ -390,7 +420,7 @@ void update_usb_status(const bool forceUpdate) {
  * The optimize attribute fixes a register Compile
  * error for amtel.
  */
-void lcd_update() _O2 {
+void _O2 lcd_update() {
   static char inbound_buffer[MAX_CURLY_COMMAND];
 
   // First report USB status.
@@ -408,15 +438,17 @@ void lcd_update() _O2 {
     }
   }
 
-  // If there's a print in progress, we need to emit the status as
-  // {TQ:<PERCENT>}
-  if (card.sdprinting) {
-    // We also need to send: T:-2538.0 E:0
-    // I have no idea what this means.
-    char message_buffer[10];
-    sprintf_P(message_buffer, PSTR("{TQ:%03i}"), card.percentDone());
-    write_to_lcd(message_buffer);
-  }
+  #if ENABLED(SDSUPPORT)
+    // If there's a print in progress, we need to emit the status as
+    // {TQ:<PERCENT>}
+    if (card.sdprinting) {
+      // We also need to send: T:-2538.0 E:0
+      // I have no idea what this means.
+      char message_buffer[10];
+      sprintf_P(message_buffer, PSTR("{TQ:%03i}"), card.percentDone());
+      write_to_lcd(message_buffer);
+    }
+  #endif
 }
 
 /**