Loom 4 v4.5
Arduino library for Internet of Things Rapid Prototyping in environmental sensing
Loading...
Searching...
No Matches
SDManager.h
1#pragma once
2
3#include <SPI.h>
4#include <SdFat.h>
5#include <OPEnS_RTC.h>
6
7#include "../../Module.h"
8#include "../../Loom_Manager.h"
9
15class SDManager : public Module{
16 protected:
17 /* These aren't used with the Hypnos */
18 void measure() override {};
19 void package() override {};
20
21 void initialize() override {};
22 void power_up() override {};
23 void power_down() override {};
24 public:
25
32 SDManager(Manager* man, int sd_chip_select);
33
37 bool begin();
38
43 bool log(DateTime currentTime);
44
52 char* readFile(const char* fileName);
53
54 /*
55 * Returns a pointer to the opened filed
56 */
57 File& getFile(const char* fileName){
58 myFile = sd.open(fileName);
59 return myFile;
60 };
61
62
68 bool writeLineToFile(const char* filename, const char* content);
69
73 const char* getDefaultFilename(){ return this->fileName; };
74
78 const char* getBatchFilename(){
79 snprintf_P(batchFileName, 260, PSTR("%s-Batch.txt"), fileNameNoExtension);
80 return batchFileName;
81 };
82
86 bool hasSDInitialized() { return sdInitialized; };
87
92 bool fileExists(const char* fileName) { return sd.exists(fileName); };
93
97 void setBatchSize(int size) { batch_size = size; };
98
102 int getCurrentBatch() { return current_batch; };
103
107 void setLogName(const char* name) {
108 strncpy(overrideFileName, name, 100);
109 };
110
111
112 /* Get whatever number we are currently appending to the SD fileNames*/
113 int getCurrentFileNumber() {return file_count;};
114
115
116 private:
117
118 Manager* manInst; // Reference to the manager
119
120 File myFile; // File object used to handle reading and writing
121 File scanningFile; // Used specifically to search through the directory
122 File root; // Open the root directory as a file
123
124 SdFat sd; // SD Card Object
125
126 int chip_select; // Chip select pin for the SD card
127 char device_name[100]; // Device name of the whole thing used as the starting point of the SD file name
128
129 char batchFileName[260]; // File name to log batches to
130 char fileName[260]; // Current file name that data is being logged to
131 char fileNameNoExtension[260]; // Current file name that data is being logged to without the file extension
132 char overrideFileName[260];
133
134 int batch_size = -1; // How many packets to log per batch
135 int current_batch = 0; // Current count of the batch
136 int file_count = 0; // What file number are we logging to
137
138 bool sdInitialized = false; // If the SD card actually initialized
139 char* headers[2]; // Contains the main and sub headers that are added to the top of the CSV files
140
141
142 void logBatch(); // Log data in batch format
143
144 void writeHeaders(); // Create the headers for the CSV file based off what info we are storing
145 bool updateCurrentFileName(); // Update the current file name to log to based on files already existing on the SD card
146};
Definition: Loom_Manager.h:18
Definition: Module.h:30
Definition: SDManager.h:15
bool fileExists(const char *fileName)
Definition: SDManager.h:92
bool writeLineToFile(const char *filename, const char *content)
Definition: SDManager.cpp:12
bool begin()
Definition: SDManager.cpp:180
void setBatchSize(int size)
Definition: SDManager.h:97
void setLogName(const char *name)
Definition: SDManager.h:107
char * readFile(const char *fileName)
Definition: SDManager.cpp:283
const char * getBatchFilename()
Definition: SDManager.h:78
const char * getDefaultFilename()
Definition: SDManager.h:73
int getCurrentBatch()
Definition: SDManager.h:102
bool hasSDInitialized()
Definition: SDManager.h:86
bool log(DateTime currentTime)
Definition: SDManager.cpp:81