Browse Source

code cleanup in ultralcd.

Bernhard Kubicek 13 years ago
parent
commit
cd2268f10a
2 changed files with 112 additions and 263 deletions
  1. 49
    0
      Marlin/ultralcd.h
  2. 63
    263
      Marlin/ultralcd.pde

+ 49
- 0
Marlin/ultralcd.h View File

@@ -72,6 +72,55 @@
72 72
     int8_t lastlineoffset;
73 73
     
74 74
     bool linechanging;
75
+    
76
+  private:
77
+    inline void updateActiveLines(const uint8_t &maxlines,volatile int &encoderpos)
78
+    {
79
+      if(linechanging) return; // an item is changint its value, do not switch lines hence
80
+      lastlineoffset=lineoffset; 
81
+      int curencoderpos=encoderpos;  
82
+      force_lcd_update=false;
83
+      if(  (abs(curencoderpos-lastencoderpos)<lcdslow) ) 
84
+      { 
85
+        lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?' ':' '); 
86
+        if(curencoderpos<0)  
87
+        {  
88
+          lineoffset--; 
89
+          if(lineoffset<0) lineoffset=0; 
90
+          curencoderpos=lcdslow-1; 
91
+          force_lcd_update=true; 
92
+        } 
93
+        if(curencoderpos>(LCD_HEIGHT-1+1)*lcdslow) 
94
+        { 
95
+          lineoffset++; 
96
+          curencoderpos=(LCD_HEIGHT-1)*lcdslow; 
97
+          if(lineoffset>(maxlines+1-LCD_HEIGHT)) 
98
+            lineoffset=maxlines+1-LCD_HEIGHT; 
99
+          if(curencoderpos>maxlines*lcdslow) 
100
+            curencoderpos=maxlines*lcdslow; 
101
+          force_lcd_update=true; 
102
+        } 
103
+        lastencoderpos=encoderpos=curencoderpos; 
104
+        activeline=curencoderpos/lcdslow;
105
+        if(activeline<0) activeline=0;
106
+        if(activeline>LCD_HEIGHT-1) activeline=LCD_HEIGHT-1;
107
+        if(activeline>maxlines) 
108
+        {
109
+          activeline=maxlines;
110
+          curencoderpos=maxlines*lcdslow;
111
+        }
112
+        lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?'>':'\003');    
113
+      } 
114
+    }
115
+    
116
+    inline void clearIfNecessary()
117
+    {
118
+      if(lastlineoffset!=lineoffset ||force_lcd_update)
119
+      {
120
+        force_lcd_update=true;
121
+         lcd.clear();
122
+      } 
123
+    }
75 124
   };
76 125
 
77 126
   //conversion routines, could need some overworking

+ 63
- 263
Marlin/ultralcd.pde View File

@@ -279,7 +279,6 @@ MainMenu::MainMenu()
279 279
   linechanging=false;
280 280
 }
281 281
 
282
-
283 282
 void MainMenu::showStatus()
