idfhack/main/task_blink.c

131 lines
3.2 KiB
C

#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 <inttypes.h>
#include <stdio.h>
#include "tasks.h"
QueueHandle_t blink_rate_q;
void blink_cmd_print(const BlinkCmd_t *cmd) {
printf("BlinkCmd { ");
switch (cmd->variant) {
case BLINK_RATE:
printf("variant: BLINK_RATE, ");
printf("rate: %" PRIu32, cmd->rate);
break;
case BLINK_STATE:
printf("variant: BLINK_STATE, ");
printf("state: %s", cmd->state ? "ON" : "OFF");
break;
default:
printf("variant: UNKNOWN (%d)", 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 = 800,
};
if (pvParams != NULL) {
command = *(BlinkCmd_t *)pvParams;
printf("Got initial configuration for leds: ");
blink_cmd_print(&command);
}
while (1) {
if (pdPASS == xQueueReceive(blink_rate_q, &command, 0))
blink_cmd_print(&command);
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;
default:
break;
}
}
}
void task_blink_cycle(void *pvParams) {
BlinkCmd_t cmd = {
.variant = BLINK_RATE,
.rate = 1000,
};
vTaskDelay(pdMS_TO_TICKS(2000));
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
cmd.rate = 50;
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
cmd.rate = 25;
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
cmd.rate = 500;
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
cmd.variant = BLINK_STATE;
cmd.state = 1;
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
cmd.variant = BLINK_RATE;
cmd.rate = 200;
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
cmd.variant = BLINK_STATE;
cmd.state = 0;
if (xQueueSend(blink_rate_q, &cmd, pdMS_TO_TICKS(200)) != pdPASS) {
printf("Error sending blink command...");
}
vTaskDelete(NULL);
}