Eliminate per-device Views and RegisterWidgets, virtual methods.

Nice code size improvement, despite supporting three device register sets instead of just one.
This commit is contained in:
Jared Boone 2015-12-13 21:28:39 -08:00
parent a0fbfcf6a2
commit a1aa6ecdbf
2 changed files with 35 additions and 67 deletions

View File

@ -93,7 +93,7 @@ void RegistersWidget::draw_values(
static_cast<Coord>((i / config.registers_per_row) * row_height) static_cast<Coord>((i / config.registers_per_row) * row_height)
}; };
const auto value = read(i); const auto value = reader(i);
const auto text = to_string_hex(value, config.value_length); const auto text = to_string_hex(value, config.value_length);
painter.draw_string( painter.draw_string(
@ -111,9 +111,18 @@ DebugMenuView::DebugMenuView(NavigationView& nav) {
{ "Memory", [&nav](){ nav.push(new DebugMemoryView { nav }); } }, { "Memory", [&nav](){ nav.push(new DebugMemoryView { nav }); } },
{ "Radio State", [&nav](){ nav.push(new NotImplementedView { nav }); } }, { "Radio State", [&nav](){ nav.push(new NotImplementedView { nav }); } },
{ "SD Card", [&nav](){ nav.push(new NotImplementedView { nav }); } }, { "SD Card", [&nav](){ nav.push(new NotImplementedView { nav }); } },
{ "RFFC5072", [&nav](){ nav.push(new DebugRFFC5072View { nav }); } }, { "RFFC5072", [&nav](){ nav.push(new RegistersView {
{ "MAX2837", [&nav](){ nav.push(new DebugMAX2837View { nav }); } }, nav, "RFFC5072", { 31, 2, 4, 4 },
{ "Si5351C", [&nav](){ nav.push(new DebugSi5351CView { nav }); } }, [](const size_t register_number) { return radio::first_if.read(register_number); }
}); } },
{ "MAX2837", [&nav](){ nav.push(new RegistersView {
nav, "MAX2837", { 32, 2, 3, 4 },
[](const size_t register_number) { return radio::second_if.read(register_number); }
}); } },
{ "Si5351C", [&nav](){ nav.push(new RegistersView {
nav, "Si5351C", { 96, 2, 2, 8 },
[](const size_t register_number) { return portapack::clock_generator.read_register(register_number); }
}); } },
{ "WM8731", [&nav](){ nav.push(new NotImplementedView { nav }); } }, { "WM8731", [&nav](){ nav.push(new NotImplementedView { nav }); } },
} }); } });
on_left = [&nav](){ nav.pop(); }; on_left = [&nav](){ nav.pop(); };

View File

@ -32,6 +32,9 @@
#include "max2837.hpp" #include "max2837.hpp"
#include "portapack.hpp" #include "portapack.hpp"
#include <functional>
#include <utility>
namespace ui { namespace ui {
class DebugMemoryView : public View { class DebugMemoryView : public View {
@ -80,7 +83,6 @@ private:
}; };
struct RegistersWidgetConfig { struct RegistersWidgetConfig {
const char* const name;
size_t registers_count; size_t registers_count;
size_t legend_length; size_t legend_length;
size_t value_length; size_t value_length;
@ -109,26 +111,22 @@ struct RegistersWidgetConfig {
class RegistersWidget : public Widget { class RegistersWidget : public Widget {
public: public:
constexpr RegistersWidget( RegistersWidget(
Rect parent_rect, RegistersWidgetConfig&& config,
const RegistersWidgetConfig& config std::function<uint32_t(const size_t register_number)>&& reader
) : Widget { parent_rect }, ) : Widget { },
config(config) config(std::move(config)),
reader(std::move(reader))
{ {
} }
std::string name() const {
return config.name;
}
virtual uint32_t read(const size_t register_number) = 0;
void update(); void update();
void paint(Painter& painter) override; void paint(Painter& painter) override;
private: private:
const RegistersWidgetConfig config; const RegistersWidgetConfig config;
const std::function<uint32_t(const size_t register_number)> reader;
static constexpr Dim row_height = 16; static constexpr Dim row_height = 16;
@ -136,62 +134,29 @@ private:
void draw_values(Painter& painter); void draw_values(Painter& painter);
}; };
class DebugRFFC5072RegistersWidget : public RegistersWidget {
public:
constexpr DebugRFFC5072RegistersWidget(
Rect parent_rect
) : RegistersWidget { parent_rect, { "RFFC5072", 31, 2, 4, 4 } }
{
}
uint32_t read(const size_t register_number) override {
return radio::first_if.read(register_number);
}
};
class DebugMAX2837RegistersWidget : public RegistersWidget {
public:
constexpr DebugMAX2837RegistersWidget(
Rect parent_rect
) : RegistersWidget { parent_rect, { "MAX2837", 32, 2, 3, 4 } }
{
}
uint32_t read(const size_t register_number) override {
return radio::second_if.read(register_number);
}
};
class DebugSi5351CRegistersWidget : public RegistersWidget {
public:
constexpr DebugSi5351CRegistersWidget(
Rect parent_rect
) : RegistersWidget { parent_rect, { "Si5351C", 96, 2, 2, 8 } }
{
}
uint32_t read(const size_t register_number) override {
return portapack::clock_generator.read_register(register_number);
}
};
template<class RegistersWidget>
class RegistersView : public View { class RegistersView : public View {
public: public:
RegistersView(NavigationView& nav) { RegistersView(
NavigationView& nav,
const std::string& title,
RegistersWidgetConfig&& config,
std::function<uint32_t(const size_t register_number)>&& reader
) : registers_widget { std::move(config), std::move(reader) }
{
add_children({ { add_children({ {
&text_title, &text_title,
&widget_registers, &registers_widget,
&button_update, &button_update,
&button_done, &button_done,
} }); } });
button_update.on_select = [this](Button&){ button_update.on_select = [this](Button&){
this->widget_registers.update(); this->registers_widget.update();
}; };
button_done.on_select = [&nav](Button&){ nav.pop(); }; button_done.on_select = [&nav](Button&){ nav.pop(); };
const auto title = widget_registers.name(); registers_widget.set_parent_rect({ 0, 48, 240, 192 });
text_title.set_parent_rect({ text_title.set_parent_rect({
static_cast<Coord>((240 - title.size() * 8) / 2), 16, static_cast<Coord>((240 - title.size() * 8) / 2), 16,
static_cast<Dim>(title.size() * 8), 16 static_cast<Dim>(title.size() * 8), 16
@ -206,9 +171,7 @@ public:
private: private:
Text text_title; Text text_title;
RegistersWidget widget_registers { RegistersWidget registers_widget;
{ 0, 48, 240, 192 }
};
Button button_update { Button button_update {
{ 16, 256, 96, 24 }, { 16, 256, 96, 24 },
@ -221,10 +184,6 @@ private:
}; };
}; };
using DebugRFFC5072View = RegistersView<DebugRFFC5072RegistersWidget>;
using DebugMAX2837View = RegistersView<DebugMAX2837RegistersWidget>;
using DebugSi5351CView = RegistersView<DebugSi5351CRegistersWidget>;
class DebugMenuView : public MenuView { class DebugMenuView : public MenuView {
public: public:
DebugMenuView(NavigationView& nav); DebugMenuView(NavigationView& nav);