ERTAppView, recent entries tracking and display.

This commit is contained in:
Jared Boone 2016-01-23 22:24:48 -08:00
parent 56218565c1
commit db2f78f0f8
3 changed files with 92 additions and 23 deletions

View File

@ -63,9 +63,41 @@ void ERTLogger::on_packet(const ert::Packet& packet) {
} }
} }
void ERTRecentEntry::update(const ert::Packet& packet) {
received_count++;
last_consumption = packet.consumption();
}
namespace ui { namespace ui {
ERTView::ERTView() { template<>
void RecentEntriesView<ERTRecentEntries>::draw(
const Entry& entry,
const Rect& target_rect,
Painter& painter,
const Style& style,
const bool is_selected
) {
const auto& draw_style = is_selected ? style.invert() : style;
std::string line = ert::format::id(entry.id) + " " + ert::format::consumption(entry.last_consumption);
if( entry.received_count > 999 ) {
line += " +++";
} else {
line += " " + to_string_dec_uint(entry.received_count, 3);
}
line.resize(target_rect.width() / 8, ' ');
painter.draw_string(target_rect.pos, draw_style, line);
}
ERTAppView::ERTAppView() {
add_children({ {
&recent_entries_view,
} });
EventDispatcher::message_map().register_handler(Message::ID::ERTPacket, EventDispatcher::message_map().register_handler(Message::ID::ERTPacket,
[this](Message* const p) { [this](Message* const p) {
const auto message = static_cast<const ERTPacketMessage*>(p); const auto message = static_cast<const ERTPacketMessage*>(p);
@ -82,29 +114,27 @@ ERTView::ERTView() {
receiver_model.set_baseband_bandwidth(2500000); receiver_model.set_baseband_bandwidth(2500000);
} }
ERTView::~ERTView() { ERTAppView::~ERTAppView() {
EventDispatcher::message_map().unregister_handler(Message::ID::ERTPacket); EventDispatcher::message_map().unregister_handler(Message::ID::ERTPacket);
} }
void ERTView::on_packet(const ert::Packet& packet) { void ERTAppView::set_parent_rect(const Rect new_parent_rect) {
View::set_parent_rect(new_parent_rect);
recent_entries_view.set_parent_rect({ 0, 0, new_parent_rect.width(), new_parent_rect.height() });
}
void ERTAppView::on_packet(const ert::Packet& packet) {
logger.on_packet(packet); logger.on_packet(packet);
if( packet.crc_ok() ) { if( packet.crc_ok() ) {
std::string msg { ert::format::type(packet.type()) }; recent.on_packet(packet.id(), packet);
recent_entries_view.set_dirty();
switch(packet.type()) {
case ert::Packet::Type::SCM:
case ert::Packet::Type::IDM:
msg += " " + ert::format::id(packet.id());
msg += " " + ert::format::consumption(packet.consumption());
break;
default:
break;
}
writeln(msg);
} }
} }
void ERTAppView::on_show_list() {
recent_entries_view.hidden(false);
recent_entries_view.focus();
}
} /* namespace ui */ } /* namespace ui */

View File

@ -22,14 +22,39 @@
#ifndef __ERT_APP_H__ #ifndef __ERT_APP_H__
#define __ERT_APP_H__ #define __ERT_APP_H__
#include "ui_console.hpp"
#include "log_file.hpp" #include "log_file.hpp"
#include "ert_packet.hpp" #include "ert_packet.hpp"
#include "recent_entries.hpp"
#include <cstddef> #include <cstddef>
#include <string> #include <string>
#include <bitset>
struct ERTRecentEntry {
using Key = ert::ID;
// TODO: Is this the right choice of invalid key value?
static constexpr Key invalid_key = 0;
ert::ID id { invalid_key };
size_t received_count { 0 };
ert::Consumption last_consumption;
ERTRecentEntry(
const Key& key
) : id { key }
{
}
Key key() const {
return id;
}
void update(const ert::Packet& packet);
};
class ERTLogger { class ERTLogger {
public: public:
@ -39,17 +64,31 @@ private:
LogFile log_file { "ert.txt" }; LogFile log_file { "ert.txt" };
}; };
using ERTRecentEntries = RecentEntries<ert::Packet, ERTRecentEntry>;
namespace ui { namespace ui {
class ERTView : public Console { using ERTRecentEntriesView = RecentEntriesView<ERTRecentEntries>;
class ERTAppView : public View {
public: public:
ERTView(); ERTAppView();
~ERTView(); ~ERTAppView();
void set_parent_rect(const Rect new_parent_rect) override;
// Prevent painting of region covered entirely by a child.
// TODO: Add flag to View that specifies view does not need to be cleared before painting.
void paint(Painter&) override { };
private: private:
ERTRecentEntries recent;
ERTLogger logger; ERTLogger logger;
ERTRecentEntriesView recent_entries_view { recent };
void on_packet(const ert::Packet& packet); void on_packet(const ert::Packet& packet);
void on_show_list();
}; };
} /* namespace ui */ } /* namespace ui */

View File

@ -529,7 +529,7 @@ void ReceiverView::on_modulation_changed(ReceiverModel::Mode mode) {
break; break;
case ReceiverModel::Mode::ERT: case ReceiverModel::Mode::ERT:
widget_content = std::make_unique<ERTView>(); widget_content = std::make_unique<ERTAppView>();
break; break;
default: default: