led is blinking
Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ sdkconfig.old
|
|||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
.espidf.*.json
|
.espidf.*.json
|
||||||
*.svd
|
*.svd
|
||||||
|
*.lock
|
||||||
|
@@ -1,10 +0,0 @@
|
|||||||
dependencies:
|
|
||||||
idf:
|
|
||||||
source:
|
|
||||||
type: idf
|
|
||||||
version: 5.4.1
|
|
||||||
direct_dependencies:
|
|
||||||
- idf
|
|
||||||
manifest_hash: 44ab24e10034773b6c40a4d547bbb6fecca72b12e3f5de2a2b95714982647afc
|
|
||||||
target: esp32s3
|
|
||||||
version: 2.0.0
|
|
160
main/beacon.c
160
main/beacon.c
@@ -6,65 +6,88 @@
|
|||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "inttypes.h"
|
#include "inttypes.h"
|
||||||
|
#include "led_strip.h"
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
static const char *TAG = "beacon";
|
static const char *TAG = "beacon";
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint32_t tick;
|
uint32_t count;
|
||||||
} timer_event_t;
|
uint32_t alarm;
|
||||||
|
} TimerEvent;
|
||||||
|
|
||||||
static QueueHandle_t timer_evt_queue = NULL;
|
typedef struct
|
||||||
gptimer_handle_t gptimer = NULL;
|
|
||||||
|
|
||||||
bool IRAM_ATTR timer_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
|
|
||||||
{
|
{
|
||||||
timer_event_t evt = {.tick = edata->count_value};
|
led_strip_handle_t ledStrip;
|
||||||
|
uint32_t size;
|
||||||
|
} LedMatrix;
|
||||||
|
|
||||||
BaseType_t high_task_wakeup = pdFALSE;
|
static LedMatrix ledMatrix = {.size = 64};
|
||||||
xQueueSendFromISR(timer_evt_queue, &evt, &high_task_wakeup);
|
static QueueHandle_t timerEvtQueue = NULL;
|
||||||
|
gptimer_handle_t gpTimer = NULL;
|
||||||
|
|
||||||
return (high_task_wakeup == pdTRUE);
|
bool IRAM_ATTR timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *userCtx)
|
||||||
|
{
|
||||||
|
TimerEvent evt = {.count = edata->count_value, .alarm = edata->alarm_value};
|
||||||
|
BaseType_t highTaskWakeup = pdFALSE;
|
||||||
|
xQueueSendFromISR(timerEvtQueue, &evt, &highTaskWakeup);
|
||||||
|
|
||||||
|
return (highTaskWakeup == pdTRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer_event_task(void *arg)
|
void timerEventTask(void *arg)
|
||||||
{
|
{
|
||||||
timer_event_t evt;
|
TimerEvent evt;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (xQueueReceive(timer_evt_queue, &evt, portMAX_DELAY))
|
if (xQueueReceive(timerEvtQueue, &evt, portMAX_DELAY))
|
||||||
{
|
{
|
||||||
static bool level = false;
|
static bool level = false;
|
||||||
level = !level;
|
level = !level;
|
||||||
gpio_set_level(CONFIG_WLED_DIN_PIN, level);
|
if (ledMatrix.ledStrip)
|
||||||
ESP_LOGI(TAG, "Timer Event: count = %" PRIu32 ", GPIO now %s", evt.tick, level ? "HIGH" : "LOW");
|
{
|
||||||
|
for (uint32_t i = 0; i < ledMatrix.size; i++)
|
||||||
|
{
|
||||||
|
led_strip_set_pixel(ledMatrix.ledStrip, i, 0, (level) ? 100 : 0, 0);
|
||||||
|
}
|
||||||
|
led_strip_refresh(ledMatrix.ledStrip);
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "Timer Event: count = %" PRIu32 ", alarm = %" PRIu32 ", GPIO now %s", evt.count, evt.alarm,
|
||||||
|
level ? "HIGH" : "LOW");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t wled_init(void)
|
esp_err_t initWled(void)
|
||||||
{
|
{
|
||||||
gpio_config_t io_conf = {.pin_bit_mask = (1ULL << CONFIG_WLED_DIN_PIN),
|
led_strip_config_t stripConfig = {.strip_gpio_num = CONFIG_WLED_DIN_PIN,
|
||||||
.mode = GPIO_MODE_OUTPUT,
|
.max_leds = ledMatrix.size,
|
||||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
.led_model = LED_MODEL_WS2812,
|
||||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_RGB,
|
||||||
.intr_type = GPIO_INTR_DISABLE};
|
.flags = {
|
||||||
esp_err_t ret = gpio_config(&io_conf);
|
.invert_out = false,
|
||||||
if (ret != ESP_OK)
|
}};
|
||||||
{
|
|
||||||
ESP_LOGE(TAG, "Failed to configure GPIO: %s", esp_err_to_name(ret));
|
led_strip_rmt_config_t rmtConfig = {.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||||
}
|
.resolution_hz = 0,
|
||||||
return ret;
|
.mem_block_symbols = 0,
|
||||||
|
.flags = {
|
||||||
|
.with_dma = true,
|
||||||
|
}};
|
||||||
|
ESP_ERROR_CHECK(led_strip_new_rmt_device(&stripConfig, &rmtConfig, &ledMatrix.ledStrip));
|
||||||
|
|
||||||
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t beacon_start(void)
|
esp_err_t startBeacon(void)
|
||||||
{
|
{
|
||||||
if (gptimer == NULL)
|
if (gpTimer == NULL)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "GPTimer not initialized");
|
ESP_LOGE(TAG, "GPTimer not initialized");
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
esp_err_t ret = gptimer_start(gptimer);
|
esp_err_t ret = gptimer_start(gpTimer);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to start gptimer: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to start gptimer: %s", esp_err_to_name(ret));
|
||||||
@@ -76,14 +99,14 @@ esp_err_t beacon_start(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t beacon_stop(void)
|
esp_err_t stopBeacon(void)
|
||||||
{
|
{
|
||||||
if (gptimer == NULL)
|
if (gpTimer == NULL)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "GPTimer not initialized");
|
ESP_LOGE(TAG, "GPTimer not initialized");
|
||||||
return ESP_ERR_INVALID_STATE;
|
return ESP_ERR_INVALID_STATE;
|
||||||
}
|
}
|
||||||
esp_err_t ret = gptimer_stop(gptimer);
|
esp_err_t ret = gptimer_stop(gpTimer);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to stop gptimer: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to stop gptimer: %s", esp_err_to_name(ret));
|
||||||
@@ -95,86 +118,77 @@ esp_err_t beacon_stop(void)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t beacon_init(void)
|
esp_err_t initBeacon(void)
|
||||||
{
|
{
|
||||||
esp_err_t ret = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
|
timerEvtQueue = xQueueCreate(10, sizeof(TimerEvent));
|
||||||
timer_evt_queue = xQueueCreate(10, sizeof(timer_event_t));
|
if (timerEvtQueue == NULL)
|
||||||
if (timer_evt_queue == NULL)
|
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to create timer event queue");
|
ESP_LOGE(TAG, "Failed to create timer event queue");
|
||||||
ret = ESP_ERR_NO_MEM;
|
ret = ESP_ERR_NO_MEM;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
gptimer_config_t timer_config = {
|
gptimer_config_t timerConfig = {
|
||||||
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
|
.clk_src = GPTIMER_CLK_SRC_DEFAULT, .direction = GPTIMER_COUNT_UP, .resolution_hz = 1000000};
|
||||||
.direction = GPTIMER_COUNT_UP,
|
ret = gptimer_new_timer(&timerConfig, &gpTimer);
|
||||||
.resolution_hz = 1000000, // 1 MHz = 1 µs
|
|
||||||
};
|
|
||||||
ret = gptimer_new_timer(&timer_config, &gptimer);
|
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to create new gptimer: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to create new gptimer: %s", esp_err_to_name(ret));
|
||||||
goto cleanup_queue;
|
goto cleanupQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
gptimer_event_callbacks_t callbacks = {
|
gptimer_event_callbacks_t callbacks = {.on_alarm = timerCallback};
|
||||||
.on_alarm = 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)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to register timer callbacks: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to register timer callbacks: %s", esp_err_to_name(ret));
|
||||||
goto cleanup_timer;
|
goto cleanupTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = gptimer_enable(gptimer);
|
ret = gptimer_enable(gpTimer);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to enable gptimer: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to enable gptimer: %s", esp_err_to_name(ret));
|
||||||
goto cleanup_timer; // Timer created but not enabled, can be deleted.
|
goto cleanupTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
gptimer_alarm_config_t alarm_config = {
|
gptimer_alarm_config_t alarmConfig = {
|
||||||
.alarm_count = 2000000, // 2.000.000 µs = 2 secs
|
.alarm_count = 2000000, .reload_count = 0, .flags.auto_reload_on_alarm = true};
|
||||||
.reload_count = 0,
|
ret = gptimer_set_alarm_action(gpTimer, &alarmConfig);
|
||||||
.flags.auto_reload_on_alarm = true,
|
|
||||||
};
|
|
||||||
ret = gptimer_set_alarm_action(gptimer, &alarm_config);
|
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to set gptimer alarm action: %s", esp_err_to_name(ret));
|
ESP_LOGE(TAG, "Failed to set gptimer alarm action: %s", esp_err_to_name(ret));
|
||||||
goto cleanup_enabled_timer; // Timer is enabled, disable before deleting.
|
goto cleanupEnabledTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseType_t task_created = xTaskCreate(timer_event_task, "timer_event_task", 4096, NULL, 10, NULL);
|
BaseType_t taskCreated = xTaskCreate(timerEventTask, "timer_event_task", 4096, NULL, 10, NULL);
|
||||||
if (task_created != pdPASS)
|
if (taskCreated != pdPASS)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "Failed to create timer event task");
|
ESP_LOGE(TAG, "Failed to create timer event task");
|
||||||
ret = ESP_ERR_NO_MEM; // Task creation often fails due to insufficient memory
|
ret = ESP_ERR_NO_MEM;
|
||||||
goto cleanup_enabled_timer;
|
goto cleanupEnabledTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Beacon module initialized.");
|
ESP_LOGI(TAG, "Beacon module initialized.");
|
||||||
goto exit;
|
goto exit;
|
||||||
|
|
||||||
cleanup_enabled_timer:
|
cleanupEnabledTimer:
|
||||||
if (gptimer)
|
if (gpTimer)
|
||||||
{
|
{
|
||||||
gptimer_disable(gptimer); // Attempt to disable
|
gptimer_disable(gpTimer);
|
||||||
}
|
}
|
||||||
cleanup_timer:
|
cleanupTimer:
|
||||||
if (gptimer)
|
if (gpTimer)
|
||||||
{
|
{
|
||||||
gptimer_del_timer(gptimer);
|
gptimer_del_timer(gpTimer);
|
||||||
gptimer = NULL;
|
gpTimer = NULL;
|
||||||
}
|
}
|
||||||
cleanup_queue:
|
cleanupQueue:
|
||||||
if (timer_evt_queue)
|
if (timerEvtQueue)
|
||||||
{
|
{
|
||||||
vQueueDelete(timer_evt_queue);
|
vQueueDelete(timerEvtQueue);
|
||||||
timer_evt_queue = NULL;
|
timerEvtQueue = NULL;
|
||||||
}
|
}
|
||||||
exit:
|
exit:
|
||||||
return ret;
|
return ret;
|
||||||
|
@@ -2,7 +2,50 @@
|
|||||||
|
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
|
|
||||||
esp_err_t beacon_init(void);
|
/**
|
||||||
esp_err_t beacon_start(void);
|
* @brief Initializes the beacon module.
|
||||||
esp_err_t beacon_stop(void);
|
*
|
||||||
esp_err_t wled_init(void);
|
* 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 initBeacon(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 startBeacon(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 stopBeacon(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 initWled(void);
|
||||||
|
@@ -14,3 +14,4 @@ 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'
|
||||||
|
18
main/main.c
18
main/main.c
@@ -2,7 +2,19 @@
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
ESP_ERROR_CHECK(beacon_init());
|
if (initBeacon() != ESP_OK)
|
||||||
ESP_ERROR_CHECK(wled_init());
|
{
|
||||||
ESP_ERROR_CHECK(beacon_start());
|
printf("Failed to initialize beacon");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (initWled() != ESP_OK)
|
||||||
|
{
|
||||||
|
printf("Failed to initialize WLED");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (startBeacon() != ESP_OK)
|
||||||
|
{
|
||||||
|
printf("Failed to start beacon");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user