From b5fbab8e29990aaab06a9f111054c1b4287af747 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 30 Apr 2016 16:26:49 -0700 Subject: [PATCH] Pass a File into CaptureThread. Groundwork for having a general output-stream interface. --- firmware/application/capture_thread.hpp | 17 ++++++----------- firmware/application/ui_record_view.cpp | 8 +++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/firmware/application/capture_thread.hpp b/firmware/application/capture_thread.hpp index 0d09f2e5..5dc29b00 100644 --- a/firmware/application/capture_thread.hpp +++ b/firmware/application/capture_thread.hpp @@ -71,11 +71,11 @@ private: class CaptureThread { public: CaptureThread( - std::string file_path, + std::unique_ptr file, size_t write_size_log2, size_t buffer_count_log2 ) : config { write_size_log2, buffer_count_log2 }, - file_path { std::move(file_path) } + file { std::move(file) } { // Need significant stack for FATFS thread = chThdCreateFromHeap(NULL, 2048, NORMALPRIO + 10, CaptureThread::static_fn, this); @@ -109,7 +109,7 @@ public: private: CaptureConfig config; - const std::string file_path; + std::unique_ptr file; static Thread* thread; static msg_t static_fn(void* arg) { @@ -118,11 +118,6 @@ private: } msg_t run() { - File file { file_path, File::openmode::out | File::openmode::binary | File::openmode::trunc }; - if( !file.is_open() ) { - return false; - } - const size_t write_size = 1U << config.write_size_log2; const auto write_buffer = std::make_unique(write_size); if( !write_buffer ) { @@ -133,7 +128,7 @@ private: while( !chThdShouldTerminate() ) { if( stream.available() >= write_size ) { - if( !transfer(stream, file, write_buffer.get(), write_size) ) { + if( !transfer(stream, write_buffer.get(), write_size) ) { return false; } } else { @@ -144,14 +139,14 @@ private: return true; } - bool transfer(StreamOutput& stream, File& file, uint8_t* const write_buffer, const size_t write_size) { + bool transfer(StreamOutput& stream, uint8_t* const write_buffer, const size_t write_size) { bool success = false; led_usb.on(); const auto bytes_to_write = stream.read(write_buffer, write_size); if( bytes_to_write == write_size ) { - if( file.write(write_buffer, write_size) ) { + if( file->write(write_buffer, write_size) ) { success = true; } } diff --git a/firmware/application/ui_record_view.cpp b/firmware/application/ui_record_view.cpp index 07f40f17..04c71ef8 100644 --- a/firmware/application/ui_record_view.cpp +++ b/firmware/application/ui_record_view.cpp @@ -95,7 +95,13 @@ void RecordView::start() { write_metadata_file(filename_stem + ".TXT"); - capture_thread = std::make_unique(filename_stem + filename_extension, buffer_size_k, buffer_count_k); + capture_thread = std::make_unique( + std::make_unique( + filename_stem + filename_extension, + File::openmode::out | File::openmode::binary | File::openmode::trunc + ), + buffer_size_k, buffer_count_k + ); button_record.set_bitmap(&bitmap_stop); }