microSD card writing is now functional. Hooray :D

This commit is contained in:
Starbeamrainbowlabs 2019-06-27 17:26:22 +01:00
parent 4e6da5604e
commit 3e3f0b6bb3
3 changed files with 18 additions and 21 deletions

View file

@ -36,16 +36,13 @@ void setup() {
peripheral_unsilence(PIN_SPI_CS_SD); peripheral_unsilence(PIN_SPI_CS_SD);
store_init();
Serial.println(freeMemory(), DEC); Serial.println(freeMemory(), DEC);
store_reading(id, gps_data); store_reading(id, gps_data);
Serial.println(freeMemory(), DEC); Serial.println(freeMemory(), DEC);
#ifdef SD_DEBUG_ENABLED #ifdef SD_DEBUG_ENABLED
store_debug(gps_data.time, 19); store_debug(gps_data.time, 19 - 1); // Don't print the null byte
Serial.println(freeMemory(), DEC);
#endif #endif
Serial.println(freeMemory(), DEC);
store_end();
Serial.println(freeMemory(), DEC);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View file

@ -5,44 +5,44 @@
#include "settings.h" #include "settings.h"
#include "gps.h" #include "gps.h"
// FUTURE: We might be able to trim it down if we disable long filenames with #define USE_LONG_FILE_NAMES 0 // FUTURE: We might be able to trim it down if we disable long filenames with #define
SdFat* card = nullptr;
void store_init() { SdFat store_init() {
card = new SdFat(); SdFat card;
if(!card->begin(PIN_SPI_CS_SD)) { if(!card.begin(PIN_SPI_CS_SD)) {
Serial.println("Error: MicroSD card init failed"); Serial.println("Error: MicroSD card init failed");
while(true) delay(100); while(true) delay(100);
} }
return card;
} }
void store_reading(uint32_t id, GPSLocation location) { void store_reading(uint32_t id, GPSLocation location) {
// Port the rest of this to SdFat from SD // Port the rest of this to SdFat from SD
SdFat card = store_init();
// 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 = card->open(SD_FILENAME, O_APPEND | O_CREAT | O_WRONLY); File handle = card.open(SD_FILENAME, O_APPEND | O_CREAT | O_WRONLY);
uint8_t places = 6;
char line[33]; handle.printField(id, '\t');
int chars = snprintf(line, 33, "%d\t%06d\t%06d\n", handle.printField(location.lat, '\t', places);
id, handle.printField(location.lng, '\n', places);
location.lat, location.lng
);
handle.write(line, chars);
handle.close(); // Syncs implicitly handle.close(); // Syncs implicitly
} }
void store_debug(char* buffer, int size) { void store_debug(char* buffer, int size) {
File handle = card->open(SD_FILENAME_DEBUG, O_APPEND | O_CREAT | O_WRONLY); SdFat card = store_init();
File handle = card.open(SD_FILENAME_DEBUG, O_APPEND | O_CREAT | O_WRONLY);
handle.write(buffer, size); handle.write(buffer, size);
handle.write('\n'); handle.write('\n');
handle.close(); handle.close();
} }
/*
void store_end() { void store_end() {
// Apparently we'ree fine so long as we don't have any open file handles - there's no end() method on the SdFat class // Apparently we'ree fine so long as we don't have any open file handles - there's no end() method on the SdFat class
// card->end(); // card->end();
delete card; delete card;
card = nullptr; card = nullptr;
} }
*/

View file

@ -11,4 +11,4 @@ void store_reading(uint32_t id, GPSLocation location);
void store_debug(char* buffer, int size); void store_debug(char* buffer, int size);
void store_end(); // void store_end();