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/projdefs.h"
#include "tasks.h"
#include <stdio.h>
#include <stdbool.h>
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));
}

View file

@ -1,29 +1,62 @@
#include "freertos/idf_additions.h"
#include "freertos/projdefs.h"
#include "portmacro.h"
#include <driver/gpio.h>
#include <esp_rom_gpio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>
#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;
}
}
}

View file

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