Thanks @kisvegabor and @Marian_M for your help, Finally it worked !!!
I’ve added many printf
to log everything in the main Arduino sketch as well as some other functions in the library.
I’m not sure what was the exact issue, now even when I try running the old sketch which wasn’t working - where I’m trying to change the LED state from the callback function button_read_1
- it works fine. If I find out why it wasn’t working correctly I’ll post it here.
Below is the complete Arduino sketch which works fine.
- LVGL library version 8.3
- eSPI version 2.4.79
- Arduino IDE version 2.0.3
- ESP32-WROOM-32 - “DOIT ESP32 DEVKIT V1”
- Display GMT130-V1.0 (240x240 RGB)
Make sure to change #define LV_TICK_CUSTOM 1
in lv_conf.h
.
change the input pin number as required in the sketch int button_pin = 0;//INPUT Pin number
#include <lvgl.h>
#include <TFT_eSPI.h>
/*Global variables*/
//For testing the external button
lv_obj_t * btn1;
lv_obj_t * led1;
lv_obj_t * labelCounter; // For debugging
const char * counterChar = "labelCounter";
int button_pin = 0;//INPUT Pin number
int debugCounter =1;//For debugging
lv_indev_t * indev_button_1;
/*Change to your screen resolution*/
static const uint16_t screenWidth = 240;
static const uint16_t screenHeight = 240;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * 24];
TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */
#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char * buf)
{
Serial.printf(buf);
Serial.flush();
}
#endif
/* Display flushing */
void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p )
{
uint32_t w = ( area->x2 - area->x1 + 1 );
uint32_t h = ( area->y2 - area->y1 + 1 );
tft.startWrite();
tft.setAddrWindow( area->x1, area->y1, w, h );
tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
tft.endWrite();
lv_disp_flush_ready( disp );
}
/*Button Callback Function*/
//Button test - Button click event Handler - On screen
static void btn_event_handler(lv_event_t * e)
{
//Original Code
lv_event_code_t code = lv_event_get_code(e);
if(code == LV_EVENT_PRESSED)
{
//Turn the LED ON
lv_led_on(led1);
lv_label_set_text(labelCounter, "Something Pressed !!!"); //For debugging
}
else if(code == LV_EVENT_RELEASED)
{
//Turn the LED OFF
lv_led_off(led1);
lv_label_set_text(labelCounter, "Nothing Pressed"); //For debugging
}
}
/**External Button lv_port_Indev functions**/
/*Initialize your buttons*/
static void button_init_1(void)
{
pinMode(button_pin, INPUT);
}
/*Will be called periodically to read the button*/
static void button_read_1(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
static uint8_t last_btn = 0;
/*Get the pressed button's ID*/
int8_t btn_act = button_get_pressed_id_1();
//printf("button_get_pressed_id_1() returned %d", btn_act);
if(btn_act >= 0) {
data->state = LV_INDEV_STATE_PR;
last_btn = btn_act;
}
else {
data->state = LV_INDEV_STATE_REL;
}
/*Save the last pressed button's ID*/
data->btn_id = last_btn;
}
/*Get ID (0, 1, 2 ..) of the pressed button*/
static int8_t button_get_pressed_id_1(void)
{
uint8_t i;
/*Check to buttons see which is being pressed (assume there are 2 buttons)*/
for(i = 0; i < 1; i++) {
/*Return the pressed button's ID*/
if(button_is_pressed_1(i)) {
return i;
}
}
/*No button pressed*/
return -1;
}
/*Test if `id` button is pressed or not*/
static bool button_is_pressed_1(uint8_t id)
{
if(id == 0)
{
if(digitalRead(button_pin) == LOW)
{
return true;
}
else if(digitalRead(button_pin) == HIGH)
return false;
}
return false;
}
//Initialization function:-
void lv_port_indev_init_1(void)
{
static lv_indev_drv_t indev_drv_1;
/*Initialize your button if you have*/
button_init_1();
/*Register a button input device*/
lv_indev_drv_init(&indev_drv_1);
indev_drv_1.type = LV_INDEV_TYPE_BUTTON;
indev_drv_1.read_cb = button_read_1;
indev_button_1 = lv_indev_drv_register(&indev_drv_1);
/*Assign buttons to points on the screen*/
static const lv_point_t btn_points_1[1] = {
{120, 90} /*Button 0 -> x:10; y:10*/
};
lv_indev_set_button_points(indev_button_1, btn_points_1);
}
void setup()
{
Serial.begin( 115200 ); /* prepare for possible serial debug */
lv_init();
#if LV_USE_LOG != 0
lv_log_register_print_cb( my_print ); /* register print function for debugging */
#endif
tft.begin(); /* TFT init */
tft.setRotation( 3 ); /* Landscape orientation, flipped */
lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * 10 );
/*Initialize the display*/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init( &disp_drv );
/*Change the following line to your display resolution*/
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register( &disp_drv );
/*External Button Driver Initialization*/
lv_port_indev_init_1();
/* Create simple widgets */
btn1 = lv_btn_create(lv_scr_act());
led1 = lv_led_create(lv_scr_act());
labelCounter = lv_label_create(lv_scr_act());
lv_obj_align(labelCounter, LV_ALIGN_CENTER, 0, 90);
lv_label_set_text(labelCounter, counterChar);
lv_obj_add_event_cb(btn1, btn_event_handler, LV_EVENT_ALL, NULL);
lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -30);
lv_obj_set_size(btn1, 180, 60);
lv_obj_t * label;
label = lv_label_create(btn1);
lv_label_set_text(label, "Button");
lv_obj_center(label);
//Create one LED to test the external button input
lv_obj_align(led1, LV_ALIGN_CENTER, 0, 50);
lv_led_set_color(led1, lv_color_hex(0xff0000));
lv_led_off(led1); // Make it Off by default
}
void loop()
{
lv_timer_handler(); /* let the GUI do its work */
delay( 5 );
}