#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#define LCD_20X04 1
#define I2C_ACK_CHECK_EN 1
#define I2C_ADDRESS_LCD 0x27
#define I2C_SCL_LCD 21
#define I2C_SDA_LCD 22
/* Comandos de la LCD */
#define CLEAR_DISPLAY 0x01
#define RETURN_HOME_UNSHIFT 0x02
#define CURSOR_RIGHT_NO_SHIFT 0x04
#define CURSOR_RIGHT_SHIFT 0x05
#define CURSOR_RIGHT_NO_SHIFT_LEFT 0x06
#define CURSOR_RIGHT_SHIFT_LEFT 0x07
#define DISPLAY_OFF 0x08
#define DISPLAY_ON_CURSOR_OFF 0x0C
#define DISPLAY_ON_CURSOR_ON_STEADY 0x0E
#define DISPLAY_ON_CURSOR_ON_BLINK 0x0F
#define SHIFT_CURSOR_LEFT 0x10
#define SHIFT_CURSOR_RIGHT 0x14
#define SHIFT_DISPLAY_LEFT 0x18
#define SHIFT_DISPLAY_RIGHT 0x1C
#define SET_4BIT_MODE 0x28
#define RETURN_HOME 0x80
/* PCF8574 */
#define PCF8574_RS 0
#define PCF8574_RW 1
#define PCF8574_EN 2
#define PCF8574_BL 3
#define LCD_RS_CMD 0
#define LCD_RS_DATA 1
/* UART */
const uart_port_t uart_num = UART_NUM_0;
const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;
TaskHandle_t HandleTask = NULL;
typedef struct {
uint8_t i2c_address;
uint8_t i2c_port;
uint8_t screen_size;
uint8_t screen_backlight;
} lcd_i2c_device_t;
void i2c_init(void);
void lcd_init(lcd_i2c_device_t * lcd);
void lcd_i2c_write_byte(lcd_i2c_device_t * lcd, uint8_t data);
void lcd_i2c_write_command(lcd_i2c_device_t * lcd, uint8_t register_select, uint8_t cmd);
void lcd_set_cursor(lcd_i2c_device_t * lcd, uint8_t column, uint8_t row);
void lcd_i2c_write_custom_char(lcd_i2c_device_t * lcd, uint8_t char_address, const uint8_t * pixels);
void i2c_init(void)
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_SDA_LCD,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_SCL_LCD,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 400000,
};
esp_err_t error = i2c_param_config(I2C_NUM_1, &i2c_config);
if (error != ESP_OK) {
while(1);
}
i2c_driver_install(I2C_NUM_1, I2C_MODE_MASTER, 0, 0, 0);
}
void lcd_init(lcd_i2c_device_t * lcd)
{
lcd_i2c_write_command(lcd, LCD_RS_CMD, RETURN_HOME_UNSHIFT);
lcd_i2c_write_command(lcd, LCD_RS_CMD, SET_4BIT_MODE);
lcd_i2c_write_command(lcd, LCD_RS_CMD, CLEAR_DISPLAY);
lcd_i2c_write_command(lcd, LCD_RS_CMD, DISPLAY_ON_CURSOR_OFF);
lcd_i2c_write_command(lcd, LCD_RS_CMD, CURSOR_RIGHT_NO_SHIFT_LEFT);
vTaskDelay(20 / portTICK_PERIOD_MS);
}
void lcd_i2c_write_byte(lcd_i2c_device_t * lcd, uint8_t data)
{
i2c_cmd_handle_t cmd_handle = i2c_cmd_link_create();
i2c_master_start(cmd_handle);
i2c_master_write_byte(cmd_handle, (lcd->i2c_address << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_EN);
i2c_master_write_byte(cmd_handle, data, 1);
i2c_master_stop(cmd_handle);
i2c_master_cmd_begin(lcd->i2c_port, cmd_handle, 100 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd_handle);
}
void lcd_i2c_write_command(lcd_i2c_device_t * lcd, uint8_t register_select, uint8_t cmd)
{
uint8_t config = (register_select)? (1 << PCF8574_RS) : 0;
config |= (lcd->screen_backlight)? (1 << PCF8574_BL) : 0;
config |= (config & 0x0F) | (0xF0 & cmd);
config |= (1 << PCF8574_EN);
lcd_i2c_write_byte(lcd, config);
ets_delay_us(10);
config &= ~(1 << PCF8574_EN);
lcd_i2c_write_byte(lcd, config);
ets_delay_us(50);
config = (config & 0x0F) | (cmd << 4);
config |= (1 << PCF8574_EN);
lcd_i2c_write_byte(lcd, config);
ets_delay_us(10);
config &= ~(1 << PCF8574_EN);
lcd_i2c_write_byte(lcd, config);
ets_delay_us(50);
if (cmd == CLEAR_DISPLAY)
{
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
void lcd_set_cursor(lcd_i2c_device_t * lcd, uint8_t column, uint8_t row)
{
switch (row) {
case 0:
lcd_i2c_write_command(lcd, LCD_RS_CMD, 0x80 + column);
break;
case 1:
lcd_i2c_write_command(lcd, LCD_RS_CMD, 0xC0 + column);
break;
case 2:
lcd_i2c_write_command(lcd, LCD_RS_CMD, 0x94 + column);
break;
case 3:
lcd_i2c_write_command(lcd, LCD_RS_CMD, 0xD4 + column);
break;
default:
break;
}
}
void lcd_i2c_write_custom_char(lcd_i2c_device_t * lcd, uint8_t address, const uint8_t * pixels)
{
lcd_i2c_write_command(lcd, LCD_RS_CMD, 0x40 | (address << 3));
for (uint8_t i = 0; i < 8; i++)
{
lcd_i2c_write_command(lcd, LCD_RS_DATA, pixels[i]);
}
lcd_i2c_write_command(lcd, LCD_RS_CMD, RETURN_HOME);
}
void lcd_i2c_print_msg(lcd_i2c_device_t * lcd, char * msg)
{
uint8_t i = 0;
while (msg[i] != '\0')
{
lcd_i2c_write_command(lcd, LCD_RS_DATA, msg[i++]);
}
}
void uartInit(){
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(char data[]){
uart_write_bytes(uart_num, (const char*)data, strlen(data));
}
void uartGets(uint8_t data[]){
// Read data from UART.
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);
data[length-1]=0;
}
void printMatrixToUART(int rows, int cols, int campo[rows][cols]) {
char buffer[100]; // Buffer para construir las filas como cadenas
for (int r = 0; r < rows; r++) {
int index = 0;
for (int c = 0; c < cols; c++) {
index += snprintf(&buffer[index], sizeof(buffer) - index, "%d ", campo[r][c]);
}
buffer[index] = '\0'; // Asegura que el buffer termina en un nulo
uartPuts(buffer); // Envía la fila como una línea de texto
uartPuts("\n"); // Nueva línea después de cada fila
}
uartPuts("\n"); // Línea vacía después de la matriz
}
void vTaskCode( void * pvParameters )
{
char Ren[10];
char Col[10];
char Rut[10];
char ruta_[10];
lcd_i2c_device_t my_lcd = {
.i2c_port = I2C_NUM_1,
.i2c_address = I2C_ADDRESS_LCD,
.screen_size = LCD_20X04,
.screen_backlight = 1,
};
vTaskDelay(20 / portTICK_PERIOD_MS);
lcd_init(&my_lcd);
while(true){
uartPuts("Renglones: \n");
uartGets((uint8_t*)Ren);
int Renglones = atoi(Ren);
uartPuts("Columnas: \n");
uartGets((uint8_t*)Col);
int Columnas = atoi(Col);
uartPuts("Rutas: \n");
uartGets((uint8_t*)Rut);
int Rutas = atoi(Rut);
// Crear la matriz del campo
int campo[Renglones][Columnas];
memset(campo, 0, sizeof(campo)); // Inicializar todo a 0 (disponible)
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);
for(int p = c1; p <= c2;p++){
campo[r][p] = 1;
}
}
int dispo = 0;
for(int r = 0; r < Renglones;r++){
for(int c = 0; c < Columnas;c++){
if(campo[r][c] == 0){
dispo++;
}
}
}
char Resul[30];
snprintf(Resul, sizeof(Resul), "Celdas libres: %d", dispo);
lcd_set_cursor(&my_lcd, 0, 0); // Mover el cursor al inicio
lcd_i2c_print_msg(&my_lcd, Resul);
printMatrixToUART(Renglones, Columnas, campo);
vTaskSuspend( HandleTask );
}
}
#define SMILE 5
#define DROP 2
void app_main()
{
i2c_init();
uartInit();
lcd_i2c_device_t my_lcd = {
.i2c_port = I2C_NUM_1,
.i2c_address = I2C_ADDRESS_LCD,
.screen_size = LCD_20X04,
.screen_backlight = 1,
};
vTaskDelay(20 / portTICK_PERIOD_MS);
lcd_init(&my_lcd);
char message[] = {'H', 'o', 'l', 'a', ' ', 'M', 'u', 'n', 'd', 'o', 0};
//lcd_i2c_print_msg(&my_lcd, message);
uartPuts("Hola Alain. \n");
xTaskCreate( vTaskCode, "vTaskCode", 2*(2*1024), NULL, 1, NULL );
vTaskDelay(250 / portTICK_PERIOD_MS);
}