Browse Source

Use lambda, fix wrap pointer

Scott Lahteine 6 years ago
parent
commit
24c23b60fa
1 changed files with 17 additions and 17 deletions
  1. 17
    17
      Marlin/src/lcd/ultralcd.cpp

+ 17
- 17
Marlin/src/lcd/ultralcd.cpp View File

@@ -204,33 +204,36 @@ millis_t next_button_update_ms;
204 204
     SETCURSOR(x, y);
205 205
     if (!string) return;
206 206
 
207
+    auto _newline = [&x, &y]() {
208
+      x = 0; y++;               // move x to string len (plus space)
209
+      SETCURSOR(0, y);          // simulate carriage return
210
+    };
211
+
207 212
     uint8_t *p = (uint8_t*)string;
208 213
     wchar_t ch;
209 214
     if (wordwrap) {
210
-      uint8_t *wrd = p, c = 0;
215
+      uint8_t *wrd = nullptr, c = 0;
216
+      // find the end of the part
211 217
       for (;;) {
218
+        if (!wrd) wrd = p;            // Get word start /before/ advancing
212 219
         p = get_utf8_value_cb(p, cb_read_byte, &ch);
213
-        const bool eol = !ch;
220
+        const bool eol = !ch;         // zero ends the string
221
+        // End or a break between phrases?
214 222
         if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
215
-          if (!c && ch == ' ') continue; // collapse extra spaces
216
-          if (x + c > LCD_WIDTH && x >= (LCD_WIDTH) / 4) { // should it wrap?
217
-            x = 0; y++;               // move x to string len (plus space)
218
-            SETCURSOR(0, y);          // simulate carriage return
219
-          }
223
+          if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
224
+          // Past the right and the word is not too long?
225
+          if (x + c > LCD_WIDTH && x >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
220 226
           c += !eol;                  // +1 so the space will be printed
221 227
           x += c;                     // advance x to new position
222 228
           while (c) {                 // character countdown
223 229
             --c;                      // count down to zero
224 230
             wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
225
-            lcd_put_wchar(ch);        // word (plus space) to the LCD
231
+            lcd_put_wchar(ch);        // character to the LCD
226 232
           }
227
-          if (eol) break;             // all done
233
+          if (eol) break;             // all done!
228 234
           wrd = nullptr;              // set up for next word
229 235
         }
230
-        else {
231
-          if (!wrd) wrd = p;          // starting a new word?
232
-          c++;                        // count word characters
233
-        }
236
+        else c++;                     // count word characters
234 237
       }
235 238
     }
236 239
     else {
@@ -239,10 +242,7 @@ millis_t next_button_update_ms;
239 242
         if (!ch) break;
240 243
         lcd_put_wchar(ch);
241 244
         x++;
242
-        if (x >= LCD_WIDTH) {
243
-          x = 0; y++;
244
-          SETCURSOR(0, y);
245
-        }
245
+        if (x >= LCD_WIDTH) _newline();
246 246
       }
247 247
     }
248 248
   }

Loading…
Cancel
Save