-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd.h
158 lines (123 loc) · 3.64 KB
/
sd.h
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
#include "PF.h"
FATFS fs;
DIR dir;
FILINFO fInfo;
int8 lineLen;
int8 initSD() {
// pinMode(10, OUTPUT);
// digitalWrite(10, true); // Disable Ethernet
return PF.begin(&fs);
}
char readChar() {
char line[1];
unsigned int bytesRead = 0;
int res = PF.readFile(line, 1, &bytesRead);
return line[0];
}
int8 loadStratList() {
int8 res = PF.openDirectory(&dir, bot.c_str());
if (res > 0) return res;
fInfo.fname[0] = 1;
stCount = 0;
while(fInfo.fname[0] != 0 && res == 0) {
res = PF.readDirectory(&dir, &fInfo);
if (fInfo.fname[0] > 1) {
strats[stCount] = fInfo.fname;
} else break;
stCount++;
if (stCount == MAX_STRATS) break;
}
if (res > 0) {
// Read error
return res;
}
return res;
}
int8 loadStrat(int8 index) {
String path = String(bot + "/" + strats[index]);
int8 res = PF.open(path.c_str());
if (res > 0) return res;
char ch = 0;
String num = "";
String varName = "";
String varValue = "";
int8 varTyp = 2;
int8 state = 0;
int8 varCount = 0;
int lineLn = 0;
while(ch != END_CHAR) {
ch = readChar();
if (ch != '\n') lineLn++;
if (ch == SKIP_CHAR) continue;
else if (ch == ' ') state++;
else if (ch == '\n') state = 2;
if (state == 0) varName += ch;
else if (state == 1) {
varValue += ch;
if (ch == 'T' || ch == 'F' || ch == 'f' || ch == 't') varTyp = 1;
if (ch == '.') varTyp = 3;
}
else if (state == 2) {
lineLen = lineLn;
lineLn = 0;
varNames[varCount] = varName;
varType[varCount] = varTyp;
if (varTyp == 1) {
if (varValue.c_str()[1] == 't' || varValue.c_str()[1] == 'T') varVals[varCount] = 1;
else varVals[varCount] = 0;
} else if (varTyp == 2) {
varVals[varCount] = (long) varValue.toInt();
} else if (varTyp == 3) {
float temp = varValue.toFloat();
memcpy(&varVals[varCount], &temp, sizeof(temp));
}
varCount++;
state = 0;
num = "";
varName = "";
varValue = "";
varTyp = 2;
}
}
vars = varCount;
return res;
}
int8 writeToFile(int8 index) {
String path = String(bot + "/" + strats[index]);
int8 res = PF.open(path.c_str());
if (res > 0) return res;
// Write every line
String nl = String('\n');
String sp = String(' ');
String end = String(END_CHAR);
unsigned int n = 0;
for (int x = 0; x < vars; x++) {
res = PF.writeFile(varNames[x].c_str(), strlen(varNames[x].c_str()), &n); // Write var name
if (res > 0) return res;
res = PF.writeFile(sp.c_str(), 1, &n); // Write space
if (res > 0) return res;
String value;
if (varType[x] == 1) {
if (varVals[x] == 1) value = "True";
else value = "False";
} else if (varType[x] == 2) value = String(varVals[x]);
else if (varType[x] == 3) {
float temp;
memcpy(&temp, &varVals[x], sizeof(temp));
value = String(temp, FLOAT_POINTS);
}
res = PF.writeFile(value.c_str(), strlen(value.c_str()), &n); // Write value
if (res > 0) return res;
int skips = lineLen - n - varNames[x].length() - 1;
String skipsStr = "";
for (int y = 0; y < skips; y++) skipsStr += SKIP_CHAR;
res = PF.writeFile(skipsStr.c_str(), strlen(skipsStr.c_str()), &n); // Write value
if (res > 0) return res;
res = PF.writeFile(nl.c_str(), 1, &n); // Write new line
if (res > 0) return res;
}
res = PF.writeFile(end.c_str(), 1, &n); // Write end char
if (res > 0) return res;
res = PF.writeFile(0,0,&n);
return res;
}