From 686f82930fb973f26fdbcfb0ed0ef2a72bd1b5bd Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Sun, 8 Jun 2025 00:16:20 +0200 Subject: [PATCH] reorganize code create light service, with splitted beacon and outdoor functions Signed-off-by: Peter Siegmund --- components/light/CMakeLists.txt | 8 +++ {main => components/light}/beacon.c | 55 ++++----------------- components/light/idf_component.yml | 17 +++++++ {main => components/light/include}/beacon.h | 12 ----- components/light/include/light.h | 27 ++++++++++ components/light/include/outdoor.h | 9 ++++ components/light/light.c | 44 +++++++++++++++++ components/light/outdoor.c | 16 ++++++ main/CMakeLists.txt | 1 - main/idf_component.yml | 1 - main/main.c | 29 ++++++++--- 11 files changed, 154 insertions(+), 65 deletions(-) create mode 100644 components/light/CMakeLists.txt rename {main => components/light}/beacon.c (67%) create mode 100644 components/light/idf_component.yml rename {main => components/light/include}/beacon.h (75%) create mode 100644 components/light/include/light.h create mode 100644 components/light/include/outdoor.h create mode 100644 components/light/light.c create mode 100644 components/light/outdoor.c diff --git a/components/light/CMakeLists.txt b/components/light/CMakeLists.txt new file mode 100644 index 0000000..b3df3c5 --- /dev/null +++ b/components/light/CMakeLists.txt @@ -0,0 +1,8 @@ +idf_component_register(SRCS + "beacon.c" + "light.c" + "outdoor.c" + INCLUDE_DIRS "include" + PRIV_REQUIRES + esp_driver_gptimer +) diff --git a/main/beacon.c b/components/light/beacon.c similarity index 67% rename from main/beacon.c rename to components/light/beacon.c index dc9ac8e..e3d9eee 100644 --- a/main/beacon.c +++ b/components/light/beacon.c @@ -4,27 +4,21 @@ #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "inttypes.h" #include "led_strip.h" +#include "light.h" #include "sdkconfig.h" #include "semaphore.h" static const char *TAG = "beacon"; -typedef struct -{ - led_strip_handle_t led_strip; - uint32_t size; -} LedMatrix_t; - -static LedMatrix_t led_matrix = {.size = 64}; static SemaphoreHandle_t timer_semaphore; gptimer_handle_t gptimer = NULL; -const uint32_t value = 10; -const uint32_t mod = 3; +static const uint32_t value = 10; +static const uint32_t mod = 2; -bool IRAM_ATTR timer_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *userCtx) +static bool IRAM_ATTR beacon_timer_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, + void *userCtx) { BaseType_t high_task_wakeup = pdFALSE; xSemaphoreGiveFromISR(timer_semaphore, &high_task_wakeup); @@ -35,12 +29,14 @@ bool IRAM_ATTR timer_callback(gptimer_handle_t timer, const gptimer_alarm_event_ return true; } -void timer_event_task(void *arg) +static void beacon_timer_event_task(void *arg) { while (true) { if (xSemaphoreTake(timer_semaphore, portMAX_DELAY)) { + LedMatrix_t led_matrix = get_led_matrix(); + static bool level = false; level = !level; if (led_matrix.led_strip) @@ -59,37 +55,6 @@ void timer_event_task(void *arg) } } -esp_err_t wled_init(void) -{ - led_strip_config_t strip_config = {.strip_gpio_num = CONFIG_WLED_DIN_PIN, - .max_leds = led_matrix.size, - .led_model = LED_MODEL_WS2812, - .color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_RGB, - .flags = { - .invert_out = false, - }}; - - led_strip_rmt_config_t rmt_config = {.clk_src = RMT_CLK_SRC_DEFAULT, - .resolution_hz = 0, - .mem_block_symbols = 0, - .flags = { - .with_dma = true, - }}; - - ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_matrix.led_strip)); - - for (uint32_t i = 0; i < led_matrix.size; i++) - { - if (i % mod != 0) - { - led_strip_set_pixel(led_matrix.led_strip, i, value, value, value); - } - } - led_strip_refresh(led_matrix.led_strip); - - return ESP_OK; -} - esp_err_t beacon_start(void) { if (gptimer == NULL) @@ -143,7 +108,7 @@ esp_err_t beacon_init(void) goto exit; } - gptimer_event_callbacks_t callbacks = {.on_alarm = timer_callback}; + gptimer_event_callbacks_t callbacks = {.on_alarm = beacon_timer_callback}; ret = gptimer_register_event_callbacks(gptimer, &callbacks, NULL); if (ret != ESP_OK) { @@ -167,7 +132,7 @@ esp_err_t beacon_init(void) goto cleanupEnabledTimer; } - BaseType_t task_created = xTaskCreate(timer_event_task, "timer_event_task", 4096, NULL, 10, NULL); + BaseType_t task_created = xTaskCreate(beacon_timer_event_task, "beacon_timer_event_task", 4096, NULL, 10, NULL); if (task_created != pdPASS) { ESP_LOGE(TAG, "Failed to create timer event task"); diff --git a/components/light/idf_component.yml b/components/light/idf_component.yml new file mode 100644 index 0000000..8cd0cd4 --- /dev/null +++ b/components/light/idf_component.yml @@ -0,0 +1,17 @@ +## IDF Component Manager Manifest File +dependencies: + ## Required IDF version + idf: + version: '>=5.4.0' + # # Put list of dependencies here + # # For components maintained by Espressif: + # component: "~1.0.0" + # # For 3rd party components: + # username/component: ">=1.0.0,<2.0.0" + # username2/component2: + # version: "~1.0.0" + # # For transient dependencies `public` flag can be set. + # # `public` flag doesn't have an effect dependencies of the `main` component. + # # All dependencies of `main` are public by default. + # public: true + espressif/led_strip: '~3.0.0' diff --git a/main/beacon.h b/components/light/include/beacon.h similarity index 75% rename from main/beacon.h rename to components/light/include/beacon.h index 5158db6..5ce57c7 100644 --- a/main/beacon.h +++ b/components/light/include/beacon.h @@ -37,15 +37,3 @@ esp_err_t beacon_start(void); * - Error codes in case of failure, indicating the specific issue. */ esp_err_t beacon_stop(void); - -/** - * @brief Initializes the WLED module. - * - * This function configures the WLED module for operation, preparing it for subsequent - * usage such as enabling lighting effects or communication. - * - * @return - * - ESP_OK: Initialization completed successfully. - * - Error codes in case of failure, indicating the specific issue. - */ -esp_err_t wled_init(void); diff --git a/components/light/include/light.h b/components/light/include/light.h new file mode 100644 index 0000000..e9bcd86 --- /dev/null +++ b/components/light/include/light.h @@ -0,0 +1,27 @@ +#pragma once + +#include "beacon.h" +#include "led_strip.h" +#include "outdoor.h" + +#include "esp_err.h" + +typedef struct +{ + led_strip_handle_t led_strip; + uint32_t size; +} LedMatrix_t; + +LedMatrix_t get_led_matrix(void); + +/** + * @brief Initializes the WLED module. + * + * This function configures the WLED module for operation, preparing it for subsequent + * usage such as enabling lighting effects or communication. + * + * @return + * - ESP_OK: Initialization completed successfully. + * - Error codes in case of failure, indicating the specific issue. + */ +esp_err_t wled_init(void); diff --git a/components/light/include/outdoor.h b/components/light/include/outdoor.h new file mode 100644 index 0000000..3c18512 --- /dev/null +++ b/components/light/include/outdoor.h @@ -0,0 +1,9 @@ +#pragma once + +#include "esp_err.h" + +esp_err_t outdoor_init(void); + +esp_err_t outdoor_start(void); + +esp_err_t outdoor_stop(void); diff --git a/components/light/light.c b/components/light/light.c new file mode 100644 index 0000000..fbba01f --- /dev/null +++ b/components/light/light.c @@ -0,0 +1,44 @@ +#include "light.h" + +#include "sdkconfig.h" + +static LedMatrix_t led_matrix = {.size = 64}; + +static const uint32_t value = 10; +static const uint32_t mod = 2; + +LedMatrix_t get_led_matrix(void) +{ + return led_matrix; +} + +esp_err_t wled_init(void) +{ + led_strip_config_t strip_config = {.strip_gpio_num = CONFIG_WLED_DIN_PIN, + .max_leds = led_matrix.size, + .led_model = LED_MODEL_WS2812, + .color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_RGB, + .flags = { + .invert_out = false, + }}; + + led_strip_rmt_config_t rmt_config = {.clk_src = RMT_CLK_SRC_DEFAULT, + .resolution_hz = 0, + .mem_block_symbols = 0, + .flags = { + .with_dma = true, + }}; + + ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_matrix.led_strip)); + + for (uint32_t i = 0; i < led_matrix.size; i++) + { + if (i % mod != 0) + { + led_strip_set_pixel(led_matrix.led_strip, i, value, value, value); + } + } + led_strip_refresh(led_matrix.led_strip); + + return ESP_OK; +} diff --git a/components/light/outdoor.c b/components/light/outdoor.c new file mode 100644 index 0000000..3106ff0 --- /dev/null +++ b/components/light/outdoor.c @@ -0,0 +1,16 @@ +#include "outdoor.h" + +esp_err_t outdoor_init(void) +{ + return ESP_OK; +} + +esp_err_t outdoor_start(void) +{ + return ESP_OK; +} + +esp_err_t outdoor_stop(void) +{ + return ESP_OK; +} \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f9add97..7dfee80 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,4 @@ idf_component_register(SRCS - "beacon.c" "main.c" INCLUDE_DIRS "." ) diff --git a/main/idf_component.yml b/main/idf_component.yml index 8cd0cd4..343c26f 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -14,4 +14,3 @@ dependencies: # # `public` flag doesn't have an effect dependencies of the `main` component. # # All dependencies of `main` are public by default. # public: true - espressif/led_strip: '~3.0.0' diff --git a/main/main.c b/main/main.c index b148661..eba03af 100644 --- a/main/main.c +++ b/main/main.c @@ -1,26 +1,43 @@ -#include "beacon.h" +#include "light.h" #include "persistence.h" #include "remote_control.h" void app_main(void) { + /// init persistence persistence_init("lighthouse"); - if (beacon_init() != ESP_OK) - { - printf("Failed to initialize beacon"); - return; - } + /// init WLED if (wled_init() != ESP_OK) { printf("Failed to initialize WLED"); return; } + + /// start beacon service + if (beacon_init() != ESP_OK) + { + printf("Failed to initialize beacon"); + return; + } if (beacon_start() != ESP_OK) { printf("Failed to start beacon"); return; } + /// start outdoor light service + if (outdoor_init() != ESP_OK) + { + printf("Failed to initialize outdoor"); + return; + } + if (outdoor_start() != ESP_OK) + { + printf("Failed to start outdoor"); + return; + } + + /// activate BLE functions remote_control_init(); }