Modify LogFile to open/close file on SD card status.

Previously, would only open on construction, which was only when the receiver mode changed, which wasn't all that useful.
This commit is contained in:
Jared Boone 2016-01-13 23:11:19 -08:00
parent 30ca545b52
commit bd07e4e7e5
8 changed files with 34 additions and 15 deletions

View file

@ -26,11 +26,24 @@
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
LogFile::LogFile(
const std::string file_path
) : file_path { file_path }
{
open();
sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) {
this->on_sd_card_status(status);
};
}
LogFile::~LogFile() {
sd_card::status_signal -= sd_card_status_signal_token;
close();
}
bool LogFile::open_for_append(const std::string& file_path) {
bool LogFile::open() {
const auto open_result = f_open(&f, file_path.c_str(), FA_WRITE | FA_OPEN_ALWAYS);
if( open_result == FR_OK ) {
const auto seek_result = f_lseek(&f, f_size(&f));
@ -70,3 +83,11 @@ bool LogFile::write(const std::string& message) {
const auto sync_result = f_sync(&f);
return (puts_result >= 0) && (sync_result == FR_OK);
}
void LogFile::on_sd_card_status(const sd_card::Status status) {
if( status == sd_card::Status::Mounted ) {
open();
} else {
close();
}
}