Bläddra i källkod

Consolidate, optimize some LCD menu code (#12450)

Scott Lahteine 6 år sedan
förälder
incheckning
3e9ffaddb6
Inget konto är kopplat till bidragsgivarens mejladress

+ 8
- 41
Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp Visa fil

@@ -363,10 +363,7 @@ void MarlinUI::clear_lcd() { lcd.clear(); }
363 363
       // Fits into,
364 364
       lcd_moveto(col, line);
365 365
       lcd_put_u8str_max_P(text, len);
366
-      while (slen < len) {
367
-        lcd_put_wchar(' ');
368
-        ++slen;
369
-      }
366
+      for (; slen < len; ++slen) lcd_put_wchar(' ');
370 367
       safe_delay(time);
371 368
     }
372 369
     else {
@@ -381,11 +378,7 @@ void MarlinUI::clear_lcd() { lcd.clear(); }
381 378
         lcd_put_u8str_max_P(p, len);
382 379
 
383 380
         // Fill with spaces
384
-        uint8_t ix = slen - i;
385
-        while (ix < len) {
386
-          lcd_put_wchar(' ');
387
-          ++ix;
388
-        }
381
+        for (uint8_t ix = slen - i; ix < len; ++ix) lcd_put_wchar(' ');
389 382
 
390 383
         // Delay
391 384
         safe_delay(dly);
@@ -995,7 +988,7 @@ void MarlinUI::draw_status_screen() {
995 988
     lcd_moveto(0, row);
996 989
     lcd_put_wchar(sel ? pre_char : ' ');
997 990
     n -= lcd_put_u8str_max_P(pstr, n);
998
-    while (n--) lcd_put_wchar(' ');
991
+    for (; n; --n) lcd_put_wchar(' ');
999 992
     lcd_put_wchar(post_char);
1000 993
   }
1001 994
 
@@ -1005,7 +998,7 @@ void MarlinUI::draw_status_screen() {
1005 998
     lcd_put_wchar(sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
1006 999
     n -= lcd_put_u8str_max_P(pstr, n);
1007 1000
     lcd_put_wchar(':');
1008
-    while (n--) lcd_put_wchar(' ');
1001
+    for (; n; --n) lcd_put_wchar(' ');
1009 1002
     if (pgm) lcd_put_u8str_P(data); else lcd_put_u8str(data);
1010 1003
   }
1011 1004
 
@@ -1025,40 +1018,14 @@ void MarlinUI::draw_status_screen() {
1025 1018
   #if ENABLED(SDSUPPORT)
1026 1019
 
1027 1020
     void draw_sd_menu_item(const bool sel, const uint8_t row, PGM_P const pstr, CardReader &theCard, const bool isDir) {
1028
-      const char post_char = isDir ? LCD_STR_FOLDER[0] : ' ',
1029
-                 sel_char = sel ? LCD_STR_ARROW_RIGHT[0] : ' ';
1030 1021
       UNUSED(pstr);
1031
-      lcd_moveto(0, row);
1032
-      lcd_put_wchar(sel_char);
1033
-
1034
-      uint8_t n = LCD_WIDTH - 2;
1035
-      const char *outstr = theCard.longest_filename();
1036
-      if (theCard.longFilename[0]) {
1037
-        #if ENABLED(SCROLL_LONG_FILENAMES)
1038
-          static uint8_t filename_scroll_hash;
1039
-          if (sel) {
1040
-            uint8_t name_hash = row;
1041
-            for (uint8_t l = FILENAME_LENGTH; l--;)
1042
-              name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ theCard.filename[l];  // rotate, xor
1043
-            if (filename_scroll_hash != name_hash) {                            // If the hash changed...
1044
-              filename_scroll_hash = name_hash;                                 // Save the new hash
1045
-              ui.filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - n); // Update the scroll limit
1046
-              ui.filename_scroll_pos = 0;                                       // Reset scroll to the start
1047
-              ui.lcd_status_update_delay = 8;                                   // Don't scroll right away
1048
-            }
1049
-            outstr += ui.filename_scroll_pos;
1050
-          }
1051
-        #else
1052
-          theCard.longFilename[n] = '\0'; // cutoff at screen edge
1053
-        #endif
1054
-      }
1055 1022
 
1056 1023
       lcd_moveto(0, row);
1057
-      lcd_put_wchar(sel_char);
1058
-      n -= lcd_put_u8str_max(outstr, n);
1059
-
1024
+      lcd_put_wchar(sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
1025
+      constexpr uint8_t maxlen = LCD_WIDTH - 2;
1026
+      uint8_t n = maxlen - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), maxlen);
1060 1027
       for (; n; --n) lcd_put_wchar(' ');
1061
-      lcd_put_wchar(post_char);
1028
+      lcd_put_wchar(isDir ? LCD_STR_FOLDER[0] : ' ');
1062 1029
     }
1063 1030
 
1064 1031
   #endif // SDSUPPORT

+ 12
- 41
Marlin/src/lcd/dogm/ultralcd_DOGM.cpp Visa fil

@@ -305,11 +305,8 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
305 305
         while (--pad >= 0) { lcd_put_wchar(' '); n--; }
306 306
       }
