From 65a8c98530c37d209bd7e40c9d509b9e7407edeb Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Thu, 30 Dec 2021 05:56:46 +0100 Subject: [PATCH] Add change value display for IP address --- CMakeLists.txt | 2 + src/changevaluedisplay.h | 4 +- src/changevaluedisplay_ip_address_t.cpp | 63 +++++++++++++++++++++++++ src/changevaluedisplay_ip_address_t.h | 47 ++++++++++++++++++ src/changevaluedisplay_string.cpp | 4 +- src/changevaluedisplay_string.h | 3 ++ src/display.h | 18 +++++++ src/textwithvaluehelper.h | 6 ++- 8 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 src/changevaluedisplay_ip_address_t.cpp create mode 100644 src/changevaluedisplay_ip_address_t.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f957c74..7c2f2b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(headers src/changevaluedisplay.h src/changevaluedisplay_bool.h src/changevaluedisplay_daylightsavingmode.h + src/changevaluedisplay_ip_address_t.h src/changevaluedisplay_sntp_sync_mode_t.h src/changevaluedisplay_string.h src/changevaluedisplay_wifi_auth_mode_t.h @@ -50,6 +51,7 @@ set(sources src/changevaluedisplay.cpp src/changevaluedisplay_bool.cpp src/changevaluedisplay_daylightsavingmode.cpp + src/changevaluedisplay_ip_address_t.cpp src/changevaluedisplay_sntp_sync_mode_t.cpp src/changevaluedisplay_string.cpp src/changevaluedisplay_wifi_auth_mode_t.cpp diff --git a/src/changevaluedisplay.h b/src/changevaluedisplay.h index f2b71d6..f886d78 100644 --- a/src/changevaluedisplay.h +++ b/src/changevaluedisplay.h @@ -77,7 +77,7 @@ void ChangeValueDisplay::start() { Base::start(); - m_value = /*static_cast*>*/(this)->getValue(); + m_value = this->getValue(); m_rotateOffset = 0; m_pressed = false; @@ -93,7 +93,7 @@ void ChangeValueDisplay::update() const auto rotateOffset = m_rotateOffset; m_rotateOffset = 0; - m_value -= rotateOffset * /*static_cast*>*/(this)->step(); + m_value -= rotateOffset * this->step(); } else { diff --git a/src/changevaluedisplay_ip_address_t.cpp b/src/changevaluedisplay_ip_address_t.cpp new file mode 100644 index 0000000..dda9afe --- /dev/null +++ b/src/changevaluedisplay_ip_address_t.cpp @@ -0,0 +1,63 @@ +#include "changevaluedisplay_ip_address_t.h" + +// local includes +#include "tftinstance.h" + +void espgui::ChangeValueDisplay::start() +{ + Base::start(); + + m_value = this->getValue(); + + m_pressed = false; +} + +void espgui::ChangeValueDisplay::initScreen() +{ + Base::initScreen(); + + tft.drawRect(25, 75, 190, 65, TFT_WHITE); + m_valueLabel.start(); + + tft.setTextFont(4); + tft.setTextColor(TFT_WHITE); + tft.drawString("Change value and", 10, 160); + tft.drawString("press button to", 10, 185); + tft.drawString("confirm and go", 10, 210); + tft.drawString("back.", 10, 235); +} + +void espgui::ChangeValueDisplay::update() +{ + Base::update(); + + if (m_pressed) + { + m_pressed = false; + if (auto result = this->setValue(m_value); result) + confirm(); + else + errorOccured(std::move(result).error()); + } +} + +void espgui::ChangeValueDisplay::redraw() +{ + Base::redraw(); + + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.setTextFont(4); + m_valueLabel.redraw(wifi_stack::toString(m_value)); +} + +void espgui::ChangeValueDisplay::buttonPressed(Button button) +{ + //Base::buttonPressed(button); + + switch (button) + { + case Button::Left: this->back(); break; + case Button::Right: m_pressed = true; break; + default:; + } +} diff --git a/src/changevaluedisplay_ip_address_t.h b/src/changevaluedisplay_ip_address_t.h new file mode 100644 index 0000000..6d4c107 --- /dev/null +++ b/src/changevaluedisplay_ip_address_t.h @@ -0,0 +1,47 @@ +#pragma once + +// 3rdparty lib includes +#include + +// local includes +#include "changevaluedisplay.h" +#include "displaywithtitle.h" +#include "confirminterface.h" +#include "backinterface.h" +#include "errorhandlerinterface.h" +#include "widgets/label.h" + +namespace espgui { + +template<> +class ChangeValueDisplay : + public DisplayWithTitle, + public virtual AccessorInterface, + public virtual ConfirmInterface, + public virtual BackInterface, + public virtual ErrorHandlerInterface +{ + using Base = DisplayWithTitle; + +public: + ChangeValueDisplay *asChangeValueDisplayIpAddress() override { return this; } + const ChangeValueDisplay *asChangeValueDisplayIpAddress() const override { return this; } + + void start() override; + void initScreen() override; + void update() override; + void redraw() override; + + void buttonPressed(Button button) override; + + wifi_stack::ip_address_t shownValue() const { return m_value; } + void setShownValue(wifi_stack::ip_address_t value) { m_value = value; } + +private: + wifi_stack::ip_address_t m_value; + bool m_pressed{}; + + Label m_valueLabel{26, 81}; // 188, 53 +}; + +} // namespace espgui diff --git a/src/changevaluedisplay_string.cpp b/src/changevaluedisplay_string.cpp index d747f39..9cfa512 100644 --- a/src/changevaluedisplay_string.cpp +++ b/src/changevaluedisplay_string.cpp @@ -7,7 +7,7 @@ void espgui::ChangeValueDisplay::start() { Base::start(); - m_value = /*static_cast*>*/(this)->getValue(); + m_value = this->getValue(); m_pressed = false; } @@ -34,7 +34,7 @@ void espgui::ChangeValueDisplay::update() if (m_pressed) { m_pressed = false; - if (auto result = /*static_cast*>*/(this)->setValue(m_value); result) + if (auto result = this->setValue(m_value); result) confirm(); else errorOccured(std::move(result).error()); diff --git a/src/changevaluedisplay_string.h b/src/changevaluedisplay_string.h index 6cb1fee..acd946a 100644 --- a/src/changevaluedisplay_string.h +++ b/src/changevaluedisplay_string.h @@ -1,5 +1,8 @@ #pragma once +// system includes +#include + // local includes #include "changevaluedisplay.h" #include "displaywithtitle.h" diff --git a/src/display.h b/src/display.h index 4827966..4de776e 100644 --- a/src/display.h +++ b/src/display.h @@ -14,6 +14,9 @@ class MenuDisplay; class ChangeValueDisplayInterface; template class ChangeValueDisplay; } // namespace espgui +namespace wifi_stack { +class ip_address_t; +} // namespace wifi_stack namespace espgui { @@ -32,6 +35,18 @@ public: } }; +template +class makeComponentArgs2 : public T1, public T2, public T3, public T4... +{ +public: + template + makeComponentArgs2(Targ1&& arg1, Targ2&& arg2) : + T2{std::forward(arg1)}, + T3{std::forward(arg2)} + { + } +}; + class Display : public virtual ButtonsInterface { public: @@ -63,6 +78,9 @@ public: virtual ChangeValueDisplay *asChangeValueDisplayString() { return nullptr; } virtual const ChangeValueDisplay *asChangeValueDisplayString() const { return nullptr; } + + virtual ChangeValueDisplay *asChangeValueDisplayIpAddress() { return nullptr; } + virtual const ChangeValueDisplay *asChangeValueDisplayIpAddress() const { return nullptr; } }; } // namespace espgui diff --git a/src/textwithvaluehelper.h b/src/textwithvaluehelper.h index c8ef429..85b2d68 100644 --- a/src/textwithvaluehelper.h +++ b/src/textwithvaluehelper.h @@ -13,15 +13,17 @@ namespace espgui { template -struct TextWithValueHelper : public virtual TextInterface +struct TextWithValueHelper : public Taccessor, public virtual TextInterface { + using Taccessor::Taccessor; + std::string text() const override { using cpputils::toString; using espcpputils::toString; using wifi_stack::toString; - return fmt::format("{} {}", Tprefix, richTextEscape(toString(Taccessor{}.getValue()))); + return fmt::format("{} {}", Tprefix, richTextEscape(toString(Taccessor::getValue()))); } };