Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MenuItem.cpp « Display « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d11db3b7b8c8917fef7f20e5eb3f11a7aeb29fcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
/*
 * MenuItem.cpp
 *
 *  Created on: 7 May 2018
 *      Author: David
 */

#include "MenuItem.h"

#if SUPPORT_12864_LCD

#include <Platform/RepRap.h>
#include <Heating/Heat.h>
#include <Platform/Platform.h>
#include <GCodes/GCodes.h>
#include <Movement/Move.h>
#include "Display.h"
#include <Tools/Tool.h>
#include <PrintMonitor/PrintMonitor.h>

#ifdef __LPC17xx__
# include "Network.h"
#else
# include <Networking/Network.h>
#endif

MenuItem::MenuItem(PixelNumber r, PixelNumber c, PixelNumber w, Alignment a, FontNumber fn, Visibility vis) noexcept
	: row(r), column(c), width(w), height(0), align(a), fontNumber(fn), visCase(vis), itemChanged(true), highlighted(false), drawn(false), next(nullptr)
{
}

/*static*/ void MenuItem::AppendToList(MenuItem **root, MenuItem *item) noexcept
{
	while (*root != nullptr)
	{
		root = &((*root)->next);
	}
	item->next = nullptr;
	*root = item;
}

// Print the item at the correct place with the correct alignment
void MenuItem::PrintAligned(Lcd& lcd, PixelNumber tOffset, PixelNumber rightMargin) noexcept
{
	PixelNumber colsToSkip = 0;
	lcd.SetFont(fontNumber);
	if (align != 0)
	{
		lcd.SetCursor(lcd.GetNumRows(), column);
		lcd.SetRightMargin(min<PixelNumber>(rightMargin, column + width));
		CorePrint(lcd);
		const PixelNumber w = lcd.GetColumn() - column;
		if (w < width)
		{
			colsToSkip = (align == RightAlign)
							? width - w - 1				// when right aligning, leave 1 pixel of space at the end
								: (width - w)/2;
		}
	}

	lcd.SetCursor(row - tOffset, column);
	lcd.SetRightMargin(min<PixelNumber>(rightMargin, column + width));
	lcd.TextInvert(highlighted);
	if (colsToSkip != 0)
	{
		lcd.ClearToMargin();
		lcd.SetCursor(row, column + colsToSkip);
	}
	CorePrint(lcd);
	if (align == 0)
	{
		lcd.ClearToMargin();
	}
	lcd.TextInvert(false);
}

bool MenuItem::IsVisible() const noexcept
{
	switch (visCase)
	{
	default:
	case 0:		return true;
	case 2:		return reprap.GetGCodes().IsReallyPrinting();
	case 3:		return !reprap.GetGCodes().IsReallyPrinting();
	case 4:		return reprap.GetPrintMonitor().IsPrinting();
	case 5:		return !reprap.GetPrintMonitor().IsPrinting();
	case 6:		{
					const PauseState ps = reprap.GetGCodes().GetPauseState();
					return ps == PauseState::pausing || ps == PauseState::paused;
				}
	case 7:		return reprap.GetGCodes().IsReallyPrintingOrResuming();
#if HAS_MASS_STORAGE
	case 10:	return MassStorage::IsDriveMounted(0);
	case 11:	return !MassStorage::IsDriveMounted(0);
#endif
	case 20:
		{		const auto tool = reprap.GetCurrentOrDefaultTool();			// this can be null, especially during startup
				return tool.IsNotNull() && tool->HasTemperatureFault();
		}
	case 28:	return reprap.GetHeat().GetStatus(reprap.GetHeat().GetBedHeater(0)) == HeaterStatus::fault;
	}
}

// Erase this item if it is drawn but should not be visible
void MenuItem::EraseIfInvisible(Lcd& lcd, PixelNumber tOffset) noexcept
{
	if (drawn && !IsVisible())
	{
		lcd.Clear(row - tOffset, column, row + height, column + width);
		drawn = false;
	}
}

TextMenuItem::TextMenuItem(PixelNumber r, PixelNumber c, PixelNumber w, Alignment a, FontNumber fn, Visibility vis, const char* t) noexcept
	: MenuItem(r, c, w, a, fn, vis), text(t)
{
}

void TextMenuItem::CorePrint(Lcd& lcd) noexcept
{
	lcd.printf("%s", text);
}

