From fc1e277423b9aa596e4d8027a800f9b7fe9a929a Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 2 Nov 2021 23:13:43 +0100 Subject: [PATCH] Refactorings again more --- main/actions/rebootaction.cpp | 26 ++++ main/actions/rebootaction.h | 24 +-- main/changevaluedisplay_unifiedmodelmode.cpp | 37 +++++ main/changevaluedisplay_unifiedmodelmode.h | 34 +---- main/displays/garagedisplay.cpp | 50 +++++++ main/displays/garagedisplay.h | 44 +----- main/displays/lockscreen.cpp | 144 ++++++++++++++++++ main/displays/lockscreen.h | 149 +------------------ main/displays/poweroffdisplay.cpp | 56 +++++++ main/displays/poweroffdisplay.h | 47 +----- main/ota.cpp | 4 +- main/ota.h | 4 +- 12 files changed, 338 insertions(+), 281 deletions(-) diff --git a/main/actions/rebootaction.cpp b/main/actions/rebootaction.cpp index e69de29..a343c6e 100644 --- a/main/actions/rebootaction.cpp +++ b/main/actions/rebootaction.cpp @@ -0,0 +1,26 @@ +#include "rebootaction.h" + +// esp-idf includes +#include + +// 3rdparty lib includes +#include + +// local includes +#include "globals.h" +#include "texts.h" + +void RebootAction::triggered() +{ + espgui::tft.fillScreen(TFT_BLACK); + espgui::tft.setTextColor(TFT_YELLOW); + + espgui::tft.drawString(TEXT_REBOOT, 5, 5, 4); + + espgui::tft.fillRect(0, 34, espgui::tft.width(), 3, TFT_WHITE); + + espgui::tft.setTextColor(TFT_WHITE); + espgui::tft.drawString("Rebooting now...", 0, 50, 4); + + esp_restart(); +} diff --git a/main/actions/rebootaction.h b/main/actions/rebootaction.h index 92b0125..3bc01f2 100644 --- a/main/actions/rebootaction.h +++ b/main/actions/rebootaction.h @@ -1,30 +1,12 @@ #pragma once -#include - +// 3rdparty lib includes #include "actioninterface.h" -#include "globals.h" -#include "texts.h" using namespace espgui; -namespace { -class RebootAction : public virtual ActionInterface +class RebootAction : public virtual espgui::ActionInterface { public: - void triggered() override - { - tft.fillScreen(TFT_BLACK); - tft.setTextColor(TFT_YELLOW); - - tft.drawString(TEXT_REBOOT, 5, 5, 4); - - tft.fillRect(0, 34, tft.width(), 3, TFT_WHITE); - - tft.setTextColor(TFT_WHITE); - tft.drawString("Rebooting now...", 0, 50, 4); - - esp_restart(); - } + void triggered() override; }; -} diff --git a/main/changevaluedisplay_unifiedmodelmode.cpp b/main/changevaluedisplay_unifiedmodelmode.cpp index e69de29..9f3ab55 100644 --- a/main/changevaluedisplay_unifiedmodelmode.cpp +++ b/main/changevaluedisplay_unifiedmodelmode.cpp @@ -0,0 +1,37 @@ +#include "changevaluedisplay_unifiedmodelmode.h" + +// esp-idf includes +#include + +// local includes +#include "utils.h" +#include "texts.h" + +namespace espgui { +ChangeValueDisplay::ChangeValueDisplay() +{ + constructMenuItem, StaticText>>(UnifiedModelMode::Commutation, *this, *this); + constructMenuItem, StaticText>>(UnifiedModelMode::Sinusoidal, *this, *this); + constructMenuItem, StaticText>>(UnifiedModelMode::FocVoltage, *this, *this); + constructMenuItem, StaticText>>(UnifiedModelMode::FocSpeed, *this, *this); + constructMenuItem, StaticText>>(UnifiedModelMode::FocTorque, *this, *this); + constructMenuItem, StaticMenuItemIcon<&espgui::icons::back>>>(*this); +} + +void ChangeValueDisplay::start() +{ + Base::start(); + + switch (const auto value = getValue()) + { + case UnifiedModelMode::Commutation: setSelectedIndex(0); break; + case UnifiedModelMode::Sinusoidal: setSelectedIndex(1); break; + case UnifiedModelMode::FocVoltage: setSelectedIndex(2); break; + case UnifiedModelMode::FocSpeed: setSelectedIndex(3); break; + case UnifiedModelMode::FocTorque: setSelectedIndex(4); break; + default: + ESP_LOGW("BOBBY", "Unknown UnifiedModelMode: %i", int(value)); + setSelectedIndex(5); + } +} +} // namespace espgui diff --git a/main/changevaluedisplay_unifiedmodelmode.h b/main/changevaluedisplay_unifiedmodelmode.h index 5e538f3..3e7b120 100644 --- a/main/changevaluedisplay_unifiedmodelmode.h +++ b/main/changevaluedisplay_unifiedmodelmode.h @@ -1,14 +1,13 @@ #pragma once -#include - +// 3rdparty lib includes #include "changevaluedisplay.h" #include "menudisplay.h" -#include "utils.h" #include "actions/setvalueaction.h" #include "actions/backproxyaction.h" #include "icons/back.h" -#include "texts.h" + +// local includes #include "unifiedmodelmode.h" namespace espgui { @@ -25,31 +24,4 @@ public: void start() override; }; - -ChangeValueDisplay::ChangeValueDisplay() -{ - constructMenuItem, StaticText>>(UnifiedModelMode::Commutation, *this, *this); - constructMenuItem, StaticText>>(UnifiedModelMode::Sinusoidal, *this, *this); - constructMenuItem, StaticText>>(UnifiedModelMode::FocVoltage, *this, *this); - constructMenuItem, StaticText>>(UnifiedModelMode::FocSpeed, *this, *this); - constructMenuItem, StaticText>>(UnifiedModelMode::FocTorque, *this, *this); - constructMenuItem, StaticMenuItemIcon<&espgui::icons::back>>>(*this); -} - -void ChangeValueDisplay::start() -{ - Base::start(); - - switch (const auto value = getValue()) - { - case UnifiedModelMode::Commutation: setSelectedIndex(0); break; - case UnifiedModelMode::Sinusoidal: setSelectedIndex(1); break; - case UnifiedModelMode::FocVoltage: setSelectedIndex(2); break; - case UnifiedModelMode::FocSpeed: setSelectedIndex(3); break; - case UnifiedModelMode::FocTorque: setSelectedIndex(4); break; - default: - ESP_LOGW("BOBBY", "Unknown UnifiedModelMode: %i", int(value)); - setSelectedIndex(5); - } -} } // namespace espgui diff --git a/main/displays/garagedisplay.cpp b/main/displays/garagedisplay.cpp index e69de29..dba370f 100644 --- a/main/displays/garagedisplay.cpp +++ b/main/displays/garagedisplay.cpp @@ -0,0 +1,50 @@ +#include "garagedisplay.h" + +// system includes + +// esp-idf includes +#include + +// 3rdparty lib includes +#include +#include +#include + +// local includes +#include "displays/menus/mainmenu.h" +#include "globals.h" +#include "texts.h" + +#ifdef FEATURE_GARAGE +void GarageDisplay::start() +{ +} + +void GarageDisplay::initScreen() +{ + espgui::tft.fillScreen(TFT_BLACK); + espgui::tft.setTextFont(4); + espgui::tft.setTextColor(TFT_YELLOW); + + espgui::tft.drawString(TEXT_GARAGE, 5, 5, 4); + + espgui::tft.fillRect(0, 34, espgui::tft.width(), 3, TFT_WHITE); + + espgui::tft.setTextColor(TFT_WHITE, TFT_BLACK); + + espgui::tft.drawString("Garage", 20, 100); +} + +void GarageDisplay::redraw() +{ +} + +void GarageDisplay::confirm() +{ +} + +void GarageDisplay::back() +{ + espgui::switchScreen(); +} +#endif diff --git a/main/displays/garagedisplay.h b/main/displays/garagedisplay.h index 2b53af2..db53db1 100644 --- a/main/displays/garagedisplay.h +++ b/main/displays/garagedisplay.h @@ -1,58 +1,18 @@ #pragma once -// system includes - -// esp-idf includes -#include - -// 3rdparty lib includes -#include - // local includes #include "display.h" -#include "actions/switchscreenaction.h" -#include "globals.h" -#include "texts.h" -#include "widgets/label.h" -namespace { #ifdef FEATURE_GARAGE -class GarageDisplay : public Display, public BackActionInterface> +class GarageDisplay : public espgui::Display { public: void start() override; void initScreen() override; void redraw() override; void confirm() override; + void back() override; private: }; - -void GarageDisplay::start() -{ -} - -void GarageDisplay::initScreen() -{ - tft.fillScreen(TFT_BLACK); - tft.setTextFont(4); - tft.setTextColor(TFT_YELLOW); - - tft.drawString(TEXT_GARAGE, 5, 5, 4); - - tft.fillRect(0, 34, tft.width(), 3, TFT_WHITE); - - tft.setTextColor(TFT_WHITE, TFT_BLACK); - - tft.drawString("Garage", 20, 100); -} - -void GarageDisplay::redraw() -{ -} - -void GarageDisplay::confirm() -{ -} #endif -} diff --git a/main/displays/lockscreen.cpp b/main/displays/lockscreen.cpp index e69de29..fe909d7 100644 --- a/main/displays/lockscreen.cpp +++ b/main/displays/lockscreen.cpp @@ -0,0 +1,144 @@ +#include "lockscreen.h" + +// 3rdparty lib includes +#include +#include + +// local includes +#include "globals.h" +#include "utils.h" +#include "texts.h" +#include "buttons.h" +#include "displays/menus/mainmenu.h" + +void Lockscreen::start() +{ + m_numbers = {0,0,0,0}; + m_currentIndex = 0; + m_pressed = false; + m_rotated = 0; + + m_oldMode = currentMode; + currentMode = &m_mode; + + profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; +} + +void Lockscreen::initScreen() +{ + espgui::tft.fillScreen(TFT_BLACK); + espgui::tft.setTextFont(4); + espgui::tft.setTextColor(TFT_YELLOW); + + espgui::tft.drawString(TEXT_LOCKVEHICLE, 5, 5); + + espgui::tft.fillRect(0, 34, espgui::tft.width(), 3, TFT_WHITE); + + espgui::tft.setTextColor(TFT_WHITE); + espgui::tft.drawString("Enter code to unlock:", 0, 50); + + espgui::tft.setTextColor(TFT_WHITE, TFT_BLACK); + + for(int i = 0; i <= 3; i++) + { + drawRect(i, 3, TFT_WHITE); + drawRect(i, 4, TFT_WHITE); + } + + for (auto &label : m_labels) + label.start(); + + espgui::tft.setTextFont(7); + + drawRect(0, 1, TFT_YELLOW); + drawRect(0, 2, TFT_YELLOW); + m_labels[0].redraw(std::to_string(m_numbers[0])); +} + +void Lockscreen::update() +{ + // just in case someone changes that settings somehow + profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; +} + +void Lockscreen::redraw() +{ + if (m_pressed) + { + drawRect(m_currentIndex, 1, TFT_BLACK); + drawRect(m_currentIndex, 2, TFT_BLACK); + + if (++m_currentIndex>=4) + { + if (m_numbers == settings.lockscreen.pin) + { + espgui::switchScreen(); +#ifdef LOCKSCREEN_PLUGIN +#pragma message "Activating Lockscreen Plugin" +#include LOCKSCREEN_PLUGIN +#endif + return; + } + + m_numbers = {0,0,0,0}; + m_currentIndex = 0; + std::for_each(std::begin(m_labels) + 1, std::end(m_labels), [](auto &label){ label.redraw({}); }); + } + + m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex])); + + drawRect(m_currentIndex, 1, TFT_YELLOW); + drawRect(m_currentIndex, 2, TFT_YELLOW); + + m_pressed = false; + } + + if (m_rotated) + { + m_numbers[m_currentIndex] -= m_rotated; + + if (m_numbers[m_currentIndex] < 0) + m_numbers[m_currentIndex]+=10; + else if (m_numbers[m_currentIndex] > 9) + m_numbers[m_currentIndex]-=10; + + m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex])); + + m_rotated = 0; + } +} + +void Lockscreen::stop() +{ + Base::stop(); + + if (currentMode == &m_mode) + { + // to avoid crash after deconstruction + m_mode.stop(); + lastMode = nullptr; + + currentMode = m_oldMode; + } + + profileButtonDisabled = false; +} + +void Lockscreen::confirm() +{ + m_pressed = true; +} + +void Lockscreen::back() +{ +} + +void Lockscreen::rotate(int offset) +{ + m_rotated += offset; +} + +void Lockscreen::drawRect(int index, int offset, uint32_t color) const +{ + espgui::tft.drawRect(m_labels[index].x()-offset, m_labels[index].y()-offset, boxWidth+(offset*2), boxHeight+(offset*2), color); +} diff --git a/main/displays/lockscreen.h b/main/displays/lockscreen.h index 934f36f..5af1037 100644 --- a/main/displays/lockscreen.h +++ b/main/displays/lockscreen.h @@ -6,20 +6,15 @@ // local includes #include "display.h" #include "widgets/label.h" -#include "globals.h" -#include "utils.h" -#include "texts.h" #include "modes/ignoreinputmode.h" -#include "buttons.h" #ifdef LOCKSCREEN_PLUGIN #include "ledstrip.h" #endif -namespace { -class Lockscreen : public Display, public DummyBack +class Lockscreen : public espgui::Display { - using Base = Display; + using Base = espgui::Display; static constexpr auto boxWidth = 35; static constexpr auto boxHeight = 50; @@ -33,16 +28,17 @@ public: void stop() override; void confirm() override; + void back() override; void rotate(int offset) override; private: void drawRect(int index, int offset, uint32_t color) const; - std::array m_labels {{ - Label{spacing, 100}, // boxWidth, boxHeight - Label{spacing*2+boxWidth, 100}, // boxWidth, boxHeight - Label{spacing*3+boxWidth*2, 100}, // boxWidth, boxHeight - Label{spacing*4+boxWidth*3, 100} // boxWidth, boxHeight + std::array m_labels {{ + espgui::Label{spacing, 100}, // boxWidth, boxHeight + espgui::Label{spacing*2+boxWidth, 100}, // boxWidth, boxHeight + espgui::Label{spacing*3+boxWidth*2, 100}, // boxWidth, boxHeight + espgui::Label{spacing*4+boxWidth*3, 100} // boxWidth, boxHeight }}; std::array m_numbers; @@ -55,132 +51,3 @@ private: ModeInterface *m_oldMode; IgnoreInputMode m_mode{0, bobbycar::protocol::ControlType::FieldOrientedControl, bobbycar::protocol::ControlMode::Speed}; }; - -void Lockscreen::start() -{ - m_numbers = {0,0,0,0}; - m_currentIndex = 0; - m_pressed = false; - m_rotated = 0; - - m_oldMode = currentMode; - currentMode = &m_mode; - - profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; -} - -void Lockscreen::initScreen() -{ - tft.fillScreen(TFT_BLACK); - tft.setTextFont(4); - tft.setTextColor(TFT_YELLOW); - - tft.drawString(TEXT_LOCKVEHICLE, 5, 5); - - tft.fillRect(0, 34, tft.width(), 3, TFT_WHITE); - - tft.setTextColor(TFT_WHITE); - tft.drawString("Enter code to unlock:", 0, 50); - - tft.setTextColor(TFT_WHITE, TFT_BLACK); - - for(int i = 0; i <= 3; i++) - { - drawRect(i, 3, TFT_WHITE); - drawRect(i, 4, TFT_WHITE); - } - - for (auto &label : m_labels) - label.start(); - - tft.setTextFont(7); - - drawRect(0, 1, TFT_YELLOW); - drawRect(0, 2, TFT_YELLOW); - m_labels[0].redraw(std::to_string(m_numbers[0])); -} - -void Lockscreen::update() -{ - // just in case someone changes that settings somehow - profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; -} - -void Lockscreen::redraw() -{ - if (m_pressed) - { - drawRect(m_currentIndex, 1, TFT_BLACK); - drawRect(m_currentIndex, 2, TFT_BLACK); - - if (++m_currentIndex>=4) - { - if (m_numbers == settings.lockscreen.pin) - { - switchScreen(); -#ifdef LOCKSCREEN_PLUGIN -#pragma message "Activating Lockscreen Plugin" -#include LOCKSCREEN_PLUGIN -#endif - return; - } - - m_numbers = {0,0,0,0}; - m_currentIndex = 0; - std::for_each(std::begin(m_labels) + 1, std::end(m_labels), [](auto &label){ label.redraw({}); }); - } - - m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex])); - - drawRect(m_currentIndex, 1, TFT_YELLOW); - drawRect(m_currentIndex, 2, TFT_YELLOW); - - m_pressed = false; - } - - if (m_rotated) - { - m_numbers[m_currentIndex] -= m_rotated; - - if (m_numbers[m_currentIndex] < 0) - m_numbers[m_currentIndex]+=10; - else if (m_numbers[m_currentIndex] > 9) - m_numbers[m_currentIndex]-=10; - - m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex])); - - m_rotated = 0; - } -} - -void Lockscreen::stop() -{ - Base::stop(); - - if (currentMode == &m_mode) - { - // to avoid crash after deconstruction - m_mode.stop(); - lastMode = nullptr; - - currentMode = m_oldMode; - } - - profileButtonDisabled = false; -} - -void Lockscreen::confirm() -{ - m_pressed = true; -} - -void Lockscreen::rotate(int offset) -{ - m_rotated += offset; -} - -void Lockscreen::drawRect(int index, int offset, uint32_t color) const -{ - tft.drawRect(m_labels[index].x()-offset, m_labels[index].y()-offset, boxWidth+(offset*2), boxHeight+(offset*2), color); -} -} diff --git a/main/displays/poweroffdisplay.cpp b/main/displays/poweroffdisplay.cpp index e69de29..f84367b 100644 --- a/main/displays/poweroffdisplay.cpp +++ b/main/displays/poweroffdisplay.cpp @@ -0,0 +1,56 @@ +#include "poweroffdisplay.h" + +// 3rdparty lib includes +#include +#include + +// local includes +#include "utils.h" +#include "globals.h" +#include "texts.h" +#include "displays/menus/mainmenu.h" + +using namespace std::chrono_literals; + +void PoweroffDisplay::start() +{ + m_startTime = espchrono::millis_clock::now(); + + for (Controller &controller : controllers) + controller.command.poweroff = true; +} + +void PoweroffDisplay::initScreen() +{ + espgui::tft.fillScreen(TFT_BLACK); + espgui::tft.setTextColor(TFT_YELLOW); + + espgui::tft.drawString(TEXT_POWEROFF, 5, 5, 4); + + espgui::tft.fillRect(0, 34, espgui::tft.width(), 3, TFT_WHITE); + + espgui::tft.setTextColor(TFT_WHITE); + espgui::tft.drawString("Trying to turn off", 15, 50, 4); + espgui::tft.drawString("both controllers", 25, 75, 4); + espgui::tft.drawString("Please stand still...", 20, 125, 4); +} + +void PoweroffDisplay::update() +{ + if (espchrono::millis_clock::now() - m_startTime >= 1000ms) + espgui::switchScreen(); +} + +void PoweroffDisplay::stop() +{ + for (Controller &controller : controllers) + controller.command.poweroff = false; +} + +void PoweroffDisplay::confirm() +{ +} + +void PoweroffDisplay::back() +{ +} diff --git a/main/displays/poweroffdisplay.h b/main/displays/poweroffdisplay.h index 6735450..3ac0b78 100644 --- a/main/displays/poweroffdisplay.h +++ b/main/displays/poweroffdisplay.h @@ -5,56 +5,19 @@ // local includes #include "display.h" -#include "utils.h" -#include "globals.h" -#include "texts.h" -namespace { -class PoweroffDisplay : public Display, public DummyConfirm, public DummyBack +class PoweroffDisplay : public espgui::Display { public: void start() override; void initScreen() override; void update() override; - void redraw() override {}; + void redraw() override {} void stop() override; + void confirm() override; + void back() override; + private: espchrono::millis_clock::time_point m_startTime; }; - -void PoweroffDisplay::start() -{ - m_startTime = espchrono::millis_clock::now(); - - for (Controller &controller : controllers) - controller.command.poweroff = true; -} - -void PoweroffDisplay::initScreen() -{ - tft.fillScreen(TFT_BLACK); - tft.setTextColor(TFT_YELLOW); - - tft.drawString(TEXT_POWEROFF, 5, 5, 4); - - tft.fillRect(0, 34, tft.width(), 3, TFT_WHITE); - - tft.setTextColor(TFT_WHITE); - tft.drawString("Trying to turn off", 15, 50, 4); - tft.drawString("both controllers", 25, 75, 4); - tft.drawString("Please stand still...", 20, 125, 4); -} - -void PoweroffDisplay::update() -{ - if (espchrono::millis_clock::now() - m_startTime >= 1000ms) - switchScreen(); -} - -void PoweroffDisplay::stop() -{ - for (Controller &controller : controllers) - controller.command.poweroff = false; -} -} diff --git a/main/ota.cpp b/main/ota.cpp index 1ec404d..06214ae 100644 --- a/main/ota.cpp +++ b/main/ota.cpp @@ -7,8 +7,8 @@ #include #ifdef FEATURE_OTA -extern cpputils::DelayedConstruction asyncOta; -extern bool asyncOtaTaskStarted; +cpputils::DelayedConstruction asyncOta; +bool asyncOtaTaskStarted{}; namespace { constexpr const char * const TAG = "BOBBYOTA"; diff --git a/main/ota.h b/main/ota.h index 8ad7c47..0b501da 100644 --- a/main/ota.h +++ b/main/ota.h @@ -9,8 +9,8 @@ // local includes #ifdef FEATURE_OTA -cpputils::DelayedConstruction asyncOta; -bool asyncOtaTaskStarted{}; +extern cpputils::DelayedConstruction asyncOta; +extern bool asyncOtaTaskStarted; void initOta(); void handleOta();