From 72ad27ca7b857f3c91560dc94f727fd0c9922db3 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 11 Mar 2018 16:08:37 +1300 Subject: [PATCH] Fixed positions reading using an iterator like method --- GpsTracker/Debug.cpp | 12 ++++-------- GpsTracker/Positions.cpp | 18 +++++++++--------- GpsTracker/Positions.h | 2 +- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index c1c120b..dee568a 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -175,16 +175,12 @@ namespace debug { char buffer[128]; hardware::i2c::eepromPowerOn(); - //Serial.print('['); for (int i = 0; i < 8; i++) { hardware::i2c::eeprom.read(128 * i, buffer, 128); for (int i = 0; i < 128; i++) { - //if (buffer[i] == 0) Serial.print(' '); - //else - Serial.print(buffer[i], HEX); + Serial.print(buffer[i], HEX); } } - //Serial.println(']'); Serial.println(); hardware::i2c::eepromPowerOff(); Log.notice("Done\n"); @@ -194,10 +190,10 @@ namespace debug { uint16_t currentEntryIndex = config::value.firstEntry; PositionEntry currentEntry; - while(positions::moveNext(currentEntryIndex)) { - positions::get(currentEntryIndex, currentEntry); + do { + if (!positions::get(currentEntryIndex, currentEntry)) break; details::displayPosition(currentEntry); - } + } while (positions::moveNext(currentEntryIndex)); } void getAndDisplayEepromLastPosition() { diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index d74e6b0..4f7cc6c 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -41,21 +41,20 @@ namespace positions { if (firstEntryIndex > _maxEntryIndex) firstEntryIndex = 0; entryAddress = getEntryAddress(lastEntryIndex); - bool success = hardware::i2c::eeprom.writeBlock(entryAddress, entry); - if (success) { - VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, battery.level, battery.voltage, gpsStatus, entry.position); + 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::value.firstEntry = firstEntryIndex; + config::value.lastEntry = lastEntryIndex; + config::write(); storage::powerOff(); } - void get(uint16_t index, PositionEntry &entry) { + bool get(uint16_t index, PositionEntry &entry) { uint16_t entryAddress = getEntryAddress(index); - if (entryAddress == -1) return; + if (entryAddress == -1) return false; VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress); @@ -64,6 +63,7 @@ namespace positions { storage::powerOff(); VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, entry.battery.level, entry.battery.voltage, entry.status, entry.position); + return true; } bool moveNext(uint16_t &index) { diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h index 7c39263..f5c6bff 100644 --- a/GpsTracker/Positions.h +++ b/GpsTracker/Positions.h @@ -13,7 +13,7 @@ struct PositionEntry { namespace positions { void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus); - void get(uint16_t index, PositionEntry &entry); + bool get(uint16_t index, PositionEntry &entry); bool moveNext(uint16_t &index); bool needsToSend();