From 58bca6788374e6a0fcd8b3a5c513e1ad520b9a10 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sun, 17 Mar 2019 05:32:12 -0500
Subject: [PATCH] Unify status scrolling further

---
 Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp   | 43 ++++++-------------
 Marlin/src/lcd/dogm/status_screen_DOGM.cpp    | 41 ++++++------------
 .../lcd/dogm/status_screen_lite_ST7920.cpp    | 36 +++++-----------
 Marlin/src/lcd/ultralcd.cpp                   | 17 +++++++-
 Marlin/src/lcd/ultralcd.h                     | 12 +++---
 5 files changed, 58 insertions(+), 91 deletions(-)

diff --git a/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp b/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp
index 5ae69c7224..ce391bb406 100644
--- a/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp
+++ b/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp
@@ -660,33 +660,22 @@ void MarlinUI::draw_status_message(const bool blink) {
       lcd_put_u8str(status_message);
 
       // Fill the rest with spaces
-      while (slen < LCD_WIDTH) {
-        lcd_put_wchar(' ');
-        ++slen;
-      }
+      while (slen < LCD_WIDTH) { lcd_put_wchar(' '); ++slen; }
     }
     else {
       // String is larger than the available space in screen.
 
       // Get a pointer to the next valid UTF8 character
-      const char *stat = status_message + status_scroll_offset;
-
-      // Get the string remaining length
-      const uint8_t rlen = utf8_strlen(stat);
-
-      // If we have enough characters to display
-      if (rlen >= LCD_WIDTH) {
-        // The remaining string fills the screen - Print it
-        lcd_put_u8str_max(stat, LCD_WIDTH);
-      }
-      else {
-
-        // The remaining string does not completely fill the screen
-        lcd_put_u8str_max(stat, LCD_WIDTH);               // The string leaves space
-        uint8_t chars = LCD_WIDTH - rlen;                 // Amount of space left in characters
-
-        lcd_put_wchar('.');                               // Always at 1+ spaces left, draw a dot
-        if (--chars) {                                    // Draw a second dot if there's space
+      // and the string remaining length
+      uint8_t rlen;
+      const char *stat = status_and_len(rlen);
+      lcd_put_u8str_max(stat, LCD_WIDTH);     // The string leaves space
+
+      // If the remaining string doesn't completely fill the screen
+      if (rlen < LCD_WIDTH) {
+        lcd_put_wchar('.');                   // Always at 1+ spaces left, draw a dot
+        uint8_t chars = LCD_WIDTH - rlen;     // Amount of space left in characters
+        if (--chars) {                        // Draw a second dot if there's space
           lcd_put_wchar('.');
           if (--chars)
             lcd_put_u8str_max(status_message, chars); // Print a second copy of the message
@@ -694,15 +683,7 @@ void MarlinUI::draw_status_message(const bool blink) {
       }
       if (last_blink != blink) {
         last_blink = blink;
-
-        // Adjust by complete UTF8 characters
-        if (status_scroll_offset < slen) {
-          status_scroll_offset++;
-          while (!START_OF_UTF8_CHAR(status_message[status_scroll_offset]))
-            status_scroll_offset++;
-        }
-        else
-          status_scroll_offset = 0;
+        advance_status_scroll();
       }
     }
   #else
diff --git a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp
index 828c26322e..726713e662 100644
--- a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp
+++ b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp
@@ -612,31 +612,24 @@ void MarlinUI::draw_status_message(const bool blink) {
     if (slen <= LCD_WIDTH) {
       // The string fits within the line. Print with no scrolling
       lcd_put_u8str(status_message);
-      for (; slen < LCD_WIDTH; ++slen) lcd_put_wchar(' ');
+      while (slen < LCD_WIDTH) { lcd_put_wchar(' '); ++slen; }
     }
     else {
       // String is longer than the available space
 
       // Get a pointer to the next valid UTF8 character
-      const char *stat = status_message + status_scroll_offset;
-
-      // Get the string remaining length
-      const uint8_t rlen = utf8_strlen(stat);
-
-      if (rlen >= LCD_WIDTH) {
-        // The remaining string fills the screen - Print it
-        lcd_put_u8str_max(stat, LCD_PIXEL_WIDTH);
-      }
-      else {
-        // The remaining string does not completely fill the screen
-        lcd_put_u8str_max(stat, LCD_PIXEL_WIDTH);         // The string leaves space
-        uint8_t chars = LCD_WIDTH - rlen;                 // Amount of space left in characters
-
-        lcd_put_wchar('.');                               // Always at 1+ spaces left, draw a dot
-        if (--chars) {                                    // Draw a second dot if there's space
+      // and the string remaining length
+      uint8_t rlen;
+      const char *stat = status_and_len(rlen);
+      lcd_put_u8str_max(stat, LCD_PIXEL_WIDTH);
+
+      // If the remaining string doesn't completely fill the screen
+      if (rlen < LCD_WIDTH) {
+        lcd_put_wchar('.');                     // Always at 1+ spaces left, draw a dot
+        uint8_t chars = LCD_WIDTH - rlen;       // Amount of space left in characters
+        if (--chars) {                          // Draw a second dot if there's space
           lcd_put_wchar('.');
-          if (--chars) {
-            // Print a second copy of the message
+          if (--chars) {                        // Print a second copy of the message
             lcd_put_u8str_max(status_message, LCD_PIXEL_WIDTH - (rlen + 2) * (MENU_FONT_WIDTH));
             lcd_put_wchar(' ');
           }
@@ -644,15 +637,7 @@ void MarlinUI::draw_status_message(const bool blink) {
       }
       if (last_blink != blink) {
         last_blink = blink;
-
-        // Adjust by complete UTF8 characters
-        if (status_scroll_offset < slen) {
-          status_scroll_offset++;
-          while (!START_OF_UTF8_CHAR(status_message[status_scroll_offset]))
-            status_scroll_offset++;
-        }
-        else
-          status_scroll_offset = 0;
+        advance_status_scroll();
       }
     }
 
diff --git a/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp b/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp
index 769fb36060..a72b3e4fc8 100644
--- a/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp
+++ b/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp
@@ -627,43 +627,27 @@ void ST7920_Lite_Status_Screen::draw_status_message() {
     if (slen <= LCD_WIDTH) {
       // String fits the LCD, so just print it
       write_str(str);
-      for (; slen < LCD_WIDTH; ++slen) write_byte(' ');
+      while (slen < LCD_WIDTH) { write_byte(' '); ++slen; }
     }
     else {
       // String is larger than the available space in screen.
 
       // Get a pointer to the next valid UTF8 character
-      const char *stat = str + ui.status_scroll_offset;
-
-      // Get the string remaining length
-      const uint8_t rlen = utf8_strlen(stat);
-
-      // If we have enough characters to display
-      if (rlen >= LCD_WIDTH) {
-        // The remaining string fills the screen - Print it
-        write_str(stat, LCD_WIDTH);
-      }
-      else {
-        // The remaining string does not completely fill the screen
-        write_str(stat);                        // The string leaves space
-        uint8_t chars = LCD_WIDTH - rlen;         // Amount of space left in characters
+      // and the string remaining length
+      uint8_t rlen;
+      const char *stat = ui.status_and_len(rlen);
+      write_str(stat, LCD_WIDTH);
 
+      // If the remaining string doesn't completely fill the screen
+      if (rlen < LCD_WIDTH) {
         write_byte('.');                        // Always at 1+ spaces left, draw a dot
+        uint8_t chars = LCD_WIDTH - rlen;       // Amount of space left in characters
         if (--chars) {                          // Draw a second dot if there's space
           write_byte('.');
-          if (--chars)
-            write_str(str, chars);              // Print a second copy of the message
+          if (--chars) write_str(str, chars);   // Print a second copy of the message
         }
       }
-
-      // Adjust by complete UTF8 characters
-      if (ui.status_scroll_offset < slen) {
-        ui.status_scroll_offset++;
-        while (!START_OF_UTF8_CHAR(str[ui.status_scroll_offset]))
-          ui.status_scroll_offset++;
-      }
-      else
-        ui.status_scroll_offset = 0;
+      ui.advance_status_scroll();
     }
 
   #else
diff --git a/Marlin/src/lcd/ultralcd.cpp b/Marlin/src/lcd/ultralcd.cpp
index ac8cf6592f..5b18ebc368 100644
--- a/Marlin/src/lcd/ultralcd.cpp
+++ b/Marlin/src/lcd/ultralcd.cpp
@@ -1162,6 +1162,21 @@ void MarlinUI::update() {
   /////////////// Status Line ////////////////
   ////////////////////////////////////////////
 
+  #if ENABLED(STATUS_MESSAGE_SCROLLING)
+    void MarlinUI::advance_status_scroll() {
+      // Advance by one UTF8 code-word
+      if (status_scroll_offset < utf8_strlen(status_message))
+        while (!START_OF_UTF8_CHAR(status_message[++status_scroll_offset]));
+      else
+        status_scroll_offset = 0;
+    }
+    char* MarlinUI::status_and_len(uint8_t &len) {
+      char *out = status_message + status_scroll_offset;
+      len = utf8_strlen(out);
+      return out;
+    }
+  #endif
+
   void MarlinUI::finish_status(const bool persist) {
 
     #if !(ENABLED(LCD_PROGRESS_BAR) && (PROGRESS_MSG_EXPIRE > 0))
@@ -1179,7 +1194,7 @@ void MarlinUI::update() {
       next_filament_display = millis() + 5000UL; // Show status message for 5s
     #endif
 
-    #if ENABLED(STATUS_MESSAGE_SCROLLING)
+    #if HAS_SPI_LCD && ENABLED(STATUS_MESSAGE_SCROLLING)
       status_scroll_offset = 0;
     #endif
 
diff --git a/Marlin/src/lcd/ultralcd.h b/Marlin/src/lcd/ultralcd.h
index 4847b33b0b..0458913405 100644
--- a/Marlin/src/lcd/ultralcd.h
+++ b/Marlin/src/lcd/ultralcd.h
@@ -277,10 +277,15 @@ public:
     static char status_message[];
     static bool has_status();
 
-
     static uint8_t status_message_level;      // Higher levels block lower levels
     static inline void reset_alert_level() { status_message_level = 0; }
 
+    #if ENABLED(STATUS_MESSAGE_SCROLLING)
+      static uint8_t status_scroll_offset;
+      static void advance_status_scroll();
+      static char* status_and_len(uint8_t &len);
+    #endif
+
     #if HAS_PRINT_PROGRESS
       #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
         static uint8_t progress_bar_percent;
@@ -327,9 +332,6 @@ public:
 
       #endif
 
-      #if ENABLED(STATUS_MESSAGE_SCROLLING)
-        static uint8_t status_scroll_offset;
-      #endif
       static uint8_t lcd_status_update_delay;
 
       #if HAS_LCD_CONTRAST
@@ -520,7 +522,7 @@ private:
   static void _synchronize();
 
   #if HAS_SPI_LCD || ENABLED(EXTENSIBLE_UI)
-    static void finishstatus(const bool persist);
+    static void finish_status(const bool persist);
   #endif
 
   #if HAS_SPI_LCD
-- 
GitLab