#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_timer.h"
#define LED_PIN GPIO_NUM_13
#define BUTTON_PIN GPIO_NUM_14
#define UART_PORT UART_NUM_0
#define BUFFER_SIZE 1024
#define ESP_INTR_FLAG_DEFAULT 0
typedef struct {
uint8_t pinNumber,
lastState;
int64_t lastTime;
} Debouncer;
void uart_monitor_task(void*);
void led_task(void*);
void button_task(void*);
void uart_send(char*);
void ini_uart(void);
void ini_gpio(void);
static EventGroupHandle_t events = NULL;
static const EventBits_t EVENT_LED_ON = BIT0;
static const EventBits_t EVENT_LED_OFF = BIT1;
static const EventBits_t EVENT_LED_TOGGLE = BIT2;
static const EventBits_t EVENT_BUTTON_PRESS = BIT3;
static const EventBits_t EVENT_READY_TO_START = BIT4;
static QueueHandle_t events_queue = NULL;
// Manejador de interrupciónes
static void IRAM_ATTR isr_handle(void *args)
{
}
void app_main()
{
/* Wait 1000 ms before start. */
const TickType_t start_delay = 1000 / portTICK_PERIOD_MS;
ini_gpio();
ini_uart();
// Instalar servicio de interrupciones y asociar handlers
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
gpio_isr_handler_add(BUTTON_PIN, isr_handle, NULL);
events = xEventGroupCreate();
events_queue = xQueueCreate(10, sizeof(EventBits_t));
xTaskCreate(uart_monitor_task, "uart_monitor_task", 1024, NULL, 1, NULL);
xTaskCreate(led_task, "led_task", 1024, NULL, 1, NULL);
xTaskCreate(button_task, "button_task", 1024, NULL, 1, NULL);
vTaskDelay(start_delay);
xEventGroupSetBits(events, EVENT_READY_TO_START);
}
void ini_uart(void)
{
const uart_port_t uart_num = UART_PORT;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};
// Install UART Driver
ESP_ERROR_CHECK(uart_driver_install(uart_num, BUFFER_SIZE * 2, BUFFER_SIZE * 2, 0, NULL, 0));
// Configure UART parameters
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
// Set Communication Pins
ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
}
void ini_gpio(void) {
// Configuracion del LED
gpio_reset_pin(LED_PIN);
gpio_intr_disable(LED_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
gpio_set_pull_mode(LED_PIN, GPIO_FLOATING);
// Configuracion del Boton
gpio_reset_pin(BUTTON_PIN);
gpio_set_intr_type(BUTTON_PIN, GPIO_INTR_POSEDGE);
gpio_set_direction(LED_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(LED_PIN, GPIO_PULLDOWN_ONLY);
}
void uart_monitor_task(void *pvParameters)
{
const uart_port_t uart_num = UART_PORT;
xEventGroupWaitBits(events,
EVENT_READY_TO_START,
pdFALSE,
pdTRUE,
portMAX_DELAY);
uint8_t data[128];
int len;
while(true)
{
len = uart_read_bytes(uart_num, data, 128 - 1, 100 / portTICK_PERIOD_MS);
if (len > 0)
{
char* test_str = "Error al leer de UART.\n";
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
}
}
}