浏览代码

Non tested implementaion of sd card positions backup

tags/v1.2.0
Bertrand Lemasle 7 年前
父节点
当前提交
f712c3b455
共有 8 个文件被更改,包括 90 次插入21 次删除
  1. +2
    -0
      GpsTracker/GpsTracker.ino
  2. +2
    -0
      GpsTracker/Positions.cpp
  3. +2
    -1
      GpsTracker/SdCard.cpp
  4. +1
    -0
      GpsTracker/SdCard.h
  5. +73
    -16
      GpsTracker/SdPositionsBackup.cpp
  6. +5
    -0
      GpsTracker/SdPositionsBackup.h
  7. +2
    -3
      GpsTracker/SdPositionsConfig.cpp
  8. +3
    -1
      GpsTracker/SdPositionsConfig.h

+ 2
- 0
GpsTracker/GpsTracker.ino 查看文件

@@ -14,6 +14,8 @@ void setup() {
rtc::setup(); rtc::setup();
hardware::sdcard::setup(); hardware::sdcard::setup();
hardware::sim808::setup(); hardware::sim808::setup();

positions::setup();
} }


void loop() { void loop() {


+ 2
- 0
GpsTracker/Positions.cpp 查看文件

@@ -29,7 +29,9 @@ 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::sd::SdPositionsBackup(); _backups[0] = new backup::sd::SdPositionsBackup();
_backups[0]->setup();
} }


