LoRaWAN-Signal-Mapping/iot/main/storage.cpp

74 lines
1.9 KiB
C++
Raw Normal View History

#include <Arduino.h>
#include <SdFat.h>
#include <EEPROM.h>
// #include <ArduinoFiles.h>
#include "settings.h"
#include "gps.h"
// FUTURE: We might be able to trim it down if we disable long filenames with #define
void store_reading(uint32_t id, GPSLocation location) {
SdFat card;
2019-07-11 12:17:01 +00:00
if(!card.begin(PIN_SPI_CS_SD, SD_SCK_MHZ(50))) {
card.initErrorHalt();
2019-06-27 14:58:01 +00:00
}
2019-06-26 18:34:29 +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-07-11 12:17:01 +00:00
SdFile handle;
if(!handle.open(SD_FILENAME, O_WRITE | O_CREAT | O_APPEND)) {
Serial.println(F("Failed to open file"));
while(true) delay(1);
}
uint8_t places = 6;
handle.printField(id, '\t');
handle.printField(location.lat, '\t', places);
handle.printField(location.lng, '\n', places);
handle.sync();
handle.close(); // Syncs implicitly
}
/*
void store_debug(char* buffer, int size) {
SdFat card;
if(!card.begin(PIN_SPI_CS_SD)) {
Serial.println("Error: MicroSD card init failed");
while(true) delay(100);
}
File handle = card.open(SD_FILENAME_DEBUG, O_APPEND | O_CREAT | O_WRONLY);
handle.write(buffer, size);
handle.write('\n');
handle.close();
}
/*
void store_end() {
// 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();
delete card;
2019-06-27 14:58:01 +00:00
card = nullptr;
}
*/
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;
}
2019-07-15 14:25:51 +00:00
void store_eeprom_float_save(uint8_t offset, float value) {
EEPROM.put(offset, value);
}
float store_eeprom_float_retrieve(uint8_t offset) {
float result;
EEPROM.get(offset, result);
return result;
}