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

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

View File

@@ -1,194 +0,0 @@
#include "beacon.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "inttypes.h"
#include "led_strip.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;
bool IRAM_ATTR 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);
if (high_task_wakeup)
{
portYIELD_FROM_ISR();
}
return true;
}
void timer_event_task(void *arg)
{
while (true)
{
if (xSemaphoreTake(timer_semaphore, portMAX_DELAY))
{
static bool level = false;
level = !level;
if (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, 0, (level) ? value : 0, 0);
}
}
led_strip_refresh(led_matrix.led_strip);
}
ESP_LOGD(TAG, "Timer Event, LED now %s", level ? "ON" : "OFF");
}
}
}
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)
{
ESP_LOGE(TAG, "GPTimer not initialized");
return ESP_ERR_INVALID_STATE;
}
esp_err_t ret = gptimer_start(gptimer);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to start gptimer: %s", esp_err_to_name(ret));
}
else
{
ESP_LOGI(TAG, "GPTimer started.");
}
return ret;
}
esp_err_t beacon_stop(void)
{
if (gptimer == NULL)
{
ESP_LOGE(TAG, "GPTimer not initialized");
return ESP_ERR_INVALID_STATE;
}
esp_err_t ret = gptimer_stop(gptimer);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to stop gptimer: %s", esp_err_to_name(ret));
}
else
{
ESP_LOGI(TAG, "GPTimer stopped.");
}
return ret;
}
esp_err_t beacon_init(void)
{
esp_err_t ret = ESP_OK;
timer_semaphore = xSemaphoreCreateBinary();
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT, .direction = GPTIMER_COUNT_UP, .resolution_hz = 1000000};
ret = gptimer_new_timer(&timer_config, &gptimer);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to create new gptimer: %s", esp_err_to_name(ret));
goto exit;
}
gptimer_event_callbacks_t callbacks = {.on_alarm = timer_callback};
ret = gptimer_register_event_callbacks(gptimer, &callbacks, NULL);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to register timer callbacks: %s", esp_err_to_name(ret));
goto cleanupTimer;
}
ret = gptimer_enable(gptimer);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to enable gptimer: %s", esp_err_to_name(ret));
goto cleanupTimer;
}
gptimer_alarm_config_t alarm_config = {
.alarm_count = 2000000, .reload_count = 0, .flags.auto_reload_on_alarm = true};
ret = gptimer_set_alarm_action(gptimer, &alarm_config);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to set gptimer alarm action: %s", esp_err_to_name(ret));
goto cleanupEnabledTimer;
}
BaseType_t task_created = xTaskCreate(timer_event_task, "timer_event_task", 4096, NULL, 10, NULL);
if (task_created != pdPASS)
{
ESP_LOGE(TAG, "Failed to create timer event task");
ret = ESP_ERR_NO_MEM;
goto cleanupEnabledTimer;
}
ESP_LOGI(TAG, "Beacon module initialized.");
goto exit;
cleanupEnabledTimer:
if (gptimer)
{
gptimer_disable(gptimer);
}
cleanupTimer:
if (gptimer)
{
gptimer_del_timer(gptimer);
gptimer = NULL;
}
exit:
return ret;
}

View File

@@ -1,51 +0,0 @@
#pragma once
#include "esp_err.h"
/**
* @brief Initializes the beacon module.
*
* This function sets up the beacon module, configuring it for subsequent operations
* such as starting or stopping the broadcast functionality.
*
* @return
* - ESP_OK: Initialization completed successfully.
* - Error codes in case of failure, indicating the specific issue.
*/
esp_err_t beacon_init(void);
/**
* @brief Starts the beacon functionality for broadcasting signals.
*
* This function initiates the process required for starting the beacon.
* It ensures the beacon is prepared to transmit signals effectively.
*
* @return
* - ESP_OK: Beacon started successfully.
* - Error codes in case of failure, indicating the specific issue.
*/
esp_err_t beacon_start(void);
/**
* @brief Stops the beacon broadcasting functionality.
*
* This function terminates the ongoing broadcasting process of the beacon
* and releases any resources allocated during the operation.
*
* @return
* - ESP_OK: Broadcasting stopped successfully.
* - 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);

View File

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

View File

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