From 6e49b44234b5d7d51063fe2bddd23abdecfc6751 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 6 Jul 2025 14:40:11 +0200 Subject: [PATCH] BlinkCommand instead of just rate --- main/main.c | 13 ++++++++---- main/task_blink.c | 53 ++++++++++++++++++++++++++++++++++++++--------- main/tasks.h | 24 +++++++++++++++------ 3 files changed, 70 insertions(+), 20 deletions(-) diff --git a/main/main.c b/main/main.c index dfb8131..a15617b 100644 --- a/main/main.c +++ b/main/main.c @@ -1,16 +1,21 @@ #include "freertos/idf_additions.h" #include "freertos/projdefs.h" #include "tasks.h" -#include +#include extern QueueHandle_t blink_rate_q; void app_main(void) { TaskHandle_t task_handle_blink; - blink_rate_q = xQueueCreate(10, sizeof(BlinkRate_t)); + blink_rate_q = xQueueCreate(10, sizeof(BlinkCmd_t)); xTaskCreate(task_blink, "task_blink", 1024, NULL, 0, &task_handle_blink); - BlinkRate_t br = {1000}; - xQueueSend(blink_rate_q, &br, pdMS_TO_TICKS(20)); + BlinkCmd_t br = { + .variant = BLINK_STATE, + .state = true, + }; + + vTaskDelay(pdMS_TO_TICKS(2000)); + xQueueSend(blink_rate_q, &br, pdMS_TO_TICKS(200)); } diff --git a/main/task_blink.c b/main/task_blink.c index 261b640..fe549f7 100644 --- a/main/task_blink.c +++ b/main/task_blink.c @@ -1,29 +1,62 @@ #include "freertos/idf_additions.h" +#include "freertos/projdefs.h" #include "portmacro.h" #include #include #include #include +#include #include "tasks.h" QueueHandle_t blink_rate_q; -void task_blink(void *pvParams) { - u32 blink_delay = BLINK_DELAY; - u32 new_delay = {}; - u32 level = 1; +void blink_cmd_print(const BlinkCmd_t *cmd) { + printf("BlinkCmd {\n"); + switch (cmd->variant) { + case BLINK_RATE: + printf(" variant: BLINK_RATE,\n"); + printf(" rate: %lu\n", cmd->rate); + break; + case BLINK_STATE: + printf(" variant: BLINK_STATE,\n"); + printf(" state: %s\n", cmd->state ? "ON" : "OFF"); + break; + default: + printf(" variant: UNKNOWN (%d)\n", cmd->variant); + break; + } + + printf("}\n"); +} + +void task_blink(void *pvParams) { esp_rom_gpio_pad_select_gpio(LED_PIN); gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT); + u32 level = 1; // For blinking + + // Set this to some initial value + BlinkCmd_t command = { + .variant = BLINK_RATE, + .rate = 1000, + }; + while (1) { - if (xQueueReceive(blink_rate_q, &new_delay, 0) == pdPASS) - blink_delay = new_delay; + if (pdPASS == xQueueReceive(blink_rate_q, &command, 0)) + blink_cmd_print(&command); - level = !level; - - gpio_set_level(LED_PIN, level); - vTaskDelay(blink_delay / portTICK_PERIOD_MS); + switch (command.variant) { + case BLINK_RATE: + level = !level; + gpio_set_level(LED_PIN, level); + vTaskDelay(command.rate / portTICK_PERIOD_MS); + break; + case BLINK_STATE: + gpio_set_level(LED_PIN, command.state); + vTaskDelay(pdMS_TO_TICKS(20)); + break; + } } } diff --git a/main/tasks.h b/main/tasks.h index f7a945b..1c39be9 100644 --- a/main/tasks.h +++ b/main/tasks.h @@ -9,9 +9,6 @@ */ #define LED_PIN 2 -#define RATE_PER_S 10 -#define BLINK_DELAY (1000 / 2 / RATE_PER_S) - typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; @@ -25,12 +22,27 @@ typedef int64_t s64; typedef float f32; typedef double f64; -// typedef enum { false = 0, true = 1 } bool; +/* Begin: task_blink.c */ + +#define LED_PIN 2 +#define RATE_PER_S 10 +#define BLINK_DELAY (1000 / 2 / RATE_PER_S) + +typedef enum { + BLINK_RATE, + BLINK_STATE, +} BlinkCmdVariant; typedef struct { - u32 rate; -} BlinkRate_t; + BlinkCmdVariant variant; + union { + u32 rate; //!< Set to blink at intervals of 'rate' + u32 state; //!< 1=ON, 0=OFF + }; +} BlinkCmd_t; void task_blink(void *pvParams); +/* End: task_blink.c */ + #endif