mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-01-11 23:39:29 -05:00
Extract general File class from LogFile.
This commit is contained in:
parent
1aa391bac8
commit
fc7a7d753d
@ -176,6 +176,7 @@ CPPSRC = main.cpp \
|
||||
../common/ert_packet.cpp \
|
||||
spectrum_analysis_app.cpp \
|
||||
sd_card.cpp \
|
||||
file.cpp \
|
||||
log_file.cpp \
|
||||
manchester.cpp \
|
||||
string_format.cpp \
|
||||
|
71
firmware/application/file.cpp
Normal file
71
firmware/application/file.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
||||
*
|
||||
* This file is part of PortaPack.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "file.hpp"
|
||||
|
||||
File::~File() {
|
||||
close();
|
||||
}
|
||||
|
||||
bool File::open_for_append(const std::string file_path) {
|
||||
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));
|
||||
if( seek_result == FR_OK ) {
|
||||
return true;
|
||||
} else {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool File::close() {
|
||||
f_close(&f);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::is_ready() {
|
||||
return f_error(&f) == 0;
|
||||
}
|
||||
|
||||
bool File::read(void* const data, const size_t bytes_to_read) {
|
||||
UINT bytes_read = 0;
|
||||
const auto result = f_read(&f, data, bytes_to_read, &bytes_read);
|
||||
return (result == FR_OK) && (bytes_read == bytes_to_read);
|
||||
}
|
||||
|
||||
bool File::write(const void* const data, const size_t bytes_to_write) {
|
||||
UINT bytes_written = 0;
|
||||
const auto result = f_write(&f, data, bytes_to_write, &bytes_written);
|
||||
return (result == FR_OK) && (bytes_written == bytes_to_write);
|
||||
}
|
||||
|
||||
bool File::puts(const std::string string) {
|
||||
const auto result = f_puts(string.c_str(), &f);
|
||||
return (result >= 0);
|
||||
}
|
||||
|
||||
bool File::sync() {
|
||||
const auto result = f_sync(&f);
|
||||
return (result == FR_OK);
|
||||
}
|
52
firmware/application/file.hpp
Normal file
52
firmware/application/file.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
||||
*
|
||||
* This file is part of PortaPack.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef __FILE_H__
|
||||
#define __FILE_H__
|
||||
|
||||
#include "ff.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
class File {
|
||||
public:
|
||||
~File();
|
||||
|
||||
bool open_for_append(const std::string file_path);
|
||||
bool close();
|
||||
|
||||
bool is_ready();
|
||||
|
||||
bool read(void* const data, const size_t bytes_to_read);
|
||||
bool write(const void* const data, const size_t bytes_to_write);
|
||||
|
||||
bool puts(const std::string string);
|
||||
|
||||
bool sync();
|
||||
|
||||
private:
|
||||
const std::string file_path;
|
||||
|
||||
FIL f;
|
||||
};
|
||||
|
||||
#endif/*__FILE_H__*/
|
@ -30,7 +30,7 @@ LogFile::LogFile(
|
||||
const std::string file_path
|
||||
) : file_path { file_path }
|
||||
{
|
||||
open();
|
||||
file.open_for_append(file_path);
|
||||
|
||||
sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) {
|
||||
this->on_sd_card_status(status);
|
||||
@ -40,30 +40,11 @@ LogFile::LogFile(
|
||||
LogFile::~LogFile() {
|
||||
sd_card::status_signal -= sd_card_status_signal_token;
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
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));
|
||||
if( seek_result == FR_OK ) {
|
||||
return true;
|
||||
} else {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LogFile::close() {
|
||||
f_close(&f);
|
||||
return true;
|
||||
file.close();
|
||||
}
|
||||
|
||||
bool LogFile::is_ready() {
|
||||
return !f_error(&f);
|
||||
return file.is_ready();
|
||||
}
|
||||
|
||||
bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
|
||||
@ -72,15 +53,13 @@ bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
|
||||
}
|
||||
|
||||
bool LogFile::write(const std::string& message) {
|
||||
const auto puts_result = f_puts(message.c_str(), &f);
|
||||
const auto sync_result = f_sync(&f);
|
||||
return (puts_result >= 0) && (sync_result == FR_OK);
|
||||
return file.puts(message) && file.sync();
|
||||
}
|
||||
|
||||
void LogFile::on_sd_card_status(const sd_card::Status status) {
|
||||
if( status == sd_card::Status::Mounted ) {
|
||||
open();
|
||||
file.open_for_append(file_path);
|
||||
} else {
|
||||
close();
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "ff.h"
|
||||
#include "file.hpp"
|
||||
#include "sd_card.hpp"
|
||||
|
||||
#include "lpc43xx_cpp.hpp"
|
||||
@ -35,8 +35,6 @@ public:
|
||||
LogFile(const std::string file_path);
|
||||
~LogFile();
|
||||
|
||||
bool open();
|
||||
bool close();
|
||||
bool is_ready();
|
||||
|
||||
bool write_entry(const rtc::RTC& datetime, const std::string& entry);
|
||||
@ -44,7 +42,7 @@ public:
|
||||
private:
|
||||
const std::string file_path;
|
||||
|
||||
FIL f;
|
||||
File file;
|
||||
|
||||
SignalToken sd_card_status_signal_token;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user