-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
204 lines (161 loc) · 3.78 KB
/
main.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
#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "SPI.h"
#include "SD.h"
#include "constants.h"
MCUFRIEND_kbv tft;
// colors of the squares
uint16_t c1;
uint16_t c2;
uint16_t c3;
void no_config() {
// print the error on the Serial Monitor
Serial.println("Config not found");
// set the text color, size and location and print the error on the display
tft.setCursor(0, 0);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("Config not found");
while (1);
}
void invalid_config() {
// print the error on the Serial Monitor
Serial.println("Config in Improper Format");
// set the text color, size and location and print the error on the display
tft.setCursor(0, 0);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("Config in Improper Format");
while (1);
}
void parse_config(File *cfg) {
// store the key and value on the current line
char key[32];
char value[32];
// add null-termination
key[31] = 0;
value[31] = 0;
for (;;) {
// skip till the start of the key
for (;;) {
int c = cfg->read();
if (c == -1) {
// there is no property, so return;
return;
}
else if (c == '"') {
break;
}
}
// read the key name
for (int i = 0; i < 31; ++i) {
int c = cfg->read();
if (c == -1) {
invalid_config();
}
else if (c == '"') {
key[i] = 0;
break;
}
key[i] = (char)c;
}
// skip till the equals sign
for (;;) {
int c = cfg->read();
if (c == -1) {
invalid_config();
}
else if (c == '=') {
break;
}
}
// skip till the start of the value
for (;;) {
int c = cfg->read();
if (c == -1) {
invalid_config();
}
else if (c == '"') {
break;
}
}
// read the value name
for (int i = 0; i < 31; ++i) {
int c = cfg->read();
if (c == -1) {
invalid_config();
}
else if (c == '"') {
value[i] = 0;
break;
}
value[i] = (char)c;
}
// print the key and value on the Serial Monitor for debugging
Serial.print(key);
Serial.print(" : ");
Serial.println(value);
if (strncmp(key, "square-1-color", 32) == 0) {
c1 = 0;
for (int i = 0; value[i] != 0; ++i) {
if ('0' <= value[i] && value[i] <= '9') {
c1 = (c1 * 16) + value[i] - '0';
}
else {
value[i] |= 32;
if ('a' <= value[i] && value[i] <= 'f') {
c1 = (c1 * 16) + value[i] - 'a' + 10;
}
}
}
}
else if (strncmp(key, "square-2-color", 32) == 0) {
c2 = 0;
for (int i = 0; value[i] != 0; ++i) {
if ('0' <= value[i] && value[i] <= '9') {
c2 = (c2 * 16) + value[i] - '0';
}
else {
value[i] |= 32;
if ('a' <= value[i] && value[i] <= 'f') {
c2 = (c2 * 16) + value[i] - 'a' + 10;
}
}
}
}
else if (strncmp(key, "square-3-color", 32) == 0) {
c3 = 0;
for (int i = 0; value[i] != 0; ++i) {
if ('0' <= value[i] && value[i] <= '9') {
c3 = (c3 * 16) + value[i] - '0';
}
else {
value[i] |= 32;
if ('a' <= value[i] && value[i] <= 'f') {
c3 = (c3 * 16) + value[i] - 'a' + 10;
}
}
}
}
}
}
void setup() {
Serial.begin(9600);
tft.begin(0x9486);
tft.fillScreen(BLACK);
if (!SD.begin(10)) {
no_config();
}
File cfg = SD.open("CONFIG.TXT", FILE_READ);
if (!cfg) {
no_config();
}
parse_config(&cfg);
cfg.close();
tft.fillRect(100, 20, 120, 120, c1);
tft.fillRect(100, 180, 120, 120, c2);
tft.fillRect(100, 340, 120, 120, c3);
}
void loop() {
}