forked from dancol90/MenuLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LcdDrawer.h
223 lines (163 loc) · 4.71 KB
/
LcdDrawer.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
#ifndef __lcd_drawer_h__
#define __lcd_drawer_h__
#define SSD1306
#include <MenuLib.h>
#include <LightLCD.h>
#define SELECTORSSTART 9
#define SELECTORHEIGHT 7
class LcdDrawer : public MenuItemDrawer {
protected:
LightLCD& lcd;
uint8_t items_per_screen;
uint8_t getNumLength(uint8_t num) {
uint8_t numLength = 0, tmp;
if(num == 0) return 1;
// Init
tmp = abs(num);
while(tmp != 0) {
tmp = tmp / 10;
numLength++;
}
if(num < 0) numLength++;
return numLength;
}
void setCenterCursor(uint8_t len, uint8_t y, uint8_t from = 0, uint8_t to = 0) {
uint8_t x;
if (to == 0)
to = lcd.width();
if( len > to - from )
x = from;
else
x = from + ( to - from - len ) / 2;
lcd.setCursor(x, y);
}
void drawCenterText(MenuItem* item, uint8_t y) {
if (item->isTextFlash()) {
const FlashString* text = reinterpret_cast<const FlashString*>(item->getText());
setCenterCursor(lcd.getStringWidth(text), y);
lcd.print(text);
} else {
const char* text = item->getText();
setCenterCursor(lcd.getStringWidth(text), y);
lcd.print(text);
}
}
void drawCenterNumber(uint8_t num, uint8_t y, uint8_t from = 0, uint8_t to = 0) {
if (to == 0)
to = lcd.width();
// A digit has an average width of 4px
setCenterCursor(4 * getNumLength(num), y, from, to);
lcd.print(num);
}
void drawMenu(Menu* menu) {
uint8_t color = 1;
uint8_t i = 0, y;
char* secText;
//lcd.drawLine(0, 7, lcd.width(), 7, 1) ;
drawCenterText(menu, 0);
ListEntry *e = menu->getCollection(), *first = e;
if (!e) return;
// Each screen contains 3 entry; when the third is reached, the next 3 will be shown
// Find the right place where to start by scanning from the top.
do {
// Skip disabled items
if (e->item->isEnabled()) {
if (i % items_per_screen == 0)
first = e;
i++;
}
if (e == menu->getSelectedListEntry())
break;
e = e->next;
} while(e);
e = first;
i = 0;
lcd.setCursor(0, SELECTORSSTART);
// Now actually draw the entries
while (e && i < items_per_screen) {
if (e->item->isEnabled()) {
y = SELECTORSSTART + SELECTORHEIGHT * i;
if(e == menu->getSelectedListEntry()) {
lcd.fillRect(0, y, lcd.width(), SELECTORHEIGHT, 1);
lcd.setTextColor(0);
} else
lcd.setTextColor(1);
secText = (char*)e->item->getSecondaryText();
if(secText) {
// Draw secondary text
lcd.setCursor(lcd.width() - lcd.getStringWidth(secText), y);
lcd.print(secText);
}
// Draw item's text
lcd.setCursor(1, y);
if (e->item->isTextFlash()) {
const FlashString* text = reinterpret_cast<const FlashString*>(e->item->getText());
lcd.print(text);
} else {
const char* text = e->item->getText();
lcd.print(text);
}
i++;
}
e = e->next;
}
lcd.setTextColor(1);
}
#if defined PCD8544
#define RECT_W 68
#define RECT_H 14
#define RECT_Y 18
#elif defined SSD1306
#define RECT_W 76
#define RECT_H 14
#define RECT_Y 18
#endif
#define RECT_X (lcd.width() - RECT_W) / 2
#define TEXT_Y RECT_Y + (RECT_H - 7) / 2
void drawSelector(NumericSelector* selector) {
// Draw title
drawCenterText(selector, 0);
//lcd.drawLine(0, 8, lcd.width(), 8, 1) ;
// Draw a rect around the value
lcd.drawRect(RECT_X, RECT_Y, RECT_W, RECT_H, 1);
// Some decoration
lcd.setCursor(RECT_X + 3, TEXT_Y);
lcd.print('<');
// Guess the x pos to center the value number
if (selector->getTypeId() == 's') { //numeric selector
drawCenterNumber(selector->getValue(), TEXT_Y);
} else if (selector->getTypeId() == 't') { //array selector
const char* text = selector->getSecondaryText();
setCenterCursor(lcd.getStringWidth(text), TEXT_Y);
lcd.print(text);
}
// Still some decorations
lcd.setCursor(RECT_X + RECT_W - 6, TEXT_Y);
lcd.println('>');
}
// Override this to extend functionalities
void virtual drawOther(MenuItem* item) {};
public:
LcdDrawer(LightLCD& lcd, uint8_t num_items) : lcd(lcd), items_per_screen(num_items) {}
void draw(MenuItem* item) {
if(!item) return;
lcd.clear();
lcd.setCursor(0, 0);
switch(item->getTypeId()) {
case 'm':
drawMenu((Menu*)item);
break;
// Actions and Check Boxes does not need to be drawn
// case 'a':
// case 'c':
case 't': //array selector
case 's': //numeric selector
drawSelector((NumericSelector*)item);
break;
default:
drawOther(item);
}
lcd.update();
}
};
#endif