-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLcdMenu.cpp
264 lines (218 loc) · 6.61 KB
/
LcdMenu.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
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
#include "LcdMenu.h"
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LcdMenu::LcdMenu(LiquidCrystal_I2C &lcd)
{
// Use 'this->' to make the difference between the
// 'pin' attribute of the class and the
// local variable 'pin' created from the parameter.
this->lcd = &lcd;
// init();
}
void LcdMenu::init()
{
// -------INIT Chars
lcd->createChar(0, Speed);
lcd->createChar(1, Bulb);
lcd->createChar(2, IDK); // TODO Think of something to put here
lcd->createChar(3, Check);
// INVERTED
lcd->createChar(4, Speed_Inverted);
lcd->createChar(5, Buld_Inverted);
lcd->createChar(6, IDK_Inverted);
lcd->createChar(7, Check_Inverted);
}
void LcdMenu::idk()
{
lcd->setCursor(0, 0);
lcd->print("Hello World!");
}
void LcdMenu::update()
{
// Button Press Handler
// - -- --- --- -- -
// LEFT
if (!selected)
{
if (digitalRead(leftButtonPin) == 1 && millis() - millsPress > pressDelay)
{
millsPress = millis();
// Serial.write("LEFT\n");
if (cursorPosition == 0)
{
cursorPosition = 2;
}
else
{
cursorPosition = (cursorPosition - 1) % 3;
}
drawMenu();
}
// RIGHT
if (digitalRead(rightButtonPin) == 1 && millis() - millsPress > pressDelay)
{
millsPress = millis();
// Serial.write("RIGHT\n");
cursorPosition = (cursorPosition + 1) % 3;
drawMenu();
}
}
// Select
if (digitalRead(selectButtonPin) == 1 && millis() - millsPress > pressDelay)
{
millsPress = millis();
selected = !selected;
drawMenu();
// Serial.write(cursorPosition);
// Serial.write("SELECT\n");
}
/* --- --- --- --- --- --- --- --- --- --- --- --- ---*/
// print information about selected option + control
if (selected)
{
switch (cursorPosition)
{
//——————————————————Speed——————————————————//
// speed of leds blinking or fading - 0%-100%
case 0:
// Serial.write("pos = 0");
lcd->setCursor(0, 0);
if (digitalRead(leftButtonPin) == 1 && leds_speed != 0 && millis() - millsPress > pressDelay)
{
millsPress = millis();
leds_speed = leds_speed - 5;
}
else if (digitalRead(rightButtonPin) == 1 && leds_speed != 100 && millis() - millsPress > pressDelay)
{
millsPress = millis();
leds_speed = leds_speed + 5;
}
lcd->print("1. Speed: " + String(leds_speed) + "% ");
break;
//——————————————————Brightness——————————————————//
case 1:
lcd->setCursor(0, 0);
if (digitalRead(leftButtonPin) == 1 && leds_brightness >= 0 && millis() - millsPress > pressDelay)
{
millsPress = millis();
leds_brightness = leds_brightness - 5;
}
else if (digitalRead(rightButtonPin) == 1 && leds_brightness <= 100 && millis() - millsPress > pressDelay)
{
millsPress = millis();
leds_brightness = leds_brightness + 5;
}
lcd->print("2. Bright: " + String(leds_brightness) + "% ");
break;
//——————————————————Modes——————————————————//
case 2:
lcd->setCursor(0, 0);
if (digitalRead(leftButtonPin) == 1 &&
led_selected_mode != 0 &&
millis() - millsPress > pressDelay)
{
millsPress = millis();
led_selected_mode = led_selected_mode - 1;
// hc_write(hc_leds);
}
else if (digitalRead(rightButtonPin) == 1 &&
led_selected_mode != 4 &&
millis() - millsPress > pressDelay)
{
millsPress = millis();
led_selected_mode = led_selected_mode + 1;
// hc_write(hc_leds);
}
lcd->print("3. Mode: " + led_modes[led_selected_mode]);
break;
/*
if (cursorPosition == 0)
{
cursorPosition = 3;
}
else
{
cursorPosition = (cursorPosition - 1) % 4;
}*/
//—————————————————— IDK ——————————————————//
case 3:
break;
}
}
// CURSOR BLINK !
cursorBlink();
}
void LcdMenu::drawMenu()
{
drawing = true;
int c = 0;
unsigned long millsSel;
// print option name based on cursor position
if (!selected)
{
switch (cursorPosition)
{
case 0:
lcd->clear();
lcd->setCursor(1, 0);
lcd->print("1. Speed");
break;
case 1:
lcd->clear();
lcd->setCursor(1, 0);
lcd->print("2. Brightness");
break;
case 2:
lcd->clear();
lcd->setCursor(1, 0);
lcd->print("3. Modes");
break;
case 3:
lcd->clear();
lcd->setCursor(1, 0);
lcd->print("4. Back");
break;
}
}
lcd->setCursor(0, 1);
for (int pos = 0; pos < 6; pos += 2)
{
if (cursorPosition * 2 == pos)
{ // draw inverted characted if currently selected, then
drawChar(pos, 1, c + 4);
}
else
{
drawChar(pos, 1, c);
}
if (c < 4)
{
c = c + 1;
}
}
// lcd->setCursor(14, 1);
// lcd->print(led_selected_mode);
drawing = false;
}
void LcdMenu::drawChar(int x, int y, int c)
{
// POSITION X , Y ------ custom character code 0-7
lcd->setCursor(x, y);
lcd->write(c);
}
void LcdMenu::cursorBlink()
{
if (millis() - millsBlink > blinkInterval)
{
millsBlink = millis();
if (cursorInverted)
{
drawChar(cursorPosition * 2, 1, cursorPosition + 4);
}
else
{
drawChar(cursorPosition * 2, 1, cursorPosition);
}
cursorInverted = !cursorInverted;
}
}