From f8a063312c52a0d2ea78df499e792362486ef916 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 13 Jan 2016 16:32:56 -0800 Subject: [PATCH] AISModel receive packets from baseband, notifies AISView. Use Signal class to distribute packets to multiple listeners. --- firmware/application/ais_app.cpp | 34 +++++++++++++------------------- firmware/application/ais_app.hpp | 19 +++++++++++++++--- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index a96b1fdf..b8d84565 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -89,6 +89,18 @@ AISModel::AISModel() { receiver_model.set_baseband_bandwidth(1750000); log_file.open_for_append("ais.txt"); + + EventDispatcher::message_map().register_handler(Message::ID::AISPacket, + [this](Message* const p) { + const auto message = static_cast(p); + const ais::Packet packet { message->packet }; + this->on_packet(packet); + } + ); +} + +AISModel::~AISModel() { + EventDispatcher::message_map().unregister_handler(Message::ID::AISPacket); } bool AISModel::on_packet(const ais::Packet& packet) { @@ -110,31 +122,13 @@ bool AISModel::on_packet(const ais::Packet& packet) { log_file.write_entry(packet.received_at(), entry); } + packet_signal.emit(packet); + return true; } namespace ui { -void AISView::on_show() { - View::on_show(); - - 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( this->model.on_packet(packet) ) { - this->on_packet(packet); - } - } - ); -} - -void AISView::on_hide() { - EventDispatcher::message_map().unregister_handler(Message::ID::AISPacket); - - View::on_hide(); -} - void AISView::truncate_entries() { while(recent.size() > 64) { recent.pop_back(); diff --git a/firmware/application/ais_app.hpp b/firmware/application/ais_app.hpp index 4a11efaf..65bd9e26 100644 --- a/firmware/application/ais_app.hpp +++ b/firmware/application/ais_app.hpp @@ -25,8 +25,12 @@ #include "ui_widget.hpp" #include "log_file.hpp" +#include "message.hpp" + #include "ais_packet.hpp" +#include "signal.hpp" + #include "lpc43xx_cpp.hpp" using namespace lpc43xx; @@ -66,11 +70,14 @@ struct AISRecentEntry { class AISModel { public: AISModel(); + ~AISModel(); - bool on_packet(const ais::Packet& packet); + Signal packet_signal; private: LogFile log_file; + + bool on_packet(const ais::Packet& packet); }; namespace ui { @@ -79,10 +86,15 @@ class AISView : public View { public: AISView() { flags.focusable = true; + + packet_signal_token = model.packet_signal += [this](const ais::Packet& packet) { + this->on_packet(packet); + }; } - void on_show() override; - void on_hide() override; + ~AISView() { + model.packet_signal -= packet_signal_token; + } void paint(Painter& painter) override; @@ -93,6 +105,7 @@ public: private: AISModel model; + SignalToken packet_signal_token; using EntryKey = ais::MMSI; EntryKey selected_key;