void TextMenuItem::Draw(Lcd& lcd, PixelNumber rightMargin, bool highlight, PixelNumber tOffset) noexcept
{
	// We ignore the 'highlight' parameter because text items are not selectable
	if (IsVisible() && (!drawn || itemChanged))
	{
		PrintAligned(lcd, tOffset, rightMargin);
		itemChanged = false;
		drawn = true;
	}
}

void TextMenuItem::UpdateWidthAndHeight(Lcd& lcd) noexcept
{
	if (width == 0)
	{
		lcd.SetFont(fontNumber);
		lcd.SetCursor(lcd.GetNumRows(), 0);
		lcd.SetRightMargin(lcd.GetNumCols());
		lcd.TextInvert(false);
		lcd.printf("%s", text);
		width = lcd.GetColumn();
		if (align == LeftAlign)
		{
			++width;			// add a space column after left-aligned text with no explicit width, so that the next item can follow immediately
		}
	}
	if (height == 0)
	{
		lcd.SetFont(fontNumber);
		height = lcd.GetFontHeight();
	}
}

ButtonMenuItem::ButtonMenuItem(PixelNumber r, PixelNumber c, PixelNumber w, FontNumber fn, Visibility vis, const char* t, const char* cmd, char const* acFile) noexcept
	: MenuItem(r, c, w, CentreAlign, fn, vis), text(t), command(cmd), m_acFile(acFile)
{
}

void ButtonMenuItem::CorePrint(Lcd& lcd) noexcept
{
	lcd.WriteSpaces(1);				// space at start in case highlighted
	lcd.printf("%s", text);
	lcd.WriteSpaces(1);				// space at end to allow for highlighting
}

void ButtonMenuItem::Draw(Lcd& lcd, PixelNumber rightMargin, bool highlight, PixelNumber tOffset) noexcept
{
	if (IsVisible() && (itemChanged || !drawn || highlight != highlighted) && column < lcd.GetNumCols())
	{
		highlighted = highlight;
		PrintAligned(lcd, tOffset, rightMargin);
		itemChanged = false;
		drawn = true;
	}
}

void ButtonMenuItem::UpdateWidthAndHeight(Lcd& lcd) noexcept
{
	if (width == 0)
	{
		lcd.SetFont(fontNumber);
		lcd.SetCursor(lcd.GetNumRows(), 0);
		lcd.SetRightMargin(lcd.GetNumCols());
		lcd.TextInvert(false);
		CorePrint(lcd);
		width = lcd.GetColumn();
	}
	if (height == 0)
	{
		lcd.SetFont(fontNumber);
		height = lcd.GetFontHeight();
	}
}

// TODO WS1: if we overflow the command or directory string, we should probably offer a return value that tells the caller to do nothing...
bool ButtonMenuItem::Select(const StringRef& cmd) noexcept
{
	const int nReplacementIndex = StringContains(command, "#0");
	if (-1 != nReplacementIndex)
	{
		cmd.copy(command, nReplacementIndex);
		cmd.cat(m_acFile);
	}
	else
	{
		cmd.copy(command);
		if (StringEqualsIgnoreCase(command, "menu") && strlen(m_acFile) != 0)
		{
			// For backwards compatibility, if 'menu' is used without any parameters, use the L parameter as the name of the menu file
			cmd.cat(' ');
			cmd.cat(m_acFile);
		}
	}

	return true;
}

PixelNumber ButtonMenuItem::GetVisibilityRowOffset(PixelNumber tCurrentOffset, PixelNumber fontHeight) const noexcept
{
	PixelNumber tOffsetRequest = tCurrentOffset;

	// Are we off the bottom of the visible window?
	if (64 + tCurrentOffset <= row + fontHeight + 1)
	{
		tOffsetRequest = row - 3;
	}

	// Should we move back up?
	if (row < tCurrentOffset + 3)
	{
		tOffsetRequest = (row > 3) ? row - 3 : 0;
	}

	return tOffsetRequest;
}

ValueMenuItem::ValueMenuItem(PixelNumber r, PixelNumber c, PixelNumber w, Alignment a, FontNumber fn, Visibility vis, bool adj, unsigned int v, unsigned int d) noexcept
	: MenuItem(r, c, ((w != 0) ? w : DefaultWidth), a, fn, vis), valIndex(v), currentFormat(PrintFormat::undefined), decimals(d), adjusting(AdjustMode::displaying), adjustable(adj)
{
}