307 307
       n -= lcd_put_u8str_max_P(pstr, n);
308
-      if (NULL != valstr) {
309
-        n -= lcd_put_u8str_max(valstr, n);
310
-      }
311
-
312
-      while (n - MENU_FONT_WIDTH > 0) { n -= lcd_put_wchar(' '); }
308
+      if (valstr) n -= lcd_put_u8str_max(valstr, n);
309
+      while (n > MENU_FONT_WIDTH) n -= lcd_put_wchar(' ');
313 310
     }
314 311
   }
315 312
 
@@ -318,10 +315,9 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
318 315
     UNUSED(pre_char);
319 316
 
320 317
     if (mark_as_selected(row, sel)) {
321
-      uint8_t n = LCD_WIDTH - 2;
322
-      n *= MENU_FONT_WIDTH;
318
+      uint8_t n = (LCD_WIDTH - 2) * (MENU_FONT_WIDTH);
323 319
       n -= lcd_put_u8str_max_P(pstr, n);
324
-      while (n - MENU_FONT_WIDTH > 0) { n -= lcd_put_wchar(' '); }
320
+      while (n > MENU_FONT_WIDTH) n -= lcd_put_wchar(' ');
325 321
       lcd_moveto(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2);
326 322
       lcd_put_wchar(post_char);
327 323
       lcd_put_wchar(' ');
@@ -332,11 +328,10 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
332 328
   void _draw_menu_item_edit(const bool sel, const uint8_t row, PGM_P const pstr, const char* const data, const bool pgm) {
333 329
     if (mark_as_selected(row, sel)) {
334 330
       const uint8_t vallen = (pgm ? utf8_strlen_P(data) : utf8_strlen((char*)data));
335
-      uint8_t n = LCD_WIDTH - 2 - vallen;
336
-      n *= MENU_FONT_WIDTH;
331
+      uint8_t n = (LCD_WIDTH - 2 - vallen) * (MENU_FONT_WIDTH);
337 332
       n -= lcd_put_u8str_max_P(pstr, n);
338 333
       lcd_put_wchar(':');
339
-      while (n - MENU_FONT_WIDTH > 0) { n -= lcd_put_wchar(' '); }
334
+      while (n > MENU_FONT_WIDTH) n -= lcd_put_wchar(' ');
340 335
       lcd_moveto(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH) * vallen, row_y2);
341 336
       if (pgm) lcd_put_u8str_P(data); else lcd_put_u8str((char*)data);
342 337
     }
@@ -399,37 +394,13 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
399 394
     void draw_sd_menu_item(const bool sel, const uint8_t row, PGM_P const pstr, CardReader &theCard, const bool isDir) {
400 395
       UNUSED(pstr);
401 396
 
402
-      mark_as_selected(row, sel);
403
-
404
-      if (!PAGE_CONTAINS(row_y1, row_y2)) return;
405
-
406
-      constexpr uint8_t maxlen = LCD_WIDTH - 1;
407
-      const char *outstr = theCard.longest_filename();
408
-      if (theCard.longFilename[0]) {
409
-        #if ENABLED(SCROLL_LONG_FILENAMES)
410
-          static uint8_t filename_scroll_hash;
411
-          if (sel) {
412
-            uint8_t name_hash = row;
413
-            for (uint8_t l = FILENAME_LENGTH; l--;)
414
-              name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ theCard.filename[l];  // rotate, xor
415
-            if (filename_scroll_hash != name_hash) {                            // If the hash changed...
416
-              filename_scroll_hash = name_hash;                                 // Save the new hash
417
-              ui.filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
418
-              ui.filename_scroll_pos = 0;                                       // Reset scroll to the start
419
-              ui.lcd_status_update_delay = 8;                                   // Don't scroll right away
420
-            }
421
-            outstr += ui.filename_scroll_pos;
422
-          }
423
-        #else
424
-          theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge
425
-        #endif
397
+      if (mark_as_selected(row, sel)) {
398
+        if (isDir) lcd_put_wchar(LCD_STR_FOLDER[0]);
399
+        constexpr uint8_t maxlen = LCD_WIDTH - 1;
400
+        const uint8_t pixw = maxlen * (MENU_FONT_WIDTH);
401
+        uint8_t n = pixw - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), pixw);
402
+        while (n > MENU_FONT_WIDTH) n -= lcd_put_wchar(' ');
426 403
       }
427
-
428
-      if (isDir) lcd_put_wchar(LCD_STR_FOLDER[0]);
429
-
430
-      uint8_t n = lcd_put_u8str_max(outstr, maxlen * (MENU_FONT_WIDTH));
431
-      n = maxlen * (MENU_FONT_WIDTH) - n;
432
-      while (n - MENU_FONT_WIDTH > 0) { n -= lcd_put_wchar(' '); }
433 404
     }
