diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index a6508c91..964ddf8d 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -21,8 +21,6 @@ #include "ais_app.hpp" -#include "event_m0.hpp" - #include "string_format.hpp" #include "baseband_api.hpp" @@ -305,16 +303,6 @@ AISAppView::AISAppView(NavigationView&) { recent_entry_detail_view.hidden(true); - EventDispatcher::message_map().register_handler(Message::ID::AISPacket, - [this](Message* const p) { - const auto message = static_cast(p); - const ais::Packet packet { message->packet }; - if( packet.is_valid() ) { - this->on_packet(packet); - } - } - ); - target_frequency_ = initial_target_frequency; radio::enable({ @@ -350,8 +338,6 @@ AISAppView::AISAppView(NavigationView&) { AISAppView::~AISAppView() { baseband::stop(); radio::disable(); - - EventDispatcher::message_map().unregister_handler(Message::ID::AISPacket); } void AISAppView::focus() { diff --git a/firmware/application/ais_app.hpp b/firmware/application/ais_app.hpp index f0f81c91..a166f47d 100644 --- a/firmware/application/ais_app.hpp +++ b/firmware/application/ais_app.hpp @@ -25,6 +25,8 @@ #include "ui_widget.hpp" #include "ui_navigation.hpp" +#include "event_m0.hpp" + #include "log_file.hpp" #include "ais_packet.hpp" @@ -173,6 +175,17 @@ private: } }; + MessageHandlerRegistration message_handler_packet { + Message::ID::AISPacket, + [this](Message* const p) { + const auto message = static_cast(p); + const ais::Packet packet { message->packet }; + if( packet.is_valid() ) { + this->on_packet(packet); + } + } + }; + uint32_t target_frequency_ = initial_target_frequency; void on_packet(const ais::Packet& packet); diff --git a/firmware/application/ert_app.cpp b/firmware/application/ert_app.cpp index bbdf5848..86b93339 100644 --- a/firmware/application/ert_app.cpp +++ b/firmware/application/ert_app.cpp @@ -21,8 +21,6 @@ #include "ert_app.hpp" -#include "event_m0.hpp" - #include "baseband_api.hpp" #include "manchester.hpp" @@ -135,14 +133,6 @@ ERTAppView::ERTAppView(NavigationView&) { &recent_entries_view, } }); - EventDispatcher::message_map().register_handler(Message::ID::ERTPacket, - [this](Message* const p) { - const auto message = static_cast(p); - const ert::Packet packet { message->type, message->packet }; - this->on_packet(packet); - } - ); - radio::enable({ initial_target_frequency, sampling_rate, @@ -164,8 +154,6 @@ ERTAppView::ERTAppView(NavigationView&) { ERTAppView::~ERTAppView() { baseband::stop(); radio::disable(); - - EventDispatcher::message_map().unregister_handler(Message::ID::ERTPacket); } void ERTAppView::focus() { diff --git a/firmware/application/ert_app.hpp b/firmware/application/ert_app.hpp index e280f379..403d9b63 100644 --- a/firmware/application/ert_app.hpp +++ b/firmware/application/ert_app.hpp @@ -24,6 +24,8 @@ #include "ui_navigation.hpp" +#include "event_m0.hpp" + #include "log_file.hpp" #include "ert_packet.hpp" @@ -124,6 +126,15 @@ private: ERTRecentEntriesView recent_entries_view { recent }; + MessageHandlerRegistration message_handler_packet { + Message::ID::ERTPacket, + [this](Message* const p) { + const auto message = static_cast(p); + const ert::Packet packet { message->type, message->packet }; + this->on_packet(packet); + } + }; + void on_packet(const ert::Packet& packet); void on_show_list(); }; diff --git a/firmware/application/event_m0.hpp b/firmware/application/event_m0.hpp index f793eaf7..704e774e 100644 --- a/firmware/application/event_m0.hpp +++ b/firmware/application/event_m0.hpp @@ -117,4 +117,23 @@ private: void init_message_queues(); }; +class MessageHandlerRegistration { +public: + template + MessageHandlerRegistration( + const Message::ID message_id, + Callback callback + ) : message_id { message_id } + { + EventDispatcher::message_map().register_handler(message_id, callback); + } + + ~MessageHandlerRegistration() { + EventDispatcher::message_map().unregister_handler(message_id); + } + +private: + const Message::ID message_id; +}; + #endif/*__EVENT_M0_H__*/ diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index bc37759f..63f9a822 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -51,6 +51,32 @@ #include +static void event_loop() { + ui::Context context; + ui::SystemView system_view { + context, + portapack::display.screen_rect() + }; + ui::Painter painter; + + EventDispatcher event_dispatcher { &system_view, painter, context }; + + MessageHandlerRegistration message_handler_shutdown { + Message::ID::Shutdown, + [&event_dispatcher](const Message* const) { + event_dispatcher.request_stop(); + } + }; + MessageHandlerRegistration message_handler_display_sleep { + Message::ID::DisplaySleep, + [&event_dispatcher](const Message* const) { + event_dispatcher.set_display_sleep(true); + } + }; + + event_dispatcher.run(); +} + int main(void) { portapack::init(); @@ -63,32 +89,13 @@ int main(void) { sdcStart(&SDCD1, nullptr); - ui::Context context; - ui::SystemView system_view { - context, - portapack::display.screen_rect() - }; - ui::Painter painter; - EventDispatcher event_dispatcher { &system_view, painter, context }; - - EventDispatcher::message_map().register_handler(Message::ID::Shutdown, - [&event_dispatcher](const Message* const) { - event_dispatcher.request_stop(); - } - ); - EventDispatcher::message_map().register_handler(Message::ID::DisplaySleep, - [&event_dispatcher](const Message* const) { - event_dispatcher.set_display_sleep(true); - } - ); - m4_init(portapack::spi_flash::baseband, portapack::memory::map::m4_code); controls_init(); lcd_frame_sync_configure(); rtc_interrupt_enable(); - event_dispatcher.run(); + event_loop(); sdcDisconnect(&SDCD1); sdcStop(&SDCD1); diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index abe27450..46f04537 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -21,8 +21,6 @@ #include "tpms_app.hpp" -#include "event_m0.hpp" - #include "baseband_api.hpp" #include "string_format.hpp" @@ -152,14 +150,6 @@ TPMSAppView::TPMSAppView(NavigationView&) { &recent_entries_view, } }); - EventDispatcher::message_map().register_handler(Message::ID::TPMSPacket, - [this](Message* const p) { - const auto message = static_cast(p); - const tpms::Packet packet { message->packet }; - this->on_packet(message->signal_type, packet); - } - ); - radio::enable({ tuning_frequency(), sampling_rate, @@ -181,8 +171,6 @@ TPMSAppView::TPMSAppView(NavigationView&) { TPMSAppView::~TPMSAppView() { baseband::stop(); radio::disable(); - - EventDispatcher::message_map().unregister_handler(Message::ID::TPMSPacket); } void TPMSAppView::focus() { diff --git a/firmware/application/tpms_app.hpp b/firmware/application/tpms_app.hpp index 24516908..ea0e9b39 100644 --- a/firmware/application/tpms_app.hpp +++ b/firmware/application/tpms_app.hpp @@ -25,6 +25,8 @@ #include "ui_widget.hpp" #include "ui_navigation.hpp" +#include "event_m0.hpp" + #include "log_file.hpp" #include "recent_entries.hpp" @@ -102,6 +104,15 @@ private: static constexpr uint32_t sampling_rate = 2457600; static constexpr uint32_t baseband_bandwidth = 1750000; + MessageHandlerRegistration message_handler_packet { + Message::ID::TPMSPacket, + [this](Message* const p) { + const auto message = static_cast(p); + const tpms::Packet packet { message->packet }; + this->on_packet(message->signal_type, packet); + } + }; + TPMSRecentEntries recent; std::unique_ptr logger; diff --git a/firmware/application/ui_audio.cpp b/firmware/application/ui_audio.cpp index 65fd3832..389a65e1 100644 --- a/firmware/application/ui_audio.cpp +++ b/firmware/application/ui_audio.cpp @@ -21,26 +21,12 @@ #include "ui_audio.hpp" -#include "event_m0.hpp" - #include "utility.hpp" #include namespace ui { -void Audio::on_show() { - EventDispatcher::message_map().register_handler(Message::ID::AudioStatistics, - [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - } - ); -} - -void Audio::on_hide() { - EventDispatcher::message_map().unregister_handler(Message::ID::AudioStatistics); -} - void Audio::paint(Painter& painter) { const auto r = screen_rect(); diff --git a/firmware/application/ui_audio.hpp b/firmware/application/ui_audio.hpp index 00d7f908..e1ae5ad7 100644 --- a/firmware/application/ui_audio.hpp +++ b/firmware/application/ui_audio.hpp @@ -26,6 +26,8 @@ #include "ui_widget.hpp" #include "ui_painter.hpp" +#include "event_m0.hpp" + #include "message.hpp" #include @@ -42,15 +44,19 @@ public: { } - void on_show() override; - void on_hide() override; - void paint(Painter& painter) override; private: int32_t rms_db_; int32_t max_db_; + MessageHandlerRegistration message_handler_statistics { + Message::ID::AudioStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + }; + void on_statistics_update(const AudioStatistics& statistics); }; diff --git a/firmware/application/ui_baseband_stats_view.cpp b/firmware/application/ui_baseband_stats_view.cpp index 0d576e81..44774465 100644 --- a/firmware/application/ui_baseband_stats_view.cpp +++ b/firmware/application/ui_baseband_stats_view.cpp @@ -21,8 +21,6 @@ #include "ui_baseband_stats_view.hpp" -#include "event_m0.hpp" - #include #include @@ -41,19 +39,6 @@ BasebandStatsView::BasebandStatsView() { } }); } -void BasebandStatsView::on_show() { - EventDispatcher::message_map().register_handler(Message::ID::BasebandStatistics, - [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - } - ); -} - -void BasebandStatsView::on_hide() { - EventDispatcher::message_map().unregister_handler(Message::ID::BasebandStatistics); -} - - static std::string ticks_to_percent_string(const uint32_t ticks) { constexpr size_t decimal_digits = 1; constexpr size_t decimal_factor = decimal_digits * 10; diff --git a/firmware/application/ui_baseband_stats_view.hpp b/firmware/application/ui_baseband_stats_view.hpp index 70026618..2c07c20b 100644 --- a/firmware/application/ui_baseband_stats_view.hpp +++ b/firmware/application/ui_baseband_stats_view.hpp @@ -23,6 +23,9 @@ #define __UI_BASEBAND_STATS_VIEW_H__ #include "ui_widget.hpp" + +#include "event_m0.hpp" + #include "message.hpp" namespace ui { @@ -31,15 +34,19 @@ class BasebandStatsView : public View { public: BasebandStatsView(); - void on_show() override; - void on_hide() override; - private: Text text_stats { { 0 * 8, 0, (4 * 4 + 3) * 8, 1 * 16 }, "", }; + MessageHandlerRegistration message_handler_stats { + Message::ID::BasebandStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + }; + void on_statistics_update(const BasebandStatistics& statistics); }; diff --git a/firmware/application/ui_channel.cpp b/firmware/application/ui_channel.cpp index 62f1b49a..3d21e535 100644 --- a/firmware/application/ui_channel.cpp +++ b/firmware/application/ui_channel.cpp @@ -21,26 +21,12 @@ #include "ui_channel.hpp" -#include "event_m0.hpp" - #include "utility.hpp" #include namespace ui { -void Channel::on_show() { - EventDispatcher::message_map().register_handler(Message::ID::ChannelStatistics, - [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - } - ); -} - -void Channel::on_hide() { - EventDispatcher::message_map().unregister_handler(Message::ID::ChannelStatistics); -} - void Channel::paint(Painter& painter) { const auto r = screen_rect(); diff --git a/firmware/application/ui_channel.hpp b/firmware/application/ui_channel.hpp index 3b7f4969..bf8e832e 100644 --- a/firmware/application/ui_channel.hpp +++ b/firmware/application/ui_channel.hpp @@ -26,6 +26,8 @@ #include "ui_widget.hpp" #include "ui_painter.hpp" +#include "event_m0.hpp" + #include "message.hpp" #include @@ -41,14 +43,18 @@ public: { } - void on_show() override; - void on_hide() override; - void paint(Painter& painter) override; private: int32_t max_db_; + MessageHandlerRegistration message_handler_stats { + Message::ID::ChannelStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + }; + void on_statistics_update(const ChannelStatistics& statistics); }; diff --git a/firmware/application/ui_rssi.cpp b/firmware/application/ui_rssi.cpp index a5646863..ab506906 100644 --- a/firmware/application/ui_rssi.cpp +++ b/firmware/application/ui_rssi.cpp @@ -21,26 +21,12 @@ #include "ui_rssi.hpp" -#include "event_m0.hpp" - #include "utility.hpp" #include namespace ui { -void RSSI::on_show() { - EventDispatcher::message_map().register_handler(Message::ID::RSSIStatistics, - [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - } - ); -} - -void RSSI::on_hide() { - EventDispatcher::message_map().unregister_handler(Message::ID::RSSIStatistics); -} - void RSSI::paint(Painter& painter) { const auto r = screen_rect(); diff --git a/firmware/application/ui_rssi.hpp b/firmware/application/ui_rssi.hpp index 5c457039..08eeadcb 100644 --- a/firmware/application/ui_rssi.hpp +++ b/firmware/application/ui_rssi.hpp @@ -26,6 +26,8 @@ #include "ui_widget.hpp" #include "ui_painter.hpp" +#include "event_m0.hpp" + #include "message.hpp" #include @@ -43,9 +45,6 @@ public: { } - void on_show() override; - void on_hide() override; - void paint(Painter& painter) override; private: @@ -53,6 +52,13 @@ private: int32_t avg_; int32_t max_; + MessageHandlerRegistration message_handler_stats { + Message::ID::RSSIStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + }; + void on_statistics_update(const RSSIStatistics& statistics); }; diff --git a/firmware/application/ui_spectrum.cpp b/firmware/application/ui_spectrum.cpp index a3b7c896..448e0766 100644 --- a/firmware/application/ui_spectrum.cpp +++ b/firmware/application/ui_spectrum.cpp @@ -21,8 +21,6 @@ #include "ui_spectrum.hpp" -#include "event_m0.hpp" - #include "spectrum_color_lut.hpp" #include "portapack.hpp" @@ -236,31 +234,11 @@ WaterfallWidget::WaterfallWidget() { } void WaterfallWidget::on_show() { - EventDispatcher::message_map().register_handler(Message::ID::ChannelSpectrumConfig, - [this](const Message* const p) { - const auto message = *reinterpret_cast(p); - this->fifo = message.fifo; - } - ); - EventDispatcher::message_map().register_handler(Message::ID::DisplayFrameSync, - [this](const Message* const) { - if( this->fifo ) { - ChannelSpectrum channel_spectrum; - while( fifo->out(channel_spectrum) ) { - this->on_channel_spectrum(channel_spectrum); - } - } - } - ); - baseband::spectrum_streaming_start(); } void WaterfallWidget::on_hide() { baseband::spectrum_streaming_stop(); - - EventDispatcher::message_map().unregister_handler(Message::ID::DisplayFrameSync); - EventDispatcher::message_map().unregister_handler(Message::ID::ChannelSpectrumConfig); } void WaterfallWidget::set_parent_rect(const Rect new_parent_rect) { diff --git a/firmware/application/ui_spectrum.hpp b/firmware/application/ui_spectrum.hpp index 8b0381f4..70fe83d5 100644 --- a/firmware/application/ui_spectrum.hpp +++ b/firmware/application/ui_spectrum.hpp @@ -25,6 +25,8 @@ #include "ui.hpp" #include "ui_widget.hpp" +#include "event_m0.hpp" + #include "message.hpp" #include @@ -86,6 +88,25 @@ private: FrequencyScale frequency_scale; ChannelSpectrumFIFO* fifo { nullptr }; + MessageHandlerRegistration message_handler_spectrum_config { + Message::ID::ChannelSpectrumConfig, + [this](const Message* const p) { + const auto message = *reinterpret_cast(p); + this->fifo = message.fifo; + } + }; + MessageHandlerRegistration message_handler_frame_sync { + Message::ID::DisplayFrameSync, + [this](const Message* const) { + if( this->fifo ) { + ChannelSpectrum channel_spectrum; + while( fifo->out(channel_spectrum) ) { + this->on_channel_spectrum(channel_spectrum); + } + } + } + }; + void on_channel_spectrum(const ChannelSpectrum& spectrum); };