void ValueMenuItem::CorePrint(Lcd& lcd) noexcept
{
	if (adjustable)
	{
		lcd.WriteSpaces(1);
	}

	if (error)
	{
		lcd.printf("***");
	}
	else
	{
		switch (currentFormat)
		{
		case PrintFormat::asFloat:
			lcd.printf("%.*f", decimals, (double)currentValue.f);
			break;

		case PrintFormat::asPercent:
			lcd.printf("%.*f%%", decimals, (double)currentValue.f);
			break;

		case PrintFormat::asUnsigned:
			lcd.printf("%u", currentValue.u);
			break;

		case PrintFormat::asSigned:
			lcd.printf("%d", currentValue.i);
			break;

		case PrintFormat::asText:
			lcd.printf("%s", textValue);
			break;

		case PrintFormat::asIpAddress:
			lcd.printf("%u.%u.%u.%u", currentValue.u & 0x000000FF, (currentValue.u >> 8) & 0x0000000FF, (currentValue.u >> 16) & 0x0000000FF, (currentValue.u >> 24) & 0x0000000FF);
			break;

		case PrintFormat::asTime:
			{
				unsigned int hours = currentValue.u/3600,
					minutes = (currentValue.u / 60) % 60,
					seconds = currentValue.u % 60;
				lcd.printf("%u:%02u:%02u", hours, minutes, seconds);
			}
			break;

		case PrintFormat::undefined:
		default:
			lcd.printf("***");
			break;
		}
	}
}

void ValueMenuItem::Draw(Lcd& lcd, PixelNumber rightMargin, bool highlight, PixelNumber tOffset) noexcept
{
	if (IsVisible())
	{
		error = false;
		textValue = nullptr;

		if (valIndex == 501)
		{
			// Item 501 is a special case because it is text, not a number. We store the current message sequence number in currentValue.
			uint16_t newSeq;
			textValue = reprap.GetLatestMessage(newSeq);
			if (newSeq != currentValue.u)
			{
				itemChanged = true;
				currentValue.u = newSeq;
				currentFormat = PrintFormat::asText;
			}
		}
		else if (adjusting != AdjustMode::adjusting)
		{
			const unsigned int itemNumber = valIndex % 100;
			const Value oldValue = currentValue;
			currentFormat = PrintFormat::asFloat;

			switch (valIndex/100)
			{
			case 0:		// heater current temperature
				currentValue.f = max<float>(reprap.GetGCodes().GetItemCurrentTemperature(itemNumber), 0.0);
				break;

			case 1:		// heater active temperature
				currentValue.f = max<float>(reprap.GetGCodes().GetItemActiveTemperature(itemNumber), 0.0);
				break;

			case 2:		// heater standby temperature
				currentValue.f = max<float>(reprap.GetGCodes().GetItemStandbyTemperature(itemNumber), 0.0);
				break;

			case 3:		// fan %
				currentValue.f = ((itemNumber == 99)
								? reprap.GetGCodes().GetMappedFanSpeed()
								: reprap.GetFansManager().GetFanValue(itemNumber)
							   ) * 100.0;
				currentFormat = PrintFormat::asPercent;
				break;

			case 4:		// extruder %
				currentValue.f = reprap.GetGCodes().GetExtrusionFactor(itemNumber) * 100.0;
				currentFormat = PrintFormat::asPercent;
				break;

			case 5:		// misc
				switch (itemNumber)
				{
				case 0:
					currentValue.f = reprap.GetGCodes().GetSpeedFactor() * 100.0;
					currentFormat = PrintFormat::asPercent;
					break;

				// case 1 is the latest message sent by M117, but it handled at the start

				case 10: // X
				case 11: // Y
				case 12: // Z
				case 13: // U
				case 14: // V
				case 15: // W
					currentValue.f = reprap.GetGCodes().GetUserCoordinate(itemNumber - 10);
					break;

				case 20:
					currentValue.i = reprap.GetCurrentToolNumber();
					currentFormat = PrintFormat::asSigned;
					break;

				case 21:	// Z baby-step
					currentValue.f = reprap.GetGCodes().GetTotalBabyStepOffset(Z_AXIS);
					break;

				// Platform's IP address is the "planned", Network's IP address is the "actual"
				case 30:
				case 31:
				case 32:
				case 33:
					currentValue.u = reprap.GetNetwork().GetIPAddress(0).GetQuad(itemNumber - 30);
					currentFormat = PrintFormat::asUnsigned;
					break;

				case 34:	// IP address in one go
					currentValue.u = reprap.GetNetwork().GetIPAddress(0).GetV4LittleEndian();
					currentFormat = PrintFormat::asIpAddress;
					break;

				case 35:	// Percentage of file that has been processed
					currentValue.f = (reprap.GetPrintMonitor().IsPrinting())
										? reprap.GetPrintMonitor().FractionOfFilePrinted() * 100.0
											: 0;
					currentFormat = PrintFormat::asPercent;
					break;

				case 36:	// Print time remaining, file-based
					currentValue.u = (reprap.GetPrintMonitor().IsPrinting())
										? static_cast<int>(reprap.GetPrintMonitor().EstimateTimeLeft(PrintEstimationMethod::fileBased))
											: 0;
					currentFormat = PrintFormat::asTime;
					break;

				case 37:	// Print time remaining, filament-based
					currentValue.u = (reprap.GetPrintMonitor().IsPrinting())
										? static_cast<int>(reprap.GetPrintMonitor().EstimateTimeLeft(PrintEstimationMethod::filamentBased))
											: 0;
					currentFormat = PrintFormat::asTime;
					break;

				case 38:	// requested speed
					currentValue.f = reprap.GetMove().GetRequestedSpeed();
					break;

				case 39:	// top speed
					currentValue.f = reprap.GetMove().GetTopSpeed();
					break;

				default:
					error = true;
				}
				break;

			default:
				error = true;
				break;
			}

			if (error)
			{
				itemChanged = true;
			}
			else
			{
				switch (currentFormat)
				{
				case PrintFormat::undefined:
					itemChanged = true;
					break;

				case PrintFormat::asFloat:
					if (currentValue.f != oldValue.f)
					{
						itemChanged = true;
					}
					break;

				case PrintFormat::asSigned:
					if (currentValue.i != oldValue.i)
					{
						itemChanged = true;
					}
					break;

				case PrintFormat::asUnsigned:
				case PrintFormat::asIpAddress:
				case PrintFormat::asText:
				case PrintFormat::asTime:
				default:
					if (currentValue.u != oldValue.u)
					{
						itemChanged = true;
					}
					break;
				}
			}
		}

		if (itemChanged || !drawn || (highlight != highlighted))
		{
			highlighted = highlight;
			PrintAligned(lcd, tOffset, rightMargin);
			itemChanged = false;
			drawn = true;
		}
	}
}

bool ValueMenuItem::Select(const StringRef& cmd) noexcept
{
	adjusting = AdjustMode::adjusting;
	return false;
}

void ValueMenuItem::UpdateWidthAndHeight(Lcd& lcd) noexcept
{
	// The width is always set for a ValueMenuItem so we just need to determine the height
	if (height == 0)
	{
		lcd.SetFont(fontNumber);
		height = lcd.GetFontHeight();
	}
}

PixelNumber ValueMenuItem::GetVisibilityRowOffset(PixelNumber tCurrentOffset, PixelNumber fontHeight) const noexcept
{
	// TODO
	return 0;
}

bool ValueMenuItem::Adjust_SelectHelper() noexcept
{
	if (adjusting == AdjustMode::adjusting)
	{
		const unsigned int itemNumber = GetReferencedToolNumber();

		bool err = false;
		switch (valIndex/100)
		{
		case 1: // heater active temperature
			if (1.0 > currentValue.f) // 0 is off
			{
				reprap.GetGCodes().SetItemActiveTemperature(itemNumber, -273.15);
			}
			else // otherwise ensure the tool is made active at the same time (really only matters for 79)
			{
				if (80 > itemNumber)
				{
					reprap.SelectTool(itemNumber, false);
				}
				reprap.GetGCodes().SetItemActiveTemperature(itemNumber, currentValue.f);
			}
			break;

		case 2: // heater standby temperature
			reprap.GetGCodes().SetItemStandbyTemperature(itemNumber, (1.0 > currentValue.f) ? -273.15 : currentValue.f);
			break;

		case 3: // fan %
			if (itemNumber == 99)
			{
				reprap.GetGCodes().SetMappedFanSpeed(currentValue.f * 0.01);
			}
			else
			{
				reprap.GetFansManager().SetFanValue(itemNumber, currentValue.f * 0.01);
			}
			break;

		case 4: // extruder %
			reprap.GetGCodes().SetExtrusionFactor(itemNumber, currentValue.f * 0.01);
			break;

		case 5: // misc.
			switch (itemNumber)
			{
			case 0:
				reprap.GetGCodes().SetSpeedFactor(currentValue.f * 0.01);
				break;

			case 20:
				reprap.SelectTool(currentValue.i, false);
				break;

			case 21: // baby stepping
				break;

			default:
				err = true;
				break;
			}
			break;

		default:
			err = true;
			break;
		}

		if (err)
		{
			reprap.GetDisplay().ErrorBeep();
		}
	}

	adjusting = AdjustMode::displaying;
	return true;
}

