From a7226820d02df32ab1ca3b8e3bc56298cbb652ba Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 14 Aug 2015 17:31:23 -0700 Subject: [PATCH] Hide implementation of MessageHandlerMap. --- firmware/application/main.cpp | 5 +- firmware/application/ui_audio.cpp | 10 ++-- firmware/application/ui_channel.cpp | 10 ++-- firmware/application/ui_debug.cpp | 10 ++-- firmware/application/ui_receiver.cpp | 14 +++-- firmware/application/ui_rssi.cpp | 10 ++-- firmware/application/ui_spectrum.hpp | 10 ++-- firmware/baseband/main.cpp | 89 ++++++++++++++-------------- firmware/common/message.hpp | 15 +++-- 9 files changed, 95 insertions(+), 78 deletions(-) diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index 8bce016c..3d5c621d 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -184,10 +184,7 @@ private: while( !shared_memory.application_queue.is_empty() ) { auto message = shared_memory.application_queue.pop(); - auto& fn = context.message_map[message->id]; - if( fn ) { - fn(message); - } + context.message_map.send(message); message->state = Message::State::Free; } diff --git a/firmware/application/ui_audio.cpp b/firmware/application/ui_audio.cpp index 08176d0d..25f06d70 100644 --- a/firmware/application/ui_audio.cpp +++ b/firmware/application/ui_audio.cpp @@ -26,13 +26,15 @@ namespace ui { void Audio::on_show() { - context().message_map[Message::ID::AudioStatistics] = [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - }; + context().message_map.register_handler(Message::ID::AudioStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + ); } void Audio::on_hide() { - context().message_map[Message::ID::AudioStatistics] = nullptr; + context().message_map.unregister_handler(Message::ID::AudioStatistics); } void Audio::paint(Painter& painter) { diff --git a/firmware/application/ui_channel.cpp b/firmware/application/ui_channel.cpp index d1fc71f6..0369c5ae 100644 --- a/firmware/application/ui_channel.cpp +++ b/firmware/application/ui_channel.cpp @@ -26,13 +26,15 @@ namespace ui { void Channel::on_show() { - context().message_map[Message::ID::ChannelStatistics] = [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - }; + context().message_map.register_handler(Message::ID::ChannelStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + ); } void Channel::on_hide() { - context().message_map[Message::ID::ChannelStatistics] = nullptr; + context().message_map.unregister_handler(Message::ID::ChannelStatistics); } void Channel::paint(Painter& painter) { diff --git a/firmware/application/ui_debug.cpp b/firmware/application/ui_debug.cpp index a8ba25ef..d337a706 100644 --- a/firmware/application/ui_debug.cpp +++ b/firmware/application/ui_debug.cpp @@ -40,13 +40,15 @@ BasebandStatsView::BasebandStatsView() { } void BasebandStatsView::on_show() { - context().message_map[Message::ID::BasebandStatistics] = [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - }; + context().message_map.register_handler(Message::ID::BasebandStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + ); } void BasebandStatsView::on_hide() { - context().message_map[Message::ID::BasebandStatistics] = nullptr; + context().message_map.unregister_handler(Message::ID::BasebandStatistics); } diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index 6cc3b5c0..96fbe5d9 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -481,15 +481,17 @@ ReceiverView::ReceiverView( receiver_model.enable(); - context().message_map[Message::ID::FSKPacket] = [](const Message* const p) { - const auto message = static_cast(p); - (void)message; - }; + context().message_map.register_handler(Message::ID::FSKPacket, + [](const Message* const p) { + const auto message = static_cast(p); + (void)message; + } + ); } ReceiverView::~ReceiverView() { - context().message_map[Message::ID::FSKPacket] = nullptr; - + context().message_map.unregister_handler(Message::ID::FSKPacket); + // TODO: Manipulating audio codec here, and in ui_receiver.cpp. Good to do // both? audio_codec.headphone_mute(); diff --git a/firmware/application/ui_rssi.cpp b/firmware/application/ui_rssi.cpp index f44c75df..72e58d97 100644 --- a/firmware/application/ui_rssi.cpp +++ b/firmware/application/ui_rssi.cpp @@ -26,13 +26,15 @@ namespace ui { void RSSI::on_show() { - context().message_map[Message::ID::RSSIStatistics] = [this](const Message* const p) { - this->on_statistics_update(static_cast(p)->statistics); - }; + context().message_map.register_handler(Message::ID::RSSIStatistics, + [this](const Message* const p) { + this->on_statistics_update(static_cast(p)->statistics); + } + ); } void RSSI::on_hide() { - context().message_map[Message::ID::RSSIStatistics] = nullptr; + context().message_map.unregister_handler(Message::ID::RSSIStatistics); } void RSSI::paint(Painter& painter) { diff --git a/firmware/application/ui_spectrum.hpp b/firmware/application/ui_spectrum.hpp index 029c0c5c..28a2acd2 100644 --- a/firmware/application/ui_spectrum.hpp +++ b/firmware/application/ui_spectrum.hpp @@ -233,13 +233,15 @@ public: } void on_show() override { - context().message_map[Message::ID::ChannelSpectrum] = [this](const Message* const p) { - this->on_channel_spectrum(reinterpret_cast(p)->spectrum); - }; + context().message_map.register_handler(Message::ID::ChannelSpectrum, + [this](const Message* const p) { + this->on_channel_spectrum(reinterpret_cast(p)->spectrum); + } + ); } void on_hide() override { - context().message_map[Message::ID::ChannelSpectrum] = nullptr; + context().message_map.unregister_handler(Message::ID::ChannelSpectrum); } void set_parent_rect(const Rect new_parent_rect) override { diff --git a/firmware/baseband/main.cpp b/firmware/baseband/main.cpp index 58d72545..c3d460e6 100755 --- a/firmware/baseband/main.cpp +++ b/firmware/baseband/main.cpp @@ -481,14 +481,16 @@ public: MessageHandlerMap& message_handlers ) : message_handlers(message_handlers) { - message_handlers[Message::ID::FSKConfiguration] = [this](const Message* const p) { - auto m = reinterpret_cast(p); - this->configure(m->configuration); - }; + message_handlers.register_handler(Message::ID::FSKConfiguration, + [this](const Message* const p) { + auto m = reinterpret_cast(p); + this->configure(m->configuration); + } + ); } ~FSKProcessor() { - message_handlers[Message::ID::FSKConfiguration] = nullptr; + message_handlers.unregister_handler(Message::ID::FSKConfiguration); } void configure(const FSKConfiguration new_configuration) { @@ -736,10 +738,7 @@ private: while( !shared_memory.baseband_queue.is_empty() ) { auto message = shared_memory.baseband_queue.pop(); - auto& fn = message_map[message->id]; - if( fn ) { - fn(message); - } + message_map.send(message); message->state = Message::State::Free; } @@ -804,49 +803,51 @@ int main(void) { EventDispatcher event_dispatcher; auto& message_handlers = event_dispatcher.message_handlers(); - message_handlers[Message::ID::BasebandConfiguration] = [&message_handlers](const Message* const p) { - auto message = reinterpret_cast(p); - if( message->configuration.mode != baseband_configuration.mode ) { + message_handlers.register_handler(Message::ID::BasebandConfiguration, + [&message_handlers](const Message* const p) { + auto message = reinterpret_cast(p); + if( message->configuration.mode != baseband_configuration.mode ) { - // TODO: Timing problem around disabling DMA and nulling and deleting old processor - auto old_p = baseband_processor; - baseband_processor = nullptr; - delete old_p; + // TODO: Timing problem around disabling DMA and nulling and deleting old processor + auto old_p = baseband_processor; + baseband_processor = nullptr; + delete old_p; - switch(message->configuration.mode) { - case 0: - baseband_processor = new NarrowbandAMAudio(); - break; + switch(message->configuration.mode) { + case 0: + baseband_processor = new NarrowbandAMAudio(); + break; - case 1: - baseband_processor = new NarrowbandFMAudio(); - break; + case 1: + baseband_processor = new NarrowbandFMAudio(); + break; - case 2: - baseband_processor = new WidebandFMAudio(); - break; + case 2: + baseband_processor = new WidebandFMAudio(); + break; - case 3: - baseband_processor = new FSKProcessor(message_handlers); - break; + case 3: + baseband_processor = new FSKProcessor(message_handlers); + break; - default: - break; - } - - if( baseband_processor ) { - if( direction == baseband::Direction::Receive ) { - rf::rssi::start(); + default: + break; } - baseband::dma::enable(direction); - } else { - baseband::dma::disable(); - rf::rssi::stop(); - } - } - baseband_configuration = message->configuration; - }; + if( baseband_processor ) { + if( direction == baseband::Direction::Receive ) { + rf::rssi::start(); + } + baseband::dma::enable(direction); + } else { + baseband::dma::disable(); + rf::rssi::stop(); + } + } + + baseband_configuration = message->configuration; + } + ); /* TODO: Ensure DMAs are configured to point at first LLI in chain. */ diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index 5a39927f..5ab4fccb 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -246,12 +246,19 @@ class MessageHandlerMap { public: using MessageHandler = std::function; - MessageHandler& operator[](Message::ID n) { - return map_[toUType(n)]; + void register_handler(const Message::ID id, MessageHandler&& handler) { + map_[toUType(id)] = std::move(handler); } - const MessageHandler& operator[](Message::ID n) const { - return map_[toUType(n)]; + void unregister_handler(const Message::ID id) { + map_[toUType(id)] = nullptr; + } + + void send(const Message* const message) { + auto& fn = map_[toUType(message->id)]; + if( fn ) { + fn(message); + } } private: