#include <stdio.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "esp_system.h"
#include "rom/ets_sys.h" ///???
#include "driver/gpio.h"
//#include "driver/i2c_master.h"
#include "driver/i2c.h"
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
#define MESSAGE_ENTERPRISE_H " Generacion "
#define MESSAGE_ENTERPRISE_L " de Tecnologia "
const char* title_1 = MESSAGE_ENTERPRISE_H;
const char* title_2 = MESSAGE_ENTERPRISE_L;
const uint8_t LCD_RS = 14, LCD_EN = 12, LCD_D4 = 18, LCD_D5 = 17, LCD_D6 = 16, LCD_D7 = 15;
void liquid_crystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
void lcd_init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
void lcd_begin(uint8_t cols, uint8_t lines);
void lcd_clear(void);
void lcd_home(void);
void lcd_set_row_offsets(int row0, int row1, int row2, int row3);
void lcd_create_char(uint8_t location, uint8_t charmap[]);
void lcd_set_cursor(uint8_t, uint8_t);
void lcd_command(uint8_t value); //inline copia el codigo de la funcion directamente donde es llamada
size_t lcd_write(uint8_t value);
////////////////////////////////////////////////////////
void lcd_no_display();
void lcd_display();
void lcd_no_blink();
void lcd_blink();
void lcd_no_cursor();
void lcd_cursor();
void lcd_scroll_display_left();
void lcd_scroll_display_right();
void lcd_left_to_right();
void lcd_right_to_left();
void lcd_autoscroll();
void lcd_no_autoscroll();
////////////////////////////////////////////////////////
void lcd_send(uint8_t value, uint8_t mode);
void lcd_pulse_enable(void);
void lcd_write_4_bits(uint8_t value);
void lcd_write_8_bits(uint8_t value);
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
uint8_t _rs_pin; // LOW: command. HIGH: character.
uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD.
uint8_t _enable_pin; // activated by a HIGH pulse.
uint8_t _data_pins[8];
uint8_t _displayfunction;
uint8_t _displaycontrol;
uint8_t _displaymode;
uint8_t _initialized;
uint8_t _numlines;
uint8_t _row_offsets[4];
void liquid_crystal(uint8_t rs, uint8_t enable, uint8_t d0,
uint8_t d1, uint8_t d2, uint8_t d3)
{
lcd_init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
}
void lcd_init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
{
_rs_pin = rs;
_rw_pin = rw;
_enable_pin = enable;
_data_pins[0] = d0;
_data_pins[1] = d1;
_data_pins[2] = d2;
_data_pins[3] = d3;
_data_pins[4] = d4;
_data_pins[5] = d5;
_data_pins[6] = d6;
_data_pins[7] = d7;
if (fourbitmode)
{
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
}
else
{
_displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
}
lcd_begin(16, 1);
}
void lcd_begin(uint8_t cols, uint8_t lines)
{
uint8_t dotsize = LCD_5x8DOTS;
if (lines > 1)
{
_displayfunction |= LCD_2LINE;
}
_numlines = lines;
lcd_set_row_offsets(0x00, 0x40, 0x00 + cols, 0x40 + cols);
// for some 1 line displays you can select a 10 pixel high font
if ((dotsize != LCD_5x8DOTS) && (lines == 1))
{
_displayfunction |= LCD_5x10DOTS;
}
gpio_set_direction(_rs_pin, GPIO_MODE_OUTPUT);
// we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
if (_rw_pin != 255)
{
gpio_set_direction(_rw_pin, GPIO_MODE_OUTPUT);
}
gpio_set_direction(_enable_pin, GPIO_MODE_OUTPUT);
// Do these once, instead of every time a character is drawn for speed reasons.
for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i)
{
gpio_set_direction(_data_pins[i], GPIO_MODE_OUTPUT);
}
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
// according to datasheet, we need at least 40 ms after power rises above 2.7 V
// before sending commands.
ets_delay_us(50000);
// Now we pull both RS and R/W low to begin commands
gpio_set_level(_rs_pin, 0);
gpio_set_level(_enable_pin, 0);
if (_rw_pin != 255)
{
gpio_set_level(_rw_pin, 0);
}
//put the LCD into 4 bit or 8 bit mode
if (! (_displayfunction & LCD_8BITMODE))
{
// this is according to the Hitachi HD44780 datasheet
// figure 24, pg 46
// we start in 8bit mode, try to set 4 bit mode
lcd_write_4_bits(0x03);
ets_delay_us(4500); // wait min 4.1ms
// second try
lcd_write_4_bits(0x03);
ets_delay_us(4500); // wait min 4.1ms
// third go!
lcd_write_4_bits(0x03);
ets_delay_us(150);
// finally, set to 4-bit interface
lcd_write_4_bits(0x02);
}
else
{
// this is according to the Hitachi HD44780 datasheet
// page 45 figure 23
// Send function set command sequence
lcd_command(LCD_FUNCTIONSET | _displayfunction);
ets_delay_us(4500); // wait more than 4.1 ms
// second try
lcd_command(LCD_FUNCTIONSET | _displayfunction);
ets_delay_us(150);
// third go
lcd_command(LCD_FUNCTIONSET | _displayfunction);
}
// finally, set # lines, font size, etc.
lcd_command(LCD_FUNCTIONSET | _displayfunction);
// turn the display on with no cursor or blinking default
_displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
lcd_display();
// clear it off
lcd_clear();
// Initialize to default text direction (for romance languages)
_displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
// set the entry mode
lcd_command(LCD_ENTRYMODESET | _displaymode);
}
void lcd_set_row_offsets(int row0, int row1, int row2, int row3)
{
_row_offsets[0] = row0;
_row_offsets[1] = row1;
_row_offsets[2] = row2;
_row_offsets[3] = row3;
}
/********** high level commands, for the user! */
void lcd_clear()
{
lcd_command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
ets_delay_us(2000); // this command takes a long time!
}
void lcd_home()
{
lcd_command(LCD_RETURNHOME); // set cursor position to zero
ets_delay_us(2000); // this command takes a long time!
}
void lcd_set_cursor(uint8_t col, uint8_t row)
{
const size_t max_lines = sizeof(_row_offsets) / sizeof(*_row_offsets);
if ( row >= max_lines )
{
row = max_lines - 1; // we count rows starting w/ 0
}
if ( row >= _numlines )
{
row = _numlines - 1; // we count rows starting w/ 0
}
lcd_command(LCD_SETDDRAMADDR | (col + _row_offsets[row]));
}
// Turn the display on/off (quickly)
void lcd_no_display()
{
_displaycontrol &= ~LCD_DISPLAYON;
lcd_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void lcd_display()
{
_displaycontrol |= LCD_DISPLAYON;
lcd_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turns the underline cursor on/off
void lcd_no_cursor()
{
_displaycontrol &= ~LCD_CURSORON;
lcd_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void lcd_cursor()
{
_displaycontrol |= LCD_CURSORON;
lcd_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// Turn on and off the blinking cursor
void lcd_no_blink()
{
_displaycontrol &= ~LCD_BLINKON;
lcd_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void lcd_blink()
{
_displaycontrol |= LCD_BLINKON;
lcd_command(LCD_DISPLAYCONTROL | _displaycontrol);
}
// These commands scroll the display without changing the RAM
void lcd_scroll_display_left(void)
{
lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
void lcd_scroll_display_right(void)
{
lcd_command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}
// This is for text that flows Left to Right
void lcd_left_to_right(void)
{
_displaymode |= LCD_ENTRYLEFT;
lcd_command(LCD_ENTRYMODESET | _displaymode);
}
// This is for text that flows Right to Left
void lcd_right_to_left(void)
{
_displaymode &= ~LCD_ENTRYLEFT;
lcd_command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'right justify' text from the cursor
void lcd_autoscroll(void)
{
_displaymode |= LCD_ENTRYSHIFTINCREMENT;
lcd_command(LCD_ENTRYMODESET | _displaymode);
}
// This will 'left justify' text from the cursor
void lcd_no_autoscroll(void)
{
_displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
lcd_command(LCD_ENTRYMODESET | _displaymode);
}
// Allows us to fill the first 8 CGRAM locations
// with custom characters
void lcd_create_char(uint8_t location, uint8_t charmap[])
{
location &= 0x7; // we only have 8 locations 0-7
lcd_command(LCD_SETCGRAMADDR | (location << 3));
for (int i=0; i<8; i++)
{
lcd_write(charmap[i]);
}
}
/*********** mid level commands, for sending data/cmds */
inline void lcd_command(uint8_t value)
{
lcd_send(value, 0);
}
inline size_t lcd_write(uint8_t value)
{
lcd_send(value, 1);
return 1; // assume success
}
/************ low level data pushing commands **********/
// write either command or data, with automatic 4/8-bit selection
void lcd_send(uint8_t value, uint8_t mode)
{
gpio_set_level(_rs_pin, mode);
// if there is a RW pin indicated, set it low to Write
if (_rw_pin != 255)
{
gpio_set_level(_rw_pin, 0);
}
if (_displayfunction & LCD_8BITMODE)
{
lcd_write_8_bits(value);
}
else
{
lcd_write_4_bits(value>>4);
lcd_write_4_bits(value);
}
}
void lcd_pulse_enable(void)
{
gpio_set_level(_enable_pin, 0);
ets_delay_us(1);
gpio_set_level(_enable_pin, 1);
ets_delay_us(1); // enable pulse must be >450 ns
gpio_set_level(_enable_pin, 0);
ets_delay_us(100); // commands need >37 us to settle
}
void lcd_write_4_bits(uint8_t value)
{
for (int i = 0; i < 4; i++)
{
gpio_set_level(_data_pins[i], (value >> i) & 0x01);
}
lcd_pulse_enable();
}
void lcd_write_8_bits(uint8_t value)
{
for (int i = 0; i < 8; i++)
{
gpio_set_level(_data_pins[i], (value >> i) & 0x01);
}
lcd_pulse_enable();
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
#define SELECT_BUTTON 45 //GPIO_NUM_45
#define DOWN_BUTTON 35 //GPIO_NUM_35
#define UP_BUTTON 36 //GPIO_NUM_36
#define START_BUTTON 37 //GPIO_NUM_37
typedef enum{
SELECT = 0,
DOWN = 1,
UP = 2,
START = 3
}ButtonId;
#define TOTAL_KEYS 4
#define LONG_PRESS_DELAY_MS 750 // Tiempo en milisegundos para considerar una pulsación como larga
typedef enum {
STATE_INIT,
STATE_WAITING,
STATE_DETECTE,
STATE_WAUTFORREALSE,
STATE_UPDATE
} FB_KEYSState;
typedef struct
{
void (*initHandler)(void*);
uint8_t* evPress[TOTAL_KEYS];
uint8_t* evLongPress[TOTAL_KEYS];
FB_KEYSState state;
}FB_KEYS;
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/*typedef enum {
STATE_INIT,
STATE_WAITING,
STATE_KEYSEV,
} FB_IUSERState;
typedef struct
{
void (*initHandler)(void*);
FB_IUSERState state;
}FB_INTERFACE_USER;*/
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void app_main(void)
{
liquid_crystal(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
lcd_begin(16, 2);
lcd_set_cursor(0, 0);
for (int i = 0; i < 16; i++)
{
lcd_write(title_1[i]);
}
lcd_set_cursor(0, 1);
for (int i = 0; i < 16; i++)
{
lcd_write(title_2[i]);
}
vTaskSuspend(NULL);
}