unsigned int ValueMenuItem::GetReferencedToolNumber() const noexcept
{
	unsigned int uToolNumber = valIndex % 100;
	if (79 == uToolNumber)
	{
		uToolNumber = reprap.GetCurrentOrDefaultTool()->Number();
	}

	return uToolNumber;
}

// Adjust the value of this item by 'clicks' click of the encoder. 'clicks' is nonzero.
// Return true if we have finished adjusting it.
bool ValueMenuItem::Adjust_AlterHelper(int clicks) noexcept
{
	itemChanged = true;			// we will probably change the value, so it will need to be re-displayed
	const unsigned int itemNumber = GetReferencedToolNumber();

	switch (valIndex/100)
	{
	case 1:	// heater active temperature
	case 2:	// heater standby temperature
		if (itemNumber < 80) // Tool heaters
		{
			// If we're decreasing, make any value smaller than 95 go to 0
			// If we're increasing, make any value between 0 and 95 jump directly to 95
			// Also cap the maximum
			if (0 > clicks) // decrementing
			{
				currentValue.f += (float)clicks;
				if (95.0 > currentValue.f)
				{
					currentValue.f = 0.0;
				}
			}
			else // incrementing
			{
				if (0.0 == currentValue.f)
				{
					currentValue.f = 95.0 - 1.0;
				}
				currentValue.f = min<int>(currentValue.f + (float)clicks, reprap.GetHeat().GetHighestTemperatureLimit(reprap.GetTool(itemNumber)->GetHeater(0)));
			}
		}
		else
		{
			currentValue.f += (float)clicks;
		}
		break;

	case 3: // fan %
		currentValue.f = constrain<float>(currentValue.f + (float)clicks, 0.0, 100.0);
		break;

	case 5: // misc.
		switch (itemNumber)
		{
		case 0: // 500 Feed Rate
			currentValue.f = constrain<float>(currentValue.f + (float)clicks, 10.0, 500.0);
			break;

		case 20: // 520 Tool Selection
			currentValue.i = constrain<int>((int)currentValue.f + clicks, -1, 255);
			break;

		case 21: // 521 baby stepping
			{
				String<SHORT_GCODE_LENGTH> cmd;
				cmd.printf("M290 Z%.2f", (double)(0.02 * clicks));
				(void) reprap.GetGCodes().ProcessCommandFromLcd(cmd.c_str());
				adjusting = AdjustMode::liveAdjusting;
			}
			break;

		default:
			if (itemNumber >= 10 && itemNumber < 10 + reprap.GetGCodes().GetVisibleAxes())	// 510-518 axis position adjustment
			{
				String<SHORT_GCODE_LENGTH> cmd;
				const float amount = ((itemNumber == 12) ? 0.02 : 0.1) * clicks;			// 0.02mm Z resolution, 0.1mm for other axes
				cmd.printf("M120 G91 G1 F3000 %c%.2f M121", 'X' + (itemNumber - 10), (double)amount);
				(void) reprap.GetGCodes().ProcessCommandFromLcd(cmd.c_str());
				adjusting = AdjustMode::liveAdjusting;
			}
			break;
		}
		break;

	default:
		currentValue.f += (float)clicks;
		break;
	}

	return false;
}

// Adjust this element, returning true if we have finished adjustment.
// 'clicks' is the number of encoder clicks to adjust by, or 0 if the button was pushed.
bool ValueMenuItem::Adjust(int clicks) noexcept
{
	return (clicks == 0)						// if button has been pressed
			?  Adjust_SelectHelper()
				: Adjust_AlterHelper(clicks);
}

#if HAS_MASS_STORAGE
FilesMenuItem::FilesMenuItem(PixelNumber r, PixelNumber c, PixelNumber w, FontNumber fn, Visibility vis, const char *cmd, const char *dir, const char *acFile, unsigned int nf) noexcept
	: MenuItem(r, c, w, LeftAlign, fn, vis), numDisplayLines(nf), command(cmd), initialDirectory(dir), m_acFile(acFile),
        m_uListingFirstVisibleIndex(0), m_uListingSelectedIndex(0)
{
	// There's no guarantee that initialDirectory has a trailing '/'
	currentDirectory.copy(initialDirectory);
	const size_t len = currentDirectory.strlen();
	if (len == 0 || '/' != currentDirectory[len - 1])
	{
		currentDirectory.cat('/');
	}

	initialDirectoryNesting = GetDirectoryNesting();
	sdCardState = notStarted;
}

