Added SD card writes -> Coords are stored as coords[0..].txt
This commit is contained in:
+49
-28
@@ -1,59 +1,80 @@
|
||||
#pragma once
|
||||
#include "FS.h"
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
|
||||
typedef enum{
|
||||
FAM_R,
|
||||
FAM_W
|
||||
} FileAccessMode;
|
||||
|
||||
class SDHelper {
|
||||
private:
|
||||
uint8_t _cs;
|
||||
FileAccessMode _fam;
|
||||
File _stream;
|
||||
String _filename;
|
||||
bool _available = false;
|
||||
SPIClass _spi;
|
||||
|
||||
String getNewFilename() {
|
||||
static unsigned long i = 0;
|
||||
String name;
|
||||
do {
|
||||
name = "coords" + String(i++) + ".txt";
|
||||
} while (SD.exists(name.c_str()));
|
||||
return name;
|
||||
String getNewFilename(fs::FS& fs) {
|
||||
unsigned long i = 0;
|
||||
|
||||
File root = fs.open("/");
|
||||
if(!root){
|
||||
Serial.println("Failed to open root of SD!");
|
||||
return "/coords.txt";
|
||||
}
|
||||
|
||||
File file = root.openNextFile();
|
||||
while(file){
|
||||
if(String(file.name()).indexOf("coords") != -1) i++;
|
||||
file = root.openNextFile();
|
||||
}
|
||||
|
||||
return "/coords" + String(i++) + ".txt";
|
||||
}
|
||||
|
||||
void appendFile(fs::FS& fs, const char* path, const char* message) {
|
||||
Serial.printf("Appending to file: %s\n", path);
|
||||
|
||||
File file = fs.open(path, FILE_APPEND);
|
||||
if(!file){
|
||||
Serial.println("Failed to open file for appending!");
|
||||
Serial.println("Trying writing insted!");
|
||||
|
||||
file = fs.open(path, FILE_WRITE);
|
||||
if(!file){
|
||||
Serial.println("Failed to open file for writing!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(file.print(message))Serial.println("Wrote message!");
|
||||
else Serial.println("Writing failed!");
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
public:
|
||||
SDHelper(uint8_t csPin, FileAccessMode fam, String filename = "") : _cs(csPin), _fam(fam), _spi(HSPI){
|
||||
SDHelper() : _spi(HSPI){
|
||||
_spi.begin(14, 2, 15, 13);
|
||||
if(!SD.begin(_cs, _spi)){
|
||||
if(!SD.begin(13, _spi)){
|
||||
_available = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(_fam == FAM_W){
|
||||
_filename = filename.length() ? filename : getNewFilename();
|
||||
_stream = SD.open(_filename.c_str(), FILE_WRITE);
|
||||
}
|
||||
else {
|
||||
_filename = filename;
|
||||
_stream = SD.open(_filename.c_str(), FILE_READ);
|
||||
uint8_t cardType = SD.cardType();
|
||||
|
||||
if (cardType == CARD_NONE) {
|
||||
Serial.println("No SD card attached");
|
||||
return;
|
||||
}
|
||||
_available = (bool)_stream;
|
||||
|
||||
_filename = getNewFilename(SD);
|
||||
}
|
||||
|
||||
~SDHelper(){
|
||||
if(_stream) _stream.close();
|
||||
}
|
||||
|
||||
bool Available() const { return _available; }
|
||||
const String& filename() const { return _filename; }
|
||||
String Filename() { return _filename; }
|
||||
|
||||
void Log(const String& msg) {
|
||||
if(_fam != FAM_W || !_stream) return;
|
||||
_stream.println(msg);
|
||||
_stream.flush();
|
||||
appendFile(SD, _filename.c_str(), (msg + "\n").c_str());
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user