forked from Open-Acidification/TankController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSD_TC.cpp
More file actions
103 lines (89 loc) · 2.1 KB
/
SD_TC.cpp
File metadata and controls
103 lines (89 loc) · 2.1 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
#include "Devices/SD_TC.h"
#include "DateTime_TC.h"
#include "Serial_TC.h"
#include "TC_util.h"
// class variables
SD_TC* SD_TC::_instance = nullptr;
// class methods
/**
* accessor for singleton
*/
SD_TC* SD_TC::instance() {
if (!_instance) {
_instance = new SD_TC();
}
return _instance;
}
// instance methods
/**
* constructor
*/
SD_TC::SD_TC() {
Serial.println(F("SD_TC()")); // Serial_TC might not be ready yet
assert(_instance == nullptr);
if (!sd.begin(SD_SELECT_PIN)) {
Serial.println(F("SD_TC failed to initialize!"));
}
}
/**
* append data to a data log file
*/
void SD_TC::appendData(const char* header, const char* line) {
char path[30];
todaysDataFileName(path, sizeof(path));
if (!sd.exists(path)) {
appendDataToPath(header, path);
COUT(header);
}
appendDataToPath(line, path);
COUT(line);
}
/**
* append data to a path
*/
void SD_TC::appendDataToPath(const char* line, const char* path) {
COUT(path);
File file = sd.open(path, O_CREAT | O_WRONLY | O_APPEND);
if (file) {
file.write(line);
file.write("\n", 1);
file.close();
COUT(file);
} else {
if (!hasHadError) {
hasHadError = true;
serial(F("Unable to open file: \"%s\""), path);
COUT("Unable to open file: \"" << path << "\"");
return;
}
}
}
/**
* append data to a serial log file
*/
void SD_TC::appendToLog(const char* line) {
DateTime_TC now = DateTime_TC::now();
char path[30];
snprintf_P(path, sizeof(path), (PGM_P)F("%4i%02i%02i.log"), now.year(), now.month(), now.day());
appendDataToPath(line, path);
}
bool SD_TC::exists(const char* path) {
return sd.exists(path);
}
bool SD_TC::format() {
return sd.format();
}
bool SD_TC::mkdir(const char* path) {
return sd.mkdir(path);
}
File SD_TC::open(const char* path, oflag_t oflag) {
return sd.open(path, oflag);
}
void SD_TC::printRootDirectory() {
sd.ls(LS_DATE | LS_SIZE | LS_R);
}
void SD_TC::todaysDataFileName(char* path, int size) {
DateTime_TC now = DateTime_TC::now();
snprintf_P(path, size, (PGM_P)F("%4i%02i%02i.csv"), now.year(), now.month(), now.day());
COUT(path);
}