Description
I defined a button event but only enters a few times on loop() functions and after never works again.
What MCU/Processor/Board and compiler are you using?
ESP32-S3-DEVKIT-1 with ESP32-S3-WROOM-2
What LVGL version are you using?
8.0
What do you want to achieve?
that button event works
What have you tried so far?
I have changed from 8.3 to 8.0 considering it was a problem on library. Also I suspect that could be a problem with chip definition, 'cos I changed esp32-devkit.json file 'cos I always get a platformio error about SHA-256 comparison failed and rebooted this was solved.
Code to reproduce
v_obj_t * label;
lv_indev_drv_t indev_drv;
static void btn_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
Serial.println(code);
if(code == LV_EVENT_CLICKED) {
static uint8_t cnt = 0;
cnt++;
/*Get the first child of the button which is the label and change its text*/
lv_obj_t * label = lv_obj_get_child(btn, 0);
lv_label_set_text_fmt(label, "Button: %d", cnt);
}
}
void touchpad_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
{
static int16_t last_x = 0;
static int16_t last_y = 0;
if (!ts.bufferEmpty() && ts.touched()) {
TS_Point p = ts.getPoint();
// Calculate position 1:1 with display size
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 480);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 800);
last_x = p.x;
last_y = p.y;
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_PRESSED;
Serial.print("Touch detected, ");
Serial.print("("); Serial.print(last_x);
Serial.print(", "); Serial.print(last_y);
Serial.println(")");
//return true;
}
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_RELEASED;
//return false;
}
/* Change to your screen resolution */
static uint32_t screenWidth = 480;
static uint32_t screenHeight = 800;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t *disp_draw_buf;
static lv_disp_drv_t disp_drv;
/* 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);
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
void setup()
{
Serial.begin(115200);
Serial.println("LVGL Hello World");
lv_init();
gfx->begin();
gfx->fillScreen(BLACK);
SPI.begin(SCK,MISO,MOSI,CS_PIN);
ts.begin();
screenWidth = gfx->width();
screenHeight = gfx->height();
disp_draw_buf = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * 10, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
lv_disp_draw_buf_init(&draw_buf, disp_draw_buf, NULL, screenWidth * 10);
/* Initialize the display */
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);
/*Initialize the input device driver*/
lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
indev_drv.read_cb = touchpad_read; /*Set your driver function*/
lv_indev_drv_register(&indev_drv);
lv_obj_t * btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/
lv_obj_center(btn); /*Set its position*/
lv_obj_set_size(btn, 120, 50); /*Set its size*/
lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/
lv_obj_t * label = lv_label_create(btn); /*Add a label to the button*/
lv_label_set_text(label, "Button"); /*Set the labels text*/
lv_obj_center(label);
Serial.println("Setup done");
}
void loop()
{
lv_timer_handler(); /* let the GUI do its work */
delay(5);
}