From d70a3aa73f56eae6b0cf77d41e29f2f3b2d286db Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 2 Nov 2021 22:47:46 +0100 Subject: [PATCH] Again refactorings more --- main/displays/ledstripcolorsdisplay.cpp | 240 ++++++++++++++++ main/displays/ledstripcolorsdisplay.h | 244 +--------------- main/displays/menus/commanddebugmenu.h | 5 - main/displays/menus/debugmenu.cpp | 59 ++++ main/displays/menus/debugmenu.h | 55 +--- main/displays/menus/dynamicdebugmenu.h | 5 - main/displays/menus/feedbackdebugmenu.h | 5 - main/displays/menus/ledstripmenu.cpp | 143 ++++++++++ main/displays/menus/ledstripmenu.h | 147 +--------- .../menus/ledstripselectanimationmenu.h | 4 - main/displays/menus/ledstripselectblinkmenu.h | 6 +- main/displays/menus/motorfeedbackdebugmenu.h | 5 - main/displays/menus/motorstatedebugmenu.h | 5 - main/displays/powersupplydisplay.cpp | 46 +++ main/displays/powersupplydisplay.h | 48 +--- main/esptexthelpers.h | 42 +-- main/ledstrip.cpp | 259 +++++++++++++++++ main/ledstrip.h | 264 +----------------- 18 files changed, 795 insertions(+), 787 deletions(-) diff --git a/main/displays/ledstripcolorsdisplay.cpp b/main/displays/ledstripcolorsdisplay.cpp index e69de29..87bdbbe 100644 --- a/main/displays/ledstripcolorsdisplay.cpp +++ b/main/displays/ledstripcolorsdisplay.cpp @@ -0,0 +1,240 @@ +#include "ledstripcolorsdisplay.h" + +// esp-idf includes +#include + +// 3rdparty lib includes +#include +#include +#include +#include +#include +#include +#include + +// local includes +#include "menudisplay.h" +#include "utils.h" +#include "icons/back.h" +#include "icons/bobbycar.h" +#include "texts.h" +#include "actions/dummyaction.h" +#include "globals.h" +#include "displays/menus/ledstripmenu.h" + +int8_t selected_side = 7; +int8_t selected_color; +bool state_select_color{false}; +bool last_state = {false}; + +const std::array Colors = { + CRGB{0,0,0}, + CRGB{255,255,255}, + CRGB{255,0,0}, + CRGB{255,255,0}, + CRGB{0,255,0}, + CRGB{0,255,255}, + CRGB{0,0,255}, + CRGB{255,0,255} +}; + +const std::array tft_colors = { + TFT_BLACK, + TFT_WHITE, + TFT_RED, + TFT_YELLOW, + TFT_GREEN, + TFT_CYAN, + TFT_BLUE, + TFT_MAGENTA +}; + +std::string LedstripColorsDisplay::text() const +{ + return TEXT_LEDSTRIPCOLORMENU; +} + +void LedstripColorsDisplay::back() +{ + if(!state_select_color) + { + espgui::switchScreen(); + } + else + { + state_select_color = false; + espgui::tft.fillRect(0, 228, espgui::tft.width(), ((espgui::tft.width() - 40) / 8) + 4, TFT_BLACK); + } +} + +void LedstripColorsDisplay::initScreen() +{ + Base::initScreen(); + + espgui::tft.setSwapBytes(true); + espgui::tft.pushImage(70, 60, bobbyicons::bobbycar.WIDTH, bobbyicons::bobbycar.HEIGHT, bobbyicons::bobbycar.buffer); + espgui::tft.setSwapBytes(false); +} + +void LedstripColorsDisplay::redraw() +{ + Base::redraw(); + + auto y_pos = ((espgui::tft.width() - 40) / 8 + 4) + 240; + if (last_state != state_select_color) + { + espgui::tft.fillRect(0,y_pos - 1, espgui::tft.width(), 20, TFT_BLACK); + last_state = state_select_color; + } + + espgui::tft.setTextFont(2); + espgui::tft.setTextColor(TFT_WHITE); + + espgui::tft.drawString(state_select_color ? + "Please select a color!" : + "Please select a side!", 50, y_pos); + + if (!already_drew_circle) + { + drawSide(static_cast(selected_side), TFT_GOLD); + already_drew_circle = true; + } +} + +void LedstripColorsDisplay::rotate(int offset) +{ + if (offset < 0) + { + if (state_select_color) + { + selected_color++; + if (selected_color > 7) + { + selected_color = 0; + } + } + else + { + selected_side++; + if (selected_side > 7) + { + selected_side = 0; + } + } + } + else if (offset > 0) + { + if (state_select_color) + { + selected_color--; + if (selected_color < 0) + { + selected_color = 7; + } + } + else + { + selected_side--; + if (selected_side < 0) + { + selected_side = 7; + } + } + } + + if (state_select_color) + { + drawColors(); + } + else + { + espgui::tft.fillRect(0, 228, espgui::tft.width(), ((espgui::tft.width() - 40) / 8) + 4, TFT_BLACK); + clearSides(); + drawSide(static_cast(selected_side), TFT_GOLD); + } +} + +void LedstripColorsDisplay::confirm() +{ + if(!state_select_color) + { + state_select_color = true; + drawColors(); + } + else + { + ledstrip_custom_colors[selected_side] = Colors[selected_color]; + // Uncomment to close select color menu on color select + /* + state_select_color = false; + espgui::tft.fillRect(0, 228, espgui::tft.width(), ((espgui::tft.width() - 40) / 8) + 4, TFT_BLACK); + */ + } +} + +void LedstripColorsDisplay::drawColors() +{ + uint16_t width = (espgui::tft.width() - 40); + auto cube_width = width / 8; + + espgui::tft.fillRect(0, 228, espgui::tft.width(), cube_width + 4, TFT_BLACK); + espgui::tft.fillRect(21, 231, width - 1, cube_width - 1, TFT_WHITE); + + espgui::tft.fillRect(20 + (selected_color * cube_width - 1), 228, cube_width + 4, cube_width + 4, TFT_YELLOW); + for (int index = 0; index < 8; index++) + { + auto offset = index * (cube_width); + espgui::tft.fillRect(22 + offset, 232, cube_width - 4, cube_width - 4, tft_colors[index]); + } +} + +void LedstripColorsDisplay::clearSides() +{ + for(int index = 0; index < 8; index++) + { + drawSide(static_cast(index), TFT_BLACK); + } +} + +void LedstripColorsDisplay::drawSide(Bobbycar_Side side, unsigned int color) +{ + const auto middle = espgui::tft.width() / 2; + const auto width = bobbyicons::bobbycar.WIDTH; + const auto height = bobbyicons::bobbycar.HEIGHT; + const auto left = middle - (width / 2); + const auto right = middle + (width / 2); + const auto above = 50; + const auto bellow = above + 10 + bobbyicons::bobbycar.HEIGHT; + + switch (side) { + case Bobbycar_Side::FRONT: + espgui::tft.fillRect(left, above, width, 5, color); + break; + case Bobbycar_Side::FRONT_LEFT: + espgui::tft.fillRect(left - 10, above + 10, 5, height / 2, color); + espgui::tft.fillRect(left, above, width / 2, 5, color); + break; + case Bobbycar_Side::LEFT: + espgui::tft.fillRect(left - 10, above + 10, 5, height, color); + break; + case Bobbycar_Side::BACK_LEFT: + espgui::tft.fillRect(left - 10, above + 10 + (height / 2), 5, height / 2, color); + espgui::tft.fillRect(left, bellow + 5, width / 2, 5, color); + break; + case Bobbycar_Side::BACK: + espgui::tft.fillRect(left, bellow + 5, width, 5, color); + break; + case Bobbycar_Side::BACK_RIGHT: + espgui::tft.fillRect(right + 5, above + 10 + (height / 2), 5, height / 2, color); + espgui::tft.fillRect(middle, bellow + 5, width / 2, 5, color); + break; + case Bobbycar_Side::RIGHT: + espgui::tft.fillRect(right + 5, above + 10, 5, height, color); + break; + case Bobbycar_Side::FRONT_RIGHT: + espgui::tft.fillRect(right + 5, above + 10, 5, height / 2, color); + espgui::tft.fillRect(middle, above, width / 2, 5, color); + break; + } + // espgui::tft.fillCircle(espgui::tft.width() / 2, 140, 100, TFT_BLACK); +} diff --git a/main/displays/ledstripcolorsdisplay.h b/main/displays/ledstripcolorsdisplay.h index db350a6..65fbcbd 100644 --- a/main/displays/ledstripcolorsdisplay.h +++ b/main/displays/ledstripcolorsdisplay.h @@ -1,57 +1,23 @@ #pragma once -// esp-idf includes -#include +// system includes +#include // 3rdparty lib includes -#include +#include #include -#include -#include -#include -#include // local includes -#include "menudisplay.h" -#include "utils.h" #include "ledstrip.h" -#include "icons/back.h" -#include "icons/bobbycar.h" -#include "texts.h" -#include "actions/dummyaction.h" -#include "globals.h" -namespace { -class LedstripMenu; -} +extern int8_t selected_side; +extern int8_t selected_color; +extern bool state_select_color; +extern bool last_state; -namespace { -static int8_t selected_side = 7; -static int8_t selected_color; -bool state_select_color{false}; -bool last_state = {false}; +extern const std::array Colors; -const std::array Colors = { - CRGB{0,0,0}, - CRGB{255,255,255}, - CRGB{255,0,0}, - CRGB{255,255,0}, - CRGB{0,255,0}, - CRGB{0,255,255}, - CRGB{0,0,255}, - CRGB{255,0,255} -}; - -const std::array tft_colors = { - TFT_BLACK, - TFT_WHITE, - TFT_RED, - TFT_YELLOW, - TFT_GREEN, - TFT_CYAN, - TFT_BLUE, - TFT_MAGENTA -}; +extern const std::array tft_colors; class LedstripColorsDisplay : public espgui::DisplayWithTitle { @@ -72,195 +38,3 @@ public: private: bool already_drew_circle{false}; }; - -std::string LedstripColorsDisplay::text() const -{ - return TEXT_LEDSTRIPCOLORMENU; -} - -void LedstripColorsDisplay::back() -{ - if(!state_select_color) - { - switchScreen(); - } - else - { - state_select_color = false; - tft.fillRect(0, 228, tft.width(), ((tft.width() - 40) / 8) + 4, TFT_BLACK); - } -} - -void LedstripColorsDisplay::initScreen() -{ - Base::initScreen(); - - tft.setSwapBytes(true); - tft.pushImage(70, 60, bobbyicons::bobbycar.WIDTH, bobbyicons::bobbycar.HEIGHT, bobbyicons::bobbycar.buffer); - tft.setSwapBytes(false); -} - -void LedstripColorsDisplay::redraw() -{ - Base::redraw(); - - auto y_pos = ((tft.width() - 40) / 8 + 4) + 240; - if (last_state != state_select_color) - { - tft.fillRect(0,y_pos - 1, tft.width(), 20, TFT_BLACK); - last_state = state_select_color; - } - - tft.setTextFont(2); - tft.setTextColor(TFT_WHITE); - - tft.drawString(state_select_color ? - "Please select a color!" : - "Please select a side!", 50, y_pos); - - if (!already_drew_circle) - { - drawSide(static_cast(selected_side), TFT_GOLD); - already_drew_circle = true; - } -} - -void LedstripColorsDisplay::rotate(int offset) -{ - if (offset < 0) - { - if (state_select_color) - { - selected_color++; - if (selected_color > 7) - { - selected_color = 0; - } - } - else - { - selected_side++; - if (selected_side > 7) - { - selected_side = 0; - } - } - } - else if (offset > 0) - { - if (state_select_color) - { - selected_color--; - if (selected_color < 0) - { - selected_color = 7; - } - } - else - { - selected_side--; - if (selected_side < 0) - { - selected_side = 7; - } - } - } - - if (state_select_color) - { - drawColors(); - } - else - { - tft.fillRect(0, 228, tft.width(), ((tft.width() - 40) / 8) + 4, TFT_BLACK); - clearSides(); - drawSide(static_cast(selected_side), TFT_GOLD); - } -} - -void LedstripColorsDisplay::confirm() -{ - if(!state_select_color) - { - state_select_color = true; - drawColors(); - } - else - { - ledstrip_custom_colors[selected_side] = Colors[selected_color]; - // Uncomment to close select color menu on color select - /* - state_select_color = false; - tft.fillRect(0, 228, tft.width(), ((tft.width() - 40) / 8) + 4, TFT_BLACK); - */ - } -} - -void LedstripColorsDisplay::drawColors() -{ - uint16_t width = (tft.width() - 40); - auto cube_width = width / 8; - - tft.fillRect(0, 228, tft.width(), cube_width + 4, TFT_BLACK); - tft.fillRect(21, 231, width - 1, cube_width - 1, TFT_WHITE); - - tft.fillRect(20 + (selected_color * cube_width - 1), 228, cube_width + 4, cube_width + 4, TFT_YELLOW); - for (int index = 0; index < 8; index++) - { - auto offset = index * (cube_width); - tft.fillRect(22 + offset, 232, cube_width - 4, cube_width - 4, tft_colors[index]); - } -} - -void LedstripColorsDisplay::clearSides() -{ - for(int index = 0; index < 8; index++) - { - drawSide(static_cast(index), TFT_BLACK); - } -} - -void LedstripColorsDisplay::drawSide(Bobbycar_Side side, unsigned int color) -{ - const auto middle = tft.width() / 2; - const auto width = bobbyicons::bobbycar.WIDTH; - const auto height = bobbyicons::bobbycar.HEIGHT; - const auto left = middle - (width / 2); - const auto right = middle + (width / 2); - const auto above = 50; - const auto bellow = above + 10 + bobbyicons::bobbycar.HEIGHT; - - switch (side) { - case Bobbycar_Side::FRONT: - tft.fillRect(left, above, width, 5, color); - break; - case Bobbycar_Side::FRONT_LEFT: - tft.fillRect(left - 10, above + 10, 5, height / 2, color); - tft.fillRect(left, above, width / 2, 5, color); - break; - case Bobbycar_Side::LEFT: - tft.fillRect(left - 10, above + 10, 5, height, color); - break; - case Bobbycar_Side::BACK_LEFT: - tft.fillRect(left - 10, above + 10 + (height / 2), 5, height / 2, color); - tft.fillRect(left, bellow + 5, width / 2, 5, color); - break; - case Bobbycar_Side::BACK: - tft.fillRect(left, bellow + 5, width, 5, color); - break; - case Bobbycar_Side::BACK_RIGHT: - tft.fillRect(right + 5, above + 10 + (height / 2), 5, height / 2, color); - tft.fillRect(middle, bellow + 5, width / 2, 5, color); - break; - case Bobbycar_Side::RIGHT: - tft.fillRect(right + 5, above + 10, 5, height, color); - break; - case Bobbycar_Side::FRONT_RIGHT: - tft.fillRect(right + 5, above + 10, 5, height / 2, color); - tft.fillRect(middle, above, width / 2, 5, color); - break; - } - // tft.fillCircle(tft.width() / 2, 140, 100, TFT_BLACK); -} - -} // Namespace diff --git a/main/displays/menus/commanddebugmenu.h b/main/displays/menus/commanddebugmenu.h index e751a63..e264a2b 100644 --- a/main/displays/menus/commanddebugmenu.h +++ b/main/displays/menus/commanddebugmenu.h @@ -10,11 +10,6 @@ #include "texts.h" #include "debugtexthelpers.h" -// forward declares -namespace { -class DebugMenu; -} // namespace - using namespace espgui; namespace { diff --git a/main/displays/menus/debugmenu.cpp b/main/displays/menus/debugmenu.cpp index e69de29..8b065e6 100644 --- a/main/displays/menus/debugmenu.cpp +++ b/main/displays/menus/debugmenu.cpp @@ -0,0 +1,59 @@ +#include "debugmenu.h" + +// 3rdparty lib includes +#include "menuitem.h" +#include "actions/switchscreenaction.h" +#include "actions/dummyaction.h" +#include "actions/toggleboolaction.h" +#include "checkboxicon.h" +#include "icons/back.h" + +// local includes +#include "utils.h" +#include "actions/loadsettingsaction.h" +#include "actions/savesettingsaction.h" +#include "actions/erasenvsaction.h" +#include "icons/lock.h" +#include "debugcolorhelpers.h" +#include "esptexthelpers.h" +#include "displays/menus/commanddebugmenu.h" +#include "displays/menus/motorstatedebugmenu.h" +#include "displays/menus/feedbackdebugmenu.h" +#include "displays/menus/motorfeedbackdebugmenu.h" +#include "displays/menus/dynamicdebugmenu.h" +#include "displays/menus/mainmenu.h" + +using namespace espgui; + +DebugMenu::DebugMenu() +{ + constructMenuItem, LoadSettingsAction>>(); + constructMenuItem, SaveSettingsAction>>(); + constructMenuItem, EraseNvsAction>>(); + constructMenuItem>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem>(); + constructMenuItem, SwitchScreenAction, FrontFeedbackColor>>(); + constructMenuItem, SwitchScreenAction, BackFeedbackColor>>(); + constructMenuItem>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem>(); + constructMenuItem, SwitchScreenAction, FrontFeedbackColor>>(); + constructMenuItem, SwitchScreenAction, FrontFeedbackColor>>(); + constructMenuItem, SwitchScreenAction, BackFeedbackColor>>(); + constructMenuItem, SwitchScreenAction, BackFeedbackColor>>(); + constructMenuItem>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void DebugMenu::back() +{ + switchScreen(); +} diff --git a/main/displays/menus/debugmenu.h b/main/displays/menus/debugmenu.h index 2ae7392..8aeb2a2 100644 --- a/main/displays/menus/debugmenu.h +++ b/main/displays/menus/debugmenu.h @@ -1,56 +1,17 @@ #pragma once -// local includes +// 3rdparty lib includes #include "menudisplay.h" -#include "utils.h" -#include "menuitem.h" -#include "actions/loadsettingsaction.h" -#include "actions/savesettingsaction.h" -#include "actions/erasenvsaction.h" -#include "actions/switchscreenaction.h" -#include "actions/dummyaction.h" -#include "actions/toggleboolaction.h" -#include "icons/lock.h" -#include "checkboxicon.h" -#include "icons/back.h" + +// local includes #include "texts.h" -#include "debugcolorhelpers.h" -using namespace espgui; - -namespace { class DebugMenu : - public MenuDisplay, - public StaticText, - public BackActionInterface> + public espgui::MenuDisplay, + public espgui::StaticText { public: - DebugMenu() - { - constructMenuItem, LoadSettingsAction>>(); - constructMenuItem, SaveSettingsAction>>(); - constructMenuItem, EraseNvsAction>>(); - constructMenuItem>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem>(); - constructMenuItem, SwitchScreenAction, FrontFeedbackColor>>(); - constructMenuItem, SwitchScreenAction, BackFeedbackColor>>(); - constructMenuItem>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem>(); - constructMenuItem, SwitchScreenAction, FrontFeedbackColor>>(); - constructMenuItem, SwitchScreenAction, FrontFeedbackColor>>(); - constructMenuItem, SwitchScreenAction, BackFeedbackColor>>(); - constructMenuItem, SwitchScreenAction, BackFeedbackColor>>(); - constructMenuItem>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); - } + DebugMenu(); + + void back() override; }; -} // namespace diff --git a/main/displays/menus/dynamicdebugmenu.h b/main/displays/menus/dynamicdebugmenu.h index 7b125a9..470464b 100644 --- a/main/displays/menus/dynamicdebugmenu.h +++ b/main/displays/menus/dynamicdebugmenu.h @@ -22,11 +22,6 @@ #include "icons/back.h" #include "texts.h" -// forward declares -namespace { -class DebugMenu; -} // namespace - using namespace espgui; namespace { diff --git a/main/displays/menus/feedbackdebugmenu.h b/main/displays/menus/feedbackdebugmenu.h index b9c4b30..5e1fb06 100644 --- a/main/displays/menus/feedbackdebugmenu.h +++ b/main/displays/menus/feedbackdebugmenu.h @@ -11,11 +11,6 @@ #include "debugtexthelpers.h" #include "debugcolorhelpers.h" -// forward declares -namespace { -class DebugMenu; -} // namespace - using namespace espgui; namespace { diff --git a/main/displays/menus/ledstripmenu.cpp b/main/displays/menus/ledstripmenu.cpp index e69de29..271c308 100644 --- a/main/displays/menus/ledstripmenu.cpp +++ b/main/displays/menus/ledstripmenu.cpp @@ -0,0 +1,143 @@ +#include "ledstripmenu.h" + +// 3rdparty lib includes +#include +#include "menuitem.h" +#include "actions/toggleboolaction.h" +#include "actions/switchscreenaction.h" +#include "icons/back.h" +#include "checkboxicon.h" +#include "changevaluedisplay.h" +#include "actioninterface.h" + +// local includes +#include "ledstripselectanimationmenu.h" +#include "ledstripselectblinkmenu.h" +#include "globals.h" +#include "accessors/settingsaccessors.h" +#ifdef FEATURE_LEDSTRIP +#include "ledstrip.h" +#endif +#include "displays/ledstripcolorsdisplay.h" +#include "displays/menus/mainmenu.h" + +#ifdef FEATURE_LEDSTRIP +namespace { +using LedsCountChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LedsCountAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using CenterOffsetChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + CenterOffsetAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using SmallOffsetChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + SmallOffsetAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using BigOffsetChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + BigOffsetAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using DeziampereChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + DeziampereAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using StVOOffsetChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LedsStVOFrontOffsetAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using StVOLengthChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LedsStVOFrontLengthAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using animationMultiplierChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + AnimationMultiplierAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +using ledstripBrightnessChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LedstripBrightnessAccessor, + BackActionInterface>, + SwitchScreenAction +>; + +class AllCustomLedsOffAction : public virtual ActionInterface +{ +public: + void triggered() override + { + for(int index = 0; index < 8; index++) + { + ledstrip_custom_colors[index] = CRGB{0,0,0}; + } + } +}; +} // namespace + +using namespace espgui; + +LedstripMenu::LedstripMenu() +{ + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableLedAnimationAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableBrakeLightsAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableBeepWhenBlinkAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableFullBlinkAccessor>>(); + + if (!simplified) { constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableLedstripStVOAccessor>>(); } + constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableLedstripStVOFrontlight>>(); + constructMenuItem, AllCustomLedsOffAction>>(); + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction>>(); + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void LedstripMenu::back() +{ + switchScreen(); +} +#endif diff --git a/main/displays/menus/ledstripmenu.h b/main/displays/menus/ledstripmenu.h index c2e44a2..d093463 100644 --- a/main/displays/menus/ledstripmenu.h +++ b/main/displays/menus/ledstripmenu.h @@ -1,152 +1,19 @@ #pragma once -#include +// 3rdparty lib includes +#include "menudisplay.h" // local includes -#include "menudisplay.h" -#include "menuitem.h" -#include "actions/toggleboolaction.h" -#include "actions/switchscreenaction.h" -#include "ledstripselectanimationmenu.h" -#include "ledstripselectblinkmenu.h" #include "texts.h" -#include "icons/back.h" -#include "checkboxicon.h" -#include "globals.h" -#include "accessors/settingsaccessors.h" + #ifdef FEATURE_LEDSTRIP -#include "ledstrip.h" -#endif -#include "changevaluedisplay.h" -#include "actioninterface.h" - -// forward declares -namespace { -class MainWindow; -class LedstripColorsDisplay; -} // namespace - -using namespace espgui; - -namespace { -#ifdef FEATURE_LEDSTRIP -class LedstripMenu; -class LedstripSelectAnimationMenu; - -using LedsCountChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - LedsCountAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using CenterOffsetChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - CenterOffsetAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using SmallOffsetChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - SmallOffsetAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using BigOffsetChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - BigOffsetAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using DeziampereChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - DeziampereAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using StVOOffsetChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - LedsStVOFrontOffsetAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using StVOLengthChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - LedsStVOFrontLengthAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using animationMultiplierChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - AnimationMultiplierAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -using ledstripBrightnessChangeScreen = makeComponent< - ChangeValueDisplay, - StaticText, - LedstripBrightnessAccessor, - BackActionInterface>, - SwitchScreenAction ->; - -class AllCustomLedsOffAction : public virtual ActionInterface -{ -public: - void triggered() { - for(int index = 0; index < 8; index++) - { - ledstrip_custom_colors[index] = CRGB{0,0,0}; - } - } -}; - class LedstripMenu : - public MenuDisplay, - public StaticText, - public BackActionInterface> + public espgui::MenuDisplay, + public espgui::StaticText { public: - LedstripMenu() - { - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableLedAnimationAccessor>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableBrakeLightsAccessor>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableBeepWhenBlinkAccessor>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableFullBlinkAccessor>>(); + LedstripMenu(); - if (!simplified) { constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableLedstripStVOAccessor>>(); } - constructMenuItem, ToggleBoolAction, CheckboxIcon, EnableLedstripStVOFrontlight>>(); - constructMenuItem, AllCustomLedsOffAction>>(); - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction>>(); - constructMenuItem, SwitchScreenAction>>(); - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - if (!simplified) { constructMenuItem, SwitchScreenAction>>(); } - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); - } + void back() override; }; #endif -} // namespace diff --git a/main/displays/menus/ledstripselectanimationmenu.h b/main/displays/menus/ledstripselectanimationmenu.h index df731a7..4a22635 100644 --- a/main/displays/menus/ledstripselectanimationmenu.h +++ b/main/displays/menus/ledstripselectanimationmenu.h @@ -32,10 +32,6 @@ class currentSelectedAnimationText : public virtual TextInterface { public: std: using namespace espgui; -namespace { - class LedstripMenu; -} - namespace { class LedstripSelectAnimationMenu : public MenuDisplay, diff --git a/main/displays/menus/ledstripselectblinkmenu.h b/main/displays/menus/ledstripselectblinkmenu.h index 06122e4..cbb862a 100644 --- a/main/displays/menus/ledstripselectblinkmenu.h +++ b/main/displays/menus/ledstripselectblinkmenu.h @@ -37,11 +37,7 @@ class currentSelectedBlinkAnimationText : public virtual TextInterface { public: using namespace espgui; -namespace { - class LedstripMenu; -} - -namespace { +namespace { class LedstripSelectBlinkMenu : public MenuDisplay, public StaticText, diff --git a/main/displays/menus/motorfeedbackdebugmenu.h b/main/displays/menus/motorfeedbackdebugmenu.h index c1a2c70..d3d6a4a 100644 --- a/main/displays/menus/motorfeedbackdebugmenu.h +++ b/main/displays/menus/motorfeedbackdebugmenu.h @@ -11,11 +11,6 @@ #include "debugtexthelpers.h" #include "debugcolorhelpers.h" -// forward declares -namespace { -class DebugMenu; -} // namespace - using namespace espgui; namespace { diff --git a/main/displays/menus/motorstatedebugmenu.h b/main/displays/menus/motorstatedebugmenu.h index 378b1a1..ff591d5 100644 --- a/main/displays/menus/motorstatedebugmenu.h +++ b/main/displays/menus/motorstatedebugmenu.h @@ -10,11 +10,6 @@ #include "texts.h" #include "debugtexthelpers.h" -// forward declares -namespace { -class DebugMenu; -} // namespace - using namespace espgui; namespace { diff --git a/main/displays/powersupplydisplay.cpp b/main/displays/powersupplydisplay.cpp index e69de29..5ed8339 100644 --- a/main/displays/powersupplydisplay.cpp +++ b/main/displays/powersupplydisplay.cpp @@ -0,0 +1,46 @@ +#include "powersupplydisplay.h" + +// 3rdparty lib includes +#include +#include +#include "actions/switchscreenaction.h" + +// local includes +#include "globals.h" +#include "displays/menus/mainmenu.h" + +#if defined(FEATURE_CAN) && defined(FEATURE_POWERSUPPLY) +void PowerSupplyDisplay::initScreen() +{ + espgui::tft.fillScreen(TFT_BLACK); + espgui::tft.setTextColor(TFT_WHITE, TFT_BLACK); + + espgui::tft.setTextFont(4); + + espgui::tft.drawString("Voltage:", 0, m_voltageLabel.y()); + m_voltageLabel.start(); + espgui::tft.drawString("Current:", 0, m_currentLabel.y()); + m_currentLabel.start(); +} + +void PowerSupplyDisplay::redraw() +{ + m_voltageLabel.redraw(std::to_string(50.4) + 'V'); + m_currentLabel.redraw(std::to_string(15.1) + 'A'); +} + +void PowerSupplyDisplay::confirm() +{ + // TODO +} + +void PowerSupplyDisplay::back() +{ + espgui::switchScreen(); +} + +void PowerSupplyDisplay::rotate(int offset) +{ + // TODO +} +#endif diff --git a/main/displays/powersupplydisplay.h b/main/displays/powersupplydisplay.h index be38517..afe7ba9 100644 --- a/main/displays/powersupplydisplay.h +++ b/main/displays/powersupplydisplay.h @@ -1,56 +1,24 @@ #pragma once -// Arduino includes -#include - -// local includes +// 3rdparty lib includes #include "display.h" -#include "actions/switchscreenaction.h" -#include "globals.h" #include "widgets/label.h" -namespace { #if defined(FEATURE_CAN) && defined(FEATURE_POWERSUPPLY) -class PowerSupplyDisplay : public Display, public DummyConfirm, public BackActionInterface> +class PowerSupplyDisplay : public espgui::Display { + using Base = espgui::Display; + public: void initScreen() override; void redraw() override; void confirm() override; + void back() override; + void rotate(int offset) override; - Label m_voltageLabel{120, 50}; - Label m_currentLabel{120, 75}; + espgui::Label m_voltageLabel{120, 50}; + espgui::Label m_currentLabel{120, 75}; }; - -void PowerSupplyDisplay::initScreen() -{ - tft.fillScreen(TFT_BLACK); - tft.setTextColor(TFT_WHITE, TFT_BLACK); - - tft.setTextFont(4); - - tft.drawString("Voltage:", 0, m_voltageLabel.y()); - m_voltageLabel.start(); - tft.drawString("Current:", 0, m_currentLabel.y()); - m_currentLabel.start(); -} - -void PowerSupplyDisplay::redraw() -{ - m_voltageLabel.redraw(std::to_string(50.4) + 'V'); - m_currentLabel.redraw(std::to_string(15.1) + 'A'); -} - -void PowerSupplyDisplay::confirm() -{ - // TODO -} - -void PowerSupplyDisplay::rotate(int offset) -{ - // TODO -} #endif -} diff --git a/main/esptexthelpers.h b/main/esptexthelpers.h index b5a47ee..d312f63 100644 --- a/main/esptexthelpers.h +++ b/main/esptexthelpers.h @@ -5,6 +5,7 @@ // 3rdparty lib includes #include +#include // local includes #include "textinterface.h" @@ -37,46 +38,7 @@ class HeapLargest32Text : public virtual espgui::TextInterface { public: std::st return fmt::format("HeapLargest32: {}", heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL|MALLOC_CAP_32BIT)); }}; class LastRebootReasonText : public virtual espgui::TextInterface { public: std::string text() const override { - std::string reset_reason_string; - switch (esp_reset_reason()) { - case 0: - reset_reason_string = "Unkown"; - break; - case 1: - reset_reason_string = "Power on"; - break; - case 2: - reset_reason_string = "External Pin"; - break; - case 3: - reset_reason_string = "esp_restart"; - break; - case 4: - reset_reason_string = "Exception/panic"; - break; - case 5: - reset_reason_string = "Interrupt wd"; - break; - case 6: - reset_reason_string = "Task wd"; - break; - case 7: - reset_reason_string = "Other wd"; - break; - case 8: - reset_reason_string = "Deepsleep"; - break; - case 9: - reset_reason_string = "Brownout"; - break; - case 10: - reset_reason_string = "SDIO"; - break; - default: - return fmt::format("Last Reboot Reason: {}", esp_reset_reason()); - } - - return fmt::format("Last Reboot Reason: {}", reset_reason_string); }}; + return fmt::format("Last Reboot Reason: {}", espcpputils::toString(esp_reset_reason())); }}; constexpr char TEXT_ESPCHIPREVISION[] = "Chip revision: "; using EspChipRevisionText = espgui::StaticText; //EspStatusTextHelper; diff --git a/main/ledstrip.cpp b/main/ledstrip.cpp index e69de29..3264dba 100644 --- a/main/ledstrip.cpp +++ b/main/ledstrip.cpp @@ -0,0 +1,259 @@ +#include "ledstrip.h" + +// 3rdparty lib includes + +// local includes +#include "globals.h" +#include "cpputils.h" +#include "espchrono.h" +#include "ledstripdefines.h" + +using namespace std::chrono_literals; + +#ifdef FEATURE_LEDSTRIP +std::vector leds; +uint8_t gHue = 0; + +int16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE; +int16_t animation_type = LEDSTRIP_ANIMATION_TYPE_DEFAULTRAINBOW; + + +void initLedStrip() +{ + animation_type = settings.ledstrip.animationType; + leds.resize(settings.ledstrip.ledsCount); + FastLED.addLeds(&*std::begin(leds), leds.size()) + .setCorrection(TypicalSMD5050); +} + +void updateLedStrip() +{ + EVERY_N_MILLISECONDS( 20 ) { gHue++; } + static bool have_disabled_beeper = false; + + if (cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) + { + std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); + if (espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms) + { + if (settings.ledstrip.enableBeepWhenBlink) + { + for (Controller &controller : controllers) + controller.command.buzzer.freq = 3; + } + auto color = CRGB{255, 255, 0}; + const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); + + if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && !settings.ledstrip.enableFullBlink) + { + std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color); + } + else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && settings.ledstrip.enableFullBlink) + { + std::fill(std::begin(leds), center, color); + } + if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && !settings.ledstrip.enableFullBlink) + { + std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color); + } + else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && settings.ledstrip.enableFullBlink) + { + std::fill(center, std::end(leds), color); + } + + } else { + if (settings.ledstrip.enableBeepWhenBlink) + { + for (Controller &controller : controllers) + controller.command.buzzer.freq = 0; + } + } + } + else + { + if (settings.ledstrip.enableBrakeLights) + { + float avgPwm{}; + for (const Controller &controller : controllers) + { + avgPwm += + controller.command.left.pwm * (controller.invertLeft ? -1 : 1) + + controller.command.right.pwm * (controller.invertRight ? -1 : 1); + } + avgPwm /= 4; + + if (avgPwm < -1.f) + { + auto color = avgSpeedKmh < -0.1f ? CRGB{255, 255, 255} : CRGB{255, 0, 0}; + + const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); + + std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); + if (settings.ledstrip.enableFullBlink) + { + std::fill(std::begin(leds), std::end(leds), color); + } + else + { + std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color); + std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color); + } + } + else + { + showAnimation(); + } + } + else + { + showAnimation(); + } + } + + if (have_disabled_beeper == false && (!(cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) || !settings.ledstrip.enableBeepWhenBlink)) + { + for (Controller &controller : controllers) + controller.command.buzzer.freq = 0; + have_disabled_beeper = true; + } + else if ((cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) && settings.ledstrip.enableBeepWhenBlink) have_disabled_beeper = false; + + if (simplified || settings.ledstrip.enableStVO) + { + const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); + + if (!(blinkAnimation == LEDSTRIP_OVERWRITE_BLINKLEFT || blinkAnimation == LEDSTRIP_OVERWRITE_BLINKBOTH) || !(espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms) || settings.ledstrip.enableFullBlink) // Condition for right + { + std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, CRGB{0, 0, 0}); + std::fill(center - settings.ledstrip.bigOffset - 1U, center - settings.ledstrip.smallOffset - 1U, CRGB{255, 0, 0}); // Right + } + if (!(blinkAnimation == LEDSTRIP_OVERWRITE_BLINKRIGHT || blinkAnimation == LEDSTRIP_OVERWRITE_BLINKBOTH) || !(espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms) || settings.ledstrip.enableFullBlink) // Condition for left + { + std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, CRGB{0, 0, 0}); + std::fill(center + settings.ledstrip.smallOffset + 1U, center + settings.ledstrip.bigOffset + 1U, CRGB{255, 0, 0}); // Left + } + + if (settings.ledstrip.stvoFrontEnable) + { + std::fill(std::begin(leds) + settings.ledstrip.stvoFrontOffset, std::begin(leds) + settings.ledstrip.stvoFrontOffset + settings.ledstrip.stvoFrontLength, CRGB{255, 255, 255}); + std::fill(std::end(leds) - settings.ledstrip.stvoFrontOffset - settings.ledstrip.stvoFrontLength, std::end(leds) - settings.ledstrip.stvoFrontOffset, CRGB{255, 255, 255}); + } + } + + FastLED.setMaxPowerInVoltsAndMilliamps(5,settings.ledstrip.deziampere * 100); + FastLED.setBrightness(settings.ledstrip.brightness); + FastLED.show(); +} + +void showAnimation() +{ + if (settings.ledstrip.enableLedAnimation && !simplified) + { + if (animation_type == LEDSTRIP_ANIMATION_TYPE_DEFAULTRAINBOW) showDefaultLedstrip(); + else if (animation_type == LEDSTRIP_ANIMATION_TYPE_BETTERRAINBOW) showBetterRainbow(); + else if (animation_type == LEDSTRIP_ANIMATION_TYPE_SPEEDSYNCANIMATION) showSpeedSyncAnimation(); + else if (animation_type == LEDSTRIP_ANIMATION_TYPE_CUSTOMCOLOR) showCustomColor(); + else showDefaultLedstrip(); + } + else + { + std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); + } +} + +void showBetterRainbow() +{ + fill_rainbow(&*std::begin(leds), leds.size(), gHue); +} + +void fill_rainbow_invert_at( struct CRGB * pFirstLED, int numToFill, int invertAtLed, + uint8_t initialhue, + float deltahue ) +{ + float huecalc = initialhue; + CHSV hsv; + hsv.hue = initialhue; + hsv.val = 255; + hsv.sat = 240; + for( int i = 0; i < numToFill; i++) { + hsv.hue = huecalc; + pFirstLED[i] = hsv; + if(i>invertAtLed){ + huecalc -= deltahue; + }else{ + huecalc += deltahue; + } + } +} + +void showSpeedSyncAnimation() +{ +#ifdef LEDS_PER_METER + const float leds_per_meter = LEDS_PER_METER; +#else + const float leds_per_meter = 144; +#endif + + static auto last_interval = espchrono::millis_clock::now(); + auto difference_ms = espchrono::ago(last_interval).count(); + + static float hue_result = 0; + + const float hue_per_led = 1. / std::max(uint8_t(1), uint8_t(settings.ledstrip.animationMultiplier)); + const float meter_per_second = avgSpeedKmh / 3.6; + const float leds_per_second = meter_per_second * leds_per_meter; + const float hue_per_second = leds_per_second * hue_per_led; + + hue_result += hue_per_second * difference_ms / 1000.f; + + fill_rainbow_invert_at(&*std::begin(leds), leds.size(),leds.size()/2, hue_result,-hue_per_led); + + last_interval = espchrono::millis_clock::now(); +} + +void showDefaultLedstrip() +{ + fadeToBlackBy(&*std::begin(leds), leds.size(), 20); + + uint8_t dothue = 0; + for (int i = 0; i < 8; i++) + { + leds[beatsin16(i + 7, 0, leds.size())] |= CHSV(dothue, 200, 255); + dothue += 32; + } +} + +void showCustomColor() +{ + const auto eighth_length = leds.size() / 8; + const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); + + std::fill(std::begin(leds), std::end(leds), ledstrip_custom_colors[int(Bobbycar_Side::FRONT)]); // Front + std::fill(center - (eighth_length / 2), center + (eighth_length / 2), ledstrip_custom_colors[int(Bobbycar_Side::BACK)]); // Back + +#ifdef LEDSTRIP_WRONG_DIRECTION + std::fill(center + (eighth_length / 2), center + (eighth_length / 2) + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::BACK_LEFT)]); // Back Left + std::fill(center - (eighth_length / 2) - eighth_length, center - (eighth_length / 2), ledstrip_custom_colors[int(Bobbycar_Side::BACK_RIGHT)]); // Back Right +#else + std::fill(center + (eighth_length / 2), center + (eighth_length / 2) + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::BACK_RIGHT)]); // Back Right + std::fill(center - (eighth_length / 2) - eighth_length, center - (eighth_length / 2), ledstrip_custom_colors[int(Bobbycar_Side::BACK_LEFT)]); // Back Left +#endif + +#ifdef LEDSTRIP_WRONG_DIRECTION + std::fill(center + (eighth_length / 2) + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::LEFT)]); // Left + std::fill(center - (eighth_length / 2) - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::RIGHT)]); // Right +#else + std::fill(center + (eighth_length / 2) + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::RIGHT)]); // Right + std::fill(center - (eighth_length / 2) - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::LEFT)]); // Left +#endif + +#ifdef LEDSTRIP_WRONG_DIRECTION + std::fill(center + (eighth_length / 2) + eighth_length + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_LEFT)]); // Front Left + std::fill(center - (eighth_length / 2) - eighth_length - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_RIGHT)]); // Front Right +#else + std::fill(center + (eighth_length / 2) + eighth_length + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_RIGHT)]); // Front Right + std::fill(center - (eighth_length / 2) - eighth_length - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_LEFT)]); // Front Left +#endif +} + +#endif diff --git a/main/ledstrip.h b/main/ledstrip.h index 25f84ef..aea82f8 100644 --- a/main/ledstrip.h +++ b/main/ledstrip.h @@ -1,19 +1,14 @@ #pragma once -#ifdef FEATURE_LEDSTRIP + +// system includes +#include + // 3rdparty lib includes #include -// local includes -#include "globals.h" -#include "cpputils.h" -#include "espchrono.h" -#include "ledstripdefines.h" - -using namespace std::chrono_literals; - -namespace { - -enum Bobbycar_Side { +#ifdef FEATURE_LEDSTRIP +enum Bobbycar_Side +{ FRONT_RIGHT, RIGHT, BACK_RIGHT, @@ -24,11 +19,11 @@ enum Bobbycar_Side { FRONT }; -std::vector leds; -uint8_t gHue = 0; +extern std::vector leds; +extern uint8_t gHue; -int16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE; -int16_t animation_type = LEDSTRIP_ANIMATION_TYPE_DEFAULTRAINBOW; +extern int16_t blinkAnimation; +extern int16_t animation_type; void showDefaultLedstrip(); void showAnimation(); @@ -36,240 +31,7 @@ void showBetterRainbow(); void showSpeedSyncAnimation(); void showCustomColor(); -void initLedStrip() -{ - animation_type = settings.ledstrip.animationType; - leds.resize(settings.ledstrip.ledsCount); - FastLED.addLeds(&*std::begin(leds), leds.size()) - .setCorrection(TypicalSMD5050); -} +void initLedStrip(); -void updateLedStrip() -{ - EVERY_N_MILLISECONDS( 20 ) { gHue++; } - static bool have_disabled_beeper = false; - - if (cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) - { - std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); - if (espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms) - { - if (settings.ledstrip.enableBeepWhenBlink) - { - for (Controller &controller : controllers) - controller.command.buzzer.freq = 3; - } - auto color = CRGB{255, 255, 0}; - const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); - - if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && !settings.ledstrip.enableFullBlink) - { - std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color); - } - else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKRIGHT && settings.ledstrip.enableFullBlink) - { - std::fill(std::begin(leds), center, color); - } - if (blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && !settings.ledstrip.enableFullBlink) - { - std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color); - } - else if(blinkAnimation != LEDSTRIP_OVERWRITE_BLINKLEFT && settings.ledstrip.enableFullBlink) - { - std::fill(center, std::end(leds), color); - } - - } else { - if (settings.ledstrip.enableBeepWhenBlink) - { - for (Controller &controller : controllers) - controller.command.buzzer.freq = 0; - } - } - } - else - { - if (settings.ledstrip.enableBrakeLights) - { - float avgPwm{}; - for (const Controller &controller : controllers) - { - avgPwm += - controller.command.left.pwm * (controller.invertLeft ? -1 : 1) + - controller.command.right.pwm * (controller.invertRight ? -1 : 1); - } - avgPwm /= 4; - - if (avgPwm < -1.f) - { - auto color = avgSpeedKmh < -0.1f ? CRGB{255, 255, 255} : CRGB{255, 0, 0}; - - const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); - - std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); - if (settings.ledstrip.enableFullBlink) - { - std::fill(std::begin(leds), std::end(leds), color); - } - else - { - std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, color); - std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, color); - } - } - else - { - showAnimation(); - } - } - else - { - showAnimation(); - } - } - - if (have_disabled_beeper == false && (!(cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) || !settings.ledstrip.enableBeepWhenBlink)) - { - for (Controller &controller : controllers) - controller.command.buzzer.freq = 0; - have_disabled_beeper = true; - } - else if ((cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)) && settings.ledstrip.enableBeepWhenBlink) have_disabled_beeper = false; - - if (simplified || settings.ledstrip.enableStVO) - { - const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); - - if (!(blinkAnimation == LEDSTRIP_OVERWRITE_BLINKLEFT || blinkAnimation == LEDSTRIP_OVERWRITE_BLINKBOTH) || !(espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms) || settings.ledstrip.enableFullBlink) // Condition for right - { - std::fill(center - settings.ledstrip.bigOffset, center - settings.ledstrip.smallOffset, CRGB{0, 0, 0}); - std::fill(center - settings.ledstrip.bigOffset - 1U, center - settings.ledstrip.smallOffset - 1U, CRGB{255, 0, 0}); // Right - } - if (!(blinkAnimation == LEDSTRIP_OVERWRITE_BLINKRIGHT || blinkAnimation == LEDSTRIP_OVERWRITE_BLINKBOTH) || !(espchrono::millis_clock::now().time_since_epoch() % 750ms < 375ms) || settings.ledstrip.enableFullBlink) // Condition for left - { - std::fill(center + settings.ledstrip.smallOffset, center + settings.ledstrip.bigOffset, CRGB{0, 0, 0}); - std::fill(center + settings.ledstrip.smallOffset + 1U, center + settings.ledstrip.bigOffset + 1U, CRGB{255, 0, 0}); // Left - } - - if (settings.ledstrip.stvoFrontEnable) - { - std::fill(std::begin(leds) + settings.ledstrip.stvoFrontOffset, std::begin(leds) + settings.ledstrip.stvoFrontOffset + settings.ledstrip.stvoFrontLength, CRGB{255, 255, 255}); - std::fill(std::end(leds) - settings.ledstrip.stvoFrontOffset - settings.ledstrip.stvoFrontLength, std::end(leds) - settings.ledstrip.stvoFrontOffset, CRGB{255, 255, 255}); - } - } - - FastLED.setMaxPowerInVoltsAndMilliamps(5,settings.ledstrip.deziampere * 100); - FastLED.setBrightness(settings.ledstrip.brightness); - FastLED.show(); -} - -void showAnimation() { - if (settings.ledstrip.enableLedAnimation && !simplified) - { - if (animation_type == LEDSTRIP_ANIMATION_TYPE_DEFAULTRAINBOW) showDefaultLedstrip(); - else if (animation_type == LEDSTRIP_ANIMATION_TYPE_BETTERRAINBOW) showBetterRainbow(); - else if (animation_type == LEDSTRIP_ANIMATION_TYPE_SPEEDSYNCANIMATION) showSpeedSyncAnimation(); - else if (animation_type == LEDSTRIP_ANIMATION_TYPE_CUSTOMCOLOR) showCustomColor(); - else showDefaultLedstrip(); - } - else - { - std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0}); - } -} - -void showBetterRainbow() { - fill_rainbow(&*std::begin(leds), leds.size(), gHue); -} - -void fill_rainbow_invert_at( struct CRGB * pFirstLED, int numToFill, int invertAtLed, - uint8_t initialhue, - float deltahue ) -{ - float huecalc = initialhue; - CHSV hsv; - hsv.hue = initialhue; - hsv.val = 255; - hsv.sat = 240; - for( int i = 0; i < numToFill; i++) { - hsv.hue = huecalc; - pFirstLED[i] = hsv; - if(i>invertAtLed){ - huecalc -= deltahue; - }else{ - huecalc += deltahue; - } - } -} - -void showSpeedSyncAnimation() { -#ifdef LEDS_PER_METER - const float leds_per_meter = LEDS_PER_METER; -#else - const float leds_per_meter = 144; -#endif - - static auto last_interval = espchrono::millis_clock::now(); - auto difference_ms = espchrono::ago(last_interval).count(); - - static float hue_result = 0; - - const float hue_per_led = 1. / std::max(uint8_t(1), uint8_t(settings.ledstrip.animationMultiplier)); - const float meter_per_second = avgSpeedKmh / 3.6; - const float leds_per_second = meter_per_second * leds_per_meter; - const float hue_per_second = leds_per_second * hue_per_led; - - hue_result += hue_per_second * difference_ms / 1000.f; - - fill_rainbow_invert_at(&*std::begin(leds), leds.size(),leds.size()/2, hue_result,-hue_per_led); - - last_interval = espchrono::millis_clock::now(); -} - -void showDefaultLedstrip() -{ - fadeToBlackBy(&*std::begin(leds), leds.size(), 20); - - uint8_t dothue = 0; - for (int i = 0; i < 8; i++) - { - leds[beatsin16(i + 7, 0, leds.size())] |= CHSV(dothue, 200, 255); - dothue += 32; - } -} - -void showCustomColor() -{ - const auto eighth_length = leds.size() / 8; - const auto center = (std::begin(leds) + (leds.size() / 2) + settings.ledstrip.centerOffset); - - std::fill(std::begin(leds), std::end(leds), ledstrip_custom_colors[int(Bobbycar_Side::FRONT)]); // Front - std::fill(center - (eighth_length / 2), center + (eighth_length / 2), ledstrip_custom_colors[int(Bobbycar_Side::BACK)]); // Back - -#ifdef LEDSTRIP_WRONG_DIRECTION - std::fill(center + (eighth_length / 2), center + (eighth_length / 2) + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::BACK_LEFT)]); // Back Left - std::fill(center - (eighth_length / 2) - eighth_length, center - (eighth_length / 2), ledstrip_custom_colors[int(Bobbycar_Side::BACK_RIGHT)]); // Back Right -#else - std::fill(center + (eighth_length / 2), center + (eighth_length / 2) + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::BACK_RIGHT)]); // Back Right - std::fill(center - (eighth_length / 2) - eighth_length, center - (eighth_length / 2), ledstrip_custom_colors[int(Bobbycar_Side::BACK_LEFT)]); // Back Left -#endif - -#ifdef LEDSTRIP_WRONG_DIRECTION - std::fill(center + (eighth_length / 2) + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::LEFT)]); // Left - std::fill(center - (eighth_length / 2) - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::RIGHT)]); // Right -#else - std::fill(center + (eighth_length / 2) + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::RIGHT)]); // Right - std::fill(center - (eighth_length / 2) - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::LEFT)]); // Left -#endif - -#ifdef LEDSTRIP_WRONG_DIRECTION - std::fill(center + (eighth_length / 2) + eighth_length + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_LEFT)]); // Front Left - std::fill(center - (eighth_length / 2) - eighth_length - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_RIGHT)]); // Front Right -#else - std::fill(center + (eighth_length / 2) + eighth_length + eighth_length, center + (eighth_length / 2) + eighth_length + eighth_length + eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_RIGHT)]); // Front Right - std::fill(center - (eighth_length / 2) - eighth_length - eighth_length - eighth_length, center - (eighth_length / 2) - eighth_length - eighth_length, ledstrip_custom_colors[int(Bobbycar_Side::FRONT_LEFT)]); // Front Left -#endif - -} -} // namespace +void updateLedStrip(); #endif