reorganize code

create light service, with splitted beacon and outdoor functions

Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
2025-06-08 00:16:20 +02:00
parent b455d0f94a
commit 686f82930f
11 changed files with 154 additions and 65 deletions

View File

@@ -0,0 +1,8 @@
idf_component_register(SRCS
"beacon.c"
"light.c"
"outdoor.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
esp_driver_gptimer
)

View File

@@ -4,27 +4,21 @@
#include "esp_log.h" #include "esp_log.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "inttypes.h"
#include "led_strip.h" #include "led_strip.h"
#include "light.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "semaphore.h" #include "semaphore.h"
static const char *TAG = "beacon"; 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; static SemaphoreHandle_t timer_semaphore;
gptimer_handle_t gptimer = NULL; gptimer_handle_t gptimer = NULL;
const uint32_t value = 10; static const uint32_t value = 10;
const uint32_t mod = 3; 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; BaseType_t high_task_wakeup = pdFALSE;
xSemaphoreGiveFromISR(timer_semaphore, &high_task_wakeup); 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; return true;
} }
void timer_event_task(void *arg) static void beacon_timer_event_task(void *arg)
{ {
while (true) while (true)
{ {
if (xSemaphoreTake(timer_semaphore, portMAX_DELAY)) if (xSemaphoreTake(timer_semaphore, portMAX_DELAY))
{ {
LedMatrix_t led_matrix = get_led_matrix();
static bool level = false; static bool level = false;
level = !level; level = !level;
if (led_matrix.led_strip) 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) esp_err_t beacon_start(void)
{ {
if (gptimer == NULL) if (gptimer == NULL)
@@ -143,7 +108,7 @@ esp_err_t beacon_init(void)
goto exit; 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); ret = gptimer_register_event_callbacks(gptimer, &callbacks, NULL);
if (ret != ESP_OK) if (ret != ESP_OK)
{ {
@@ -167,7 +132,7 @@ esp_err_t beacon_init(void)
goto cleanupEnabledTimer; 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) if (task_created != pdPASS)
{ {
ESP_LOGE(TAG, "Failed to create timer event task"); ESP_LOGE(TAG, "Failed to create timer event task");

View File

@@ -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'

View File

@@ -37,15 +37,3 @@ esp_err_t beacon_start(void);
* - Error codes in case of failure, indicating the specific issue. * - Error codes in case of failure, indicating the specific issue.
*/ */
esp_err_t beacon_stop(void); 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);

View File

@@ -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);

View File

@@ -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);

44
components/light/light.c Normal file
View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -1,5 +1,4 @@
idf_component_register(SRCS idf_component_register(SRCS
"beacon.c"
"main.c" "main.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
) )

View File

@@ -14,4 +14,3 @@ dependencies:
# # `public` flag doesn't have an effect dependencies of the `main` component. # # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default. # # All dependencies of `main` are public by default.
# public: true # public: true
espressif/led_strip: '~3.0.0'

View File

@@ -1,26 +1,43 @@
#include "beacon.h" #include "light.h"
#include "persistence.h" #include "persistence.h"
#include "remote_control.h" #include "remote_control.h"
void app_main(void) void app_main(void)
{ {
/// init persistence
persistence_init("lighthouse"); persistence_init("lighthouse");
if (beacon_init() != ESP_OK) /// init WLED
{
printf("Failed to initialize beacon");
return;
}
if (wled_init() != ESP_OK) if (wled_init() != ESP_OK)
{ {
printf("Failed to initialize WLED"); printf("Failed to initialize WLED");
return; return;
} }
/// start beacon service
if (beacon_init() != ESP_OK)
{
printf("Failed to initialize beacon");
return;
}
if (beacon_start() != ESP_OK) if (beacon_start() != ESP_OK)
{ {
printf("Failed to start beacon"); printf("Failed to start beacon");
return; 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(); remote_control_init();
} }