diff --git a/firmware/application/capture_thread.cpp b/firmware/application/capture_thread.cpp index 8fab7940..e905deb9 100644 --- a/firmware/application/capture_thread.cpp +++ b/firmware/application/capture_thread.cpp @@ -77,9 +77,11 @@ Thread* CaptureThread::thread = nullptr; CaptureThread::CaptureThread( std::unique_ptr writer, size_t write_size, - size_t buffer_count + size_t buffer_count, + std::function&& error_callback ) : config { write_size, buffer_count }, - writer { std::move(writer) } + writer { std::move(writer) }, + error_callback { std::move(error_callback) } { // Need significant stack for FATFS thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this); @@ -94,10 +96,6 @@ CaptureThread::~CaptureThread() { } } -const Optional& CaptureThread::error() const { - return last_error; -} - void CaptureThread::check_fifo_isr() { // TODO: Prevent over-signalling by transmitting a set of // flags from the baseband core. diff --git a/firmware/application/capture_thread.hpp b/firmware/application/capture_thread.hpp index 2b35423e..ce4c1485 100644 --- a/firmware/application/capture_thread.hpp +++ b/firmware/application/capture_thread.hpp @@ -44,7 +44,8 @@ public: CaptureThread( std::unique_ptr writer, size_t write_size, - size_t buffer_count + size_t buffer_count, + std::function&& error_callback ); ~CaptureThread(); @@ -52,8 +53,6 @@ public: return config; } - const Optional& error() const; - static void check_fifo_isr(); private: @@ -61,12 +60,15 @@ private: CaptureConfig config; std::unique_ptr writer; - Optional last_error; + std::function error_callback; static Thread* thread; static msg_t static_fn(void* arg) { auto obj = static_cast(arg); - obj->last_error = obj->run(); + const auto error = obj->run(); + if( error.is_valid() && obj->error_callback ) { + obj->error_callback(error.value()); + } return 0; } diff --git a/firmware/application/ui_record_view.cpp b/firmware/application/ui_record_view.cpp index 4d385e15..a11aefd3 100644 --- a/firmware/application/ui_record_view.cpp +++ b/firmware/application/ui_record_view.cpp @@ -269,7 +269,11 @@ void RecordView::start() { button_record.set_bitmap(&bitmap_stop); capture_thread = std::make_unique( std::move(writer), - write_size, buffer_count + write_size, buffer_count, + [](File::Error error) { + CaptureThreadErrorMessage message { error.code() }; + EventDispatcher::send_message(message); + } ); } } @@ -301,11 +305,6 @@ Optional RecordView::write_metadata_file(const std::string& filenam void RecordView::on_tick_second() { if( is_active() ) { - const auto error = capture_thread->error(); - if( error.is_valid() ) { - stop(); - report_error(error.value().what()); - } const auto dropped_percent = std::min(99U, capture_thread->state().dropped_percent()); const auto s = to_string_dec_uint(dropped_percent, 2, ' ') + "\%"; text_record_dropped.set(s); @@ -328,6 +327,7 @@ void RecordView::on_tick_second() { } void RecordView::report_error(const std::string& message) { + stop(); if( on_error ) { on_error(message); } diff --git a/firmware/application/ui_record_view.hpp b/firmware/application/ui_record_view.hpp index ddc72e9f..d6111bf1 100644 --- a/firmware/application/ui_record_view.hpp +++ b/firmware/application/ui_record_view.hpp @@ -115,6 +115,18 @@ private: }; std::unique_ptr capture_thread; + + MessageHandlerRegistration message_handler_capture_thread_error { + Message::ID::CaptureThreadError, + [this](const Message* const p) { + const auto message = *reinterpret_cast(p); + this->on_capture_thread_error(message.error); + } + }; + + void on_capture_thread_error(File::Error error) { + report_error(error.what()); + } }; } /* namespace ui */ diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index 640cd1d0..8ee6cfe7 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -64,6 +64,7 @@ public: SpectrumStreamingConfig = 15, DisplaySleep = 16, CaptureConfig = 17, + CaptureThreadError = 18, MAX }; @@ -494,4 +495,16 @@ public: CaptureConfig* const config; }; +class CaptureThreadErrorMessage : public Message { +public: + constexpr CaptureThreadErrorMessage( + uint32_t error + ) : Message { ID::CaptureThreadError }, + error { error } + { + } + + uint32_t error; +}; + #endif/*__MESSAGE_H__*/