284 283
 { 
285 284
 #if LCD_HEIGHT==4
@@ -426,131 +425,46 @@ void MainMenu::showStatus()
426 425
 
427 426
 enum {ItemP_exit, ItemP_home, ItemP_origin, ItemP_preheat, ItemP_extrude, ItemP_disstep};
428 427
 
428
+//any action must not contain a ',' character anywhere, or this breaks:
429
+#define MENUITEM(repaint_action, click_action) \
430
+  {\
431
+    if(force_lcd_update)  { lcd.setCursor(0,line);  repaint_action; } \
432
+    if((activeline==line) && CLICKED) {click_action} \
433
+  }
434
+  
429 435
 void MainMenu::showPrepare()
430 436
 {
431 437
  uint8_t line=0;
432
- if(lastlineoffset!=lineoffset)
433
- {
434
-   force_lcd_update=true;
435
-   clear(); 
436
- }
438
+ clearIfNecessary();
437 439
  for(int8_t i=lineoffset;i<lineoffset+LCD_HEIGHT;i++)
438 440
  {
439 441
    //Serial.println((int)(line-lineoffset));
440 442
   switch(i)
441 443
   {
442 444
     case ItemP_exit:
443
-      {
444
-        if(force_lcd_update)
445
-        {
446
-          lcd.setCursor(0,line);lcdprintPGM(" Prepare");
447
-        }
448
-        if((activeline==line) && CLICKED)
449
-        {
450
-          BLOCK
451
-          status=Main_Menu;
452
-          beepshort();
453
-        }
454
-      }break;
445
+      MENUITEM(  lcdprintPGM(" Prepare")  ,  BLOCK;status=Main_Menu;beepshort(); ) ;
446
+      break;
455 447
     case ItemP_home:
456
-      {
457
-        if(force_lcd_update)
458
-        {
459
-          lcd.setCursor(0,line);lcdprintPGM(" Auto Home");
460
-        }
461
-        if((activeline==line) && CLICKED)
462
-        {
463
-          BLOCK
464
-          enquecommand("G28 X-105 Y-105 Z0");
465
-          beepshort();
466
-        }
467
-      }break;
448
+      MENUITEM(  lcdprintPGM(" Auto Home")  ,  BLOCK;enquecommand("G28 X-105 Y-105 Z0");beepshort(); ) ;
449
+      break;
468 450
     case ItemP_origin:
469
-      {
470
-        if(force_lcd_update)
471
-        {
472
-          lcd.setCursor(0,line);lcdprintPGM(" Set Origin");
473
-          
474
-        }
475
-        if((activeline==line) && CLICKED)
476
-        {
477
-          BLOCK
478
-          enquecommand("G92 X0 Y0 Z0");
479
-          beepshort();
480
-        }
481
-      }break;
451
+      MENUITEM(  lcdprintPGM(" Set Origin")  ,  BLOCK;enquecommand("G92 X0 Y0 Z0");beepshort(); ) ;
452
+      break;
482 453
     case ItemP_preheat:
483
-      {
484
-        if(force_lcd_update)
485
-        {
486
-          lcd.setCursor(0,line);lcdprintPGM(" Preheat"); 
487
-        }
488
-        if((activeline==line) && CLICKED)
489
-        {
490
-          BLOCK
491
-          setTargetHotend0(170);
492
-          beepshort();
493
-        }
494
-      }break;
454
+      MENUITEM(  lcdprintPGM(" Preheat")  ,  BLOCK;setTargetHotend0(170);beepshort(); ) ;
455
+      break;
495 456
     case ItemP_extrude:
496
-      {
497
-        if(force_lcd_update)
498
-        {
499
-          lcd.setCursor(0,line);lcdprintPGM(" Extrude");
500
-        }
501
-        if((activeline==line) && CLICKED)
502
-        {
503
-          BLOCK
504
-          enquecommand("G92 E0");
505
-          enquecommand("G1 F700 E50");
506
-          beepshort();
507
-        }
508
-      }break;
457
+      MENUITEM(  lcdprintPGM(" Extrude")  ,  BLOCK;enquecommand("G92 E0");enquecommand("G1 F700 E50");beepshort(); ) ;
458
+      break;
509 459
     case ItemP_disstep:
510
-      {
511
-        if(force_lcd_update)
512
-        {
513
-          lcd.setCursor(0,line);lcdprintPGM(" Disable Steppers");
514
-        }
515
-        if((activeline==line) && CLICKED)
516
-        {
517
-          BLOCK
518
-          enquecommand("M84");
519
-          beepshort();
520
-        }
521
-      }break;
460
+      MENUITEM(  lcdprintPGM(" Disable Steppers")  ,  BLOCK;enquecommand("M84");beepshort(); ) ;
461
+      break;
522 462
     default:   
523 463
       break;
524 464
   }
525 465
   line++;
526 466
  }
527
- lastlineoffset=lineoffset;
528
- if((encoderpos/lcdslow!=lastencoderpos/lcdslow)||force_lcd_update)
529
- {
530
-   
531
-    lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?' ':' ');
532
-    
533
-    if(encoderpos<0)
534
-    {
535
-     lineoffset--;
536
-     if(lineoffset<0)
537
-       lineoffset=0;
538
-     encoderpos=0;
539
-     force_lcd_update=true;
540
-    }
541
-    if(encoderpos/lcdslow>3)
542
-    {
543
-     lineoffset++;
544
-     encoderpos=3*lcdslow;
545
-     if(lineoffset>(ItemP_disstep+1-LCD_HEIGHT))
546
-       lineoffset=ItemP_disstep+1-LCD_HEIGHT;
547
-     force_lcd_update=true;
548
-    }
549
-    //encoderpos=encoderpos%LCD_HEIGHT;
550
-    lastencoderpos=encoderpos;
551
-    activeline=encoderpos/lcdslow;
552
-    lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?'>':'\003');   
553
-  } 
467
+ updateActiveLines(ItemP_disstep,encoderpos);
554 468
 }
555 469
 enum {
556 470
   ItemC_exit, ItemC_nozzle, 
@@ -563,31 +477,35 @@ enum {
563 477
   ItemC_aret,ItemC_esteps, ItemC_store, ItemC_load,ItemC_failsafe
564 478
 };
565 479
 
480
+//does not work
481
+// #define MENUCHANGEITEM(repaint_action,  enter_action, accept_action,  change_action) \
482
+//   {\
483
+//     if(force_lcd_update)  { lcd.setCursor(0,line);  repaint_action; } \
484
+//     if(activeline==line)  \
485
+//     { \
486
+//       if(CLICKED) \
487
+//       { \
488
+//         linechanging=!linechanging; \
489
+//         if(linechanging)  {enter_action;} \
490
+//         else {accept_action;} \
491
+//       }  \
492
+//       else \
493
+//       if(linechanging) {change_action};}\
494
+//   }
495
+//   
496
+
497
+  
566 498
 void MainMenu::showControl()
567 499
 {
568 500
  uint8_t line=0;
569
- if((lastlineoffset!=lineoffset)||force_lcd_update)
570
- {
571
-   force_lcd_update=true;
572
-   clear();
573
- }
501
+ clearIfNecessary();
574 502
  for(int8_t i=lineoffset;i<lineoffset+LCD_HEIGHT;i++)
575 503
  {
576 504
   switch(i)
577 505
   {
578 506
     case ItemC_exit:
579
-      {
580
-        if(force_lcd_update)
581
-        {
582
-          lcd.setCursor(0,line);lcdprintPGM(" Control");
583
-        }
584
-        if((activeline==line) && CLICKED)
585
-        {
586
-          BLOCK
587
-          status=Main_Menu;
588
-          beepshort();
589
-        }
590
-      }break;
507
+      MENUITEM(  lcdprintPGM(" Control")  ,  BLOCK;status=Main_Menu;beepshort(); ) ;
508
+      break;
591 509
     case ItemC_nozzle:
592 510
       {
593 511
         if(force_lcd_update)
@@ -1134,35 +1052,7 @@ void MainMenu::showControl()
1134 1052
   }
1135 1053
   line++;
1136 1054
  }
1137
- lastlineoffset=lineoffset;
1138
-
1139
- if(!linechanging &&  ((encoderpos/lcdslow!=lastencoderpos/lcdslow)||force_lcd_update))
1140
- {
1141
-   
1142
-    lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?' ':' ');
1143
-    
1144
-    if(encoderpos<0)
1145
-    {
1146
-     lineoffset--;
1147
-     if(lineoffset<0)
1148
-       lineoffset=0;
1149
-     encoderpos=0;
1150
-     force_lcd_update=true;
1151
-    }
1152
-    if(encoderpos/lcdslow>3)
1153
-    {
1154
-     lineoffset++;
1155
-     encoderpos=3*lcdslow;
1156
-     if(lineoffset>(ItemC_failsafe+1-LCD_HEIGHT))
1157
-       lineoffset=ItemC_failsafe+1-LCD_HEIGHT;
1158
-     force_lcd_update=true;
1159
-    }
1160
-    //encoderpos=encoderpos%LCD_HEIGHT;
1161
-    lastencoderpos=encoderpos;
1162
-    activeline=encoderpos/lcdslow;
1163
-    if(activeline>3) activeline=3;
1164
-    lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?'>':'\003');   
1165
-  } 
1055
+ updateActiveLines(ItemC_failsafe,encoderpos);
1166 1056
 }
1167 1057
 
1168 1058
 
@@ -1175,14 +1065,11 @@ void MainMenu::showSD()
1175 1065
 #ifdef SDSUPPORT
1176 1066
  uint8_t line=0;
1177 1067
 
1178
- if(lastlineoffset!=lineoffset)
1179
- {
1180
-   force_lcd_update=true; 
1181
- }
1068
+ clearIfNecessary();
1182 1069
  static uint8_t nrfiles=0;
1183 1070
  if(force_lcd_update)
1184 1071
  {
1185
-   clear();
1072
+  clear();
1186 1073
   if(card.cardOK)
1187 1074
   {
1188 1075
     nrfiles=card.getnrfilenames();
@@ -1192,7 +1079,6 @@ void MainMenu::showSD()
1192 1079
     nrfiles=0;
1193 1080
     lineoffset=0;
1194 1081
   }
1195
-  //Serial.print("Nr files:"); Serial.println((int)nrfiles);
1196 1082
  }
1197 1083
  
1198 1084
  for(int8_t i=lineoffset;i<lineoffset+LCD_HEIGHT;i++)
@@ -1200,18 +1086,8 @@ void MainMenu::showSD()
1200 1086
   switch(i)
1201 1087
   {
1202 1088
     case 0:
1203
-      {
1204
-        if(force_lcd_update)
1205
-        {
1206
-          lcd.setCursor(0,line);lcdprintPGM(" File");
1207
-        }
1208
-        if((activeline==line) && CLICKED)
1209
-        {
1210
-          BLOCK
1211
-          status=Main_Menu;
1212
-          beepshort();
1213
-        }
1214
-      }break;
1089
+      MENUITEM(  lcdprintPGM(" File")  ,  BLOCK;status=Main_Menu;beepshort(); ) ;
1090
+      break;
1215 1091
     case 1:
1216 1092
       {
1217 1093
         if(force_lcd_update)
@@ -1272,93 +1148,33 @@ void MainMenu::showSD()
1272 1148
   }
1273 1149
   line++;
1274 1150
  }
1275
- lastlineoffset=lineoffset;
1276
- if((encoderpos!=lastencoderpos)||force_lcd_update)
1277
- {
1278
-   
1279
-    lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?' ':' ');
1280
-    
1281
-    if(encoderpos<0)
1282
-    {
1283
-     lineoffset--;
1284
-     if(lineoffset<0)
1285
-       lineoffset=0;
1286
-     encoderpos=0;
1287
-     force_lcd_update=true;
1288
-    }
1289
-    if(encoderpos/lcdslow>3)
1290
-    {
1291
-     lineoffset++;
1292
-     encoderpos=3*lcdslow;
1293
-     if(lineoffset>(1+nrfiles+1-LCD_HEIGHT))
1294
-       lineoffset=1+nrfiles+1-LCD_HEIGHT;
1295
-     force_lcd_update=true;
1296
-     
1297
-    }
1298
-    lastencoderpos=encoderpos;
1299
-    activeline=encoderpos;
1300
-    if(activeline>3) 
1301
-    {
1302
-      activeline=3;
1303
-    }
1304
-    if(activeline<0) 
1305
-    {
1306
-      activeline=0;
1307
-    }
1308
-    if(activeline>1+nrfiles) activeline=1+nrfiles;
1309
-    if(lineoffset>1+nrfiles) lineoffset=1+nrfiles;
1310
-    lcd.setCursor(0,activeline);lcd.print((activeline+lineoffset)?'>':'\003');   
1311
-    
1312
-  }
1151
+ updateActiveLines(1+nrfiles,encoderpos);
1313 1152
 #endif
1314 1153
 }
1315 1154
 
1316 1155
 enum {ItemM_watch, ItemM_prepare, ItemM_control, ItemM_file };
1317 1156
 void MainMenu::showMainMenu()
