-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuni_config.cpp
210 lines (185 loc) · 5.27 KB
/
uni_config.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
// Persistent Configuration
#include "uni_config.h"
#include <string.h>
#include "uni_sd.h"
extern UniSd sd;
#define CONFIG_FILENAME "/config.txt"
UniConfig::UniConfig()
{
}
void UniConfig::setup() {
// read Config
if (!readConfig()) {
_loadedFromDefault = true;
// Default Config, as the config file is not found
_config.start = true;
_config.difficulty = 0;
_config.up = true;
_config.race_number = 0;
_config.bib_number_length = 3;
_config.start_line_countdown = false;
_config.finish_line_spacing = 500;
_config.mode = 1;
} else {
_loadedFromDefault = false;
}
}
// Return true if the config exists on disk
bool UniConfig::loadedFromDefault() {
return _loadedFromDefault;
}
char *UniConfig::filename() {
snprintf(_config.filename, FILENAME_MAX_LENGTH, "/race_%s_%s_%s_%d.txt",
_config.difficulty == 0 ? "Beginner" : _config.difficulty == 1 ? "Advanced" : "Expert",
_config.up ? "Up" : "Down",
_config.start ? "Start" : "Finish",
_config.race_number);
return _config.filename;
}
// Start/difficulty/up/race_number
void UniConfig::toggle_start() {
_config.start = !_config.start;
}
void UniConfig::increase_difficulty() {
_config.difficulty = (_config.difficulty + 1) % 3;
}
void UniConfig::toggle_up() {
_config.up = !_config.up;
}
void UniConfig::increment_race_number() {
_config.race_number = (_config.race_number + 1) % 10;
}
bool UniConfig::get_start() {
return _config.start;
}
int UniConfig::get_difficulty() {
return _config.difficulty;
}
bool UniConfig::get_up() {
return _config.up;
}
int UniConfig::get_race_number() {
return _config.race_number;
}
// bib_number_length
void UniConfig::toggle_bib_number_length() {
if (_config.bib_number_length == 3) {
_config.bib_number_length = 4;
} else {
_config.bib_number_length = 3;
}
}
int UniConfig::get_bib_number_length() {
return _config.bib_number_length;
}
// start_line_countdown
void UniConfig::toggle_start_line_countdown() {
_config.start_line_countdown = !_config.start_line_countdown;
}
bool UniConfig::get_start_line_countdown() {
return _config.start_line_countdown;
}
// finish_line_spacing
void UniConfig::reset_finish_line_spacing() {
_config.finish_line_spacing = 500;
}
void UniConfig::increment_finish_line_spacing(int ms) {
_config.finish_line_spacing = (_config.finish_line_spacing + ms) % 1000;
}
int UniConfig::get_finish_line_spacing() {
return _config.finish_line_spacing;
}
// mode
int UniConfig::mode() {
return _config.mode;
}
void UniConfig::setMode(int mode) {
_config.mode = mode;
}
/* ******************* PRIVATE METHODS ******************* */
// Return true if the string starts with prefix
bool UniConfig::prefix(const char *str, const char *prefix)
{
return strncmp(prefix, str, strlen(prefix)) == 0;
}
// Return the value after the prefix
char *UniConfig::value(const char *str, const char *prefix)
{
char *result = (char *)str;
return result + strlen(prefix);
}
/* TODO, Refactor this into a condensed format?
[
[&_config.start, "START:", "bool"],
[&_config.difficulty, "DIFFICULTY:", "int"],
etc
]
*/
// Read the config, return true on success
bool UniConfig::readConfig() {
int max_config_string = 100;
char data_string[max_config_string];
if (sd.readFile(CONFIG_FILENAME, data_string, max_config_string)) {
char *token;
token = strtok(data_string, "\n");
while(token != NULL) {
if (prefix(token, "START:")) {
_config.start = atoi(value(token, "START:")) == 1;
} else if (prefix(token, "DIFF:")) {
_config.difficulty = atoi(value(token, "DIFF:"));
} else if (prefix(token, "UP:")) {
_config.up = atoi(value(token, "UP:")) == 1;
} else if (prefix(token, "RACE:")) {
_config.race_number = atoi(value(token, "RACE:"));
} else if (prefix(token, "BIB_DIGITS:")) {
_config.bib_number_length = atoi(value(token, "BIB_DIGITS:"));
} else if (prefix(token, "COUNTDOWN:")) {
_config.start_line_countdown = atoi(value(token, "COUNTDOWN:")) == 1;
} else if (prefix(token, "SPACING:")) {
_config.finish_line_spacing = atoi(value(token, "SPACING:"));
} else if (prefix(token, "MODE:")) {
_config.mode = atoi(value(token, "MODE:"));
}
Serial.println("Got Config: ");
Serial.println(token);
token = strtok(NULL, "\n"); // Next token
}
return true;
} else {
return false;
}
}
// Writes the configuration to the SD Card
// the format is:
// config_name|configuration value
bool UniConfig::writeConfig() {
int max_config_string = 100;
char data_string[max_config_string];
snprintf(data_string, max_config_string,
"%s%d\n"
"%s%d\n"
"%s%d\n"
"%s%d\n"
"%s%d\n"
"%s%d\n"
"%s%d\n"
"%s%d\n",
"START:", _config.start ? 1 : 0,
"DIFF:", _config.difficulty,
"UP:", _config.up ? 1 : 0,
"RACE:", _config.race_number,
"BIB_DIGITS:", _config.bib_number_length,
"COUNTDOWN:", _config.start_line_countdown ? 1 : 0,
"SPACING:", _config.finish_line_spacing,
"MODE:", _config.mode
);
sd.clearFile(CONFIG_FILENAME);
if (sd.writeFile(CONFIG_FILENAME, data_string)) {
Serial.println("Write File");
Serial.println(data_string);
return true;
} else {
Serial.println("Failed to write config file");
return false;
}
}