-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton-functions.ino
197 lines (173 loc) · 4.31 KB
/
button-functions.ino
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
void buttonInit()
{
// encBtnA.setDebounceTime(0);
// encBtnB.setDebounceTime(0);
btn1.setPressedHandler([](Button2 & b) {
DEBUG_PRINTLN("Button 1 SHORT click...");
b1();
});
btn2.setPressedHandler([](Button2 & b) {
DEBUG_PRINTLN("Button 2 SHORT click...");
b2();
});
btn3.setPressedHandler([](Button2 & b) {
DEBUG_PRINTLN("Button 3 SHORT click...");
b3();
});
// encoder push-button handler
encBtnP.setTapHandler([](Button2 & b) {
unsigned int time = b.wasPressedFor();
DEBUG_PRINTLN(time);
if (time > 3000) { // > 3sec enters config menu
DEBUG_PRINTLN("very long click ... do nothing");
}
else if (time > 600) {
DEBUG_PRINTLN("Encoder Button PUSH long click...");
// longer click while in select menu exit and restore prev mode
if (opMode == MODE_SELECT) {
DEBUG_PRINTLN("exit mode select menu");
DEBUG_PRINTMQTT("exit mode select menu");
opMode = prevMode;
}
else if (opMode == APP_CONFIG) {
DEBUG_PRINTLN("exit config (sub) mode");
DEBUG_PRINTMQTT("exit config sub menu");
// ... or shall we exit menu directly?
opMode = static_cast<opModes>(MODE_SELECT);
allLedsOff();
drawModeSelectMenu();
return;
}
else {
DEBUG_PRINTLN("enter mode select menu");
DEBUG_PRINTMQTT("enter mode select menu");
prevMode = opMode;
opMode = static_cast<opModes>(MODE_SELECT);
selectMode = prevMode;
allLedsOff();
drawModeSelectMenu();
return;
}
}
else {
DEBUG_PRINTLN("Button PUSH short click...");
// if we are in select menu then need short button click to enter/select that mode
if (opMode == MODE_SELECT) {
if (selectMode == APP_CONFIG) {
prevMode = opMode;
opMode = APP_CONFIG;
DEBUG_PRINTLN("enter app config menu");
DEBUG_PRINTMQTT("enter app config menu");
drawConfigMenu();
return;
}
opMode = selectMode;
DEBUG_PRINTF("select menu: choosing mode %s\n", mode2str(opMode));
DEBUG_PRINTMQTT("select menu, choosing a new opmode");
}
if (opMode == APP_CONFIG) {
// in config mode (brightness) and short click...
DEBUG_PRINTLN("in app config menu... short click -> exit");
DEBUG_PRINTMQTT("in app config menu... short click");
opMode = static_cast<opModes>(MODE_SELECT);
drawModeSelectMenu();
allLedsOff();
return;
}
if (opMode == DISCO) {
// we exit disco mode ... clear leds
DEBUG_PRINTLN("exit disco mode");
allLedsOff();
}
if (opMode == CLOCK) {
// we exit clock mode, reset flag
// so we get time again immediately after a mode change
showTimeNow = true;
}
toggleRed = true;
toggleYellow = true;
toggleGreen = true;
}
tft.fillScreen(TFT_BLACK);
isMqttAvailable = mqttClient.publish(mqttOpmode, mode2str(opMode), true);
drawTrafficLight(0);
drawModeText(opMode);
//allLedsOff();
DEBUG_PRINT("TRAFFIC_LIGHT_MODE: ");
DEBUG_PRINTLN(TRAFFIC_LIGHT_MODE);
});
}
void buttonLoop()
{
btn1.loop();
btn2.loop();
btn3.loop();
encBtnP.loop();
}
// FIXME: function names
// FIXME: draw simley on tft in mood mode
// FIXME: call drawTrafficLight from here instead of within fill functions?!
void b1() // RED Button
{
switch (opMode) {
case TRAFFIC: {
fillTopRed(toggleRed);
break;
}
case MOOD: {
drawSmiley(toggleRed ? HAPPY : NO_MOOD, MATRIX_POS_TOP);
break;
}
case TRAFFIC_MANUAL:
TRAFFIC_LIGHT_MODE += 1;
TRAFFIC_LIGHT_MODE %= _NUM_TRAFFIC_LIGHT_MODES_;
handleTrafficLight();
break;
case STARTAMPEL: // start
startSequence();
break;
case TRAFFIC_AUTO:
case DISCO:
case APP_CONFIG:
default:
break;
}
toggleRed = !toggleRed;
}
void b2() // YELLOW Button
{
switch (opMode) {
case TRAFFIC:
fillMiddleYellow(toggleYellow);
break;
case MOOD:
drawSmiley(toggleYellow ? NEUTRAL : NO_MOOD, MATRIX_POS_MIDDLE);
break;
default:
break;
}
toggleYellow = !toggleYellow;
}
void b3() // GREEN Button
{
switch (opMode) {
case TRAFFIC: {
fillBottomGreen(toggleGreen);
break;
}
case MOOD: {
drawSmiley(toggleGreen ? SAD : NO_MOOD, MATRIX_POS_BOTTOM);
break;
}
case STARTAMPEL: { // stop
unsigned long time = millis() - stopwatch;
drawTime(time);
DEBUG_PRINT("time: ");
DEBUG_PRINTLN(time);
break;
}
default:
break;
}
toggleGreen = !toggleGreen;
}