Wrap message handler registrations in class to subscribe/unsubscribe automatically.

This commit is contained in:
Jared Boone 2016-05-11 22:53:09 -07:00
parent e298e1ec5a
commit 7d4dd03418
18 changed files with 139 additions and 149 deletions

View File

@ -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<const AISPacketMessage*>(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() {

View File

@ -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<const AISPacketMessage*>(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);

View File

@ -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<const ERTPacketMessage*>(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() {

View File

@ -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<const ERTPacketMessage*>(p);
const ert::Packet packet { message->type, message->packet };
this->on_packet(packet);
}
};
void on_packet(const ert::Packet& packet);
void on_show_list();
};

View File

@ -117,4 +117,23 @@ private:
void init_message_queues();
};
class MessageHandlerRegistration {
public:
template<typename Callback>
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__*/

View File

@ -51,6 +51,32 @@
#include <string.h>
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);

View File

@ -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<const TPMSPacketMessage*>(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() {

View File

@ -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<const TPMSPacketMessage*>(p);
const tpms::Packet packet { message->packet };
this->on_packet(message->signal_type, packet);
}
};
TPMSRecentEntries recent;
std::unique_ptr<TPMSLogger> logger;

View File

@ -21,26 +21,12 @@
#include "ui_audio.hpp"
#include "event_m0.hpp"
#include "utility.hpp"
#include <algorithm>
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<const AudioStatisticsMessage*>(p)->statistics);
}
);
}
void Audio::on_hide() {
EventDispatcher::message_map().unregister_handler(Message::ID::AudioStatistics);
}
void Audio::paint(Painter& painter) {
const auto r = screen_rect();

View File

@ -26,6 +26,8 @@
#include "ui_widget.hpp"
#include "ui_painter.hpp"
#include "event_m0.hpp"
#include "message.hpp"
#include <cstdint>
@ -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<const AudioStatisticsMessage*>(p)->statistics);
}
};
void on_statistics_update(const AudioStatistics& statistics);
};

View File

@ -21,8 +21,6 @@
#include "ui_baseband_stats_view.hpp"
#include "event_m0.hpp"
#include <string>
#include <algorithm>
@ -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<const BasebandStatisticsMessage*>(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;

View File

@ -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<const BasebandStatisticsMessage*>(p)->statistics);
}
};
void on_statistics_update(const BasebandStatistics& statistics);
};

View File

@ -21,26 +21,12 @@
#include "ui_channel.hpp"
#include "event_m0.hpp"
#include "utility.hpp"
#include <algorithm>
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<const ChannelStatisticsMessage*>(p)->statistics);
}
);
}
void Channel::on_hide() {
EventDispatcher::message_map().unregister_handler(Message::ID::ChannelStatistics);
}
void Channel::paint(Painter& painter) {
const auto r = screen_rect();

View File

@ -26,6 +26,8 @@
#include "ui_widget.hpp"
#include "ui_painter.hpp"
#include "event_m0.hpp"
#include "message.hpp"
#include <cstdint>
@ -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<const ChannelStatisticsMessage*>(p)->statistics);
}
};
void on_statistics_update(const ChannelStatistics& statistics);
};

View File

@ -21,26 +21,12 @@
#include "ui_rssi.hpp"
#include "event_m0.hpp"
#include "utility.hpp"
#include <algorithm>
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<const RSSIStatisticsMessage*>(p)->statistics);
}
);
}
void RSSI::on_hide() {
EventDispatcher::message_map().unregister_handler(Message::ID::RSSIStatistics);
}
void RSSI::paint(Painter& painter) {
const auto r = screen_rect();

View File

@ -26,6 +26,8 @@
#include "ui_widget.hpp"
#include "ui_painter.hpp"
#include "event_m0.hpp"
#include "message.hpp"
#include <cstdint>
@ -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<const RSSIStatisticsMessage*>(p)->statistics);
}
};
void on_statistics_update(const RSSIStatistics& statistics);
};

View File

@ -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<const ChannelSpectrumConfigMessage*>(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) {

View File

@ -25,6 +25,8 @@
#include "ui.hpp"
#include "ui_widget.hpp"
#include "event_m0.hpp"
#include "message.hpp"
#include <cstdint>
@ -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<const ChannelSpectrumConfigMessage*>(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);
};