bool acquire(PositionEntryMetadata &metadata) { bool acquire(PositionEntryMetadata &metadata) {


+ 2
- 1
GpsTracker/SdCard.cpp 查看文件

@@ -4,9 +4,10 @@ namespace hardware {
namespace sdcard { namespace sdcard {


SdFat filesystem; SdFat filesystem;
bool available = false;


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


} }

+ 1
- 0
GpsTracker/SdCard.h 查看文件

@@ -7,6 +7,7 @@ namespace hardware {
namespace sdcard { namespace sdcard {


extern SdFat filesystem; extern SdFat filesystem;
extern bool available;


void setup(); void setup();
} }

+ 73
- 16
GpsTracker/SdPositionsBackup.cpp 查看文件

@@ -5,6 +5,9 @@
#include "Config.h" #include "Config.h"
#include "SdFat.h" #include "SdFat.h"
#include "Positions.h" #include "Positions.h"
#include "Debug.h"

#define LOGGER_NAME "Positions::backup::sd"


namespace positions { namespace positions {
namespace backup { namespace backup {
@@ -20,38 +23,92 @@ namespace positions {
positions::count(sdConfig.lastSavedEntry) > sdConfig.saveThreshold; positions::count(sdConfig.lastSavedEntry) > sdConfig.saveThreshold;
} }


void appendPositions(const File &file, SdPositionConfig_t &sdConfig) {
void getPositionsFileName(uint16_t fileIndex, char *buffer) {
sprintf(buffer, POSITIONS_FILENAME, fileIndex);
}

void ensurePositionsFolder() {
char positionsFolder[] = POSITIONS_FOLDER;

hardware::sdcard::filesystem.chdir();
if (!hardware::sdcard::filesystem.exists(positionsFolder)) {
hardware::sdcard::filesystem.mkdir(positionsFolder, true);
}

hardware::sdcard::filesystem.chdir(positionsFolder);
}

void selectFile(SdPositionConfig_t &sdConfig, File &file) {
char positionFileName[POSITIONS_FILENAME_LENGTH];

if (sdConfig.fileRecords >= sdConfig.maxRecordsPerFile) {
if (file.isOpen()) {
file.close();
config::backup::sd::set(sdConfig);
}

sdConfig.fileIndex++;
sdConfig.filePosition = 0;
sdConfig.fileRecords = 0;
}

if (!file.isOpen()) {
ensurePositionsFolder();
getPositionsFileName(sdConfig.fileIndex, positionFileName);
file.open(positionFileName, O_RDWR | O_CREAT);
}
}

void appendPosition(File &file, SdPositionConfig_t &sdConfig, PositionEntry &entry) {
VERBOSE("appendPosition");

const char fieldTerminator = ',';

file.printField(entry.metadata.batteryLevel, fieldTerminator);
file.printField(entry.metadata.batteryVoltage, fieldTerminator);
file.printField(entry.metadata.temperature, fieldTerminator);
file.printField(entry.metadata.timeToFix, fieldTerminator);
file.printField(static_cast<uint8_t>(entry.metadata.status), fieldTerminator);
file.println(entry.position);

sdConfig.filePosition = file.position();
}

void appendPositions(SdPositionConfig_t &sdConfig) {
VERBOSE("appendPositions");

uint16_t currentEntryIndex = sdConfig.lastSavedEntry + 1; uint16_t currentEntryIndex = sdConfig.lastSavedEntry + 1;
PositionEntry currentEntry; PositionEntry currentEntry;
File file;


hardware::i2c::powerOn(); hardware::i2c::powerOn();
do { do {
if (!positions::get(currentEntryIndex, currentEntry)) break; if (!positions::get(currentEntryIndex, currentEntry)) break;

sdConfig.filePosition = file.position();
} while (true);
selectFile(sdConfig, file);
appendPosition(file, sdConfig, currentEntry);
sdConfig.lastSavedEntry = currentEntryIndex;
} while (positions::moveNext(currentEntryIndex));
hardware::i2c::powerOff(); hardware::i2c::powerOff();


if (file.isOpen()) file.close();
config::backup::sd::set(sdConfig);
} }
} }


void SdPositionsBackup::setup() {
config::backup::sd::setup();
}
void SdPositionsBackup::setup() { }


void SdPositionsBackup::backup() { void SdPositionsBackup::backup() {
SdPositionConfig_t sdConfig;

if (!details::isBackupNeeded(sdConfig)) return;
VERBOSE("backup");


char *filename;
hardware::sdcard::filesystem.chdir("positions");
File file = hardware::sdcard::filesystem.open(filename, O_RDWR | O_CREAT);
if (!hardware::sdcard::available) {
VERBOSE_MSG("backup", "not available");
return;
}


file.seek(sdConfig.filePosition);
file.find('\n');
SdPositionConfig_t sdConfig;


details::appendPositions(file, sdConfig);
if (!details::isBackupNeeded(sdConfig)) return;
details::appendPositions(sdConfig);
} }


} }


+ 5
- 0
GpsTracker/SdPositionsBackup.h 查看文件

@@ -3,6 +3,11 @@
#include "PositionsBackup.h" #include "PositionsBackup.h"
#include "RawSdFile.h" #include "RawSdFile.h"


#define POSITIONS_FOLDER "positions"
#define POSITIONS_FILENAME "positions-%05d.csv"
#define POSITIONS_FILENAME_LENGTH 19


using namespace sd; using namespace sd;


namespace positions { namespace positions {


+ 2
- 3
GpsTracker/SdPositionsConfig.cpp 查看文件

@@ -40,8 +40,6 @@ namespace config {


} }


void setup() { }

SdPositionConfig_t get() { SdPositionConfig_t get() {
if (value.seed != POSITIONS_CONFIG_SEED) details::read(); if (value.seed != POSITIONS_CONFIG_SEED) details::read();


@@ -57,7 +55,8 @@ namespace config {
VERBOSE("reset"); VERBOSE("reset");
SdPositionConfig_t config = { SdPositionConfig_t config = {
POSITIONS_CONFIG_SEED, POSITIONS_CONFIG_SEED,
POSITIONS_CONFIG_DeFAULT_SAVE_THRESHOLD,
POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD,
POSITIONS_CONFIG_DEFAULT_MAX_RECORDS_PER_FILE,
0xFFFF, 0xFFFF,
0, 0,
0, 0,


+ 3
- 1
GpsTracker/SdPositionsConfig.h 查看文件

@@ -4,11 +4,13 @@


#define POSITIONS_CONFIG_FILENAME "positions.config" #define POSITIONS_CONFIG_FILENAME "positions.config"
#define POSITIONS_CONFIG_SEED 45 #define POSITIONS_CONFIG_SEED 45
#define POSITIONS_CONFIG_DeFAULT_SAVE_THRESHOLD 10
#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10
#define POSITIONS_CONFIG_DEFAULT_MAX_RECORDS_PER_FILE 5


struct SdPositionConfig_t { struct SdPositionConfig_t {
uint8_t seed; uint8_t seed;
uint8_t saveThreshold; uint8_t saveThreshold;
uint8_t maxRecordsPerFile;
uint16_t lastSavedEntry; uint16_t lastSavedEntry;
uint16_t fileIndex; uint16_t fileIndex;
uint32_t filePosition; uint32_t filePosition;


正在加载...
取消
保存