diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 9b7327c..5528be7 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -7,22 +7,36 @@ namespace config { Config value; - void format() { - VERBOSE("format"); - hardware::i2c::eepromPowerOn(); - for (int i = 0; i < 1024; i++) - { - hardware::i2c::eeprom.write(i, 0); + namespace details { + + void read() { + VERBOSE("read"); + hardware::i2c::eepromPowerOn(); + hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); + if (!String(CONFIG_SEED).equals(value.seed)) reset(); + hardware::i2c::eepromPowerOff(); + + } + + void write() { + VERBOSE_FORMAT("write", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + + hardware::i2c::eepromPowerOn(); + int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); + hardware::i2c::eepromPowerOff(); } - hardware::i2c::eepromPowerOff(); } - void write() { - VERBOSE_FORMAT("write", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + Config get() { + if (value.seed[0] == '\0') details::read(); - hardware::i2c::eepromPowerOn(); - int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); - hardware::i2c::eepromPowerOff(); + VERBOSE_FORMAT("get", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + return value; + } + + void set(Config config) { + value = config; + details::write(); } void reset() { @@ -35,19 +49,10 @@ namespace config { }; value = config; - //format(); - write(); + details::write(); VERBOSE_FORMAT("reset", "value : %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); } - void read() { - hardware::i2c::eepromPowerOn(); - hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); - if (!String(CONFIG_SEED).equals(value.seed)) reset(); - hardware::i2c::eepromPowerOff(); - - VERBOSE_FORMAT("read", "%s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); - - } + } \ No newline at end of file diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 9e438fa..3d44f49 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -25,8 +25,6 @@ struct Config { namespace config { - extern Config value; - static const sleepTimings_t defaultSleepTimings[] PROGMEM = { { 5, SLEEP_DEFAULT_TIME_SECONDS }, { 10, 1200 }, @@ -38,8 +36,9 @@ namespace config { { 180, 180 }, }; - void read(); + Config get(); + void set(Config config); + void reset(); - void write(); } \ No newline at end of file diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index dee568a..4b3fe41 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -168,7 +168,7 @@ namespace debug { } void getAndDisplayEepromConfig() { - config::read(); + config::get(); } void getAndDisplayEepromContent() { @@ -183,21 +183,23 @@ namespace debug { } Serial.println(); hardware::i2c::eepromPowerOff(); - Log.notice("Done\n"); + Log.notice(F("Done\n")); } void getAndDisplayEepromPositions() { - uint16_t currentEntryIndex = config::value.firstEntry; + uint16_t currentEntryIndex = config::get().firstEntry; PositionEntry currentEntry; + hardware::i2c::eepromPowerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; details::displayPosition(currentEntry); } while (positions::moveNext(currentEntryIndex)); + hardware::i2c::eepromPowerOff(); } void getAndDisplayEepromLastPosition() { - uint16_t lastEntryIndex = config::value.lastEntry; + uint16_t lastEntryIndex = config::get().lastEntry; PositionEntry lastEntry; positions::get(lastEntryIndex, lastEntry); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 6cb2bf3..bc58e67 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -10,8 +10,6 @@ void setup() { if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); #endif - config::read(); - rtc::setup(); hardware::sim808::setup(); } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 4f7cc6c..78e5412 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -23,32 +23,24 @@ namespace positions { void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus) { VERBOSE("appendLast"); - uint16_t lastEntryIndex; - uint16_t firstEntryIndex; uint16_t entryAddress; PositionEntry entry = { battery, gpsStatus }; strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); storage::powerOn(); - config::read(); + Config config = config::get(); - firstEntryIndex = config::value.firstEntry; - lastEntryIndex = config::value.lastEntry; + config.lastEntry++; + if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0; + if (config.lastEntry == config.firstEntry) config.firstEntry++; + if (config.firstEntry > _maxEntryIndex) config.firstEntry = 0; - lastEntryIndex++; - if (lastEntryIndex > _maxEntryIndex) lastEntryIndex = 0; - if (lastEntryIndex == firstEntryIndex) firstEntryIndex++; - if (firstEntryIndex > _maxEntryIndex) firstEntryIndex = 0; - - entryAddress = getEntryAddress(lastEntryIndex); + entryAddress = getEntryAddress(config.lastEntry); hardware::i2c::eeprom.writeBlock(entryAddress, entry); VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, battery.level, battery.voltage, gpsStatus, entry.position); - config::value.firstEntry = firstEntryIndex; - config::value.lastEntry = lastEntryIndex; - config::write(); - + config::set(config); storage::powerOff(); } @@ -67,8 +59,7 @@ namespace positions { } bool moveNext(uint16_t &index) { - config::read(); - if (index == config::value.lastEntry) return false; + if (index == config::get().lastEntry) return false; if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++;