From 7222fb1829ab7f50b772eb41f909cbfe31d0be47 Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Sun, 25 May 2025 18:27:48 +0200 Subject: [PATCH] LIGHT ON/OFF testing for WLEDs Signed-off-by: Peter Siegmund --- components/led_matrix/include/led_matrix.h | 2 ++ components/led_matrix/led_matrix.c | 25 +++++++++++----------- components/remote_control/CMakeLists.txt | 1 + components/remote_control/led_service.c | 12 +++++++++-- components/remote_control/remote_control.c | 4 ++++ main/main.c | 1 - 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/components/led_matrix/include/led_matrix.h b/components/led_matrix/include/led_matrix.h index a544425..365864a 100644 --- a/components/led_matrix/include/led_matrix.h +++ b/components/led_matrix/include/led_matrix.h @@ -3,3 +3,5 @@ #include void led_matrix_init(void *args); +uint32_t led_matrix_get_size(); +void led_matrix_set_pixel(uint32_t index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/components/led_matrix/led_matrix.c b/components/led_matrix/led_matrix.c index 8b99cd2..a2f8e45 100644 --- a/components/led_matrix/led_matrix.c +++ b/components/led_matrix/led_matrix.c @@ -5,17 +5,9 @@ #include "esp_log.h" #include "sdkconfig.h" -typedef struct -{ - uint8_t red; - uint8_t green; - uint8_t blue; -} led_data_t; - typedef struct { led_strip_handle_t led_strip; - led_data_t *data; uint32_t size; } led_matrix_t; @@ -24,7 +16,6 @@ led_matrix_t led_matrix; static void led_strip_init(uint8_t gpio_pin, uint32_t size) { led_matrix.size = size; - led_matrix.data = (led_data_t *)malloc(sizeof(led_data_t) * size); led_strip_config_t strip_config = { .strip_gpio_num = gpio_pin, @@ -46,6 +37,16 @@ static void led_strip_init(uint8_t gpio_pin, uint32_t size) ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_matrix.led_strip)); } +uint32_t led_matrix_get_size() +{ + return led_matrix.size; +} + +void led_matrix_set_pixel(uint32_t index, uint8_t red, uint8_t green, uint8_t blue) +{ + led_strip_set_pixel(led_matrix.led_strip, index, red, green, blue); +} + void led_matrix_init(void *args) { ESP_LOGI(pcTaskGetName(NULL), "Calling led_matrix_init()"); @@ -54,17 +55,15 @@ void led_matrix_init(void *args) int value = 0; for (int i = 0; i < led_matrix.size; i++) { - led_strip_set_pixel(led_matrix.led_strip, i, value, 0, 0); + led_matrix_set_pixel(i, 0, value, 0); } - led_strip_refresh(led_matrix.led_strip); while (true) { + led_strip_refresh(led_matrix.led_strip); vTaskDelay(pdMS_TO_TICKS(100)); } - free(led_matrix.data); - ESP_LOGI(pcTaskGetName(NULL), "Exiting led_matrix_init()"); vTaskDelete(NULL); } diff --git a/components/remote_control/CMakeLists.txt b/components/remote_control/CMakeLists.txt index 919018a..ee82fd8 100644 --- a/components/remote_control/CMakeLists.txt +++ b/components/remote_control/CMakeLists.txt @@ -2,4 +2,5 @@ idf_component_register(SRCS "led_service.c" "device_service.c" "remote_control.c INCLUDE_DIRS "include" PRIV_REQUIRES bt + led_matrix ) diff --git a/components/remote_control/led_service.c b/components/remote_control/led_service.c index 19d0f46..92546cd 100644 --- a/components/remote_control/led_service.c +++ b/components/remote_control/led_service.c @@ -1,5 +1,7 @@ #include "include/led_service.h" +#include "led_matrix.h" + static const char *TAG = "led_service"; // Write data to ESP32 defined as server @@ -18,13 +20,19 @@ int ls_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ strncmp(received_payload, CMD_LIGHT_ON, payload_len) == 0) { ESP_LOGI(TAG, "LIGHT ON"); - // TODO: Implement action for LIGHT ON + for (int i = 0; i < led_matrix_get_size(); i++) + { + led_matrix_set_pixel(i, 10, 10, 0); + } } else if (payload_len == (sizeof(CMD_LIGHT_OFF) - 1) && strncmp(received_payload, CMD_LIGHT_OFF, payload_len) == 0) { ESP_LOGI(TAG, "LIGHT OFF"); - // TODO: Implement action for LIGHT OFF + for (int i = 0; i < led_matrix_get_size(); i++) + { + led_matrix_set_pixel(i, 0, 0, 0); + } } else if (payload_len == (sizeof(CMD_FAN_ON) - 1) && strncmp(received_payload, CMD_FAN_ON, payload_len) == 0) diff --git a/components/remote_control/remote_control.c b/components/remote_control/remote_control.c index 159e009..8a26e4b 100644 --- a/components/remote_control/remote_control.c +++ b/components/remote_control/remote_control.c @@ -14,6 +14,8 @@ #include "services/gatt/ble_svc_gatt.h" #include "sdkconfig.h" +#include "led_matrix.h" + #include "include/device_service.h" #include "include/led_service.h" @@ -137,4 +139,6 @@ void ble_init(void *args) ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC; nimble_port_freertos_init(host_task); // Run the host task + + xTaskCreatePinnedToCore(led_matrix_init, "led_matrix", configMINIMAL_STACK_SIZE * 2, NULL, 5, NULL, 1); } diff --git a/main/main.c b/main/main.c index 781d936..cda6723 100644 --- a/main/main.c +++ b/main/main.c @@ -9,6 +9,5 @@ void app_main(void) { persistence_init("miniature_town"); - xTaskCreatePinnedToCore(led_matrix_init, "led_matrix", configMINIMAL_STACK_SIZE * 2, NULL, 5, NULL, 1); ble_init(NULL); }