434 405
 
435 406
   #endif // SDSUPPORT

+ 30
- 2
Marlin/src/lcd/ultralcd.cpp Visa fil

@@ -112,9 +112,37 @@ millis_t next_button_update_ms;
112 112
 
113 113
 #if HAS_LCD_MENU
114 114
   #include "menu/menu.h"
115
+  #include "../sd/cardreader.h"
116
+
117
+  #if ENABLED(SDSUPPORT)
118
+
119
+    #if ENABLED(SCROLL_LONG_FILENAMES)
120
+      uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
121
+    #endif
122
+
123
+    const char * const MarlinUI::scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll) {
124
+      const char *outstr = theCard.longest_filename();
125
+      if (theCard.longFilename[0]) {
126
+        #if ENABLED(SCROLL_LONG_FILENAMES)
127
+          if (doScroll) {
128
+            for (uint8_t l = FILENAME_LENGTH; l--;)
129
+              hash = ((hash << 1) | (hash >> 7)) ^ theCard.filename[l];      // rotate, xor
130
+            static uint8_t filename_scroll_hash;
131
+            if (filename_scroll_hash != hash) {                              // If the hash changed...
132
+              filename_scroll_hash = hash;                                   // Save the new hash
133
+              filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
134
+              filename_scroll_pos = 0;                                       // Reset scroll to the start
135
+              lcd_status_update_delay = 8;                                   // Don't scroll right away
136
+            }
137
+            outstr += filename_scroll_pos;
138
+          }
139
+        #else
140
+          theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge
141
+        #endif
142
+      }
143
+      return outstr;
144
+    }
115 145
 
116
-  #if ENABLED(SDSUPPORT) && ENABLED(SCROLL_LONG_FILENAMES)
117
-    uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
118 146
   #endif
119 147
 
120 148
   screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR

+ 18
- 15
Marlin/src/lcd/ultralcd.h Visa fil

@@ -23,6 +23,12 @@
23 23
 
24 24
 #include "../inc/MarlinConfig.h"
25 25
 
26
+#if HAS_BUZZER
27
+  #include "../libs/buzzer.h"
28
+#endif
29
+
30
+#define HAS_ENCODER_ACTION (HAS_LCD_MENU || ENABLED(ULTIPANEL_FEEDMULTIPLY))
31
+
26 32
 #if HAS_SPI_LCD
27 33
 
28 34
   #include "../Marlin.h"
@@ -32,16 +38,6 @@
32 38
     #include "../module/motion.h" // for active_extruder
33 39
   #endif
34 40
 
35
-#endif
36
-
37
-#if HAS_BUZZER
38
-  #include "../libs/buzzer.h"
39
-#endif
40
-
41
-#define HAS_ENCODER_ACTION (HAS_LCD_MENU || ENABLED(ULTIPANEL_FEEDMULTIPLY))
42
-
43
-#if HAS_SPI_LCD
44
-
45 41
   enum LCDViewAction : uint8_t {
46 42
     LCDVIEW_NONE,
47 43
     LCDVIEW_REDRAW_NOW,
@@ -66,6 +62,10 @@
66 62
 
67 63
   #if HAS_LCD_MENU
68 64
 
65
+    #if ENABLED(SDSUPPORT)
66
+      #include "../sd/cardreader.h"
67
+    #endif
68
+
69 69
     typedef void (*screenFunc_t)();
70 70
     typedef void (*menuAction_t)();
71 71
 
@@ -213,9 +213,6 @@
213 213
   };
214 214
 #endif
215 215
 
216
-#define LCD_MESSAGEPGM(x)      ui.setstatusPGM(PSTR(x))
217
-#define LCD_ALERTMESSAGEPGM(x) ui.setalertstatusPGM(PSTR(x))
218
-
219 216
 ////////////////////////////////////////////
220 217
 //////////// MarlinUI Singleton ////////////
221 218
 ////////////////////////////////////////////
@@ -379,8 +376,11 @@ public:
379 376
       static void enable_encoder_multiplier(const bool onoff);
380 377
     #endif
381 378
 
382
-    #if ENABLED(SCROLL_LONG_FILENAMES)
383
-      static uint8_t filename_scroll_pos, filename_scroll_max;
379
+    #if ENABLED(SDSUPPORT)
380
+      #if ENABLED(SCROLL_LONG_FILENAMES)
381
+        static uint8_t filename_scroll_pos, filename_scroll_max;
382
+      #endif
383
+      static const char * const scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll);
384 384
     #endif
385 385
 
386 386
     #if IS_KINEMATIC
@@ -524,3 +524,6 @@ private:
524 524
 };
525 525
 
526 526
 extern MarlinUI ui;
527
+
528
+#define LCD_MESSAGEPGM(x)      ui.setstatusPGM(PSTR(x))
529
+#define LCD_ALERTMESSAGEPGM(x) ui.setalertstatusPGM(PSTR(x))

Laddar…
Avbryt
Spara