Browse Source

Consolidate, optimize some LCD menu code (#12450)

Scott Lahteine 6 years ago
parent
commit
3e9ffaddb6
No account linked to committer's email address

+ 8
- 41
Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp View File

363
       // Fits into,
363
       // Fits into,
364
       lcd_moveto(col, line);
364
       lcd_moveto(col, line);
365
       lcd_put_u8str_max_P(text, len);
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
       safe_delay(time);
367
       safe_delay(time);
371
     }
368
     }
372
     else {
369
     else {
381
         lcd_put_u8str_max_P(p, len);
378
         lcd_put_u8str_max_P(p, len);
382
 
379
 
383
         // Fill with spaces
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
         // Delay
383
         // Delay
391
         safe_delay(dly);
384
         safe_delay(dly);
995
     lcd_moveto(0, row);
988
     lcd_moveto(0, row);
996
     lcd_put_wchar(sel ? pre_char : ' ');
989
     lcd_put_wchar(sel ? pre_char : ' ');
997
     n -= lcd_put_u8str_max_P(pstr, n);
990
     n -= lcd_put_u8str_max_P(pstr, n);
998
-    while (n--) lcd_put_wchar(' ');
991
+    for (; n; --n) lcd_put_wchar(' ');
999
     lcd_put_wchar(post_char);
992
     lcd_put_wchar(post_char);
1000
   }
993
   }
1001
 
994
 
1005
     lcd_put_wchar(sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
998
     lcd_put_wchar(sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
1006
     n -= lcd_put_u8str_max_P(pstr, n);
999
     n -= lcd_put_u8str_max_P(pstr, n);
1007
     lcd_put_wchar(':');
1000
     lcd_put_wchar(':');
1008
-    while (n--) lcd_put_wchar(' ');
1001
+    for (; n; --n) lcd_put_wchar(' ');
1009
     if (pgm) lcd_put_u8str_P(data); else lcd_put_u8str(data);
1002
     if (pgm) lcd_put_u8str_P(data); else lcd_put_u8str(data);
1010
   }
1003
   }
1011
 
1004
 
1025
   #if ENABLED(SDSUPPORT)
1018
   #if ENABLED(SDSUPPORT)
1026
 
1019
 
1027
     void draw_sd_menu_item(const bool sel, const uint8_t row, PGM_P const pstr, CardReader &theCard, const bool isDir) {
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
       UNUSED(pstr);
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
       lcd_moveto(0, row);
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
       for (; n; --n) lcd_put_wchar(' ');
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
   #endif // SDSUPPORT
1031
   #endif // SDSUPPORT

+ 12
- 41
Marlin/src/lcd/dogm/ultralcd_DOGM.cpp View File

305
         while (--pad >= 0) { lcd_put_wchar(' '); n--; }
305
         while (--pad >= 0) { lcd_put_wchar(' '); n--; }
306
       }
306
       }
307
       n -= lcd_put_u8str_max_P(pstr, n);
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
     UNUSED(pre_char);
315
     UNUSED(pre_char);
319
 
316
 
320
     if (mark_as_selected(row, sel)) {
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
       n -= lcd_put_u8str_max_P(pstr, n);
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
       lcd_moveto(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2);
321
       lcd_moveto(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2);
326
       lcd_put_wchar(post_char);
322
       lcd_put_wchar(post_char);
327
       lcd_put_wchar(' ');
323
       lcd_put_wchar(' ');
332
   void _draw_menu_item_edit(const bool sel, const uint8_t row, PGM_P const pstr, const char* const data, const bool pgm) {
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
     if (mark_as_selected(row, sel)) {
329
     if (mark_as_selected(row, sel)) {
334
       const uint8_t vallen = (pgm ? utf8_strlen_P(data) : utf8_strlen((char*)data));
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
       n -= lcd_put_u8str_max_P(pstr, n);
332
       n -= lcd_put_u8str_max_P(pstr, n);
338
       lcd_put_wchar(':');
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
       lcd_moveto(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH) * vallen, row_y2);
335
       lcd_moveto(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH) * vallen, row_y2);
341
       if (pgm) lcd_put_u8str_P(data); else lcd_put_u8str((char*)data);
336
       if (pgm) lcd_put_u8str_P(data); else lcd_put_u8str((char*)data);
342
     }
337
     }
399
     void draw_sd_menu_item(const bool sel, const uint8_t row, PGM_P const pstr, CardReader &theCard, const bool isDir) {
394
     void draw_sd_menu_item(const bool sel, const uint8_t row, PGM_P const pstr, CardReader &theCard, const bool isDir) {
400
       UNUSED(pstr);
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
   #endif // SDSUPPORT
406
   #endif // SDSUPPORT

+ 30
- 2
Marlin/src/lcd/ultralcd.cpp View File

112
 
112
 
113
 #if HAS_LCD_MENU
113
 #if HAS_LCD_MENU
114
   #include "menu/menu.h"
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
   #endif
146
   #endif
119
 
147
 
120
   screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR
148
   screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR

+ 18
- 15
Marlin/src/lcd/ultralcd.h View File

23
 
23
 
24
 #include "../inc/MarlinConfig.h"
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
 #if HAS_SPI_LCD
32
 #if HAS_SPI_LCD
27
 
33
 
28
   #include "../Marlin.h"
34
   #include "../Marlin.h"
32
     #include "../module/motion.h" // for active_extruder
38
     #include "../module/motion.h" // for active_extruder
33
   #endif
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
   enum LCDViewAction : uint8_t {
41
   enum LCDViewAction : uint8_t {
46
     LCDVIEW_NONE,
42
     LCDVIEW_NONE,
47
     LCDVIEW_REDRAW_NOW,
43
     LCDVIEW_REDRAW_NOW,
66
 
62
 
67
   #if HAS_LCD_MENU
63
   #if HAS_LCD_MENU
68
 
64
 
65
+    #if ENABLED(SDSUPPORT)
66
+      #include "../sd/cardreader.h"
67
+    #endif
68
+
69
     typedef void (*screenFunc_t)();
69
     typedef void (*screenFunc_t)();
70
     typedef void (*menuAction_t)();
70
     typedef void (*menuAction_t)();
71
 
71
 
213
   };
213
   };
214
 #endif
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
 //////////// MarlinUI Singleton ////////////
217
 //////////// MarlinUI Singleton ////////////
221
 ////////////////////////////////////////////
218
 ////////////////////////////////////////////
379
       static void enable_encoder_multiplier(const bool onoff);
376
       static void enable_encoder_multiplier(const bool onoff);
380
     #endif
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
     #endif
384
     #endif
385
 
385
 
386
     #if IS_KINEMATIC
386
     #if IS_KINEMATIC
524
 };
524
 };
525
 
525
 
526
 extern MarlinUI ui;
526
 extern MarlinUI ui;
527
+
528
+#define LCD_MESSAGEPGM(x)      ui.setstatusPGM(PSTR(x))
529
+#define LCD_ALERTMESSAGEPGM(x) ui.setalertstatusPGM(PSTR(x))

Loading…
Cancel
Save