1318 1157
 {
1319
-   //if(int(encoderpos/lcdslow)!=int(lastencoderpos/lcdslow))
1320
-   //  force_lcd_update=true;
1158
+
1321 1159
   #ifndef ULTIPANEL
1322 1160
     force_lcd_update=false;
1323 1161
   #endif
1324
-   //Serial.println((int)activeline);
1325
-   if(force_lcd_update)
1326
-     clear();
1162
+   
1163
+  clearIfNecessary();
1327 1164
   for(int8_t line=0;line<LCD_HEIGHT;line++)
1328 1165
   {
1329 1166
     switch(line)
1330 1167
     { 
1331 1168
       case ItemM_watch:
1332
-      {
1333
-        if(force_lcd_update) {lcd.setCursor(0,line);lcdprintPGM(" Watch   \x7E");}
1334
-        if((activeline==line)&&CLICKED)
1335
-        {
1336
-          BLOCK;
1337
-          beepshort();
1338
-          status=Main_Status;
1339
-        }
1340
-      } break;
1169
+        MENUITEM(  lcdprintPGM(" Watch")  ,  BLOCK;status=Main_Status;beepshort(); ) ;
1170
+       break;
1341 1171
       case ItemM_prepare:
1342
-      {
1343
-        if(force_lcd_update) {lcd.setCursor(0,line);lcdprintPGM(" Prepare \x7E");}
1344
-        if((activeline==line)&&CLICKED)
1345
-        {
1346
-          BLOCK;
1347
-          status=Main_Prepare;
1348
-          beepshort();
1349
-        }
1350
-      } break;
1172
+        MENUITEM(  lcdprintPGM(" Prepare \x7E")  ,  BLOCK;status=Main_Prepare;beepshort(); ) ;
1173
+      break;
1351 1174
        
1352 1175
       case ItemM_control:
1353
-      {
1354
-        if(force_lcd_update) {lcd.setCursor(0,line);lcdprintPGM(" Control \x7E");}
1355
-        if((activeline==line)&&CLICKED)
1356
-        {
1357
-          BLOCK;
1358
-          status=Main_Control;
1359
-          beepshort();
1360
-        }
1361
-      }break;
1176
+        MENUITEM(  lcdprintPGM(" Control \x7E")  ,  BLOCK;status=Main_Control;beepshort(); ) ;
1177
+      break;
1362 1178
       #ifdef SDSUPPORT
1363 1179
       case ItemM_file:    
1364 1180
       {
@@ -1392,6 +1208,9 @@ void MainMenu::showMainMenu()
1392 1208
           beepshort();
1393 1209
         }
1394 1210
       }break;
1211
+      #else
1212
+      case ItemM_file:
1213
+        break;
1395 1214
       #endif
1396 1215
       default: 
1397 1216
         SERIAL_ERROR_START;
@@ -1399,24 +1218,7 @@ void MainMenu::showMainMenu()
1399 1218
       break;
1400 1219
     }
1401 1220
   }
1402
-  if(activeline<0) 
1403
-    activeline=0;
1404
-  if(activeline>=LCD_HEIGHT) 
1405
-    activeline=LCD_HEIGHT-1;
1406
-  if((encoderpos!=lastencoderpos)||force_lcd_update)
1407
-  {
1408
-    lcd.setCursor(0,activeline);lcd.print(activeline?' ':' ');
1409
-    if(encoderpos<0) encoderpos=0;
1410
-    if(encoderpos>3*lcdslow) 
1411
-      encoderpos=3*lcdslow;
1412
-    activeline=abs(encoderpos/lcdslow)%LCD_HEIGHT;
1413
-    if(activeline<0) 
1414
-      activeline=0;
1415
-    if(activeline>=LCD_HEIGHT) 
1416
-      activeline=LCD_HEIGHT-1;
1417
-    lastencoderpos=encoderpos;
1418
-    lcd.setCursor(0,activeline);lcd.print(activeline?'>':'\003');
1419
-  }
1221
+  updateActiveLines(3,encoderpos);
1420 1222
 }
1421 1223
 
1422 1224
 void MainMenu::update()
@@ -1433,20 +1235,18 @@ void MainMenu::update()
1433 1235
       if(CARDINSERTED)
1434 1236
       {
1435 1237
         card.initsd();
1436
-        lcd_status("Card inserted");
1238
+        LCD_MESSAGEPGM("Card inserted");
1437 1239
       }
1438 1240
       else
1439 1241
       {
1440 1242
         card.release();
1441
-        lcd_status("Card removed");
1243
+        LCD_MESSAGEPGM("Card removed");
1442 1244
       }
1443 1245
     }
1444 1246
   #endif
1445 1247
  
1446 1248
   if(status!=oldstatus)
1447 1249
   {
1448
-    //Serial.println(status);
1449
-    //clear();
1450 1250
     force_lcd_update=true;
1451 1251
     encoderpos=0;
1452 1252
     lineoffset=0;

Loading…
Cancel
Save