From 6027055695f6fa49b857bec0552bf362e0b2bb56 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Tue, 21 Jul 2020 04:00:39 -0500
Subject: [PATCH] Optimize LCD, Wifi, etc. libraries (#18730)

---
 Marlin/src/gcode/host/M360.cpp                |  3 +-
 Marlin/src/inc/Conditionals_LCD.h             |  8 ++++
 Marlin/src/lcd/HD44780/ultralcd_HD44780.h     | 31 +++++-------
 Marlin/src/pins/pins.h                        |  1 -
 .../scripts/common-features-dependencies.py   | 21 ++++----
 buildroot/tests/esp32-tests                   |  3 +-
 platformio.ini                                | 48 +++++--------------
 7 files changed, 47 insertions(+), 68 deletions(-)

diff --git a/Marlin/src/gcode/host/M360.cpp b/Marlin/src/gcode/host/M360.cpp
index 63a0b6dfc44..146abe24d4a 100644
--- a/Marlin/src/gcode/host/M360.cpp
+++ b/Marlin/src/gcode/host/M360.cpp
@@ -115,8 +115,7 @@ void GcodeSuite::M360() {
   xyz_pos_t cmin = dmin, cmax = dmax;
   apply_motion_limits(cmin);
   apply_motion_limits(cmax);
-  const xyz_pos_t lmin = dmin.asLogical(), lmax = dmax.asLogical(),
-                  wmin = cmin.asLogical(), wmax = cmax.asLogical();
+  const xyz_pos_t wmin = cmin.asLogical(), wmax = cmax.asLogical();
 
   PGMSTR(MIN_STR, "Min");
   PGMSTR(MAX_STR, "Max");
diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h
index 10755c180ea..972d1042ee4 100644
--- a/Marlin/src/inc/Conditionals_LCD.h
+++ b/Marlin/src/inc/Conditionals_LCD.h
@@ -399,6 +399,14 @@
   #endif
 #endif
 
+#if ENABLED(SR_LCD_3W_NL)
+  // Feature checks for SR_LCD_3W_NL
+#elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)
+  #define USES_LIQUIDTWI2
+#elif ANY(HAS_CHARACTER_LCD, LCD_I2C_TYPE_PCF8575, LCD_I2C_TYPE_PCA8574, SR_LCD_2W_NL, LCM1602)
+  #define USES_LIQUIDCRYSTAL
+#endif
+
 #if ENABLED(ULTIPANEL) && DISABLED(NO_LCD_MENUS)
   #define HAS_LCD_MENU 1
 #endif
diff --git a/Marlin/src/lcd/HD44780/ultralcd_HD44780.h b/Marlin/src/lcd/HD44780/ultralcd_HD44780.h
index 12bf86a1618..4fce2daa07f 100644
--- a/Marlin/src/lcd/HD44780/ultralcd_HD44780.h
+++ b/Marlin/src/lcd/HD44780/ultralcd_HD44780.h
@@ -22,26 +22,13 @@
 #pragma once
 
 /**
- * Implementation of the LCD display routines for a Hitachi HD44780 display.
- * These are the most common LCD character displays.
+ * Hitachi HD44780 display defines and headers
  */
 
 #include "../../inc/MarlinConfig.h"
 
-#if LCD_HEIGHT > 3
-  #include "../../libs/duration_t.h"
-#endif
-
-////////////////////////////////////
-// Setup button and encode mappings for each panel (into 'buttons' variable
-//
-// This is just to map common functions (across different panels) onto the same
-// macro name. The mapping is independent of whether the button is directly connected or
-// via a shift/i2c register.
-
-////////////////////////////////////
-// Create LCD class instance and chipset-specific information
 #if ENABLED(LCD_I2C_TYPE_PCF8575)
+
   // NOTE: These are register-mapped pins on the PCF8575 controller, not Arduino pins.
   #define LCD_I2C_PIN_BL  3
   #define LCD_I2C_PIN_EN  2
@@ -58,6 +45,7 @@
   #define LCD_CLASS LiquidCrystal_I2C
 
 #elif ENABLED(LCD_I2C_TYPE_MCP23017)
+
   // For the LED indicators (which may be mapped to different events in update_indicators())
   #define LCD_HAS_STATUS_INDICATORS
   #define LED_A 0x04 //100
@@ -69,40 +57,45 @@
   #define LCD_CLASS LiquidTWI2
 
 #elif ENABLED(LCD_I2C_TYPE_MCP23008)
+
   #include <Wire.h>
   #include <LiquidTWI2.h>
   #define LCD_CLASS LiquidTWI2
 
 #elif ENABLED(LCD_I2C_TYPE_PCA8574)
+
   #include <LiquidCrystal_I2C.h>
   #define LCD_CLASS LiquidCrystal_I2C
 
 #elif ENABLED(SR_LCD_2W_NL)
+
   // 2 wire Non-latching LCD SR from:
   // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/schematics#!shiftregister-connection
-//  extern "C" void __cxa_pure_virtual() { while (1); }
   #include <LCD.h>
   #include <LiquidCrystal_SR.h>
   #define LCD_CLASS LiquidCrystal_SR
+
 #elif ENABLED(SR_LCD_3W_NL)
 
-//NewLiquidCrystal was not working for me, but this worked first try
-//https://github.com/mikeshub/SailfishLCD
-//uses the code directly from Sailfish
+  // NewLiquidCrystal didn't work, so this uses
+  // https://github.com/mikeshub/SailfishLCD
 
   #include <SailfishLCD.h>
   #define LCD_CLASS LiquidCrystalSerial
 
 #elif ENABLED(LCM1602)
+
   #include <Wire.h>
   #include <LCD.h>
   #include <LiquidCrystal_I2C.h>
   #define LCD_CLASS LiquidCrystal_I2C
 
 #else
+
   // Standard directly connected LCD implementations
   #include <LiquidCrystal.h>
   #define LCD_CLASS LiquidCrystal
+
 #endif
 
 #include "../fontutils.h"
diff --git a/Marlin/src/pins/pins.h b/Marlin/src/pins/pins.h
index 61db5a77f52..e92001a25c9 100644
--- a/Marlin/src/pins/pins.h
+++ b/Marlin/src/pins/pins.h
@@ -615,7 +615,6 @@
 //
 
 #elif MB(ESPRESSIF_ESP32)
-
   #include "esp32/pins_ESP32.h"                 // ESP32                                  env:esp32
 #elif MB(MRR_ESPA)
   #include "esp32/pins_MRR_ESPA.h"              // ESP32                                  env:esp32
diff --git a/buildroot/share/PlatformIO/scripts/common-features-dependencies.py b/buildroot/share/PlatformIO/scripts/common-features-dependencies.py
index 72abfdd67b5..179af222942 100644
--- a/buildroot/share/PlatformIO/scripts/common-features-dependencies.py
+++ b/buildroot/share/PlatformIO/scripts/common-features-dependencies.py
@@ -20,21 +20,24 @@ def load_config():
 	config.read("platformio.ini")
 	items = config.items('features')
 	for key in items:
-		deps = re.sub(',\\s*', '\n', key[1]).strip().split('\n')
-		if not key[0].upper() in FEATURE_DEPENDENCIES:
-			FEATURE_DEPENDENCIES[key[0].upper()] = {
+		ukey = key[0].upper()
+		if not ukey in FEATURE_DEPENDENCIES:
+			FEATURE_DEPENDENCIES[ukey] = {
 				'lib_deps': []
 			}
+		deps = re.sub(',\\s*', '\n', key[1]).strip().split('\n')
 		for dep in deps:
 			parts = dep.split('=')
 			name = parts.pop(0)
 			rest = '='.join(parts)
 			if name == 'extra_scripts':
-				FEATURE_DEPENDENCIES[key[0].upper()]['extra_scripts'] = rest
+				FEATURE_DEPENDENCIES[ukey]['extra_scripts'] = rest
 			elif name == 'src_filter':
-				FEATURE_DEPENDENCIES[key[0].upper()]['src_filter'] = rest
+				FEATURE_DEPENDENCIES[ukey]['src_filter'] = rest
+			elif name == 'lib_ignore':
+				FEATURE_DEPENDENCIES[ukey]['lib_ignore'] = rest
 			else:
-				FEATURE_DEPENDENCIES[key[0].upper()]['lib_deps'] += [dep]
+				FEATURE_DEPENDENCIES[ukey]['lib_deps'] += [dep]
 
 def get_all_known_libs():
 	known_libs = []
@@ -126,7 +129,7 @@ def search_compiler():
 		# the first path have the compiler
 		for path in env['ENV']['PATH'].split(';'):
 			if not re.search(r'platformio\\packages.*\\bin', path):
-				continue			
+				continue
 			#print(path)
 			for file in os.listdir(path):
 				if file.endswith("g++.exe"):
@@ -172,7 +175,9 @@ def load_marlin_features():
 
 def MarlinFeatureIsEnabled(env, feature):
 	load_marlin_features()
-	return feature in env["MARLIN_FEATURES"]
+	r = re.compile(feature)
+	matches = list(filter(r.match, env["MARLIN_FEATURES"]))
+	return len(matches) > 0
 
 # add a method for others scripts to check if a feature is enabled
 env.AddMethod(MarlinFeatureIsEnabled)
diff --git a/buildroot/tests/esp32-tests b/buildroot/tests/esp32-tests
index ccc01a1c1c8..992b3ec5ff4 100755
--- a/buildroot/tests/esp32-tests
+++ b/buildroot/tests/esp32-tests
@@ -11,11 +11,10 @@ set -e
 #
 restore_configs
 opt_set MOTHERBOARD BOARD_ESPRESSIF_ESP32
-opt_enable WIFISUPPORT GCODE_MACROS BAUD_RATE_GCODE M115_GEOMETRY_REPORT REPETIER_GCODE_M360
+opt_enable WIFISUPPORT WEBSUPPORT GCODE_MACROS BAUD_RATE_GCODE M115_GEOMETRY_REPORT REPETIER_GCODE_M360
 opt_add WIFI_SSID "\"ssid\""
 opt_add WIFI_PWD "\"password\""
 opt_set TX_BUFFER_SIZE 64
-opt_add WEBSUPPORT
 exec_test $1 $2 "ESP32 with WIFISUPPORT and WEBSUPPORT"
 
 #
diff --git a/platformio.ini b/platformio.ini
index 865755d73fe..c59f2790733 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -40,21 +40,25 @@ lib_deps           =
 TFT_LVGL_UI         = MKS-LittlevGL=https://github.com/makerbase-mks/MKS-LittlevGL/archive/master.zip
                       src_filter=+<src/lcd/extui/lib/mks_ui>
 HAS_TRINAMIC_CONFIG = TMCStepper@~0.7.1
-SR_LCD_2W_NL        = SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
 SR_LCD_3W_NL        = SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
-DIGIPOT_MCP4018     = SlowSoftI2CMaster
-DIGIPOT_MCP4451     = SlowSoftI2CMaster
+DIGIPOT_MCP4...     = SlowSoftI2CMaster
 HAS_TMC26X          = TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
 HAS_L64XX           = Arduino-L6470@0.8.0
 NEOPIXEL_LED        = Adafruit NeoPixel@1.5.0
 MAX6675_IS_MAX31865 = Adafruit MAX31865 library@~1.1.0
 HAS_GRAPHICAL_LCD   = U8glib-HAL@0.4.1
                       src_filter=+<src/lcd/dogm>
-HAS_CHARACTER_LCD   = LiquidCrystal@1.5.0, LiquidTWI2@1.2.7
+USES_LIQUIDCRYSTAL  = LiquidCrystal@1.5.0
+USES_LIQUIDTWI2     = LiquidTWI2@1.2.7
 TOUCH_UI_FTDI_EVE   = src_filter=+<src/lcd/extui/lib/ftdi_eve_touch_ui>
 HAS_DGUS_LCD        = src_filter=+<src/lcd/extui/lib/dgus>
 DWIN_CREALITY_LCD   = src_filter=+<src/lcd/dwin>
 HAS_LCD_MENU        = src_filter=+<src/lcd/menu>
+(ESP32_)?WIFISUPPORT = AsyncTCP, ESP Async WebServer
+  ESP3DLib=https://github.com/luc-github/ESP3DLib.git
+  arduinoWebSockets=https://github.com/Links2004/arduinoWebSockets.git
+  ESP32SSDP=https://github.com/luc-github/ESP32SSDP.git
+  lib_ignore=ESPAsyncTCP
 
 #
 # Default values apply to all 'env:' prefixed environments
@@ -306,14 +310,11 @@ lib_compat_mode   = strict
 extra_scripts     = ${common.extra_scripts}
   Marlin/src/HAL/LPC1768/upload_extra_script.py
 src_filter        = ${common.default_src_filter} +<src/HAL/LPC1768>
-lib_deps          = Servo
+lib_deps          = ${common.lib_deps}
+  Servo
   LiquidCrystal@1.0.0
-  U8glib-HAL@0.4.1
-  TMCStepper@~0.7.1
   Adafruit NeoPixel=https://github.com/p3p/Adafruit_NeoPixel/archive/1.5.0.zip
-  SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
 build_flags       = ${common.build_flags} -DU8G_HAL_LINKS -IMarlin/src/HAL/LPC1768/include -IMarlin/src/HAL/LPC1768/u8g
-lib_ignore        = LiquidTWI2
   # debug options for backtrace
   #-funwind-tables
   #-mpoke-function-name
@@ -361,8 +362,7 @@ build_flags   = !python Marlin/src/HAL/STM32F1/build_flags.py
   ${common.build_flags} -std=gnu++14 -DHAVE_SW_SERIAL
 build_unflags = -std=gnu++11
 src_filter    = ${common.default_src_filter} +<src/HAL/STM32F1>
-lib_ignore    =
-  SPI
+lib_ignore    = SPI
 lib_deps      = ${common.lib_deps}
   SoftwareSerialM
 
@@ -395,18 +395,10 @@ build_flags       = ${common_stm32f1.build_flags}
 extra_scripts     = ${common.extra_scripts}
   pre:buildroot/share/PlatformIO/scripts/STM32F1_create_variant.py
   buildroot/share/PlatformIO/scripts/STM32F103RC_MEEB_3DP.py
-lib_deps          =
-  TMCStepper@~0.7.1
-  Adafruit MAX31865 library@~1.1.0
-  U8glib-HAL@0.4.1
-  Arduino-L6470@0.8.0
-  SlowSoftI2CMaster
-  LiquidTWI2@1.2.7
+lib_deps          = ${common.lib_deps}
   Adafruit NeoPixel=https://github.com/ccccmagicboy/Adafruit_NeoPixel#meeb_3dp_use
-  SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
   SoftwareSerialM
   USBComposite for STM32F1@0.91
-lib_ignore        = SPI
 debug_tool        = stlink
 upload_protocol   = dfu
 
@@ -498,7 +490,6 @@ lib_deps          = ${common_stm32f1.lib_deps}
 platform      = ${common_stm32.platform}
 board         = disco_f407vg
 build_flags   = ${common.build_flags} -DUSE_STM32GENERIC -DSTM32GENERIC -DSTM32F4 -DMENU_USB_SERIAL -DMENU_SERIAL=SerialUSB -DHAL_IWDG_MODULE_ENABLED
-lib_ignore    = Adafruit NeoPixel, TMCStepper
 src_filter    = ${common.default_src_filter} +<src/HAL/STM32_F4_F7> -<src/HAL/STM32_F4_F7/STM32F7>
 
 #
@@ -508,7 +499,6 @@ src_filter    = ${common.default_src_filter} +<src/HAL/STM32_F4_F7> -<src/HAL/ST
 platform      = ${common_stm32.platform}
 board         = remram_v1
 build_flags   = ${common.build_flags} -DUSE_STM32GENERIC -DSTM32GENERIC -DSTM32F7 -DMENU_USB_SERIAL -DMENU_SERIAL=SerialUSB -DHAL_IWDG_MODULE_ENABLED
-lib_ignore    = Adafruit NeoPixel, TMCStepper
 src_filter    = ${common.default_src_filter} +<src/HAL/STM32_F4_F7> -<src/HAL/STM32_F4_F7/STM32F4>
 
 #
@@ -550,7 +540,6 @@ build_flags   = ${common_stm32f1.build_flags}
 build_unflags = ${common_stm32f1.build_unflags}
   -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1 -DERROR_LED_PORT=GPIOE -DERROR_LED_PIN=6
 lib_ignore    = ${common_stm32f1.lib_ignore}
-  LiquidTWI2
 
 #
 # MKS Robin Mini (STM32F103VET6)
@@ -911,13 +900,6 @@ monitor_speed = 500000
 platform      = espressif32@1.11.2
 board         = esp32dev
 build_flags   = ${common.build_flags} -DCORE_DEBUG_LEVEL=0
-lib_deps      = ${common.lib_deps}
-  AsyncTCP=https://github.com/me-no-dev/AsyncTCP/archive/master.zip
-  ESPAsyncWebServer=https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip
-  ESP3DLib=https://github.com/luc-github/ESP3DLib.git
-  arduinoWebSockets=https://github.com/Links2004/arduinoWebSockets.git
-  ESP32SSDP=https://github.com/luc-github/ESP32SSDP.git
-lib_ignore    = LiquidCrystal, LiquidTWI2, SailfishLCD, ESPAsyncTCP
 src_filter    = ${common.default_src_filter} +<src/HAL/ESP32>
 upload_speed  = 115200
 #upload_port   = marlinesp.local
@@ -929,8 +911,6 @@ upload_speed  = 115200
 [env:teensy31]
 platform      = teensy
 board         = teensy31
-lib_deps      = ${common.lib_deps}
-  TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
 src_filter    = ${common.default_src_filter} +<src/HAL/TEENSY31_32>
 
 #
@@ -939,8 +919,6 @@ src_filter    = ${common.default_src_filter} +<src/HAL/TEENSY31_32>
 [env:teensy35]
 platform      = teensy
 board         = teensy35
-lib_deps      = ${common.lib_deps}
-  TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
 src_filter    = ${common.default_src_filter} +<src/HAL/TEENSY35_36>
 
 #
@@ -964,6 +942,4 @@ src_filter      = ${common.default_src_filter} +<src/HAL/LINUX>
 platform    = atmelavr
 board       = megaatmega2560
 build_flags = -c -H -std=gnu++11 -Wall -Os -D__MARLIN_FIRMWARE__
-lib_deps    = ${common.lib_deps}
-  TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
 src_filter  = +<src/Marlin.cpp>