forked from dancol90/MenuLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SSD1306Drawer.h
197 lines (144 loc) · 4.63 KB
/
SSD1306Drawer.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
#ifndef __ssd1306_drawer_h__
#define __ssd1306_drawer_h__
#include <MenuLib.h>
#include <SSD1306Wire.h>
#define SELECTORSSTART 16
#define SELECTORHEIGHT 10
#define SELECTORFONTMARGIN 2
#define RECT_W 76
#define RECT_H 14
#define RECT_Y 18
#define RECT_X (DISPLAY_WIDTH - RECT_W) / 2
#define TEXT_Y RECT_Y
class SSD1306Drawer : public MenuItemDrawer {
protected:
SSD1306Wire& 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 drawCenterText(MenuItem* item, uint8_t y) {
if (item->isTextFlash()) {
const FlashString* text = reinterpret_cast<const FlashString*>(item->getText());
lcd.setTextAlignment(TEXT_ALIGN_CENTER);
lcd.drawString(DISPLAY_WIDTH/2, y, text);
} else {
const char* text = item->getText();
lcd.setTextAlignment(TEXT_ALIGN_CENTER);
lcd.drawString(DISPLAY_WIDTH/2, y, text);
}
}
void drawCenterNumber(uint8_t num, uint8_t y, uint8_t from = 0, uint8_t to = 0) {
lcd.setTextAlignment(TEXT_ALIGN_CENTER);
lcd.drawString(DISPLAY_WIDTH/2, y, String(num));
}
void drawMenu(Menu* menu) {
uint8_t i = 0, y;
char* secText;
drawCenterText(menu, 0);
lcd.drawLine(0, 14, DISPLAY_WIDTH, 14) ;
ListEntry *e = menu->getCollection(), *first = e;
if (!e) return;
// Each screen contains X entry; when the third is reached, the next X 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;
// Now actually draw the entries
while (e && i < items_per_screen) {
if (e->item->isEnabled()) {
y = SELECTORSSTART + SELECTORHEIGHT * i;
if(e == menu->getSelectedListEntry()) {
lcd.setColor(WHITE);
lcd.fillRect(0, y, DISPLAY_WIDTH, SELECTORHEIGHT);
lcd.setColor(INVERSE);
} else
lcd.setColor(WHITE);
secText = (char*)e->item->getSecondaryText();
if(secText) {
// Draw secondary text
lcd.setTextAlignment(TEXT_ALIGN_LEFT);
lcd.drawString(DISPLAY_WIDTH - lcd.getStringWidth(secText) - 1, y - SELECTORFONTMARGIN, secText);
}
// Draw item's text
if (e->item->isTextFlash()) {
const FlashString* text = reinterpret_cast<const FlashString*>(e->item->getText());
lcd.setTextAlignment(TEXT_ALIGN_LEFT);
lcd.drawString(1, y - SELECTORFONTMARGIN, text);
} else {
const char* text = e->item->getText();
lcd.setTextAlignment(TEXT_ALIGN_LEFT);
lcd.drawString(1, y - SELECTORFONTMARGIN, text);
}
i++;
}
e = e->next;
}
lcd.setColor(WHITE);
}
void drawSelector(NumericSelector* selector) {
// Draw title
drawCenterText(selector, 0);
lcd.drawLine(0, 14, DISPLAY_WIDTH, 14) ;
// Draw a rect around the value
lcd.setColor(WHITE);
lcd.drawRect(RECT_X, RECT_Y, RECT_W, RECT_H);
// Some decoration
lcd.setTextAlignment(TEXT_ALIGN_LEFT);
lcd.drawString(RECT_X + 2, TEXT_Y, "<");
// 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();
lcd.setTextAlignment(TEXT_ALIGN_CENTER);
lcd.drawString(DISPLAY_WIDTH/2, TEXT_Y, text);
}
// Still some decorations
lcd.setTextAlignment(TEXT_ALIGN_LEFT);
lcd.drawString(RECT_X + RECT_W - 8, TEXT_Y, ">");
}
// Override this to extend functionalities
void virtual drawOther(MenuItem* item) {};
public:
SSD1306Drawer(SSD1306Wire& lcd, uint8_t num_items) : lcd(lcd), items_per_screen(num_items) {}
void draw(MenuItem* item) {
if(!item) return;
lcd.clear();
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.display();
}
};
#endif