From 09add5cdeee839e0c4c127d55c99d51a55a543d5 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 9 Jun 2022 15:51:44 +0200 Subject: [PATCH] Implemented buttonHeld; still needs some work --- src/buttonsinterface.h | 1 + src/menudisplay.cpp | 118 +++++++++++++++++++++++++++++++++-------- src/menudisplay.h | 7 +-- 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/src/buttonsinterface.h b/src/buttonsinterface.h index 143bb5f..4b455ba 100644 --- a/src/buttonsinterface.h +++ b/src/buttonsinterface.h @@ -22,6 +22,7 @@ public: virtual void buttonPressed(Button button) = 0; virtual void buttonReleased(Button button) = 0; + virtual void buttonHeld(Button button) {}; }; } // namespace espgui diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index 62f458a..6e50e1d 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -3,6 +3,8 @@ // local includes #include "tftinstance.h" +#include "esp_log.h" + using namespace std::chrono_literals; namespace espgui { @@ -15,6 +17,18 @@ void MenuDisplay::start() m_rotateOffset = 0; m_pressed = false; + + m_upScrolling = false; + m_upHoldingSince = std::nullopt; + + m_downScrolling = false; + m_downHoldingSince = std::nullopt; + + m_leftScrolling = false; + m_leftHoldingSince = std::nullopt; + + m_rightScrolling = false; + m_rightHoldingSince = std::nullopt; } void MenuDisplay::initScreen() @@ -37,6 +51,84 @@ void MenuDisplay::update() { Base::update(); + { + const auto ago = espchrono::ago(*m_upHoldingSince); + if (m_upHoldingSince && (m_upScrolling || ago > getInitialScrollSpeed())) + { + if (ago > getScrollSpeed()) + { + if (!m_upScrolling) + { + // gets executed one time + buttonHeld(espgui::Up); + } + m_upScrolling = true; + m_upHoldingSince = espchrono::millis_clock::now(); + + // do sth + m_rotateOffset--; + } + } + } + + { + const auto ago = espchrono::ago(*m_downHoldingSince); + if (m_downHoldingSince && (m_downScrolling || ago > getInitialScrollSpeed())) + { + if (ago > getScrollSpeed()) + { + if (!m_downScrolling) + { + // gets executed one time + buttonHeld(espgui::Down); + } + m_downScrolling = true; + m_downHoldingSince = espchrono::millis_clock::now(); + + // do sth + m_rotateOffset++; + } + } + } + + { + const auto ago = espchrono::ago(*m_leftHoldingSince); + if (m_leftHoldingSince && (m_leftScrolling || ago > getInitialScrollSpeed())) + { + if (ago > getScrollSpeed()) + { + if (!m_leftScrolling) + { + // gets executed one time + buttonHeld(espgui::Left); + } + m_leftScrolling = true; + m_leftHoldingSince = espchrono::millis_clock::now(); + + // do sth + } + } + } + + { + const auto ago = espchrono::ago(*m_rightHoldingSince); + if (m_rightHoldingSince && (m_rightScrolling || ago > getInitialScrollSpeed())) + { + if (ago > getScrollSpeed()) + { + if (!m_rightScrolling) + { + // gets executed one time + buttonHeld(espgui::Right); + } + m_rightScrolling = true; + m_rightHoldingSince = espchrono::millis_clock::now(); + + // do sth + } + } + } + if (!m_pressed) { const auto offset = m_rotateOffset; @@ -95,27 +187,6 @@ void MenuDisplay::redraw() { Base::redraw(); - // wait for holding since to be above initial scroll speed, then start scrolling in interval set by getScrollSpeed() - if (m_upHoldingSince && (espchrono::ago(*m_upHoldingSince) > getInitialScrollSpeed() || m_upScrolling)) - { - if (espchrono::ago(*m_upHoldingSince) > getScrollSpeed()) - { - m_upScrolling = true; - m_rotateOffset--; - m_upHoldingSince = espchrono::millis_clock::now(); - } - } - - if (m_downHoldingSince && (espchrono::ago(*m_downHoldingSince) > getInitialScrollSpeed() || m_downScrolling)) - { - if (espchrono::ago(*m_downHoldingSince) > getScrollSpeed()) - { - m_downScrolling = true; - m_rotateOffset++; - m_downHoldingSince = espchrono::millis_clock::now(); - } - } - tft.setTextFont(4); tft.setTextColor(TFT_YELLOW, TFT_BLACK); @@ -238,4 +309,9 @@ void MenuDisplay::buttonReleased(Button button) default:; } } + +void MenuDisplay::buttonHeld(Button button) +{ + ESP_LOGI("Menudisplay", "buttonHeld %d", button); +} } // namespace espgui diff --git a/src/menudisplay.h b/src/menudisplay.h index dcf42a7..e8d2295 100644 --- a/src/menudisplay.h +++ b/src/menudisplay.h @@ -39,6 +39,7 @@ public: void buttonPressed(Button button) override; void buttonReleased(Button button) override; + void buttonHeld(Button button) override; MenuDisplay *asMenuDisplay() override { return this; } const MenuDisplay *asMenuDisplay() const override { return this; } @@ -157,13 +158,13 @@ private: bool m_upScrolling; std::optional m_upHoldingSince; - bool m_downScrolling{}; + bool m_downScrolling; std::optional m_downHoldingSince; - bool m_leftScrolling{}; + bool m_leftScrolling; std::optional m_leftHoldingSince; - bool m_rightScrolling{}; + bool m_rightScrolling; std::optional m_rightHoldingSince; }; } // namespace espgui