2019-06-13 13:13:03 +00:00
# include <Arduino.h>
2019-06-26 17:08:42 +00:00
# include <SdFat.h>
2019-07-02 11:58:55 +00:00
# include <EEPROM.h>
2019-06-27 14:49:39 +00:00
// #include <ArduinoFiles.h>
2019-06-13 13:13:03 +00:00
# include "settings.h"
2019-06-26 14:46:07 +00:00
# include "gps.h"
2019-06-20 12:56:08 +00:00
2019-06-27 16:26:22 +00:00
// FUTURE: We might be able to trim it down if we disable long filenames with #define
2019-06-27 14:49:39 +00:00
2019-06-27 16:26:22 +00:00
SdFat store_init ( ) {
SdFat card ;
if ( ! card . begin ( PIN_SPI_CS_SD ) ) {
2019-06-27 14:58:01 +00:00
Serial . println ( " Error: MicroSD card init failed " ) ;
while ( true ) delay ( 100 ) ;
}
2019-06-27 16:26:22 +00:00
return card ;
2019-06-27 14:49:39 +00:00
}
2019-06-13 13:13:03 +00:00
2019-06-26 14:46:07 +00:00
void store_reading ( uint32_t id , GPSLocation location ) {
2019-06-26 18:34:29 +00:00
// Port the rest of this to SdFat from SD
2019-06-27 16:26:22 +00:00
SdFat card = store_init ( ) ;
2019-06-26 18:34:29 +00:00
2019-06-13 13:13:03 +00:00
// 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.
2019-06-27 16:26:22 +00:00
File handle = card . open ( SD_FILENAME , O_APPEND | O_CREAT | O_WRONLY ) ;
uint8_t places = 6 ;
handle . printField ( id , ' \t ' ) ;
handle . printField ( location . lat , ' \t ' , places ) ;
handle . printField ( location . lng , ' \n ' , places ) ;
2019-06-27 14:49:39 +00:00
handle . close ( ) ; // Syncs implicitly
2019-06-13 13:13:03 +00:00
}
2019-06-20 12:56:08 +00:00
void store_debug ( char * buffer , int size ) {
2019-06-27 16:26:22 +00:00
SdFat card = store_init ( ) ;
File handle = card . open ( SD_FILENAME_DEBUG , O_APPEND | O_CREAT | O_WRONLY ) ;
2019-06-26 14:46:07 +00:00
handle . write ( buffer , size ) ;
2019-06-27 14:49:39 +00:00
handle . write ( ' \n ' ) ;
2019-06-26 14:46:07 +00:00
handle . close ( ) ;
2019-06-20 12:56:08 +00:00
}
2019-06-27 14:49:39 +00:00
2019-06-27 16:26:22 +00:00
/*
2019-06-27 14:49:39 +00:00
void store_end ( ) {
2019-07-02 11:58:55 +00:00
// Apparently we're fine so long as we don't have any open file handles - there's no end() method on the SdFat class
2019-06-27 14:58:01 +00:00
// card->end();
2019-06-27 14:49:39 +00:00
delete card ;
2019-06-27 14:58:01 +00:00
card = nullptr ;
2019-06-27 14:49:39 +00:00
}
2019-06-27 16:26:22 +00:00
*/
2019-07-02 11:58:55 +00:00
void store_eeprom_uint32_save ( uint8_t offset , uint32_t value ) {
EEPROM . put ( offset , value ) ;
}
uint32_t store_eeprom_uint32_retrieve ( uint8_t offset ) {
uint32_t result ;
EEPROM . get ( offset , result ) ;
return result ;
}