-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPboyMenuGUI.cpp
151 lines (125 loc) · 5.05 KB
/
ESPboyMenuGUI.cpp
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
/*
ESPboy_MenuGUI class
for www.ESPboy.com project by RomanS
https://hackaday.io/project/164830-espboy-games-iot-stem-for-education-fun
v1.0
*/
#include "ESPboyMenuGUI.h"
#define SOUNDPIN D3
ESPboyMenuGUI::ESPboyMenuGUI(TFT_eSPI *tftMenuGUI, Adafruit_MCP23017 *mcpMenuGUI) {
tft = tftMenuGUI;
mcp = mcpMenuGUI;
#ifdef U8g2
u8f = new U8g2_for_TFT_eSPI;
u8f->begin(*tft);
u8f->setFontMode(1); // use u8g2 none transparent mode
u8f->setBackgroundColor(TFT_BLACK);
u8f->setFontDirection(0); // left to right
u8f->setFont(u8g2_font_4x6_t_cyrillic);
#endif
}
uint8_t ESPboyMenuGUI::getKeys() { return (~mcp->readGPIOAB() & 255); }
void ESPboyMenuGUI::menuDraw(){
static uint16_t scalingFactor;
static uint16_t previousRect = 0;
if(menuList.menuItemsQuantity>1){
if(menuList.menuItemsQuantity>=MENU_MAX_LINES_ONSCREEN)
scalingFactor = ((MENU_MAX_LINES_ONSCREEN*MENU_SPACE_BETWEEN_LINES-6)*1000)/(menuList.menuItemsQuantity-1);
else
scalingFactor = ((menuList.menuItemsQuantity*MENU_SPACE_BETWEEN_LINES-6)*1000)/(menuList.menuItemsQuantity-1);
}
else scalingFactor=1;
tft->drawRect(0, previousRect*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, TFT_BLACK);
tft->fillRect(125,0, 3, 128, TFT_BLACK);
if (menuList.menuCurrent+1 > MENU_MAX_LINES_ONSCREEN + menuList.menuOffset) {
tft->fillScreen(TFT_BLACK);
menuList.menuOffset++;}
if (menuList.menuCurrent < menuList.menuOffset) {
tft->fillScreen(TFT_BLACK);
menuList.menuOffset--;}
if(menuList.menuItemsQuantity>=MENU_MAX_LINES_ONSCREEN)
tft->drawLine(126,0, 126, MENU_MAX_LINES_ONSCREEN*MENU_SPACE_BETWEEN_LINES-2, TFT_BLUE);
else
tft->drawLine(126,0, 126, menuList.menuItemsQuantity*MENU_SPACE_BETWEEN_LINES-2, TFT_BLUE);
for (uint8_t i=0;; i++){
if(i>=menuList.menuItemsQuantity || i>=MENU_MAX_LINES_ONSCREEN) break;
#ifndef U8g2
if(menuList.menuLine[i+menuList.menuOffset][0] == '-'){
tft->setTextColor(menuList.menuUnselectedLineColor);
tft->drawString(&menuList.menuLine[i+menuList.menuOffset][1], 3, i*MENU_SPACE_BETWEEN_LINES+2);
}
else{
tft->setTextColor(menuList.menuLineColor);
tft->drawString(menuList.menuLine[i+menuList.menuOffset], 3, i*MENU_SPACE_BETWEEN_LINES+2);
}
#else
if(menuList.menuLine[i+menuList.menuOffset][0] == '-'){
u8f->setForegroundColor(menuList.menuUnselectedLineColor);
u8f->drawStr(3, i*MENU_SPACE_BETWEEN_LINES+1+GUI_FONT_HEIGHT, &menuList.menuLine[i+menuList.menuOffset][1]);
}
else {
u8f->setForegroundColor(menuList.menuLineColor);
u8f->drawStr(3, i*MENU_SPACE_BETWEEN_LINES+1+GUI_FONT_HEIGHT, menuList.menuLine[i+menuList.menuOffset]);
}
#endif
if((i+menuList.menuOffset) == menuList.menuCurrent){
tft->drawRect(0, i*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, menuList.menuSelectionColor);
previousRect=i;}
}
tft->fillRect(125, (scalingFactor*menuList.menuCurrent+2)/1000, 3, 5, TFT_YELLOW);
}
uint16_t ESPboyMenuGUI::menuInit(const char** menuLinesF, uint16_t menuLineColorF, uint16_t menuUnselectedLineColorF, uint16_t menuSelectionColorF){
uint16_t count=0;
static uint8_t keyPressed;
tft->fillScreen(TFT_BLACK);
menuList.menuLine = menuLinesF;
menuList.menuLineColor = menuLineColorF;
menuList.menuUnselectedLineColor = menuUnselectedLineColorF;
menuList.menuCurrent = 0;
menuList.menuSelectionColor = menuSelectionColorF;
menuList.menuOffset=0;
while(menuLinesF[count++]);
menuList.menuItemsQuantity = count-1;
menuDraw();
while(1){
while (!getKeys())delay(50);
keyPressed = getKeys();
if (keyPressed&MenuGUI_PAD_UP && menuList.menuCurrent > 0) {
menuList.menuCurrent--;
#ifdef buttonclicks
tone(SOUNDPIN,10,10);
#endif
menuDraw();
}
if (keyPressed&MenuGUI_PAD_DOWN && menuList.menuCurrent+1 < menuList.menuItemsQuantity) {
menuList.menuCurrent++;
#ifdef buttonclicks
tone(SOUNDPIN,10,10);
#endif
menuDraw();
}
if (keyPressed&MenuGUI_PAD_ACT && menuList.menuLine[menuList.menuCurrent][0] != '-') {
#ifdef buttonclicks
tone(SOUNDPIN,200,10);
#endif
tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, TFT_BLACK);
delay(50);
tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, menuList.menuSelectionColor);
delay(50);
tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, TFT_BLACK);
delay(50);
tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, menuList.menuSelectionColor);
delay(200);
tft->fillScreen(TFT_BLACK);
return(menuList.menuCurrent+1);
}
if (keyPressed&MenuGUI_PAD_ESC){
#ifdef buttonclicks
tone(SOUNDPIN,200,10);
#endif
tft->fillScreen(TFT_BLACK);
return(0);
}
delay(120);
}
};