Browse Source

Reduced config access to EEPROM

tags/v1.2.0
Bertrand Lemasle 7 years ago
parent
commit
0bb7171895
5 changed files with 45 additions and 50 deletions
  1. +28
    -23
      GpsTracker/Config.cpp
  2. +3
    -4
      GpsTracker/Config.h
  3. +6
    -4
      GpsTracker/Debug.cpp
  4. +0
    -2
      GpsTracker/GpsTracker.ino
  5. +8
    -17
      GpsTracker/Positions.cpp

+ 28
- 23
GpsTracker/Config.cpp View File

@@ -7,22 +7,36 @@
namespace config { namespace config {
Config value; 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() { void reset() {
@@ -35,19 +49,10 @@ namespace config {
}; };


value = config; value = config;
//format();
write();
details::write();


VERBOSE_FORMAT("reset", "value : %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); 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);

}
} }

+ 3
- 4
GpsTracker/Config.h View File

@@ -25,8 +25,6 @@ struct Config {


namespace config { namespace config {


extern Config value;

static const sleepTimings_t defaultSleepTimings[] PROGMEM = { static const sleepTimings_t defaultSleepTimings[] PROGMEM = {
{ 5, SLEEP_DEFAULT_TIME_SECONDS }, { 5, SLEEP_DEFAULT_TIME_SECONDS },
{ 10, 1200 }, { 10, 1200 },
@@ -38,8 +36,9 @@ namespace config {
{ 180, 180 }, { 180, 180 },
}; };


void read();
Config get();
void set(Config config);

void reset(); void reset();
void write();


} }

+ 6
- 4
GpsTracker/Debug.cpp View File

@@ -168,7 +168,7 @@ namespace debug {
} }


void getAndDisplayEepromConfig() { void getAndDisplayEepromConfig() {
config::read();
config::get();
} }


void getAndDisplayEepromContent() { void getAndDisplayEepromContent() {
@@ -183,21 +183,23 @@ namespace debug {
} }
Serial.println(); Serial.println();
hardware::i2c::eepromPowerOff(); hardware::i2c::eepromPowerOff();
Log.notice("Done\n");
Log.notice(F("Done\n"));
} }


void getAndDisplayEepromPositions() { void getAndDisplayEepromPositions() {
uint16_t currentEntryIndex = config::value.firstEntry;
uint16_t currentEntryIndex = config::get().firstEntry;
PositionEntry currentEntry; PositionEntry currentEntry;


hardware::i2c::eepromPowerOn();
do { do {
if (!positions::get(currentEntryIndex, currentEntry)) break; if (!positions::get(currentEntryIndex, currentEntry)) break;
details::displayPosition(currentEntry); details::displayPosition(currentEntry);
} while (positions::moveNext(currentEntryIndex)); } while (positions::moveNext(currentEntryIndex));
hardware::i2c::eepromPowerOff();
} }


void getAndDisplayEepromLastPosition() { void getAndDisplayEepromLastPosition() {
uint16_t lastEntryIndex = config::value.lastEntry;
uint16_t lastEntryIndex = config::get().lastEntry;
PositionEntry lastEntry; PositionEntry lastEntry;


positions::get(lastEntryIndex, lastEntry); positions::get(lastEntryIndex, lastEntry);


+ 0
- 2
GpsTracker/GpsTracker.ino View File

@@ -10,8 +10,6 @@ void setup() {
if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial);
#endif #endif
config::read();

rtc::setup(); rtc::setup();
hardware::sim808::setup(); hardware::sim808::setup();
} }


+ 8
- 17
GpsTracker/Positions.cpp View File

@@ -23,32 +23,24 @@ namespace positions {
void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus) { void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus) {
VERBOSE("appendLast"); VERBOSE("appendLast");


uint16_t lastEntryIndex;
uint16_t firstEntryIndex;
uint16_t entryAddress; uint16_t entryAddress;
PositionEntry entry = { battery, gpsStatus }; PositionEntry entry = { battery, gpsStatus };
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);


storage::powerOn(); 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); 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); 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(); storage::powerOff();
} }


@@ -67,8 +59,7 @@ namespace positions {
} }


bool moveNext(uint16_t &index) { 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 if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
else index++; else index++;


Loading…
Cancel
Save