Finish SdFat refactor, but it's untested.

This commit is contained in:
Starbeamrainbowlabs 2019-06-27 15:49:39 +01:00
parent 151b9544f2
commit 18342c55fc
1 changed files with 23 additions and 16 deletions

View File

@ -1,36 +1,43 @@
#include <Arduino.h> #include <Arduino.h>
#include <SdFat.h> #include <SdFat.h>
// #include <ArduinoFiles.h>
#include "settings.h" #include "settings.h"
#include "gps.h" #include "gps.h"
SdFat* card = NULL; // FUTURE: We might be able to trim it down if we disable long filenames with #define USE_LONG_FILE_NAMES 0
SdFat* card = nullptr;
void store_init() {
card = new SdFat();
card->begin(PIN_SPI_CS_SD);
}
void store_reading(uint32_t id, GPSLocation location) { void store_reading(uint32_t id, GPSLocation location) {
SdFat card;
card.begin(PIN_SPI_CS_SD);
// Port the rest of this to SdFat from SD // Port the rest of this to SdFat from SD
// Open the file to write the data to. If it doesn't exist it will be created. // Open the file to write the data to. If it doesn't exist it will be created.
// Unlike elsewhere, FILE_WRITE opens the file with the cursor starting at the end, so it's basically actually equivalent to FILE_APPEND that we use in other environments. Confusing, I know. // Unlike elsewhere, FILE_WRITE opens the file with the cursor starting at the end, so it's basically actually equivalent to FILE_APPEND that we use in other environments. Confusing, I know.
File handle = SD.open(SD_FILENAME, FILE_WRITE); File handle = card->open(SD_FILENAME, O_APPEND | O_CREAT | O_WRONLY);
handle.print(id); char line[33];
handle.print(F("\t")); int chars = snprintf(line, 33, "%d\t%06d\t%06d\n",
id,
location.lat, location.lng
);
handle.write(line, chars);
handle.print(location.lat); handle.close(); // Syncs implicitly
handle.print(F("\t"));
handle.print(location.lng);
handle.println();
handle.close();
} }
void store_debug(char* buffer, int size) { void store_debug(char* buffer, int size) {
File handle = SD.open(SD_FILENAME_DEBUG, FILE_WRITE); File handle = card->open(SD_FILENAME_DEBUG, O_APPEND | O_CREAT | O_WRONLY);
handle.write(buffer, size); handle.write(buffer, size);
handle.println(); handle.write('\n');
handle.close(); handle.close();
} }
void store_end() {
card.end();
delete card;
}