Bladeren bron

Reorganized config code to allow for main vs sd vs another config

tags/v1.2.0
Bertrand Lemasle 7 jaren geleden
bovenliggende
commit
e1babd94b5
16 gewijzigde bestanden met toevoegingen van 236 en 62 verwijderingen
  1. +40
    -38
      GpsTracker/Config.cpp
  2. +12
    -11
      GpsTracker/Config.h
  3. +3
    -3
      GpsTracker/Debug.cpp
  4. +1
    -0
      GpsTracker/GpsTracker.h
  5. +2
    -1
      GpsTracker/GpsTracker.ino
  6. +1
    -1
      GpsTracker/Hardware.cpp
  7. +4
    -4
      GpsTracker/Positions.cpp
  8. +1
    -0
      GpsTracker/PositionsBackup.h
  9. +40
    -0
      GpsTracker/RawSdFile.cpp
  10. +32
    -0
      GpsTracker/RawSdFile.h
  11. +13
    -0
      GpsTracker/SdCard.cpp
  12. +13
    -0
      GpsTracker/SdCard.h
  13. +23
    -0
      GpsTracker/SdPositionsBackup.cpp
  14. +15
    -4
      GpsTracker/SdPositionsBackup.h
  15. +13
    -0
      GpsTracker/SdPositionsConfig.cpp
  16. +23
    -0
      GpsTracker/SdPositionsConfig.h

+ 40
- 38
GpsTracker/Config.cpp Bestand weergeven

@@ -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();
}

}
}

+ 12
- 11
GpsTracker/Config.h Bestand weergeven

@@ -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();
}
}

+ 3
- 3
GpsTracker/Debug.cpp Bestand weergeven

@@ -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);


+ 1
- 0
GpsTracker/GpsTracker.h Bestand weergeven

@@ -7,5 +7,6 @@
#include "Debug.h"
#include "Config.h"
#include "Core.h"
#include "SdCard.h"

#define LOGGER_NAME "GpsTracker"

+ 2
- 1
GpsTracker/GpsTracker.ino Bestand weergeven

@@ -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();


+ 1
- 1
GpsTracker/Hardware.cpp Bestand weergeven

@@ -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() {


+ 4
- 4
GpsTracker/Positions.cpp Bestand weergeven

@@ -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++;


+ 1
- 0
GpsTracker/PositionsBackup.h Bestand weergeven

@@ -5,6 +5,7 @@ namespace positions {

class PositionsBackup {
public:
virtual void setup();
virtual void backup();
};



+ 40
- 0
GpsTracker/RawSdFile.cpp Bestand weergeven

@@ -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);
}
}

+ 32
- 0
GpsTracker/RawSdFile.h Bestand weergeven

@@ -0,0 +1,32 @@
#pragma once

#include <SdFat.h>

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 <typename T> 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 <typename T> void read(const uint32_t pos, const T &val)
{
read(pos, (void*)&val, sizeof(val));
}
};
}

+ 13
- 0
GpsTracker/SdCard.cpp Bestand weergeven

@@ -0,0 +1,13 @@
#include "SdCard.h"

namespace hardware {
namespace sdcard {

SdFat filesystem;

void setup() {
filesystem.begin(SD_SS);
}

}
}

+ 13
- 0
GpsTracker/SdCard.h Bestand weergeven

@@ -0,0 +1,13 @@
#pragma once

#include <SdFat.h>
#include "Pins.h"

namespace hardware {
namespace sdcard {

extern SdFat filesystem;

void setup();
}
}

+ 23
- 0
GpsTracker/SdPositionsBackup.cpp Bestand weergeven

@@ -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();

}

}
}
}

+ 15
- 4
GpsTracker/SdPositionsBackup.h Bestand weergeven

@@ -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();
};
}
}
}

+ 13
- 0
GpsTracker/SdPositionsConfig.cpp Bestand weergeven

@@ -0,0 +1,13 @@
#include "SdPositionsConfig.h"

namespace config {
namespace backup {
namespace sd {

void setup() {

}

}
}
}

+ 23
- 0
GpsTracker/SdPositionsConfig.h Bestand weergeven

@@ -0,0 +1,23 @@
#pragma once

#include <Arduino.h>

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();

}
}
}

Laden…
Annuleren
Opslaan