Newer
Older
ESP32-RetroPlay / main / tasks / led_task.c
// led_task.c
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_log.h" // For ESP_LOG macros
#include "led_task.h" // For start_led_control_task declaration
#include "button_led_comm.h" // To access xLedControlQueue
// #include "display_task.h" // To access display_update_text

static const char *TAG = "LED_TASK";

#define BLINK_GPIO 41 
#define MAX_LINE_LENGTH 32 // Maximum length for a line of text on the OLED
#define N_DISPLAY_LINES 8 // Number of lines on the OLED display
/**
 * @brief FreeRTOS task to control the LED based on signals received from a queue.
 *
 * This task waits for boolean signals in the xLedControlQueue. When a signal is
 * received, it sets the LED state accordingly.
 *
 * @param pvParameters Not used in this task.
 */
static void led_control_task(void *pvParameters)
{
    // --- LED Configuration ---
    gpio_reset_pin(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    bool desired_led_state;
    char oled_message_buffer[MAX_LINE_LENGTH]; // Buffer to format OLED message

    ESP_LOGI(TAG, "LED Control Task started. Waiting for signals...");

    while (1)
    {
        if (xQueueReceive(xLedControlQueue, &desired_led_state, portMAX_DELAY) == pdTRUE)
        {
            if (desired_led_state) {
                 gpio_set_level(BLINK_GPIO, 1); // Turn LED ON
                 // Format the message for the OLED
                snprintf(oled_message_buffer, MAX_LINE_LENGTH, "LED: ON");

            } else {
                 gpio_set_level(BLINK_GPIO, 0); // Turn LED OFF
                 snprintf(oled_message_buffer, MAX_LINE_LENGTH, "LED: OFF");
            }
            ESP_LOGI(TAG, "Received signal, LED set to %s", desired_led_state ? "ON" : "OFF");
           
            // // --- Update OLED display with LED status ---
            // // Prepare an array of string pointers for display_update_text.
            // // We'll update only one specific line, so other pointers can be NULL.
            // const char *lines_to_update[N_DISPLAY_LINES];

            // // It's good practice to initialize the array to NULL,
            // // or copy current display_lines content if you want to preserve other lines.
            // // For simplicity, here we'll assume other lines are handled by other calls
            // // or will be cleared if not explicitly set.
            // for (int i = 0; i < N_DISPLAY_LINES; i++) {
            //     lines_to_update[i] = NULL;
            // }

            // // Set the message for the desired line.
            // // For example, display LED status on the first line (index 0).
            // lines_to_update[0] = oled_message_buffer;

            // // Call the display update function.
            // // We pass N_DISPLAY_LINES as the count, as display_update_text expects
            // // an array of that size, with NULLs for lines not being updated.
            // display_update_text(lines_to_update, N_DISPLAY_LINES);
        }
    }
}

/**
 * @brief Initializes and starts the LED control task.
 */
esp_err_t start_led_control_task(void)
{
    if (xTaskCreate(led_control_task, "LED_Control_Task", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL) != pdPASS)
    {
        ESP_LOGE(TAG, "Failed to create LED Control task!");
        return ESP_FAIL;
    }
    ESP_LOGI(TAG, "LED Control task created successfully!");
    return ESP_OK;
}