diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 02fc246..99811e2 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -5,53 +5,55 @@ #define LOGGER_NAME "Config" namespace config { - Config value; + namespace main { - namespace details { + Config value; - void read() { - VERBOSE("read"); - hardware::i2c::powerOn(); - hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); - if (!String(CONFIG_SEED).equals(value.seed)) reset(); - hardware::i2c::powerOff(); + namespace details { - } + void read() { + VERBOSE("read"); + hardware::i2c::powerOn(); + hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); + if (!String(CONFIG_SEED).equals(value.seed)) reset(); + hardware::i2c::powerOff(); + + } - void write() { - VERBOSE_FORMAT("write", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); + void write() { + VERBOSE_FORMAT("write", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); - hardware::i2c::powerOn(); - int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); - hardware::i2c::powerOff(); + hardware::i2c::powerOn(); + int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); + hardware::i2c::powerOff(); + } } - } - Config get() { - if (value.seed[0] == '\0') details::read(); + Config get() { + if (value.seed[0] == '\0') details::read(); - VERBOSE_FORMAT("get", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); - return value; - } + VERBOSE_FORMAT("get", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); + return value; + } - void set(Config config) { - value = config; - details::write(); - } + void set(const Config config) { + value = config; + details::write(); + } - void reset() { - VERBOSE("reset"); - Config config = { - CONFIG_SEED, - VERSION, - "Vodafone", //TODO : read from SD - 0xFFFF, - 0xFFFF, - }; - - value = config; - details::write(); - } + void reset() { + VERBOSE("reset"); + Config config = { + CONFIG_SEED, + VERSION, + "Vodafone", //TODO : read from SD + 0xFFFF, + 0xFFFF, + }; + + value = config; + details::write(); + } - + } } \ No newline at end of file diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 5de2870..ef03867 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -28,18 +28,19 @@ namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { { 5, SLEEP_DEFAULT_TIME_SECONDS }, - { 10, 1200 }, - { 20, 600 }, - { 30, 540 }, - { 50, 480 }, - { 80, 240 }, - { 100, 210 }, - { 180, 180 }, + { 10, 1200 }, + { 20, 600 }, + { 30, 540 }, + { 50, 480 }, + { 80, 240 }, + { 100, 210 }, + { 180, 180 }, }; - Config get(); - void set(Config config); - - void reset(); + namespace main { + Config get(); + void set(const Config config); + void reset(); + } } \ No newline at end of file diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index f54508a..615294d 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -180,7 +180,7 @@ namespace debug { } void getAndDisplayEepromConfig() { - config::get(); + config::main::get(); } void getAndDisplayEepromContent() { @@ -200,7 +200,7 @@ namespace debug { } void getAndDisplayEepromPositions() { - uint16_t currentEntryIndex = config::get().firstEntry; + uint16_t currentEntryIndex = config::main::get().firstEntry; PositionEntry currentEntry; hardware::i2c::powerOn(); @@ -212,7 +212,7 @@ namespace debug { } void getAndDisplayEepromLastPosition() { - uint16_t lastEntryIndex = config::get().lastEntry; + uint16_t lastEntryIndex = config::main::get().lastEntry; PositionEntry lastEntry; positions::get(lastEntryIndex, lastEntry); diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h index 8e90e49..c1c5eca 100644 --- a/GpsTracker/GpsTracker.h +++ b/GpsTracker/GpsTracker.h @@ -7,5 +7,6 @@ #include "Debug.h" #include "Config.h" #include "Core.h" +#include "SdCard.h" #define LOGGER_NAME "GpsTracker" diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 440ca89..75589bc 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -12,6 +12,7 @@ void setup() { #endif rtc::setup(); + hardware::sdcard::setup(); hardware::sim808::setup(); } @@ -55,7 +56,7 @@ void loop() { debug::getAndDisplayEepromConfig(); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG: - config::reset(); + config::main::reset(); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT: debug::getAndDisplayEepromContent(); diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 378ea82..1f36283 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -65,7 +65,7 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs(config::get().apn); + device.enableGprs(config::main::get().apn); } void networkPowerOff() { diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index d720539..606a4aa 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -29,7 +29,7 @@ namespace positions { //TODO : enable/disable based on config _backupLength = 1; _backups = new backup::PositionsBackup*[_backupLength]; - _backups[0] = new backup::SdPositionsbackup(); + _backups[0] = new backup::sd::SdPositionsBackup(); } bool acquire(PositionEntryMetadata &metadata) { @@ -70,7 +70,7 @@ namespace positions { strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); hardware::i2c::powerOn(); - Config config = config::get(); + Config config = config::main::get(); config.lastEntry++; if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; @@ -82,7 +82,7 @@ namespace positions { 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); - config::set(config); + config::main::set(config); hardware::i2c::powerOff(); } @@ -101,7 +101,7 @@ namespace positions { } bool moveNext(uint16_t &index) { - if (index == config::get().lastEntry) return false; + if (index == config::main::get().lastEntry) return false; if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++; diff --git a/GpsTracker/PositionsBackup.h b/GpsTracker/PositionsBackup.h index 590459b..f3ebaaf 100644 --- a/GpsTracker/PositionsBackup.h +++ b/GpsTracker/PositionsBackup.h @@ -5,6 +5,7 @@ namespace positions { class PositionsBackup { public: + virtual void setup(); virtual void backup(); }; diff --git a/GpsTracker/RawSdFile.cpp b/GpsTracker/RawSdFile.cpp new file mode 100644 index 0000000..099d111 --- /dev/null +++ b/GpsTracker/RawSdFile.cpp @@ -0,0 +1,40 @@ +#include "RawSdFile.h" + +namespace sd { + + RawSdFile::RawSdFile(SdFat *sd, const char *filename) { + _sd = sd; + _filename = filename; + } + + RawSdFile::~RawSdFile() { + flush(); + } + + void RawSdFile::open(const uint32_t pos) { + if (!_file.isOpen()) { + _sd->chdir(); + _file = _sd->open(_filename, O_RDWR | O_CREAT); + } + + _file.seek(pos); + } + + void RawSdFile::flush() { + if (!_file.isOpen()) return; + + _file.close(); + } + + void RawSdFile::write(const uint32_t pos, const void *val, const size_t size) { + open(pos); + _file.write(val, size); + } + + void RawSdFile::read(const uint32_t pos, void *val, const size_t size) { + open(pos); + if (!_file.available()) return; + + _file.read(val, size); + } +} \ No newline at end of file diff --git a/GpsTracker/RawSdFile.h b/GpsTracker/RawSdFile.h new file mode 100644 index 0000000..bb3bb32 --- /dev/null +++ b/GpsTracker/RawSdFile.h @@ -0,0 +1,32 @@ +#pragma once + +#include + +namespace sd { + class RawSdFile { + private: + SdFat * _sd; + const char *_filename; + File _file; + + void open(const uint32_t pos); + + public: + RawSdFile(SdFat *sd, const char *filename); + ~RawSdFile(); + + void flush(); + + void write(const uint32_t pos, const void *val, const size_t size); + template void write(const uint32_t pos, const T &val) + { + write(pos, (void*)&val, sizeof(val)); + } + + void read(const uint32_t pos, void *val, const size_t size); + template void read(const uint32_t pos, const T &val) + { + read(pos, (void*)&val, sizeof(val)); + } + }; +} diff --git a/GpsTracker/SdCard.cpp b/GpsTracker/SdCard.cpp new file mode 100644 index 0000000..33243ad --- /dev/null +++ b/GpsTracker/SdCard.cpp @@ -0,0 +1,13 @@ +#include "SdCard.h" + +namespace hardware { + namespace sdcard { + + SdFat filesystem; + + void setup() { + filesystem.begin(SD_SS); + } + + } +} \ No newline at end of file diff --git a/GpsTracker/SdCard.h b/GpsTracker/SdCard.h new file mode 100644 index 0000000..9596915 --- /dev/null +++ b/GpsTracker/SdCard.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include "Pins.h" + +namespace hardware { + namespace sdcard { + + extern SdFat filesystem; + + void setup(); + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp new file mode 100644 index 0000000..d470e12 --- /dev/null +++ b/GpsTracker/SdPositionsBackup.cpp @@ -0,0 +1,23 @@ +#include "SdPositionsBackup.h" +#include "SdPositionsConfig.h" +#include "SdCard.h" + +#include "Config.h" + +namespace positions { + namespace backup { + namespace sd { + + void SdPositionsBackup::setup() { + config::backup::sd::setup(); + _config = new RawSdFile(&hardware::sdcard::filesystem, POSITIONS_CONFIG_FILENAME); + } + + void SdPositionsBackup::backup() { + Config referenceConfig = config::main::get(); + + } + + } + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h index 56f4bec..9e4d8f0 100644 --- a/GpsTracker/SdPositionsBackup.h +++ b/GpsTracker/SdPositionsBackup.h @@ -1,13 +1,24 @@ #pragma once #include "PositionsBackup.h" +#include "RawSdFile.h" + +#define POSITIONS_CONFIG_FILENAME "positions.config" + +using namespace sd; namespace positions { namespace backup { + namespace sd { + + class SdPositionsBackup : public PositionsBackup { + private: + RawSdFile * _config; + public: + void setup(); + void backup(); + }; - class SdPositionsbackup : public PositionsBackup { - public: - void backup(); - }; + } } } \ No newline at end of file diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp new file mode 100644 index 0000000..3ab0add --- /dev/null +++ b/GpsTracker/SdPositionsConfig.cpp @@ -0,0 +1,13 @@ +#include "SdPositionsConfig.h" + +namespace config { + namespace backup { + namespace sd { + + void setup() { + + } + + } + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsConfig.h b/GpsTracker/SdPositionsConfig.h new file mode 100644 index 0000000..9922cd8 --- /dev/null +++ b/GpsTracker/SdPositionsConfig.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +struct SdPositionConfig_t { + uint16_t fileIndex; + uint32_t position; + size_t size; +}; + +namespace config { + namespace backup { + namespace sd { + + void setup(); + + SdPositionConfig_t get(); + void set(SdPositionConfig_t config); + void reset(); + + } + } +} \ No newline at end of file