void FilesMenuItem::vResetViewState() noexcept
{
	m_uListingSelectedIndex = 0;
	m_uListingFirstVisibleIndex = 0;
}

void FilesMenuItem::EnterDirectory() noexcept
{
	vResetViewState();

	m_uHardItemsInDirectory = 0;
	FileInfo oFileInfo;
	if (MassStorage::FindFirst(currentDirectory.c_str(), oFileInfo))
	{
		do
		{
			if (oFileInfo.fileName[0] != '.')
			{
				++m_uHardItemsInDirectory;
			}
		}
		while (MassStorage::FindNext(oFileInfo));
	}

	itemChanged = true;							// force a redraw
}

uint8_t FilesMenuItem::GetDirectoryNesting() const noexcept
{
	const char *pcPathElement = currentDirectory.c_str();
	uint8_t uNumSlashes = 0;

	while ('\0' != *pcPathElement)
	{
		if (('/' == *pcPathElement) && ('\0' != *(pcPathElement + 1))) // don't count a trailing slash
		{
			++uNumSlashes;
		}
		++pcPathElement;
	}
	return uNumSlashes;
}

bool FilesMenuItem::bInSubdirectory() const noexcept
{
	return GetDirectoryNesting() > initialDirectoryNesting;
}

unsigned int FilesMenuItem::uListingEntries() const noexcept
{
	return bInSubdirectory() ? (1 + m_uHardItemsInDirectory) : m_uHardItemsInDirectory;
}

void FilesMenuItem::Draw(Lcd& lcd, PixelNumber rightMargin, bool highlight, PixelNumber tOffset) noexcept
{
	// The 'highlight' parameter is not used to highlight this item, but it is still used to tell whether this item is selected or not
	if (!IsVisible())
	{
		sdCardState = notStarted;
	}
	else if (!drawn || itemChanged || highlighted != highlight)
	{
		switch (sdCardState)
		{
		case notStarted:
			if (MassStorage::CheckDriveMounted(currentDirectory.c_str()))
			{
				sdCardState = mounted;
				EnterDirectory();
			}
			else
			{
				sdCardState = mounting;
			}
			break;

		case mounting:
			{
				const size_t card = (isdigit(currentDirectory[0]) && currentDirectory[1] == ':') ? currentDirectory[0] - '0' : 0;
				String<StringLength50> reply;
				switch(MassStorage::Mount(card, reply.GetRef(), false))
				{
				case GCodeResult::notFinished:
					return;

				case GCodeResult::ok:
					sdCardState = mounted;
					EnterDirectory();
					break;

				default:
					reply.copy("Internal error");
					// no break
				case GCodeResult::error:
					sdCardState = error;
					lcd.SetFont(fontNumber);
					lcd.SetCursor(row, column);
					lcd.SetRightMargin(rightMargin);
					lcd.ClearToMargin();
					lcd.SetCursor(row, column);
					lcd.printf("%s", reply.c_str());
					break;
				}
			}
			break;

		case mounted:
			ListFiles(lcd, rightMargin, highlight, tOffset);
			break;

		case error:
			break;
		}
	}
}

