-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.cpp
More file actions
400 lines (363 loc) · 12.3 KB
/
Options.cpp
File metadata and controls
400 lines (363 loc) · 12.3 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "Options.h"
Option::Option(OptionType type, const char* text) {
// text is assumed to be a correct value
size_t i;
for(i = 0; text[i]; ++i) {
this->text[i] = text[i];
}
this->text[i] = '\0';
this->inFocus = false;
this->type = type;
}
MenuOption::MenuOption(const char* text, Menu* (*createMenu)(void)) : Option(menuTransition, text),
createMenu(createMenu) {
}
MenuOption::MenuOption(const MenuOption& other) {
this->type = other.type;
int i;
for(i = 0; other.text[i]; ++i) {
this->text[i] = other.text[i];
}
this->text[i] = '\0';
this->createMenu = other.createMenu;
}
MenuOption& MenuOption::operator=(const MenuOption& other) {
this->type = other.type;
int i;
for(i = 0; other.text[i]; ++i) {
this->text[i] = other.text[i];
}
this->text[i] = '\0';
this->createMenu = other.createMenu;
return *this;
}
void MenuOption::focus(Menu** currentMenu) {
Menu* oldMenu = *currentMenu;
(*currentMenu)->clear();
*currentMenu = this->createMenu();
/**
* @brief
* So obviously, in a regular environment this would not work. We are essentially freeing this option with the freeOptions call.
* However, since Arduino doesn't have an OS and therefore there's no other process running, the free operation will not actually
* result in they bytes being changed to anything, so the program keeps on executing them, even if they aren't technically part of the
* address space anymore.
* This might be unstable if Arduino has illegal memory access errors, but it doesn't seem to have any.
*/
oldMenu->freeOptions();
delete oldMenu;
// this->unfocus();
}
void MenuOption::getTextValue(char* writeHere) {
size_t i;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
writeHere[i] = '\0';
}
SystemOption::SystemOption(const char* text, byte pin, byte baseValue, byte currentValue, byte stepValue, void (*eepromUpdate)(byte)) : Option(sysValue, text), pin(pin),
baseValue(baseValue), stepValue(stepValue), currentValue(currentValue), eepromUpdate(eepromUpdate) {
}
/// it's assumed xVal and yVal are either -1, 0 or 1
void SystemOption::joystickInput(int xVal, int yVal, Menu* currentMenu) {
if(yVal == -1) {
if(this->currentValue == this->baseValue + this->stepValue * 5) // maximum possible values
return;
this->currentValue += this->stepValue;
analogWrite(this->pin, this->currentValue);
currentMenu->updateOptionValue(this);
}
else if(yVal == 1) {
if(this->currentValue == this->baseValue - this->stepValue * 5)
return;
this->currentValue -= this->stepValue;
analogWrite(this->pin, this->currentValue);
currentMenu->updateOptionValue(this);
}
}
void SystemOption::getTextValue(char* writeHere) {
size_t i = 0;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
char number[20];
itoa(5 - (baseValue - currentValue) / stepValue, number, 10);
for(size_t j = 0; number[j]; ++j) {
if(5 - (baseValue - currentValue) / stepValue < 10 && j == 0) {
writeHere[i++] = ' ';
}
writeHere[i++] = number[j];
}
writeHere[i++] = '\n';
writeHere[i] = '\0';
}
LEDOption::LEDOption(const char* text, LedControl* lc, byte brightValue) : Option(ledValue, text), lc(lc),
brightValue(brightValue) { }
void LEDOption::joystickInput(int xVal, int yVal, Menu* currentMenu) {
if(yVal == -1) {
if(this->brightValue == 15)
return;
this->brightValue += 1;
this->updateMatrix();
currentMenu->updateOptionValue(this);
}
else if(yVal == 1) {
if(this->brightValue == 0)
return;
this->brightValue -= 1;
this->updateMatrix();
currentMenu->updateOptionValue(this);
}
}
void LEDOption::updateMatrix() {
this->lc->setIntensity(0, this->brightValue);
for(int i = 0; i < 8; ++i) {
this->lc->setRow(0, i, B11111111);
}
}
void LEDOption::getTextValue(char* writeHere) {
size_t i = 0;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
char number[20];
itoa(this->brightValue, number, 10);
for(size_t j = 0; number[j]; ++j) {
if(this->brightValue < 10 && j == 0) {
writeHere[i++] = ' ';
}
writeHere[i++] = number[j];
}
writeHere[i++] = '\n';
writeHere[i] = '\0';
}
GameOption::GameOption(const char* text, int* valAddr, byte minVal, byte maxVal, byte currentValue, OptionType type) : Option(type, text), valAddr(valAddr), minVal(minVal),
maxVal(maxVal), currentValue(currentValue) { }
void GameOption::joystickInput(int xVal, int yVal, Menu* currentMenu) {
if(yVal == -1) {
if(this->currentValue == this->maxVal) {// maximum possible values
this->currentValue = this->minVal;
}
else {
this->currentValue += 1;
}
if(this->type == gameValue) {
*this->valAddr = this->currentValue;
}
else if(this->type == volumeOption) {
SoundsManager::changeVolume(this->transformVolume());
}
else if(this->type == themeOption) {
SoundsManager::changeInGame(this->currentValue);
SoundsManager::switchMenuState(false);
SoundsManager::playTheme();
}
else if(this->type == soundOption) {
SoundsManager::setSounds(this->currentValue);
}
currentMenu->updateOptionValue(this);
}
else if(yVal == 1) {
if(this->currentValue == this->minVal) {// maximum possible values
this->currentValue = this->maxVal;
}
else {
this->currentValue -= 1;
}
if(this->type == gameValue) {
*this->valAddr = this->currentValue;
}
else if(this->type == volumeOption) {
SoundsManager::changeVolume(this->transformVolume());
}
else if(this->type == themeOption) {
SoundsManager::changeInGame(this->currentValue);
SoundsManager::switchMenuState(false);
SoundsManager::playTheme();
}
else if(this->type == soundOption) {
SoundsManager::setSounds(this->currentValue);
}
currentMenu->updateOptionValue(this);
}
}
void GameOption::unfocus() {
this->inFocus = false;
if(this->type == gameValue) {
RWHelper::writeByte(DIFF_ADDR, this->currentValue);
}
else if(this->type == volumeOption) {
RWHelper::writeByte(RWHelper::volAddr, this->currentValue);
}
else if(this->type == themeOption) {
SoundsManager::switchMenuState(true);
SoundsManager::playTheme();
RWHelper::writeByte(RWHelper::themeAddr, this->currentValue);
}
else if(this->type == soundOption) {
RWHelper::writeByte(RWHelper::soundAddr, this->currentValue);
}
}
void GameOption::getTextValue(char* writeHere) {
size_t i = 0;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
if(this->type != soundOption && this->type != gameValue) {
char number[20];
itoa(this->currentValue, number, 10);
for(size_t j = 0; number[j]; ++j) {
if(this->currentValue < 10 && j == 0) {
writeHere[i++] = ' ';
}
writeHere[i++] = number[j];
}
}
else if(this->type == soundOption) {
writeHere[i++] = ' ';
if(this->currentValue) {
writeHere[i++] = 'O';
writeHere[i++] = 'N';
writeHere[i++] = ' ';
}
else {
writeHere[i++] = 'O';
writeHere[i++] = 'F';
writeHere[i++] = 'F';
}
}
else if(this->type == gameValue) {
writeHere[i++] = ' ';
if(this->currentValue == NORMAL) {
writeHere[i++] = 'N';
writeHere[i++] = 'O';
writeHere[i++] = 'R';
writeHere[i++] = 'M';
writeHere[i++] = 'A';
writeHere[i++] = 'L';
}
else if(this->currentValue == HARD) {
writeHere[i++] = 'H';
writeHere[i++] = 'A';
writeHere[i++] = 'R';
writeHere[i++] = 'D';
writeHere[i++] = ' ';
writeHere[i++] = ' ';
}
}
writeHere[i++] = '\n';
writeHere[i] = '\0';
}
const char NameOption::alphabet[ALPH_SIZE] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.', '!', ' '};
NameOption::NameOption(const char* text, int score, Menu* (*nextMenu)(void)) : Option(nameOption, text), score(score),
crChar(0), crIndex(10), updatedIndex(false), nextMenu(nextMenu), lastFlash(millis()), flashState(true) {
for(byte j = 0; j < NAME_LIMIT; ++j) {
this->vals[j] = 10;
}
}
void NameOption::focus(Menu** currentMenu) {
this->inFocus = true;
this->currentMenu = currentMenu;
}
void NameOption::unfocus() {
this->name[this->crChar] = NameOption::alphabet[this->crIndex];
this->crIndex = this->vals[++crChar];
if(crChar == NAME_LIMIT) {
this->inFocus = false;
this->name[NAME_LIMIT] == '\0';
RWHelper::writeHigh(this->score, this->name);
Menu* oldMenu = *currentMenu;
(*currentMenu)->clear();
*currentMenu = this->nextMenu();
oldMenu->freeOptions();
delete oldMenu;
}
}
void NameOption::joystickInput(int xVal, int yVal, Menu* currentMenu) {
if(yVal == -1) {
this->updatedIndex = true;
this->flashState = true;
this->lastFlash = millis();
if(this->crIndex > 0) {
this->crIndex--;
}
else {
this->crIndex = ALPH_SIZE - 1;
}
this->vals[crChar] = this->crIndex;
}
else if(yVal == 1) {
this->updatedIndex = true;
this->flashState = true;
this->lastFlash = millis();
if(this->crIndex < ALPH_SIZE - 1) {
this->crIndex++;
}
else {
this->crIndex = 0;
}
this->vals[crChar] = this->crIndex;
}
}
bool NameOption::changedChar() {
bool retval = this->updatedIndex;
this->updatedIndex = false;
return retval;
}
void NameOption::getTextValue(char* writeHere) {
size_t i = 0;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
for(byte j = 0; j < NAME_LIMIT; ++j) {
if(j != this->crChar) {
writeHere[i++] = NameOption::alphabet[this->vals[j]];
}
else {
if(millis() - this->lastFlash >= LETTER_FLASH) {
this->flashState = !this->flashState;
this->lastFlash = millis();
}
if(this->flashState || this->inFocus == false) {
writeHere[i++] = NameOption::alphabet[this->vals[j]];
}
else {
writeHere[i++] = ' ';
}
}
}
writeHere[i++] = '\n'; // name option will always be treated as last name option
writeHere[i] = '\0';
}
DisplayOption::DisplayOption(const char* text, int* value, Menu* currentMenu) : Option(valueDisplay, text), value(value), oldValue(*value), currentMenu(currentMenu),
lastChecked(0) {}
const byte DisplayOption::checkInterval = 10;
void DisplayOption::checkValue() {
if(millis() - DisplayOption::lastChecked >= DisplayOption::checkInterval) {
if(this->oldValue != (*this->value)) {
this->oldValue = *this->value;
this->currentMenu->updateOptionValue(this);
}
this->lastChecked = millis();
}
}
void DisplayOption::getTextValue(char* writeHere) {
size_t i = 0;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
char number[20];
itoa((*this->value), number, 10);
for(size_t j = 0; number[j]; ++j) {
writeHere[i++] = number[j];
}
writeHere[i++] = '\n';
writeHere[i] = '\0';
}
GreetingOption::GreetingOption(const char* text) : Option(greeting, text) {}
void GreetingOption::getTextValue(char* writeHere) {
size_t i = 0;
for(i = 0; this->text[i]; ++i){
writeHere[i] = this->text[i];
}
writeHere[i] = '\0';
}