diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index d6461a3..20c7f71 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -16,9 +16,7 @@ namespace core { setSleepTime(); } - if (positions::needsToSend()) { - positions::send(); - } + positions::doBackup(); } void setSleepTime() { diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 38f6121..d720539 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -1,4 +1,5 @@ #include "Positions.h" +#include "SdPositionsBackup.h" #include "Debug.h" #include "Config.h" @@ -12,11 +13,23 @@ namespace positions { - uint16_t _maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; + backup::PositionsBackup **_backups; + size_t _backupLength; - uint16_t getEntryAddress(uint16_t index) { - if (index > _maxEntryIndex) return -1; - return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); + namespace details { + uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; + + uint16_t getEntryAddress(uint16_t index) { + if (index > maxEntryIndex) return -1; + return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); + } + } + + void setup() { + //TODO : enable/disable based on config + _backupLength = 1; + _backups = new backup::PositionsBackup*[_backupLength]; + _backups[0] = new backup::SdPositionsbackup(); } bool acquire(PositionEntryMetadata &metadata) { @@ -60,11 +73,11 @@ namespace positions { Config config = config::get(); config.lastEntry++; - if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0; + if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; if (config.lastEntry == config.firstEntry) config.firstEntry++; - if (config.firstEntry > _maxEntryIndex) config.firstEntry = 0; + if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0; - entryAddress = getEntryAddress(config.lastEntry); + entryAddress = details::getEntryAddress(config.lastEntry); hardware::i2c::eeprom.writeBlock(entryAddress, entry); VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); @@ -74,7 +87,7 @@ namespace positions { } bool get(uint16_t index, PositionEntry &entry) { - uint16_t entryAddress = getEntryAddress(index); + uint16_t entryAddress = details::getEntryAddress(index); if (entryAddress == -1) return false; VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress); @@ -90,17 +103,15 @@ namespace positions { bool moveNext(uint16_t &index) { if (index == config::get().lastEntry) return false; - if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way + if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++; return true; } - bool needsToSend() { - return false; - } - - void send() { - + void doBackup() { + for (int i = 0; i < _backupLength; i++) { + _backups[i]->backup(); + } } } \ No newline at end of file diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h index a2c914f..e91fb87 100644 --- a/GpsTracker/Positions.h +++ b/GpsTracker/Positions.h @@ -1,6 +1,7 @@ #pragma once #include +#include "PositionsBackup.h" #define POSITION_SIZE 115 @@ -18,12 +19,13 @@ struct PositionEntry { }; //sizeof = 125 namespace positions { + + void setup(); bool acquire(PositionEntryMetadata &metadata); void appendLast(const PositionEntryMetadata &metadata); bool get(uint16_t index, PositionEntry &entry); bool moveNext(uint16_t &index); - bool needsToSend(); - void send(); + void doBackup(); } \ No newline at end of file diff --git a/GpsTracker/PositionsBackup.h b/GpsTracker/PositionsBackup.h new file mode 100644 index 0000000..590459b --- /dev/null +++ b/GpsTracker/PositionsBackup.h @@ -0,0 +1,12 @@ +#pragma once + +namespace positions { + namespace backup { + + class PositionsBackup { + public: + virtual void backup(); + }; + + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h new file mode 100644 index 0000000..56f4bec --- /dev/null +++ b/GpsTracker/SdPositionsBackup.h @@ -0,0 +1,13 @@ +#pragma once + +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + + class SdPositionsbackup : public PositionsBackup { + public: + void backup(); + }; + } +} \ No newline at end of file