From 273f9491f875e4d905e160f0501402bf0e717fdd Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Sat, 20 Sep 2025 20:21:42 +0200 Subject: [PATCH] some optimizations regarding LED color Signed-off-by: Peter Siegmund --- .../connectivity-manager/src/wifi_manager.c | 4 + firmware/components/insa/CMakeLists.txt | 3 + .../insa/include/common/ColorSettingsMenu.h | 29 +++++ .../components/insa/include/common/Menu.h | 2 +- .../components/insa/include/common/Widget.h | 10 +- .../insa/include/ui/DayColorSettingsMenu.h | 14 +++ .../components/insa/include/ui/MainMenu.h | 36 +++---- .../insa/include/ui/NightColorSettingsMenu.h | 15 +++ .../insa/src/common/ColorSettingsMenu.cpp | 101 ++++++++++++++++++ firmware/components/insa/src/common/Menu.cpp | 6 +- .../components/insa/src/common/Widget.cpp | 8 +- .../insa/src/ui/DayColorSettingsMenu.cpp | 5 + firmware/components/insa/src/ui/LightMenu.cpp | 11 +- .../insa/src/ui/LightSettingsMenu.cpp | 75 ++++++++++--- .../insa/src/ui/NightColorSettingsMenu.cpp | 5 + .../components/led-manager/CMakeLists.txt | 1 + .../led-manager/include/led_manager.h | 1 + .../led-manager/src/led_manager.cpp | 45 +++++--- firmware/main/app_task.cpp | 18 ++-- firmware/main/button_handling.c | 2 +- firmware/main/main.cpp | 13 ++- firmware/partitions.csv | 4 +- firmware/sdkconfig.defaults | 24 ++++- firmware/sdkconfig.release | 4 - firmware/src/ui/Device.cpp | 10 +- firmware/version.txt | 2 +- 26 files changed, 357 insertions(+), 91 deletions(-) create mode 100644 firmware/components/insa/include/common/ColorSettingsMenu.h create mode 100644 firmware/components/insa/include/ui/DayColorSettingsMenu.h create mode 100644 firmware/components/insa/include/ui/NightColorSettingsMenu.h create mode 100644 firmware/components/insa/src/common/ColorSettingsMenu.cpp create mode 100644 firmware/components/insa/src/ui/DayColorSettingsMenu.cpp create mode 100644 firmware/components/insa/src/ui/NightColorSettingsMenu.cpp diff --git a/firmware/components/connectivity-manager/src/wifi_manager.c b/firmware/components/connectivity-manager/src/wifi_manager.c index da8f92f..7295df8 100644 --- a/firmware/components/connectivity-manager/src/wifi_manager.c +++ b/firmware/components/connectivity-manager/src/wifi_manager.c @@ -27,6 +27,7 @@ static int s_retry_num = 0; static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { +#if CONFIG_WIFI_ENABLED if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { led_behavior_t led0_behavior = { @@ -67,10 +68,12 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_ s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } +#endif } void wifi_manager_init() { +#if CONFIG_WIFI_ENABLED s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_netif_init()); @@ -119,4 +122,5 @@ void wifi_manager_init() { ESP_LOGE(TAG, "Unexpected event"); } +#endif } diff --git a/firmware/components/insa/CMakeLists.txt b/firmware/components/insa/CMakeLists.txt index 3353338..76ad02c 100644 --- a/firmware/components/insa/CMakeLists.txt +++ b/firmware/components/insa/CMakeLists.txt @@ -1,12 +1,15 @@ idf_component_register(SRCS + src/common/ColorSettingsMenu.cpp src/common/InactivityTracker.cpp src/common/Menu.cpp src/common/ScrollBar.cpp src/common/Widget.cpp src/data/MenuItem.cpp + src/ui/DayColorSettingsMenu.cpp src/ui/LightMenu.cpp src/ui/LightSettingsMenu.cpp src/ui/MainMenu.cpp + src/ui/NightColorSettingsMenu.cpp src/ui/ClockScreenSaver.cpp src/ui/ScreenSaver.cpp src/ui/SettingsMenu.cpp diff --git a/firmware/components/insa/include/common/ColorSettingsMenu.h b/firmware/components/insa/include/common/ColorSettingsMenu.h new file mode 100644 index 0000000..dfe5fa0 --- /dev/null +++ b/firmware/components/insa/include/common/ColorSettingsMenu.h @@ -0,0 +1,29 @@ +#pragma once + +#include "common/Menu.h" + +namespace ColorSettingsMenuOptions +{ +constexpr auto RED = "red_"; +constexpr auto GREEN = "green_"; +constexpr auto BLUE = "blue_"; +} // namespace ColorSettingsMenuOptions + +class ColorSettingsMenu : public Menu +{ + public: + /** + * @brief Constructs a ColorSettingsMenu with the specified options + * @param options Pointer to menu configuration options structure + * @details Initializes the menu with color settings options + */ + explicit ColorSettingsMenu(menu_options_t *options, std::string prefix); + + void onButtonPressed(const MenuItem &menuItem, const ButtonType button) override; + + void onExit() override; + + private: + std::string m_suffix; + menu_options_t *m_options; +}; diff --git a/firmware/components/insa/include/common/Menu.h b/firmware/components/insa/include/common/Menu.h index ccf251b..354e6a2 100644 --- a/firmware/components/insa/include/common/Menu.h +++ b/firmware/components/insa/include/common/Menu.h @@ -159,7 +159,7 @@ class Menu : public Widget * @note New items are created as selection items with auto-generated names * in the format "Section X" where X is the item number */ - void setItemSize(size_t size); + void setItemSize(size_t size, int8_t startIndex = 0); /** * @brief Toggles the boolean state of a toggle menu item diff --git a/firmware/components/insa/include/common/Widget.h b/firmware/components/insa/include/common/Widget.h index e1b27c1..f222778 100644 --- a/firmware/components/insa/include/common/Widget.h +++ b/firmware/components/insa/include/common/Widget.h @@ -63,7 +63,7 @@ class Widget * @note This method is typically called by the UI management system during * screen transitions or focus changes. */ - virtual void enter(); + virtual void onEnter(); /** * @brief Called when the widget is temporarily paused or loses focus @@ -76,7 +76,7 @@ class Widget * only if pause behavior is needed. * @note The widget should be prepared to resume from this state when resume() is called. */ - virtual void pause(); + virtual void onPause(); /** * @brief Called when the widget resumes from a paused state @@ -89,7 +89,7 @@ class Widget * only if resume behavior is needed. * @note This method should restore the widget to the state it was in before pause() was called. */ - virtual void resume(); + virtual void onResume(); /** * @brief Called when the widget is being destroyed or exits the system @@ -99,11 +99,11 @@ class Widget * save final state, or release resources that are not automatically freed. * * @note The base implementation is empty, allowing derived classes to override - * only if exit behavior is needed. + * only if onExit behavior is needed. * @note This method is called before the widget's destructor and provides * an opportunity for controlled shutdown of widget-specific resources. */ - virtual void exit(); + virtual void onExit(); /** * @brief Updates the widget's internal state based on elapsed time diff --git a/firmware/components/insa/include/ui/DayColorSettingsMenu.h b/firmware/components/insa/include/ui/DayColorSettingsMenu.h new file mode 100644 index 0000000..0b6b234 --- /dev/null +++ b/firmware/components/insa/include/ui/DayColorSettingsMenu.h @@ -0,0 +1,14 @@ +#pragma once + +#include "common/ColorSettingsMenu.h" + +class DayColorSettingsMenu final : public ColorSettingsMenu +{ + public: + /** + * @brief Constructs a DayColorSettingsMenu with the specified options + * @param options Pointer to menu configuration options structure + * @details Initializes the menu with day color settings options + */ + explicit DayColorSettingsMenu(menu_options_t *options); +}; diff --git a/firmware/components/insa/include/ui/MainMenu.h b/firmware/components/insa/include/ui/MainMenu.h index 14ac6f6..862e86a 100644 --- a/firmware/components/insa/include/ui/MainMenu.h +++ b/firmware/components/insa/include/ui/MainMenu.h @@ -19,84 +19,84 @@ * @details This final class inherits from Menu and represents the main menu interface * of the application. It serves as the primary entry point for user interaction * and provides navigation to all major application features and sub-menus. - * + * * The MainMenu class customizes the base Menu functionality by: * - Defining application-specific menu items during construction * - Implementing custom button handling for main menu navigation * - Managing transitions to sub-menus and application features - * + * * This class is typically the first screen presented to users after the splash * screen and serves as the central hub for all application functionality. - * + * * @note This class is marked as final and cannot be inherited from. - * + * * @see Menu for base menu functionality * @see menu_options_t for configuration structure */ class MainMenu final : public Menu { -public: + public: /** * @brief Constructs the main menu with the specified configuration * @param options Pointer to menu options configuration structure - * + * * @pre options must not be nullptr and must remain valid for the menu's lifetime * @pre options->u8g2 must be initialized and ready for graphics operations * @pre All callback functions in options must be properly configured * @post MainMenu is initialized with application-specific menu items and ready for use - * + * * @details The constructor initializes the main menu by setting up all the * primary application menu items such as: * - Settings access * - Feature-specific menus * - System controls - * - Application exit options - * + * - Application onExit options + * * @note The menu does not take ownership of the options structure and assumes * it remains valid throughout the menu's lifetime. */ explicit MainMenu(menu_options_t *options); -private: + private: /** * @brief Handles button press events specific to main menu items * @param menuItem * @param button Type of button that was pressed - * + * * @details Overrides the base Menu class method to provide main menu-specific * button handling logic. This method processes user interactions with * main menu items and initiates appropriate actions such as: * - Navigation to sub-menus (Settings, Light Control, etc.) * - Direct feature activation * - System operations - * + * * The method uses the menu item ID to determine which action to take and * utilizes the callback functions provided in m_options to perform screen * transitions or other application-level operations. - * + * * @note This method is called by the base Menu class when a button press * occurs on a menu item, after the base class has handled standard * navigation operations. - * + * * @see Menu::onButtonPressed for the base implementation * @see ButtonType for available button types */ - void onButtonPressed(const MenuItem& menuItem, ButtonType button) override; + void onButtonPressed(const MenuItem &menuItem, ButtonType button) override; /** * @brief Pointer to menu options configuration structure * @details Stores a reference to the menu configuration passed during construction. * This pointer provides access to the display context and callback functions * needed for menu operations, screen transitions, and user interaction handling. - * + * * The configuration includes: * - Display context for rendering operations * - Screen management callbacks for navigation * - Input handling callbacks for button events - * + * * @note This pointer is not owned by the MainMenu and must remain valid * throughout the menu's lifetime. It is managed by the application framework. - * + * * @warning Must not be modified after construction as it may be shared * with other components. */ diff --git a/firmware/components/insa/include/ui/NightColorSettingsMenu.h b/firmware/components/insa/include/ui/NightColorSettingsMenu.h new file mode 100644 index 0000000..e825768 --- /dev/null +++ b/firmware/components/insa/include/ui/NightColorSettingsMenu.h @@ -0,0 +1,15 @@ + +#pragma once + +#include "common/ColorSettingsMenu.h" + +class NightColorSettingsMenu final : public ColorSettingsMenu +{ + public: + /** + * @brief Constructs a NightColorSettingsMenu with the specified options + * @param options Pointer to menu configuration options structure + * @details Initializes the menu with night color settings options + */ + explicit NightColorSettingsMenu(menu_options_t *options); +}; diff --git a/firmware/components/insa/src/common/ColorSettingsMenu.cpp b/firmware/components/insa/src/common/ColorSettingsMenu.cpp new file mode 100644 index 0000000..3fec3fc --- /dev/null +++ b/firmware/components/insa/src/common/ColorSettingsMenu.cpp @@ -0,0 +1,101 @@ +#include "common/ColorSettingsMenu.h" + +#include "led_manager.h" + +namespace ColorSettingsMenuItem +{ +constexpr auto RED = 0; +constexpr auto GREEN = 1; +constexpr auto BLUE = 2; +} // namespace ColorSettingsMenuItem + +ColorSettingsMenu::ColorSettingsMenu(menu_options_t *options, std::string suffix) + : Menu(options), m_suffix(std::move(suffix)), m_options(options) +{ + + std::vector values; + for (size_t i = 0; i <= 254; i++) + { + values.emplace_back(std::to_string(i)); + } + + int red_value = 0; + if (m_options && m_options->persistenceManager) + { + std::string key = ColorSettingsMenuOptions::RED + m_suffix; + red_value = m_options->persistenceManager->GetValue(key, red_value); + } + addSelection(ColorSettingsMenuItem::RED, "Rot", values, red_value); + + int green_value = 0; + if (m_options && m_options->persistenceManager) + { + std::string key = ColorSettingsMenuOptions::GREEN + m_suffix; + green_value = m_options->persistenceManager->GetValue(key, green_value); + } + addSelection(ColorSettingsMenuItem::GREEN, "Gruen", values, green_value); + + int blue_value = 0; + if (m_options && m_options->persistenceManager) + { + std::string key = ColorSettingsMenuOptions::BLUE + m_suffix; + blue_value = m_options->persistenceManager->GetValue(key, blue_value); + } + addSelection(ColorSettingsMenuItem::BLUE, "Blau", values, blue_value); +} + +void ColorSettingsMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType button) +{ + switch (menuItem.getId()) + { + case ColorSettingsMenuItem::RED: + if (button == ButtonType::LEFT || button == ButtonType::RIGHT) + { + const auto item = switchValue(menuItem, button); + const auto value = getItem(item.getId()).getIndex(); + if (m_options && m_options->persistenceManager) + { + std::string key = ColorSettingsMenuOptions::RED + m_suffix; + m_options->persistenceManager->SetValue(key, value); + } + } + break; + + case ColorSettingsMenuItem::GREEN: + if (button == ButtonType::LEFT || button == ButtonType::RIGHT) + { + const auto item = switchValue(menuItem, button); + const auto value = getItem(item.getId()).getIndex(); + if (m_options && m_options->persistenceManager) + { + std::string key = ColorSettingsMenuOptions::GREEN + m_suffix; + m_options->persistenceManager->SetValue(key, value); + } + } + break; + + case ColorSettingsMenuItem::BLUE: + if (button == ButtonType::LEFT || button == ButtonType::RIGHT) + { + const auto item = switchValue(menuItem, button); + const auto value = getItem(item.getId()).getIndex(); + if (m_options && m_options->persistenceManager) + { + std::string key = ColorSettingsMenuOptions::BLUE + m_suffix; + m_options->persistenceManager->SetValue(key, value); + } + } + break; + } +} + +void ColorSettingsMenu::onExit() +{ + if (m_options && m_options->persistenceManager) + { + m_options->persistenceManager->Save(); + + led_event_data_t payload = {.value = 42}; + send_event(EVENT_LED_REFRESH, &payload); + } +} diff --git a/firmware/components/insa/src/common/Menu.cpp b/firmware/components/insa/src/common/Menu.cpp index f6d2bfb..67703c5 100644 --- a/firmware/components/insa/src/common/Menu.cpp +++ b/firmware/components/insa/src/common/Menu.cpp @@ -51,19 +51,19 @@ size_t Menu::getItemCount() const return m_items.size(); } -void Menu::setItemSize(const size_t size) +void Menu::setItemSize(const size_t size, int8_t startIndex) { if ((m_items.size() - 1) < size) { for (size_t i = m_items.size() - 1; i < size; i++) { - auto caption = std::string("Bereich ") + std::to_string(i + 1); + auto caption = std::string("Bereich ") + std::to_string(i + 1 - startIndex); auto index = 0; if (m_options && m_options->persistenceManager) { constexpr int key_length = 20; char key[key_length] = ""; - snprintf(key, key_length, "section_%zu", i + 1); + snprintf(key, key_length, "section_%zu", i + 1 - startIndex); index = m_options->persistenceManager->GetValue(key, index); } addSelection(i + 1, caption, m_items.at(0).getValues(), index); diff --git a/firmware/components/insa/src/common/Widget.cpp b/firmware/components/insa/src/common/Widget.cpp index 124c1a6..a5da811 100644 --- a/firmware/components/insa/src/common/Widget.cpp +++ b/firmware/components/insa/src/common/Widget.cpp @@ -4,19 +4,19 @@ Widget::Widget(u8g2_t *u8g2) : u8g2(u8g2) { } -void Widget::enter() +void Widget::onEnter() { } -void Widget::pause() +void Widget::onPause() { } -void Widget::resume() +void Widget::onResume() { } -void Widget::exit() +void Widget::onExit() { } diff --git a/firmware/components/insa/src/ui/DayColorSettingsMenu.cpp b/firmware/components/insa/src/ui/DayColorSettingsMenu.cpp new file mode 100644 index 0000000..e5f02e0 --- /dev/null +++ b/firmware/components/insa/src/ui/DayColorSettingsMenu.cpp @@ -0,0 +1,5 @@ +#include "ui/DayColorSettingsMenu.h" + +DayColorSettingsMenu::DayColorSettingsMenu(menu_options_t *options) : ColorSettingsMenu(options, "day") +{ +} diff --git a/firmware/components/insa/src/ui/LightMenu.cpp b/firmware/components/insa/src/ui/LightMenu.cpp index 5cef49e..3ec9c5b 100644 --- a/firmware/components/insa/src/ui/LightMenu.cpp +++ b/firmware/components/insa/src/ui/LightMenu.cpp @@ -58,20 +58,19 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto { toggle(menuItem); const auto value = getItem(menuItem.getId()).getValue() == "1"; + led_event_data_t payload = {.value = 42}; if (value) { - led_event_data_t payload = {.value = 42}; send_event(EVENT_LED_ON, &payload); } else { - led_event_data_t payload = {.value = 0}; send_event(EVENT_LED_OFF, &payload); } + if (m_options && m_options->persistenceManager) { m_options->persistenceManager->SetValue(LightMenuOptions::LIGHT_ACTIVE, value); - m_options->persistenceManager->Save(); } } break; @@ -83,14 +82,14 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto if (button == ButtonType::LEFT || button == ButtonType::RIGHT) { const auto value = getItem(item.getId()).getIndex(); - led_event_data_t payload = {.value = value}; - send_event(EVENT_LED_DAY + value, &payload); - if (m_options && m_options->persistenceManager) { m_options->persistenceManager->SetValue(LightMenuOptions::LIGHT_MODE, value); m_options->persistenceManager->Save(); } + + led_event_data_t payload = {.value = value}; + send_event(EVENT_LED_DAY + value, &payload); } break; } diff --git a/firmware/components/insa/src/ui/LightSettingsMenu.cpp b/firmware/components/insa/src/ui/LightSettingsMenu.cpp index 029d299..b8440e1 100644 --- a/firmware/components/insa/src/ui/LightSettingsMenu.cpp +++ b/firmware/components/insa/src/ui/LightSettingsMenu.cpp @@ -1,13 +1,19 @@ #include "ui/LightSettingsMenu.h" +#include "common/Common.h" +#include "ui/DayColorSettingsMenu.h" +#include "ui/NightColorSettingsMenu.h" + /** * @namespace LightSettingsMenuItem * @brief Constants for light settings menu item identifiers */ namespace LightSettingsMenuItem { -constexpr uint8_t SECTION_COUNTER = 0; ///< ID for the section counter menu item -} +constexpr uint8_t RGB_SETTING_DAY = 0; +constexpr uint8_t RGB_SETTING_NIGHT = 1; +constexpr uint8_t SECTION_COUNTER = 2; +} // namespace LightSettingsMenuItem std::string LightSettingsMenu::CreateKey(const int index) { @@ -19,6 +25,10 @@ std::string LightSettingsMenu::CreateKey(const int index) LightSettingsMenu::LightSettingsMenu(menu_options_t *options) : Menu(options), m_options(options) { + addText(LightSettingsMenuItem::RGB_SETTING_DAY, "Tag (Farbe)"); + addText(LightSettingsMenuItem::RGB_SETTING_NIGHT, "Nacht (Farbe)"); + + /* // Create values vector for section counts (1-99) std::vector values; for (size_t i = 1; i <= 99; i++) @@ -34,24 +44,59 @@ LightSettingsMenu::LightSettingsMenu(menu_options_t *options) : Menu(options), m } addSelection(LightSettingsMenuItem::SECTION_COUNTER, "Sektionen", values, value); - setItemSize(std::stoull(getItem(0).getValue())); + setItemSize(std::stoull(getItem(LightSettingsMenuItem::SECTION_COUNTER).getValue()), + LightSettingsMenuItem::SECTION_COUNTER); + */ } void LightSettingsMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType button) { - // Handle value switching for the current menu item - switchValue(menuItem, button); + std::shared_ptr widget; - // Update the section list size based on the section counter value - if (menuItem.getId() == 0) + switch (button) { - setItemSize(std::stoull(getItem(0).getValue())); - } + case ButtonType::SELECT: + switch (menuItem.getId()) + { + case LightSettingsMenuItem::RGB_SETTING_DAY: + widget = std::make_shared(m_options); + break; - // Persist the changed section values if persistence is available - if (m_options && m_options->persistenceManager) - { - const auto value = getItem(menuItem.getId()).getIndex(); - m_options->persistenceManager->SetValue(CreateKey(menuItem.getId()), value); + case LightSettingsMenuItem::RGB_SETTING_NIGHT: + widget = std::make_shared(m_options); + break; + + default: + break; + } + + if (m_options && m_options->pushScreen) + { + m_options->pushScreen(widget); + } + break; + + case ButtonType::RIGHT: + case ButtonType::LEFT: + // Handle value switching for the current menu item + switchValue(menuItem, button); + + // Update the section list size based on the section counter value + if (menuItem.getId() == LightSettingsMenuItem::SECTION_COUNTER) + { + setItemSize(std::stoull(getItem(LightSettingsMenuItem::SECTION_COUNTER).getValue()), + LightSettingsMenuItem::SECTION_COUNTER); + } + + // Persist the changed section values if persistence is available + if (m_options && m_options->persistenceManager) + { + const auto value = getItem(menuItem.getId()).getIndex(); + m_options->persistenceManager->SetValue(CreateKey(menuItem.getId()), value); + } + break; + + default: + break; } -} \ No newline at end of file +} diff --git a/firmware/components/insa/src/ui/NightColorSettingsMenu.cpp b/firmware/components/insa/src/ui/NightColorSettingsMenu.cpp new file mode 100644 index 0000000..1a42968 --- /dev/null +++ b/firmware/components/insa/src/ui/NightColorSettingsMenu.cpp @@ -0,0 +1,5 @@ +#include "ui/NightColorSettingsMenu.h" + +NightColorSettingsMenu::NightColorSettingsMenu(menu_options_t *options) : ColorSettingsMenu(options, "night") +{ +} \ No newline at end of file diff --git a/firmware/components/led-manager/CMakeLists.txt b/firmware/components/led-manager/CMakeLists.txt index ecde6b0..6a6cdd5 100644 --- a/firmware/components/led-manager/CMakeLists.txt +++ b/firmware/components/led-manager/CMakeLists.txt @@ -3,6 +3,7 @@ idf_component_register(SRCS src/led_status.c INCLUDE_DIRS "include" PRIV_REQUIRES + insa u8g2 esp_event esp_timer diff --git a/firmware/components/led-manager/include/led_manager.h b/firmware/components/led-manager/include/led_manager.h index c5b9a8c..4fd25a2 100644 --- a/firmware/components/led-manager/include/led_manager.h +++ b/firmware/components/led-manager/include/led_manager.h @@ -8,6 +8,7 @@ enum EVENT_LED_OFF, EVENT_LED_DAY, EVENT_LED_NIGHT, + EVENT_LED_REFRESH }; typedef struct diff --git a/firmware/components/led-manager/src/led_manager.cpp b/firmware/components/led-manager/src/led_manager.cpp index b806abb..8364cdb 100644 --- a/firmware/components/led-manager/src/led_manager.cpp +++ b/firmware/components/led-manager/src/led_manager.cpp @@ -1,26 +1,30 @@ #include "led_manager.h" +#include "common/ColorSettingsMenu.h" #include "esp_event.h" #include "esp_log.h" +#include "hal_esp32/PersistenceManager.h" +#include "led_status.h" #include "led_strip.h" #include "sdkconfig.h" led_strip_handle_t led_strip; -static const uint32_t value = 5; +static const int value = 125; +static const uint32_t max_leds = 500; ESP_EVENT_DECLARE_BASE(LED_EVENTS_BASE); ESP_EVENT_DEFINE_BASE(LED_EVENTS_BASE); esp_event_loop_handle_t loop_handle; -const char *TAG = "LED"; +const char *TAG = "led_manager"; uint64_t wled_init(void) { led_strip_config_t strip_config = { .strip_gpio_num = CONFIG_WLED_DIN_PIN, - .max_leds = 500, + .max_leds = max_leds, .led_model = LED_MODEL_WS2812, .color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB, .flags = @@ -41,26 +45,35 @@ uint64_t wled_init(void) ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip)); - for (uint32_t i = 0; i < 3; i++) - { - led_strip_set_pixel(led_strip, i, 10, 10, 0); - } - led_strip_refresh(led_strip); - return ESP_OK; } void event_handler(void *arg, esp_event_base_t base, int32_t id, void *event_data) { - if (id == EVENT_LED_ON || id == EVENT_LED_OFF) + uint8_t red = 0; + uint8_t green = 0; + uint8_t blue = 0; + + if (id != EVENT_LED_OFF) { - auto brightness = (id == EVENT_LED_ON) ? value : 0; - for (uint32_t i = 0; i < 500; i++) - { - led_strip_set_pixel(led_strip, i, brightness, brightness, brightness); - } - led_strip_refresh(led_strip); + auto persistenceManager = PersistenceManager(); + persistenceManager.Load(); + + auto mode = persistenceManager.GetValue("light_mode", 0); + auto light_mode = (mode == 0) ? "day" : "night"; + red = persistenceManager.GetValue(std::string(ColorSettingsMenuOptions::RED) + light_mode, value); + green = persistenceManager.GetValue(std::string(ColorSettingsMenuOptions::GREEN) + light_mode, value); + blue = persistenceManager.GetValue(std::string(ColorSettingsMenuOptions::BLUE) + light_mode, value); } + + for (uint32_t i = 0; i < max_leds; i++) + { + led_strip_set_pixel(led_strip, i, red, green, blue); + } + led_strip_refresh(led_strip); + + led_behavior_t led2_behavior = {.mode = LED_MODE_SOLID, .color = {.r = red, .g = green, .b = blue}}; + led_status_set_behavior(2, led2_behavior); } uint64_t register_handler(void) diff --git a/firmware/main/app_task.cpp b/firmware/main/app_task.cpp index 44e9a13..ff02fad 100644 --- a/firmware/main/app_task.cpp +++ b/firmware/main/app_task.cpp @@ -30,6 +30,7 @@ uint8_t received_signal; std::shared_ptr m_widget; std::vector> m_history; std::unique_ptr m_inactivityTracker; +std::shared_ptr m_persistenceManager; extern QueueHandle_t buttonQueue; @@ -58,7 +59,7 @@ void setScreen(const std::shared_ptr &screen) m_widget = screen; m_history.clear(); m_history.emplace_back(m_widget); - m_widget->enter(); + m_widget->onEnter(); } } @@ -68,10 +69,10 @@ void pushScreen(const std::shared_ptr &screen) { if (m_widget) { - m_widget->pause(); + m_widget->onPause(); } m_widget = screen; - m_widget->enter(); + m_widget->onEnter(); m_history.emplace_back(m_widget); } } @@ -83,22 +84,27 @@ void popScreen() m_history.pop_back(); if (m_widget) { - m_widget->exit(); + if (m_persistenceManager != nullptr) + { + m_persistenceManager->Save(); + } + m_widget->onExit(); } m_widget = m_history.back(); - m_widget->resume(); + m_widget->onResume(); } } static void init_ui(void) { + m_persistenceManager = std::make_shared(); options = { .u8g2 = &u8g2, .setScreen = [](const std::shared_ptr &screen) { setScreen(screen); }, .pushScreen = [](const std::shared_ptr &screen) { pushScreen(screen); }, .popScreen = []() { popScreen(); }, .onButtonClicked = nullptr, - .persistenceManager = std::make_shared(), + .persistenceManager = m_persistenceManager, }; m_widget = std::make_shared(&options); m_inactivityTracker = std::make_unique(60000, []() { diff --git a/firmware/main/button_handling.c b/firmware/main/button_handling.c index 997a654..01c0a8e 100644 --- a/firmware/main/button_handling.c +++ b/firmware/main/button_handling.c @@ -36,7 +36,7 @@ static void button_event_cb(void *arg, void *usr_data) button_user_data_t *data = (button_user_data_t *)usr_data; uint8_t gpio_num = data->gpio; - ESP_DIAG_EVENT(TAG, "Button pressed on GPIO %d", gpio_num); + ESP_LOGI(TAG, "Button pressed on GPIO %d", gpio_num); if (xQueueSend(buttonQueue, &gpio_num, 0) != pdTRUE) { diff --git a/firmware/main/main.cpp b/firmware/main/main.cpp index 488c2a3..f0aca25 100644 --- a/firmware/main/main.cpp +++ b/firmware/main/main.cpp @@ -5,6 +5,7 @@ #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "hal_esp32/PersistenceManager.h" #include "led_manager.h" #include "led_status.h" #include "nvs_flash.h" @@ -31,12 +32,18 @@ extern "C" register_handler(); - xTaskCreatePinnedToCore(app_task, "main_loop", 4096, NULL, tskIDLE_PRIORITY + 1, NULL, portNUM_PROCESSORS - 1); + xTaskCreatePinnedToCore(app_task, "app_task", 4096, NULL, tskIDLE_PRIORITY + 1, NULL, portNUM_PROCESSORS - 1); // xTaskCreatePinnedToCore(ble_manager_task, "ble_manager", 4096, NULL, tskIDLE_PRIORITY + 1, NULL, // portNUM_PROCESSORS - 1); - led_event_data_t payload = {.value = 42}; - send_event(EVENT_LED_ON, &payload); + auto persistence = PersistenceManager(); + persistence.Load(); + + if (persistence.GetValue("light_active", false)) + { + led_event_data_t payload = {.value = 42}; + send_event(EVENT_LED_ON, &payload); + } } #ifdef __cplusplus } diff --git a/firmware/partitions.csv b/firmware/partitions.csv index cb1f0dd..7729c75 100644 --- a/firmware/partitions.csv +++ b/firmware/partitions.csv @@ -1,6 +1,6 @@ # Name , Type , SubType , Offset , Size , Flags nvs , data , nvs , 0x9000 , 24k , phy_init , data , phy , , 4k , -factory , app , factory , 0x10000 , 3584K , -coredump , data , coredump , , 64k , +factory , app , factory , 0x10000 , 3072K , +coredump , data , coredump , , 576k , fctry , data , nvs , , 24k , diff --git a/firmware/sdkconfig.defaults b/firmware/sdkconfig.defaults index 5b88704..f08c42a 100755 --- a/firmware/sdkconfig.defaults +++ b/firmware/sdkconfig.defaults @@ -9,7 +9,29 @@ CONFIG_LOG_MAXIMUM_LEVEL=3 # Flash Size CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y -CONFIG_ESPTOOLPY_FLASHSIZE="4MB" # Partitions CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_OFFSET=0x8000 +CONFIG_PARTITION_TABLE_MD5=y + +CONFIG_ESP_INSIGHTS_ENABLED=y +CONFIG_ESP_INSIGHTS_META_VERSION_10=n + +CONFIG_DIAG_ENABLE_METRICS=y +CONFIG_DIAG_ENABLE_HEAP_METRICS=y +CONFIG_DIAG_ENABLE_WIFI_METRICS=y +CONFIG_DIAG_ENABLE_VARIABLES=y +CONFIG_DIAG_ENABLE_NETWORK_VARIABLES=y + +# Core dump +CONFIG_ESP32_ENABLE_COREDUMP=y +CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH=y +CONFIG_ESP32_COREDUMP_DATA_FORMAT_ELF=y +CONFIG_ESP32_COREDUMP_CHECKSUM_CRC32=y +CONFIG_ESP32_CORE_DUMP_MAX_TASKS_NUM=64 +CONFIG_ESP32_CORE_DUMP_STACK_SIZE=1024 + +CONFIG_ESP_RMAKER_DEF_TIMEZONE="Europe/Berlin" diff --git a/firmware/sdkconfig.release b/firmware/sdkconfig.release index d02ef38..22f3cf1 100644 --- a/firmware/sdkconfig.release +++ b/firmware/sdkconfig.release @@ -1,7 +1,3 @@ -# Core dump -CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH=y -CONFIG_ESP_COREDUMP_CAPTURE_DRAM=y - # Build type CONFIG_APP_REPRODUCIBLE_BUILD=y diff --git a/firmware/src/ui/Device.cpp b/firmware/src/ui/Device.cpp index 53faf62..f479c4a 100644 --- a/firmware/src/ui/Device.cpp +++ b/firmware/src/ui/Device.cpp @@ -92,7 +92,7 @@ void Device::SetScreen(const std::shared_ptr &screen) m_widget = screen; m_history.clear(); m_history.emplace_back(m_widget); - m_widget->enter(); + m_widget->onEnter(); } } @@ -102,11 +102,11 @@ void Device::PushScreen(const std::shared_ptr &screen) { if (m_widget) { - m_widget->pause(); + m_widget->onPause(); } m_widget = screen; m_history.emplace_back(m_widget); - m_widget->enter(); + m_widget->onEnter(); } } @@ -116,11 +116,11 @@ void Device::PopScreen() { if (m_widget) { - m_widget->exit(); + m_widget->onExit(); } m_history.pop_back(); m_widget = m_history.back(); - m_widget->resume(); + m_widget->onResume(); } } diff --git a/firmware/version.txt b/firmware/version.txt index 8a9ecc2..6c6aa7c 100644 --- a/firmware/version.txt +++ b/firmware/version.txt @@ -1 +1 @@ -0.0.1 \ No newline at end of file +0.1.0 \ No newline at end of file