-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTFT_Menu.h
301 lines (252 loc) · 7.85 KB
/
TFT_Menu.h
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
///////////////////////////////////////////////////////////////////////////
//
// TFT_Menu - A simple Arduino menu class for small ST7735 TFT displays
// using a clickable rotary encoder.
//
// 3rd Party arduino libraries used:
//
// Bodmer's Arduino graphics library for ST7735:
// https://github.com/Bodmer/TFT_ST7735
//
// 0xPIT's Atmel AVR C++ RotaryEncoder Implementation
// https://github.com/0xPIT/encoder
//
// Paul Stoffregen's TimerOne Library with optimization and expanded hardware support
// https://github.com/PaulStoffregen/TimerOne
//
// Code by Russ Hughes (russ@owt.com) September 2018
//
// License:
// This is free software. You can redistribute it and/or modify it under
// the terms of Creative Commons Attribution 3.0 United States License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
// or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
//
///////////////////////////////////////////////////////////////////////////
#ifndef __TFT_MENU_H__
#define __TFT_MENU_H__
///////////////////////////////////////////////////////////////////////////
//
// The R & B color bits are reversed on my ebay green tag display
// so these colors work for mine... YMMV.
//
///////////////////////////////////////////////////////////////////////////
#define TFT_BLACK 0x0000 /* 0, 0, 0 */
#define TFT_RED 0x001F /* 0, 0, 255 */
#define TFT_GREEN 0x07E0 /* 0, 255, 0 */
#define TFT_BLUE 0xF800 /* 255, 0, 0 */
#define TFT_WHITE 0xFFFF /* 255, 255, 255 */
///////////////////////////////////////////////////////////////////////////
//
// MENU structure:
//
// option: Value to display
// value: Value to return on button press
//
// NOTE: must be defined in a function since it is stored in flash
// using the __FlashStringHelper
//
///////////////////////////////////////////////////////////////////////////
typedef struct _MENU
{
const __FlashStringHelper *option;
int value;
} MENU;
///////////////////////////////////////////////////////////////////////////
//
// TFT_MENU Class to display a simple menu on a TFT display
//
///////////////////////////////////////////////////////////////////////////
class TFT_MENU
{
public:
TFT_MENU(TFT_ST7735 tft, ClickEncoder *encoder, int menuFont = 1, uint8_t textSize = 1);
int8_t show(MENU menu[], int8_t active = 1);
ClickEncoder::Button getButton();
void setColors(
uint16_t headerForground,
uint16_t headerBackground,
uint16_t normalForeground,
uint16_t normalBackground,
uint16_t selectedForgound,
uint16_t selectedBackground);
private:
void printSpaces(int8_t spaces = 1);
int font;
int16_t fontHeight;
int16_t fontWidth;
uint8_t maxChars;
uint8_t maxLines;
uint16_t hF = TFT_BLUE;
uint16_t hB = TFT_WHITE;
uint16_t nF = TFT_WHITE;
uint16_t nB = TFT_BLUE;
uint16_t sF = TFT_WHITE;
uint16_t sB = TFT_RED;
ClickEncoder::Button button = ClickEncoder::Open;
};
///////////////////////////////////////////////////////////////////////////
//
// TFT_MENU constuctor
//
// tft: tft object
// encoder: ClickEncoder object
// menuFont: Font to use for the menu, defaults to 1
// textSize: Size of text to use for the menu, defaults to 1
//
///////////////////////////////////////////////////////////////////////////
TFT_MENU::TFT_MENU(TFT_ST7735 tft, ClickEncoder *encoder, int menuFont, uint8_t textSize) {
font = menuFont;
fontHeight = tft.fontHeight(font) * textSize;
fontWidth = tft.textWidth((char *) " ", font) * textSize;
maxChars = tft.width() / (fontWidth);
maxLines = tft.height() / (fontHeight);
}
///////////////////////////////////////////////////////////////////////////
//
// show: Show the menu[]
//
// Parameters:
//
// menu[]: Array of MENU structures containing the menu
//
// The First entry's option will be shown as the menus Title and is not selectable.
// The Last entry's option must be NULL.
//
// active: Active menu option at start, defaults to the first
//
// Returns:
// the current option when the button was held, clicked or doubleclicked.
//
// Note:
// use getButton to determine how the button was clicked.
//
///////////////////////////////////////////////////////////////////////////
int8_t TFT_MENU::show(MENU menu[], int8_t active = 1) {
uint8_t menuCount = 0;
uint8_t first = 1;
uint8_t lfirst = 0;
uint8_t count = 0;
uint8_t line = 0;
uint8_t current = active;
uint8_t lCurrent = 0;
uint8_t i = 0;
uint8_t j = 0;
boolean again = true;
// adjust first if the current option would be
// off the screen
if (current > maxLines-1)
first = current - maxLines+2;
// Clear screen
tft.fillScreen(nB);
// Print the menu header centered on the first line
tft.setTextColor(hF, hB);
uint8_t len = strlen_PF(menu[0].option);
if (len < maxChars)
i = ((maxChars/2)-(len/2)) * fontWidth;
else
i = 1;
tft.fillRect(0,0,127,fontHeight, hB);
tft.setCursor(i, 0);
tft.println(menu[0].option);
// determine the number of items in the menu
i = 0;
while(menu[i++].option)
menuCount++;
// display the menu
tft.setTextColor(nF, nB);
do {
if (current != lCurrent || first != lfirst) { // if current or first has changed draw the menu
tft.setCursor(0, fontHeight); // skip the first display line (the title)
lCurrent = current;
lfirst = first;
for (i = 0; i < maxLines-1; i++) { // for as many lines that wil fit on the display
j = first+i;
if (j < menuCount) {
if (j == current) {
tft.setTextColor(sF, sB);
tft.print(F(">"));
tft.setTextColor(nF, nB);
tft.print(menu[j].option);
}
else {
printSpaces(1);
tft.print(menu[j].option);
}
printSpaces(maxChars - strlen_PF(menu[j].option));
tft.println();
}
}
}
switch(encoder->getValue()) {
case 0:
button = encoder->getButton();
switch(button) {
case ClickEncoder::Clicked:
case ClickEncoder::DoubleClicked:
again = false;
break;
case ClickEncoder::Held:
while (encoder->getButton() != ClickEncoder::Released);
again = false;
break;
}
break;
case 1:
if (current > 1) {
current--;
if (current < first)
first--;
}
break;
case -1:
if (current < menuCount-1) {
current++;
if (current > maxLines -1)
first = current - maxLines+2;
}
break;
}
}
while (again);
return menu[current].value;
}
///////////////////////////////////////////////////////////////////////////
//
// getButton: Returns how the button was clicked
//
///////////////////////////////////////////////////////////////////////////
ClickEncoder::Button TFT_MENU::getButton() {
return button;
}
///////////////////////////////////////////////////////////////////////////
//
// setColors: Set the text colors used by the menu
//
///////////////////////////////////////////////////////////////////////////
void TFT_MENU::setColors(
uint16_t headerForground,
uint16_t headerBackground,
uint16_t normalForeground,
uint16_t normalBackground,
uint16_t selectedForgound,
uint16_t selectedBackground)
{
hF = headerForground;
hB = headerBackground;
nF = normalForeground;
nB = normalBackground;
sF = selectedForgound;
sB = selectedBackground;
}
///////////////////////////////////////////////////////////////////////////
//
// printSpaces: Prints spaces on TFT, used to clear to the end of the line
//
///////////////////////////////////////////////////////////////////////////
void TFT_MENU::printSpaces(int8_t spaces) {
if (spaces > 0)
for (int i = 0; i < spaces; i++)
tft.print(F(" "));
}
#endif // __TFT_MENU_H__