-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdcard.ino
236 lines (204 loc) · 4.53 KB
/
sdcard.ino
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
/*
* SD Card variables
*/
bool card_present = false;
bool obc_log_present = false;
bool track_log_present = false;
bool com_log_present = false;
int32_t sdcard_init()
{
DEBUG.print(F("[OBC] SD Card init..."));
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
if (!sd.begin(SD_CS, SPI_HALF_SPEED))
{
DEBUG.println(F("NO SD Card present"));
card_present = false;
return -1; //TODO error codes
}
DEBUG.println(F("OK"));
DEBUG.println(F("[OBC] create log files:"));
sd_init_obc_log();
sd_init_com_log();
sd_init_track_log();
card_present = true;
DEBUG.println(F("DONE"));
return 0;
}
int32_t sd_init_obc_log()
{
DEBUG.print(F("...init OBC log..."));
if (dataFile.open("obc_hk.csv", O_RDWR | O_CREAT | O_AT_END))
{
dataFile.println(F("time,ext_temp,pcb_temp"));
dataFile.close();
DEBUG.println(F("OK"));
dataFile.close();
obc_log_present = true;
return 0;
}
dataFile.close();
DEBUG.println(F("File error!"));
obc_log_present = false;
return -1; //TODO error codes
}
int32_t sd_init_com_log()
{
DEBUG.print(F("...init COM log..."));
if (dataFile.open("com_hk.csv", O_RDWR | O_CREAT | O_AT_END))
{
dataFile.println(F("time,COM_temp,sent_msg"));
dataFile.close();
DEBUG.println(F("OK"));
dataFile.close();
com_log_present = true;
return 0;
}
dataFile.close();
DEBUG.println(F("File error!"));
com_log_present = false;
return -1; //TODO error codes
}
int32_t sd_init_track_log()
{
DEBUG.print(F("...init TRACK log..."));
if (dataFile.open("gps.csv", O_RDWR | O_CREAT | O_AT_END))
{
dataFile.println(F("GPS time,latitude,longitude,altitude,GPS fix,satellites"));
dataFile.close();
DEBUG.println(F("OK"));
dataFile.close();
track_log_present = true;
return 0;
}
dataFile.close();
DEBUG.println(F("File error!"));
track_log_present = false;
return -1; //TODO error codes
}
bool sdcard_is_card_present()
{
return card_present;
}
int sdcard_push_obc_log(void)
{
if( !obc_log_present)
{
return -1;
}
take_mutex();
DEBUG.print(F("[OBC] STORE OBC HK..."));
// if the file is available, write to it:
if (dataFile.open("obc_hk.csv", O_RDWR | O_CREAT | O_AT_END))
{
dataFile.println(obc_hk_buff);
dataFile.close();
DEBUG.println(F("OK"));
give_mutex();
return 0;
}
dataFile.close();
DEBUG.println(F("file error!"));
give_mutex();
return -2;
}
int sdcard_push_com_log(void)
{
if( !com_log_present)
{
return -1;
}
take_mutex();
DEBUG.print(F("[OBC] STORE COM HK..."));
// if the file is available, write to it:
if (dataFile.open("com_hk.csv", O_RDWR | O_CREAT | O_AT_END))
{
dataFile.println(radio_hk_buff);
dataFile.close();
DEBUG.println(F("OK"));
give_mutex();
return 0;
}
dataFile.close();
DEBUG.println(F("file error!"));
give_mutex();
return -2;
}
int sdcard_push_track_log(void)
{
if( !track_log_present)
{
return -1;
}
take_mutex();
DEBUG.print(F("[OBC] STORE GPS TRACK..."));
// if the file is available, write to it:
if (dataFile.open("gps.csv", O_RDWR | O_CREAT | O_AT_END))
{
dataFile.print(GPS_time[GPS_valid]);
dataFile.print(F(","));
dataFile.print(GPS_lati[GPS_valid]);
dataFile.print(F(","));
dataFile.print(GPS_long[GPS_valid]);
dataFile.print(F(","));
dataFile.print(GPS_Altitude[GPS_valid]);
dataFile.print(F(","));
dataFile.print(GPS_Fix);
dataFile.print(F(","));
dataFile.println(GPS_Satellites);
dataFile.close();
DEBUG.println(F("OK"));
give_mutex();
return 0;
}
dataFile.close();
DEBUG.println(F("file error!"));
give_mutex();
return -2;
}
/*
int read_config(void)
{
File dataFile = SD.open("config1.cfg");
if (dataFile)
{
// read from the file until there's nothing else in it:
int i=0;
while (dataFile.available())
{
config_data[i]=dataFile.read();
i++;
}
// close the file:
dataFile.close();
return 0;
} else
{
return 2;
// if the file didn't open, print an error:
}
return 1;
}
void parseConfig(void)
{
TERMINATION=0;
callsign = "";
int j=0;
for(int i=0; config_data[i] != 'e'; i++)
{
if( config_data[i] == '\r')
{
j++;
i+=2;
}
if( j == 0)
{
callsign += String(config_data[i]);
}
else if( j == 1)
{
TERMINATION = TERMINATION * 10;
TERMINATION += (config_data[i] - '0');
}
}
}*/