#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
const uart_port_t uart_num = UART_NUM_0;
// Setup UART buffered IO with event queue
const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;
static uint8_t ucParameterToPass;
TaskHandle_t xHandle = NULL;
char R[5];
char C[5];
char Ru[5];
char ruta_[10];
void uartInit() {
const uart_port_t uart_num = UART_NUM_2;
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_CTS_RTS,
.rx_flow_ctrl_thresh = 122,
};
// Configure UART parameters
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
// Install UART driver using an event queue here
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
}
void uartPuts(const char data[]) {
uart_write_bytes(uart_num, (const char*)data, strlen(data));
}
void uartGets(uint8_t* data) {
int length = 0;
while (!length) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
}
length = uart_read_bytes(uart_num, data, length, 100 / portTICK_PERIOD_MS);
for (int i = 0; i < length; i++) {
if (data[i] == '\n' || data[i] == '\r') {
data[i] = '\0';
break;
}
}
}
void vTaskCode( void * pvParameters )
{
uartPuts("Renglones:\n");
uartGets((uint8_t*)R);
int Ren = atoi(R);
uartPuts("Columnas:\n");
uartGets((uint8_t*)C);
int Colum = atoi(C);
uartPuts("Rutas:\n");
uartGets((uint8_t*)Ru);
int Rutas = atoi(Ru);
// Inicializamos el campo
int campo[Ren][Colum];
memset(campo, 0, sizeof(campo));
// Procesamos las rutas
for (int i = 0; i < Rutas; i++) {
uartPuts("r c1 c2:\n");
uartGets((uint8_t*)ruta_);
int r, c1, c2;
sscanf(ruta_, "%d %d %d", &r, &c1, &c2);
// Marcamos las celdas ocupadas
for (int p = c1; p <= c2; p++) {
campo[r][p] = 1;
}
}
// Contamos celdas disponibles
int celdas_disponibles = 0;
for (int i = 0; i < Ren; i++) {
for (int j = 0; j < Colum; j++) {
if (campo[i][j] == 0) {
celdas_disponibles++;
}
}
}
// Imprimimos el resultado
char result[50];
snprintf(result, sizeof(result), "Celdas disponibles: %d\n", celdas_disponibles);
uartPuts(result);
vTaskSuspend( xHandle );
}
void app_main() {
uartInit();
xTaskCreate( vTaskCode, "vTaskCode", 2*(1024*2), NULL, 1, NULL );
}