void FilesMenuItem::ListFiles(Lcd& lcd, PixelNumber rightMargin, bool highlight, PixelNumber tOffset) noexcept
{
	lcd.SetFont(fontNumber);
	lcd.SetRightMargin(rightMargin);
	uint8_t line = 0;

	// If we are in a subdirectory then we prepend ".." to the list of files
	unsigned int dirEntriesToSkip;
	if (bInSubdirectory())
	{
		if (m_uListingFirstVisibleIndex == 0)
		{
			lcd.SetCursor(row, column);
			lcd.printf("  ..");
			lcd.ClearToMargin();
			if (highlight && m_uListingSelectedIndex == 0)
			{
				// Overwriting the initial spaces with '>' avoids shifting the following text when we change the selection
				lcd.SetCursor(row, column);
				lcd.write('>');
			}
			line = 1;
			dirEntriesToSkip = 0;
		}
		else
		{
			dirEntriesToSkip = m_uListingFirstVisibleIndex - 1;
		}
	}
	else
	{
		dirEntriesToSkip = m_uListingFirstVisibleIndex;
	}

	// Seek to the first file that is in view
	FileInfo oFileInfo;
	bool gotFileInfo = MassStorage::FindFirst(currentDirectory.c_str(), oFileInfo);
	while (gotFileInfo)
	{
		if (oFileInfo.fileName[0] != '.')
		{
			if (dirEntriesToSkip == 0)
			{
				break;
			}
			--dirEntriesToSkip;
		}
		gotFileInfo =  MassStorage::FindNext(oFileInfo);
	}

	// We always iterate the entire viewport so that old listing lines that may not be overwritten are cleared
	while (line < numDisplayLines)
	{
		lcd.SetCursor(row + (lcd.GetFontHeight() * line), column);

		// If there's actually a file to describe (not just ensuring viewport line clear)
		if (gotFileInfo)
		{
			lcd.printf("  ");
			if (oFileInfo.isDirectory)
			{
				lcd.printf("./");
			}
			lcd.printf("%s", oFileInfo.fileName.c_str());
			lcd.ClearToMargin();
			if (highlight && m_uListingSelectedIndex == line + m_uListingFirstVisibleIndex)
			{
				lcd.SetCursor(row + (lcd.GetFontHeight() * line), column);
				lcd.write('>');
			}
		}
		else
		{
			lcd.ClearToMargin();
		}

		++line;
		if (line == numDisplayLines)
		{
			break;		// skip getting more file info for efficiency
		}

		do
		{
			gotFileInfo = MassStorage::FindNext(oFileInfo);
		} while (gotFileInfo && oFileInfo.fileName[0] == '.');
	}

	MassStorage::AbandonFindNext();				// release the mutex, there may be more files that we don't have room to display

	itemChanged = false;
	drawn = true;
	highlighted = highlight;
}

void FilesMenuItem::Enter(bool bForwardDirection) noexcept
{
	if (bForwardDirection || uListingEntries() == 0)
	{
		m_uListingSelectedIndex = 0;
		m_uListingFirstVisibleIndex = 0;					// select the first item and start the list from the first item
	}
	else
	{
		m_uListingSelectedIndex = uListingEntries() - 1;	// select the last item
		m_uListingFirstVisibleIndex = ((uListingEntries() > numDisplayLines) ? (uListingEntries() - numDisplayLines) : 0);
	}
	itemChanged = true;
}

int FilesMenuItem::Advance(int nCounts) noexcept
{
	// In case of empty directory, there's nothing the control itself can do
	if (uListingEntries() != 0)
	{
		while (nCounts > 0)
		{
			// Advancing one more would take us past the end of the list
			// Instead, return the remaining count so that the other selectable menu items can be scrolled.
			if (m_uListingSelectedIndex + 1 == uListingEntries())
			{
				break;
			}

			++m_uListingSelectedIndex;
			--nCounts;

			// Move the visible portion of the list down, if required
			if (m_uListingSelectedIndex == m_uListingFirstVisibleIndex + numDisplayLines)
			{
				++m_uListingFirstVisibleIndex;
			}
		}

		while (nCounts < 0)
		{
			// We're already at the first item in the visible list; return the remaining action to the menu system.
			if (0 == m_uListingSelectedIndex)
			{
				break;
			}

			--m_uListingSelectedIndex;
			++nCounts;

			// Move the visible portion of the list up, if required
			if (m_uListingSelectedIndex < m_uListingFirstVisibleIndex)
			{
				--m_uListingFirstVisibleIndex;
			}
		}

		itemChanged = true;
	}

	return nCounts;
}

