@@ -5,53 +5,55 @@ | |||||
#define LOGGER_NAME "Config" | #define LOGGER_NAME "Config" | ||||
namespace 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(); | |||||
} | |||||
} | |||||
} | } |
@@ -28,18 +28,19 @@ namespace config { | |||||
static const sleepTimings_t defaultSleepTimings[] PROGMEM = { | static const sleepTimings_t defaultSleepTimings[] PROGMEM = { | ||||
{ 5, SLEEP_DEFAULT_TIME_SECONDS }, | { 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(); | |||||
} | |||||
} | } |
@@ -180,7 +180,7 @@ namespace debug { | |||||
} | } | ||||
void getAndDisplayEepromConfig() { | void getAndDisplayEepromConfig() { | ||||
config::get(); | |||||
config::main::get(); | |||||
} | } | ||||
void getAndDisplayEepromContent() { | void getAndDisplayEepromContent() { | ||||
@@ -200,7 +200,7 @@ namespace debug { | |||||
} | } | ||||
void getAndDisplayEepromPositions() { | void getAndDisplayEepromPositions() { | ||||
uint16_t currentEntryIndex = config::get().firstEntry; | |||||
uint16_t currentEntryIndex = config::main::get().firstEntry; | |||||
PositionEntry currentEntry; | PositionEntry currentEntry; | ||||
hardware::i2c::powerOn(); | hardware::i2c::powerOn(); | ||||
@@ -212,7 +212,7 @@ namespace debug { | |||||
} | } | ||||
void getAndDisplayEepromLastPosition() { | void getAndDisplayEepromLastPosition() { | ||||
uint16_t lastEntryIndex = config::get().lastEntry; | |||||
uint16_t lastEntryIndex = config::main::get().lastEntry; | |||||
PositionEntry lastEntry; | PositionEntry lastEntry; | ||||
positions::get(lastEntryIndex, lastEntry); | positions::get(lastEntryIndex, lastEntry); | ||||
@@ -7,5 +7,6 @@ | |||||
#include "Debug.h" | #include "Debug.h" | ||||
#include "Config.h" | #include "Config.h" | ||||
#include "Core.h" | #include "Core.h" | ||||
#include "SdCard.h" | |||||
#define LOGGER_NAME "GpsTracker" | #define LOGGER_NAME "GpsTracker" |
@@ -12,6 +12,7 @@ void setup() { | |||||
#endif | #endif | ||||
rtc::setup(); | rtc::setup(); | ||||
hardware::sdcard::setup(); | |||||
hardware::sim808::setup(); | hardware::sim808::setup(); | ||||
} | } | ||||
@@ -55,7 +56,7 @@ void loop() { | |||||
debug::getAndDisplayEepromConfig(); | debug::getAndDisplayEepromConfig(); | ||||
break; | break; | ||||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG: | case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG: | ||||
config::reset(); | |||||
config::main::reset(); | |||||
break; | break; | ||||
case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT: | case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT: | ||||
debug::getAndDisplayEepromContent(); | debug::getAndDisplayEepromContent(); | ||||
@@ -65,7 +65,7 @@ namespace hardware { | |||||
VERBOSE("networkPowerOn"); | VERBOSE("networkPowerOn"); | ||||
powerOn(); | powerOn(); | ||||
device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); | device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); | ||||
device.enableGprs(config::get().apn); | |||||
device.enableGprs(config::main::get().apn); | |||||
} | } | ||||
void networkPowerOff() { | void networkPowerOff() { | ||||
@@ -29,7 +29,7 @@ namespace positions { | |||||
//TODO : enable/disable based on config | //TODO : enable/disable based on config | ||||
_backupLength = 1; | _backupLength = 1; | ||||
_backups = new backup::PositionsBackup*[_backupLength]; | _backups = new backup::PositionsBackup*[_backupLength]; | ||||
_backups[0] = new backup::SdPositionsbackup(); | |||||
_backups[0] = new backup::sd::SdPositionsBackup(); | |||||
} | } | ||||
bool acquire(PositionEntryMetadata &metadata) { | bool acquire(PositionEntryMetadata &metadata) { | ||||
@@ -70,7 +70,7 @@ namespace positions { | |||||
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); | strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); | ||||
hardware::i2c::powerOn(); | hardware::i2c::powerOn(); | ||||
Config config = config::get(); | |||||
Config config = config::main::get(); | |||||
config.lastEntry++; | config.lastEntry++; | ||||
if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; | 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); | 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(); | hardware::i2c::powerOff(); | ||||
} | } | ||||
@@ -101,7 +101,7 @@ namespace positions { | |||||
} | } | ||||
bool moveNext(uint16_t &index) { | 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 | if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way | ||||
else index++; | else index++; | ||||
@@ -5,6 +5,7 @@ namespace positions { | |||||
class PositionsBackup { | class PositionsBackup { | ||||
public: | public: | ||||
virtual void setup(); | |||||
virtual void backup(); | virtual void backup(); | ||||
}; | }; | ||||
@@ -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); | |||||
} | |||||
} |
@@ -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)); | |||||
} | |||||
}; | |||||
} |
@@ -0,0 +1,13 @@ | |||||
#include "SdCard.h" | |||||
namespace hardware { | |||||
namespace sdcard { | |||||
SdFat filesystem; | |||||
void setup() { | |||||
filesystem.begin(SD_SS); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,13 @@ | |||||
#pragma once | |||||
#include <SdFat.h> | |||||
#include "Pins.h" | |||||
namespace hardware { | |||||
namespace sdcard { | |||||
extern SdFat filesystem; | |||||
void setup(); | |||||
} | |||||
} |
@@ -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(); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -1,13 +1,24 @@ | |||||
#pragma once | #pragma once | ||||
#include "PositionsBackup.h" | #include "PositionsBackup.h" | ||||
#include "RawSdFile.h" | |||||
#define POSITIONS_CONFIG_FILENAME "positions.config" | |||||
using namespace sd; | |||||
namespace positions { | namespace positions { | ||||
namespace backup { | namespace backup { | ||||
namespace sd { | |||||
class SdPositionsBackup : public PositionsBackup { | |||||
private: | |||||
RawSdFile * _config; | |||||
public: | |||||
void setup(); | |||||
void backup(); | |||||
}; | |||||
class SdPositionsbackup : public PositionsBackup { | |||||
public: | |||||
void backup(); | |||||
}; | |||||
} | |||||
} | } | ||||
} | } |
@@ -0,0 +1,13 @@ | |||||
#include "SdPositionsConfig.h" | |||||
namespace config { | |||||
namespace backup { | |||||
namespace sd { | |||||
void setup() { | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -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(); | |||||
} | |||||
} | |||||
} |