Code robustness #2
@@ -38,7 +38,8 @@ void ClockScreenSaver::updateTextDimensions()
|
|||||||
|
|
||||||
void ClockScreenSaver::getCurrentTimeString(char *buffer, size_t bufferSize) const
|
void ClockScreenSaver::getCurrentTimeString(char *buffer, size_t bufferSize) const
|
||||||
{
|
{
|
||||||
if (m_options && m_options->persistenceManager->GetValue("light_active", false))
|
if (m_options && m_options->persistenceManager->GetValue("light_active", false) &&
|
||||||
|
m_options->persistenceManager->GetValue("light_mode", 0) == 0)
|
||||||
{
|
{
|
||||||
char *simulated_time = get_time();
|
char *simulated_time = get_time();
|
||||||
if (simulated_time != nullptr)
|
if (simulated_time != nullptr)
|
||||||
|
|||||||
@@ -11,12 +11,14 @@ namespace LightMenuItem
|
|||||||
{
|
{
|
||||||
constexpr auto ACTIVATE = 0; ///< ID for the light activation toggle
|
constexpr auto ACTIVATE = 0; ///< ID for the light activation toggle
|
||||||
constexpr auto MODE = 1; ///< ID for the light mode selection
|
constexpr auto MODE = 1; ///< ID for the light mode selection
|
||||||
|
constexpr auto VARIANT = 2; ///< ID for the light variant selection
|
||||||
} // namespace LightMenuItem
|
} // namespace LightMenuItem
|
||||||
|
|
||||||
namespace LightMenuOptions
|
namespace LightMenuOptions
|
||||||
{
|
{
|
||||||
constexpr auto LIGHT_ACTIVE = "light_active";
|
constexpr auto LIGHT_ACTIVE = "light_active";
|
||||||
constexpr auto LIGHT_MODE = "light_mode";
|
constexpr auto LIGHT_MODE = "light_mode";
|
||||||
|
constexpr auto LIGHT_VARIANT = "light_variant";
|
||||||
} // namespace LightMenuOptions
|
} // namespace LightMenuOptions
|
||||||
|
|
||||||
LightMenu::LightMenu(menu_options_t *options) : Menu(options), m_options(options)
|
LightMenu::LightMenu(menu_options_t *options) : Menu(options), m_options(options)
|
||||||
@@ -40,6 +42,17 @@ LightMenu::LightMenu(menu_options_t *options) : Menu(options), m_options(options
|
|||||||
mode_value = m_options->persistenceManager->GetValue(LightMenuOptions::LIGHT_MODE, mode_value);
|
mode_value = m_options->persistenceManager->GetValue(LightMenuOptions::LIGHT_MODE, mode_value);
|
||||||
}
|
}
|
||||||
addSelection(LightMenuItem::MODE, "Modus", items, mode_value);
|
addSelection(LightMenuItem::MODE, "Modus", items, mode_value);
|
||||||
|
|
||||||
|
std::vector<std::string> variants;
|
||||||
|
variants.emplace_back("1");
|
||||||
|
variants.emplace_back("2");
|
||||||
|
variants.emplace_back("3");
|
||||||
|
int variant_value = 2;
|
||||||
|
if (m_options && m_options->persistenceManager)
|
||||||
|
{
|
||||||
|
variant_value = m_options->persistenceManager->GetValue(LightMenuOptions::LIGHT_VARIANT, variant_value);
|
||||||
|
}
|
||||||
|
addSelection(LightMenuItem::VARIANT, "Variante", variants, variant_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType button)
|
void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType button)
|
||||||
@@ -82,6 +95,23 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case LightMenuItem::VARIANT: {
|
||||||
|
// Change light variant using left/right buttons
|
||||||
|
const auto item = switchValue(menuItem, button);
|
||||||
|
if (button == ButtonType::LEFT || button == ButtonType::RIGHT)
|
||||||
|
{
|
||||||
|
const auto value = getItem(item.getId()).getIndex() + 1;
|
||||||
|
if (m_options && m_options->persistenceManager)
|
||||||
|
{
|
||||||
|
m_options->persistenceManager->SetValue(LightMenuOptions::LIGHT_VARIANT, value);
|
||||||
|
m_options->persistenceManager->Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
start_simulation();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Handle unknown menu items (no action required)
|
// Handle unknown menu items (no action required)
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <esp_heap_caps.h>
|
#include <esp_heap_caps.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -24,6 +25,15 @@ static char *time_to_string(int hhmm)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static TaskHandle_t simulation_task_handle = NULL;
|
static TaskHandle_t simulation_task_handle = NULL;
|
||||||
|
static SemaphoreHandle_t simulation_mutex = NULL;
|
||||||
|
|
||||||
|
static void ensure_mutex_initialized(void)
|
||||||
|
{
|
||||||
|
if (simulation_mutex == NULL)
|
||||||
|
{
|
||||||
|
simulation_mutex = xSemaphoreCreateMutex();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The struct is extended with a 'next' pointer to form a linked list.
|
// The struct is extended with a 'next' pointer to form a linked list.
|
||||||
typedef struct light_item_node_t
|
typedef struct light_item_node_t
|
||||||
@@ -131,14 +141,14 @@ void cleanup_light_items(void)
|
|||||||
|
|
||||||
static void initialize_light_items(void)
|
static void initialize_light_items(void)
|
||||||
{
|
{
|
||||||
if (head != NULL)
|
cleanup_light_items();
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Light schedule already initialized.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
initialize_storage();
|
initialize_storage();
|
||||||
load_file("/spiffs/schema_03.csv");
|
|
||||||
|
static char filename[30];
|
||||||
|
auto persistence = PersistenceManager();
|
||||||
|
int variant = persistence.GetValue("light_variant", 1);
|
||||||
|
snprintf(filename, sizeof(filename), "/spiffs/schema_%02d.csv", variant);
|
||||||
|
load_file(filename);
|
||||||
|
|
||||||
if (head == NULL)
|
if (head == NULL)
|
||||||
{
|
{
|
||||||
@@ -256,6 +266,11 @@ void simulate_cycle(void *args)
|
|||||||
if (cycle_duration_minutes <= 0)
|
if (cycle_duration_minutes <= 0)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Invalid cycle duration: %d minutes. Must be positive.", cycle_duration_minutes);
|
ESP_LOGE(TAG, "Invalid cycle duration: %d minutes. Must be positive.", cycle_duration_minutes);
|
||||||
|
if (simulation_mutex != NULL && xSemaphoreTake(simulation_mutex, portMAX_DELAY) == pdTRUE)
|
||||||
|
{
|
||||||
|
simulation_task_handle = NULL;
|
||||||
|
xSemaphoreGive(simulation_mutex);
|
||||||
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -355,12 +370,29 @@ void start_simulation_task(void)
|
|||||||
|
|
||||||
void stop_simulation_task(void)
|
void stop_simulation_task(void)
|
||||||
{
|
{
|
||||||
|
ensure_mutex_initialized();
|
||||||
|
|
||||||
|
if (xSemaphoreTake(simulation_mutex, portMAX_DELAY) == pdTRUE)
|
||||||
|
{
|
||||||
if (simulation_task_handle != NULL)
|
if (simulation_task_handle != NULL)
|
||||||
{
|
{
|
||||||
vTaskDelete(simulation_task_handle);
|
TaskHandle_t handle_to_delete = simulation_task_handle;
|
||||||
simulation_task_handle = NULL;
|
simulation_task_handle = NULL;
|
||||||
|
xSemaphoreGive(simulation_mutex);
|
||||||
|
|
||||||
|
// Prüfe ob der Task noch existiert bevor er gelöscht wird
|
||||||
|
eTaskState state = eTaskGetState(handle_to_delete);
|
||||||
|
if (state != eDeleted && state != eInvalid)
|
||||||
|
{
|
||||||
|
vTaskDelete(handle_to_delete);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
xSemaphoreGive(simulation_mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void start_simulation(void)
|
void start_simulation(void)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ void app_main(void)
|
|||||||
|
|
||||||
xTaskCreatePinnedToCore(app_task, "app_task", 8192, NULL, tskIDLE_PRIORITY + 5, NULL,
|
xTaskCreatePinnedToCore(app_task, "app_task", 8192, NULL, tskIDLE_PRIORITY + 5, NULL,
|
||||||
CONFIG_FREERTOS_NUMBER_OF_CORES - 1);
|
CONFIG_FREERTOS_NUMBER_OF_CORES - 1);
|
||||||
xTaskCreatePinnedToCore(ble_manager_task, "ble_manager", 4096, NULL, tskIDLE_PRIORITY + 1, NULL,
|
// xTaskCreatePinnedToCore(ble_manager_task, "ble_manager", 4096, NULL, tskIDLE_PRIORITY + 1, NULL,
|
||||||
CONFIG_FREERTOS_NUMBER_OF_CORES - 1);
|
// CONFIG_FREERTOS_NUMBER_OF_CORES - 1);
|
||||||
}
|
}
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0.1.1
|
0.1.2
|
||||||
Reference in New Issue
Block a user