This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
SDCard.cpp
132 lines (109 loc) · 2.32 KB
/
SDCard.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
/*
SDCard.c
*/
#include "SDCard.h"
SDCard::SDCard(int pin)
{
_list = NULL;
_pin = pin;
pinMode(_pin, OUTPUT);
_logfile = (char*) malloc(sizeof(char)*14);
if(DEBUG)
Serial.println("Initializing SD Card...");
if(!SD.begin(_pin))
{
Serial.println("Initialization Failed");
delay(5000);
return;
}
//Create a new file, with the proper filename
strcpy(_logfile, "MASTLOG00.txt");
for(int i=0; i<100; i++)
{
_logfile[7] = '0' + i/10;
_logfile[8] = '0' + i%10;
if(!SD.exists(_logfile))
break;
}
delay(2000);
File testFile = SD.open(_logfile, FILE_WRITE);
if(!testFile)
{
Serial.println("Couldn't Create File");
delay(5000);
return;
}
testFile.close();
delay(5000);
}
char* SDCard::getFilename()
{
//Returns a copy of the filename as a C string
char* filename = (char*) malloc(sizeof(char)*14);
strcpy(filename, _logfile);
return filename;
}
/*
Adds a DataSource to the list of sources.
*/
void SDCard::addDataSource(char* label, void* data, DataType type)
{
DataSource* link = (DataSource*) malloc(sizeof(DataSource));
link->label = label;
link->data = data;
link->type = type;
link->head = _list;
_list = link;
}
/*
Writes all the dataSources to the file, returning a 0 if successful.
Error Codes:
0 - File write successful
1 - File failed to open
Need to test to see if floats can be written to the file without issue.
If not, will need to write a method to do this.
*/
int SDCard::writeLine()
{
DataSource* link = _list;
int* idata;
double* ddata;
char* cdata;
float* fdata;
//Open the file
File f = SD.open(_logfile, FILE_WRITE);
if(!f)//File failed to open
return 1;
while(link!=NULL)
{
f.write("###");
f.flush();
f.write(link->label);
f.flush();
switch(link->type) {
case INT :
idata = (int *) link->data;
f.print(*idata, DEC);
break;
case DOUBLE:
ddata = (double *) link->data;
f.print(*ddata, DEC);
break;
case CHAR:
cdata = (char *) link->data;
f.write(cdata);
break;
case FLOAT:
fdata = (float *) link->data;
f.print(*fdata);
break;
}
f.flush();
f.write(",");
f.flush();
link=link->head;//Travel to the next link
}
//Close the file
f.write("***\n");
f.close();
}