-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
348 lines (279 loc) · 8.64 KB
/
config.cpp
File metadata and controls
348 lines (279 loc) · 8.64 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
/**
* @file config.cpp
* @brief Functions for loading, saving and modifying the config file
*/
#include "config.h"
#include "interface.h"
#include "servo.h"
#include "show.h"
#include <SD.h>
#define CONF_BYTE_SIZE 0x30F
char configFile[8] = "FIG.CFG";
File CONFIG_FILE;
uint8_t conf[CONF_BYTE_SIZE + 1] = {};
void loadConfig() {
if (SD.exists(configFile)) {
CONFIG_FILE = SD.open(configFile);
if (CONFIG_FILE) {
while (CONFIG_FILE.available()) {
CONFIG_FILE.read(conf, sizeof(conf));
}
CONFIG_FILE.close();
} else {
Serial.print("Error opening: ");
Serial.println(configFile);
}
} else {
Serial.print("File ");
Serial.print(configFile);
Serial.println(" does not exist");
}
}
void saveConfig() {
if (SD.exists(configFile)) {
Serial.print("Show ");
Serial.print(configFile);
Serial.print(" already exists. Overwrite? 'y' or 'n' ");
switch (getChar()) {
case 'y':
SD.remove(configFile);
break;
default:
return;
break;
}
}
CONFIG_FILE = SD.open(configFile, FILE_WRITE);
if (CONFIG_FILE) {
CONFIG_FILE.write(conf, sizeof(conf));
CONFIG_FILE.close();
setupServos();
} else {
Serial.print("Error opening: ");
Serial.println(configFile);
}
}
char* getFigureName() {
static char name[17];
memset(name, 0, sizeof name);
for (uint8_t c = 0; c <= 16; c++) {
if (conf[c] != 0x00) {
name[c] = conf[c];
}
}
return name;
}
char* getInputName(uint8_t number) {
uint16_t inputBase = 0x108 + (number * 0x10);
static char name[9];
memset(name, 0, sizeof name);
for (uint8_t c = 0; c <= 8; c++) {
if (conf[inputBase + c] != 0x00) {
name[c] = conf[inputBase + c];
}
}
return name;
}
void setInputName(uint8_t number, char* name) {
uint16_t inputBase = 0x108 + (number * 0x10);
for (uint8_t c = 0; c < 8; c++) {
if (name[c] != 0x00) {
conf[inputBase + c] = name[c];
} else {
conf[inputBase + c] = 0x00;
}
}
}
char* getServoName(uint8_t number) {
uint16_t servoBase = 0x208 + (number * 0x10);
static char name[9];
memset(name, 0, sizeof name);
for (uint8_t c = 0; c <= 8; c++) {
if (conf[servoBase + c] != 0x00) {
name[c] = conf[servoBase + c];
}
}
return name;
}
void setServoName(uint8_t number, char* name) {
uint16_t servoBase = 0x208 + (number * 0x10);
for (uint8_t c = 0; c < 8; c++) {
if (name[c] != 0x00) {
conf[servoBase + c] = name[c];
} else {
conf[servoBase + c] = 0x00;
}
}
}
input_t getInputData(uint8_t number) {
input_t i;
uint16_t inputBase = 0x100 + (number * 0x10);
i.enabled = conf[inputBase];
i.pin = conf[inputBase + 1];
i.min = (conf[inputBase + 3] << 8) + conf[inputBase + 2];
i.max = (conf[inputBase + 5] << 8) + conf[inputBase + 4];
i.value = 0;
return i;
}
servo_t getServoData(uint8_t number) {
servo_t s;
uint16_t servoBase = 0x200 + (number * 0x10);
uint16_t servoFilterBase = 0x300 + number;
s.enabled = conf[servoBase];
s.pin = conf[servoBase + 1];
s.min = (conf[servoBase + 3] << 8) + conf[servoBase + 2];
s.max = (conf[servoBase + 5] << 8) + conf[servoBase + 4];
s.input = getInputData(conf[servoBase + 6]);
s.invert = conf[servoBase + 7];
s.value = 0;
s.filter = conf[servoFilterBase];
return s;
}
uint16_t getServoCenter(uint8_t number) {
uint16_t servoBase = 0x200 + (number * 0x10);
uint16_t min = (conf[servoBase + 3] << 8) + conf[servoBase + 2];
uint16_t max = (conf[servoBase + 5] << 8) + conf[servoBase + 4];
uint16_t center = ((max - min) / 2) + min;
return center;
}
uint16_t* minmaxInput(uint8_t input) {
uint16_t inputBase = 0x100 + (input * 0x10);
uint16_t potValue = analogRead(conf[inputBase + 1]);
uint16_t potValuePrev = potValue;
uint16_t potMin = potValue;
uint16_t potMax = potValue;
uint32_t millisNow = 0;
uint32_t millisPrev = 0;
while (Serial.available() <= 0) {
millisNow = millis();
if (millisNow - millisPrev >= 40) {
millisPrev = millisNow;
potValue = analogRead(conf[inputBase + 1]);
if (potValue != potValuePrev) {
potValuePrev = potValue;
if (potValue < potMin) {
potMin = potValue;
}
if (potValue > potMax) {
potMax = potValue;
}
Serial.print("Min: ");
Serial.print(potMin);
Serial.print(" | Max: ");
Serial.println(potMax);
}
}
}
while (Serial.available() > 0) {
Serial.read();
}
static uint16_t values[2] = {potMin, potMax};
return values;
}
void configInput(uint8_t number) {
uint16_t inputBase = 0x100 + (number * 0x10);
uint8_t teensyPins[16] = {0, 1, 2, 3, 6, 7, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
Serial.print("---- Configure Input #");
Serial.print(number);
Serial.println(" ----");
Serial.println(getInputName(number));
conf[inputBase + 1] = teensyPins[number]; // Pin
Serial.print("\nEnabled 0-1: ");
conf[inputBase] = getInt(); // Enabled
Serial.println("Set Min / Max for input, 'a' to accept...");
uint16_t* MinMax = minmaxInput(conf[inputBase + 1]);
uint16_t potMin = MinMax[0];
uint16_t potMax = MinMax[1];
conf[inputBase + 3] = (potMin & 0xFF00) >> 8; // Min High
conf[inputBase + 2] = (potMin & 0x00FF); // Min Low
conf[inputBase + 5] = (potMax & 0xFF00) >> 8; // Max High
conf[inputBase + 4] = (potMax & 0x00FF); // Max Low
processInputs();
}
void configServo(uint8_t number) {
uint16_t servoBase = 0x200 + (number * 0x10);
Serial.print("\n---- Configure Servo #");
Serial.print(number);
Serial.println(" ----");
Serial.println(getServoName(number));
conf[servoBase + 1] = number; // Pin
Serial.print("\nEnabled 0-1: ");
conf[servoBase] = getInt(); // Enabled
printInputs();
Serial.print("Which input would you like to use 0-15: ");
conf[servoBase + 6] = getInt(); // Input
Serial.println(getInputName(conf[servoBase + 6]));
Serial.print("Invert 0-1: ");
conf[servoBase + 7] = getInt(); // Invert
Serial.println("\n\nSet Min for servo, 'a' to accept...");
uint16_t min = minmaxServo(conf[servoBase + 6], conf[servoBase + 1]);
conf[servoBase + 3] = (min & 0xFF00) >> 8; // Min High
conf[servoBase + 2] = (min & 0x00FF); // Min Low
Serial.println("\n\nSet Max for servo, 'a' to accept...");
uint16_t max = minmaxServo(conf[servoBase + 6], conf[servoBase + 1]);
conf[servoBase + 5] = (max & 0xFF00) >> 8; // Max High
conf[servoBase + 4] = (max & 0x00FF); // Max Low
processServos();
}
void invertServo(uint8_t number) {
uint16_t servoBase = 0x200 + (number * 0x10);
uint8_t value = conf[servoBase + 7];
if (value == 1) {
conf[servoBase + 7] = 0;
} else {
conf[servoBase + 7] = 1;
}
Serial.print("Servo #");
Serial.print(number);
Serial.println(" inverted");
}
void filterServo(uint8_t number) {
uint16_t servoFilterBase = 0x300 + number;
uint8_t currentValue = conf[servoFilterBase];
Serial.print("---- Configure Servo #");
Serial.print(number);
Serial.println(" Filter ----");
Serial.println(getServoName(number));
Serial.print("Current filter value: ");
Serial.println(currentValue);
Serial.print("Set new filter value: ");
uint8_t newValue = getInt();
conf[servoFilterBase] = newValue;
}
void toggleServo(uint8_t number) {
uint16_t servoBase = 0x200 + (number * 0x10);
uint8_t value = conf[servoBase];
Serial.print("Servo #");
Serial.print(number);
if (value == 1) {
conf[servoBase + 7] = 0;
Serial.println(" disabled");
} else {
conf[servoBase + 7] = 1;
Serial.println(" enabled");
}
}
void printServos() {
Serial.println();
for (int s = 0; s < 16; s++ ) {
uint16_t servoBase = 0x200 + (s * 0x10);
uint8_t enabled = conf[servoBase];
if (enabled) {
Serial.print(s);
Serial.print(": ");
Serial.println(getServoName(s));
}
}
}
void printInputs() {
Serial.println();
for (int i = 0; i < 16; i++ ) {
uint16_t inputBase = 0x100 + (i * 0x10);
uint8_t enabled = conf[inputBase];
if (enabled) {
Serial.print(i);
Serial.print(": ");
Serial.println(getInputName(i));
}
}
}