bool FilesMenuItem::Select(const StringRef& cmd) noexcept
{
	// Several cases:
	// TEST 1. ".." entry - call EnterDirectory(), using saved state information
	// TEST 2. Directory - call EnterDirectory(), adding to saved state information
	// 3. File - run command with filename as argument

	// Get information on the item selected

	if (bInSubdirectory() && 0 == m_uListingSelectedIndex) // meaning ".."
	{
		// TODO: go up one level rather than to logical root
		// There's no guarantee that initialDirectory has a trailing '/'
		currentDirectory.copy(initialDirectory);
		const size_t len = currentDirectory.strlen();
		if (len == 0 || '/' != currentDirectory[len - 1])
		{
			currentDirectory.cat('/');
		}
		EnterDirectory();
	}
	else
	{
		// If subdir:
		// If ".." is visible, the selected file is visible index m_uListingSelectedIndex, fs index m_uListingSelectedIndex - 1
		// If ".." is not visible, the selected file is visible index m_uListingSelectedIndex, fs index m_uListingSelectedIndex - 1
		// If logical root:
		// ".." is never visible, so the selected file is visible index m_uListingSelectedIndex, fs index m_uListingSelectedIndex
		unsigned int dirEntriesToSkip = bInSubdirectory() ? m_uListingSelectedIndex - 1 : m_uListingSelectedIndex;

		// Seek to the selected file
		FileInfo oFileInfo;
		bool gotFileInfo = MassStorage::FindFirst(currentDirectory.c_str(), oFileInfo);
		while (gotFileInfo)
		{
			if (oFileInfo.fileName[0] != '.')
			{
				if (dirEntriesToSkip == 0)
				{
					break;
				}
				--dirEntriesToSkip;
			}
			gotFileInfo = MassStorage::FindNext(oFileInfo);
		}
		MassStorage::AbandonFindNext();

		if (gotFileInfo)	// handles empty directory (no action)
		{
			if (oFileInfo.isDirectory)
			{
				// Build the new directory, and ensure it's terminated with a forward slash
				currentDirectory.cat(oFileInfo.fileName.c_str());
				currentDirectory.cat('/');
				EnterDirectory();
			}
			else
			{
				int nReplacementIndex = StringContains(command, "#0");
				if (nReplacementIndex != -1)
				{
					cmd.copy(command, nReplacementIndex);
					cmd.cat('"');
					cmd.cat(currentDirectory.c_str());
					cmd.cat(oFileInfo.fileName.c_str());
					cmd.cat('"');
					cmd.cat(command + nReplacementIndex + strlen("#0"));
				}
				else
				{
					cmd.copy(command);
				}

				// TODO: do this on the way in and it might be less work...
				//   On the other hand, this only occurs when an item is selected so it's O(1) vs. O(n)
				nReplacementIndex = cmd.Contains("menu");
				if (nReplacementIndex != -1)
				{
					cmd.Truncate(nReplacementIndex);
					cmd.cat(m_acFile);
				}

				return true;
			}
		}
	}

	return false;
}

void FilesMenuItem::UpdateWidthAndHeight(Lcd& lcd) noexcept
{
	// The width is always set for a FilesMenuItem so we just need to determine the height
	if (height == 0)
	{
		lcd.SetFont(fontNumber);
		height = lcd.GetFontHeight() * numDisplayLines;
	}
}

PixelNumber FilesMenuItem::GetVisibilityRowOffset(PixelNumber tCurrentOffset, PixelNumber fontHeight) const noexcept
{
	// TODO
	return 0;
}
#endif

// Image menu item members
// The image file format is:
// Byte 0 = number of columns
// Byte 1 = number of rows
// Remaining bytes = data, 1 row at a time. If the number of columns is not a multiple of 8 then the data for each row is padded to a multiple of 8 bits.
ImageMenuItem::ImageMenuItem(PixelNumber r, PixelNumber c, Visibility vis, const char *pFileName) noexcept
	: MenuItem(r, c, 0, 0, 0, vis)
{
	fileName.copy(pFileName);
}

void ImageMenuItem::Draw(Lcd& lcd, PixelNumber rightMargin, bool highlight, PixelNumber tOffset) noexcept
{
	if (IsVisible() && (!drawn || itemChanged || highlight != highlighted))
	{
		FileStore * const fs = reprap.GetPlatform().OpenFile(MENU_DIR, fileName.c_str(), OpenMode::read);
		if (fs != nullptr)
		{
			uint8_t widthAndHeight[2];
			if (fs->Read(widthAndHeight, 2) == 2)
			{
				const PixelNumber cols = widthAndHeight[0];
				const PixelNumber rows = widthAndHeight[1];
				if (cols != 0 && cols <= lcd.GetNumCols() && rows != 0)
				{
					const size_t bytesPerRow = (cols + 7)/8;
					for (PixelNumber irow = 0; irow < rows; ++irow)
					{
						uint8_t buffer[lcd.GetNumCols()/8];
						if (fs->Read(buffer, bytesPerRow) != (int)bytesPerRow)
						{
							break;
						}
						lcd.BitmapRow(row - tOffset + irow, column,  cols, buffer, highlight);
					}
				}
			}
			fs->Close();
		}
		itemChanged = false;
		drawn = true;
		highlighted = highlight;
	}
}

void ImageMenuItem::UpdateWidthAndHeight(Lcd& lcd) noexcept
{
	if (width == 0 || height == 0)
	{
		FileStore * const fs = reprap.GetPlatform().OpenFile(MENU_DIR, fileName.c_str(), OpenMode::read);
		if (fs != nullptr)
		{
			uint8_t w[2];
			fs->Read(w, 2);			// read the number of columns
			fs->Close();
			width = w[0];
			height = w[1];
		}
	}
}

#endif

// End