Abstract CaptureThread target class into Writer, implement RawFileWriter.

This commit is contained in:
Jared Boone 2016-04-30 16:34:50 -07:00
parent b5fbab8e29
commit 6a17be1f2c
2 changed files with 27 additions and 7 deletions

View File

@ -35,6 +35,27 @@ using namespace hackrf::one;
#include <cstring> #include <cstring>
class Writer {
public:
virtual bool write(const void* const write_buffer, const size_t write_size) = 0;
};
class RawFileWriter : public Writer {
public:
RawFileWriter(
const std::string& filename
) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc }
{
}
bool write(const void* const write_buffer, const size_t write_size) override {
return file.write(write_buffer, write_size);
}
private:
File file;
};
class StreamOutput { class StreamOutput {
public: public:
StreamOutput( StreamOutput(
@ -71,11 +92,11 @@ private:
class CaptureThread { class CaptureThread {
public: public:
CaptureThread( CaptureThread(
std::unique_ptr<File> file, std::unique_ptr<Writer> writer,
size_t write_size_log2, size_t write_size_log2,
size_t buffer_count_log2 size_t buffer_count_log2
) : config { write_size_log2, buffer_count_log2 }, ) : config { write_size_log2, buffer_count_log2 },
file { std::move(file) } writer { std::move(writer) }
{ {
// Need significant stack for FATFS // Need significant stack for FATFS
thread = chThdCreateFromHeap(NULL, 2048, NORMALPRIO + 10, CaptureThread::static_fn, this); thread = chThdCreateFromHeap(NULL, 2048, NORMALPRIO + 10, CaptureThread::static_fn, this);
@ -109,7 +130,7 @@ public:
private: private:
CaptureConfig config; CaptureConfig config;
std::unique_ptr<File> file; std::unique_ptr<Writer> writer;
static Thread* thread; static Thread* thread;
static msg_t static_fn(void* arg) { static msg_t static_fn(void* arg) {
@ -146,7 +167,7 @@ private:
const auto bytes_to_write = stream.read(write_buffer, write_size); const auto bytes_to_write = stream.read(write_buffer, write_size);
if( bytes_to_write == write_size ) { if( bytes_to_write == write_size ) {
if( file->write(write_buffer, write_size) ) { if( writer->write(write_buffer, write_size) ) {
success = true; success = true;
} }
} }

View File

@ -96,9 +96,8 @@ void RecordView::start() {
write_metadata_file(filename_stem + ".TXT"); write_metadata_file(filename_stem + ".TXT");
capture_thread = std::make_unique<CaptureThread>( capture_thread = std::make_unique<CaptureThread>(
std::make_unique<File>( std::make_unique<RawFileWriter>(
filename_stem + filename_extension, filename_stem + filename_extension
File::openmode::out | File::openmode::binary | File::openmode::trunc
), ),
buffer_size_k, buffer_count_k buffer_size_k, buffer_count_k
); );