-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
235 lines (205 loc) · 6.96 KB
/
menu.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
#include "menu.h"
#include "names.h"
#include "display.h"
#include "simulation.h"
typedef struct {
float step;
float min;
float max;
CRGB::HTMLColorCode color;
const bool *name;
CRGB::HTMLColorCode name_color;
} ParamSetting;
const ParamSetting parameters[] PROGMEM = {
{0.01, 0.0, 1.0, CRGB::DarkBlue, name_diffA, CRGB::DimGray},
{0.001, 0.0, 0.1, CRGB::DarkBlue, name_feedA, CRGB::DimGray},
{0.01, 0.0, 0.25, CRGB::DarkBlue, name_dripA, CRGB::DimGray},
{0.01, 0.0, 1.0, CRGB::DarkRed, name_diffB, CRGB::DimGray},
{0.001, 0.0, 0.1, CRGB::DarkRed, name_killB, CRGB::DimGray},
{0.01, 0.0, 0.25, CRGB::DarkRed, name_dripB, CRGB::DimGray}
};
#define PARAM_COUNT (sizeof parameters / sizeof(ParamSetting))
fixed *values[] = { &diffA, &feedA, &dripA, &diffB, &killB, &dripB };
fixed paramValue(int16_t analog, float divisor = 1023.0, float min = 0.0, float max = 1.0) {
return constrain(float(analog) / divisor, min, max);
}
void printParam(fixed value, uint8_t width = 7, uint8_t prec = 4) {
char output[8];
dtostrf(float(value), min(width, 7), min(prec, width), output);
Serial.print(output);
}
void initMenu() {
pinMode(ENTER_PIN, INPUT_PULLUP);
pinMode(PREV_PIN, INPUT_PULLUP);
pinMode(NEXT_PIN, INPUT_PULLUP);
Serial.print(F("Start:")); printParam(feedA); printParam(killB); Serial.print(F("\n"));
}
bool isVisible = false;
bool showsValue = false;
bool needsUpdate = false;
// Current menu item, where PARAM_COUNT means (virtual) exist item
uint8_t itemNumber = 0;
ParamSetting itemSettings;
void activateItem(uint8_t index) {
itemNumber = index;
if (itemNumber < PARAM_COUNT) memcpy_P(&itemSettings, ¶meters[itemNumber], sizeof(ParamSetting));
}
void showMenu(unsigned long now) {
isVisible = true;
needsUpdate = true;
activateItem(0);
}
void drawMenu(unsigned long now) {
if (!isVisible) return;
if (showsValue && itemNumber < PARAM_COUNT) {
// Parameter value
drawValue(*values[itemNumber], itemSettings.min, itemSettings.max, itemSettings.color);
} else if (!showsValue && itemNumber < PARAM_COUNT) {
// Parameter item
drawName(itemSettings.name, itemSettings.name_color);
} else if (!showsValue) {
// Virtual exit item
drawName(name_close, CRGB::DarkOrchid);
}
}
void hideMenu(unsigned long now) {
isVisible = false;
saveParameters();
}
void showValue(unsigned long now) {
showsValue = true;
needsUpdate = true;
}
void hideValue(unsigned long now) {
showsValue = false;
needsUpdate = true;
}
void updateBrightness(int delta) {
auto brightness = getBrightness() + delta;
Serial.print(F("BRIGHTNESS: ")); Serial.println(brightness);
setBrightness(brightness);
}
void handleEnter(unsigned long now, bool longPress = false) {
if (longPress) return;
if (!isVisible) {
showMenu(now);
return;
}
if (showsValue) {
hideValue(now);
} else {
if (itemNumber < PARAM_COUNT) {
showValue(now);
} else {
hideMenu(now);
}
}
}
void handlePrev(unsigned long now, bool longPress = false) {
if (!isVisible) {
updateBrightness(-8);
} else if (showsValue) {
fixed value = *values[itemNumber] - itemSettings.step;
*values[itemNumber] = constrain(value, itemSettings.min, itemSettings.max);
} else if (itemNumber == 0) {
activateItem(PARAM_COUNT);
} else {
activateItem(itemNumber - 1);
}
needsUpdate = true;
}
void handleNext(unsigned long now, bool longPress = false) {
if (!isVisible) {
updateBrightness(+8);
} else if (showsValue) {
fixed value = *values[itemNumber] + itemSettings.step;
*values[itemNumber] = constrain(value, itemSettings.min, itemSettings.max);
} else if (itemNumber < PARAM_COUNT) {
activateItem(itemNumber + 1);
} else {
activateItem(0);
}
needsUpdate = true;
}
#define ENTER_BUTTON 0
#define PREV_BUTTON 1
#define NEXT_BUTTON 2
#define DEBOUNCE_DELAY 50
#define REPEAT_DELAY 500
#define REPEAT_PAUSE 250
bool loopMenu(unsigned long now) {
static const uint8_t pins[] = { ENTER_PIN, PREV_PIN, NEXT_PIN };
static bool down[] = { false, false, false };
static unsigned long time[] = { 0, 0, 0 };
static unsigned long repeat[] = { 0, 0, 0 };
static bool repeating[] = { false, false, false };
bool done = false;
for (uint8_t idx = 0; idx < 3; idx++) {
bool button = (digitalRead(pins[idx]) == LOW) ? true : false;
if (button == down[idx]) {
time[idx] = now;
if (button) {
if (repeat[idx] == 0) {
// Start repeating
repeat[idx] = now;
} else if (now - repeat[idx] > (repeating[idx] ? REPEAT_PAUSE : REPEAT_DELAY)) {
// Perform repeat after delay
switch (idx) {
case 0:
if (!repeating[idx]) {
resetSimulation();
} else {
// Serial.println(F("REPEAT ENTER"));
handleEnter(now, true);
}
break;
case 1:
// Serial.println(F("REPEAT PREV"));
handlePrev(now, true);
break;
case 2:
// Serial.println(F("REPEAT NEXT"));
handleNext(now, true);
break;
}
repeat[idx] = 0;
repeating[idx] = true;
}
}
} else if (now - time[idx] > DEBOUNCE_DELAY) {
if (button && !down[idx]) {
// Button down
down[idx] = true;
} else if (!button && down[idx]) {
down[idx] = false;
if (!repeating[idx]) {
// Handle button on up
switch (idx) {
case 0:
// Serial.println(F("ENTER"));
handleEnter(now);
done = true;
break;
case 1:
// Serial.println(F("PREV"));
handlePrev(now);
done = true;
break;
case 2:
// Serial.println(F("NEXT"));
handleNext(now);
done = true;
break;
}
}
repeat[idx] = 0;
repeating[idx] = false;
}
}
}
if (isVisible && needsUpdate) {
needsUpdate = false;
drawMenu(now);
}
return isVisible || done;
}