BlinkCommand instead of just rate

This commit is contained in:
Imbus 2025-07-06 14:40:11 +02:00
parent 615574685d
commit 6e49b44234
3 changed files with 70 additions and 20 deletions

View file

@ -1,16 +1,21 @@
#include "freertos/idf_additions.h" #include "freertos/idf_additions.h"
#include "freertos/projdefs.h" #include "freertos/projdefs.h"
#include "tasks.h" #include "tasks.h"
#include <stdio.h> #include <stdbool.h>
extern QueueHandle_t blink_rate_q; extern QueueHandle_t blink_rate_q;
void app_main(void) { void app_main(void) {
TaskHandle_t task_handle_blink; 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); xTaskCreate(task_blink, "task_blink", 1024, NULL, 0, &task_handle_blink);
BlinkRate_t br = {1000}; BlinkCmd_t br = {
xQueueSend(blink_rate_q, &br, pdMS_TO_TICKS(20)); .variant = BLINK_STATE,
.state = true,
};
vTaskDelay(pdMS_TO_TICKS(2000));
xQueueSend(blink_rate_q, &br, pdMS_TO_TICKS(200));
} }

View file

@ -1,29 +1,62 @@
#include "freertos/idf_additions.h" #include "freertos/idf_additions.h"
#include "freertos/projdefs.h"
#include "portmacro.h" #include "portmacro.h"
#include <driver/gpio.h> #include <driver/gpio.h>
#include <esp_rom_gpio.h> #include <esp_rom_gpio.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <stdio.h>
#include "tasks.h" #include "tasks.h"
QueueHandle_t blink_rate_q; QueueHandle_t blink_rate_q;
void task_blink(void *pvParams) { void blink_cmd_print(const BlinkCmd_t *cmd) {
u32 blink_delay = BLINK_DELAY; printf("BlinkCmd {\n");
u32 new_delay = {};
u32 level = 1;
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); esp_rom_gpio_pad_select_gpio(LED_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT); 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) { while (1) {
if (xQueueReceive(blink_rate_q, &new_delay, 0) == pdPASS) if (pdPASS == xQueueReceive(blink_rate_q, &command, 0))
blink_delay = new_delay; blink_cmd_print(&command);
level = !level; switch (command.variant) {
case BLINK_RATE:
gpio_set_level(LED_PIN, level); level = !level;
vTaskDelay(blink_delay / portTICK_PERIOD_MS); 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;
}
} }
} }

View file

@ -9,9 +9,6 @@
*/ */
#define LED_PIN 2 #define LED_PIN 2
#define RATE_PER_S 10
#define BLINK_DELAY (1000 / 2 / RATE_PER_S)
typedef uint8_t u8; typedef uint8_t u8;
typedef uint16_t u16; typedef uint16_t u16;
typedef uint32_t u32; typedef uint32_t u32;
@ -25,12 +22,27 @@ typedef int64_t s64;
typedef float f32; typedef float f32;
typedef double f64; 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 { typedef struct {
u32 rate; BlinkCmdVariant variant;
} BlinkRate_t; union {
u32 rate; //!< Set to blink at intervals of 'rate'
u32 state; //!< 1=ON, 0=OFF
};
} BlinkCmd_t;
void task_blink(void *pvParams); void task_blink(void *pvParams);
/* End: task_blink.c */
#endif #endif