Button Event not triggering

Description

Button Event not triggering

What MCU/Processor/Board and compiler are you using?

Arduino GIGA/Adafruit RA8875/7" touchscreen

What do you want to achieve?

get simple program with button and textbox working

What have you tried so far?

Have a simple program that updates a textbox with a counter and a button that triggers an event.
The textbox counter works fine. Can’t get the button event to trigger when the button is touched. Have log turned on for use messages. I see the message I put in my_touch_read_cb when I touch the screen. The button event messages print out a handful of times when I start the app but after they never show up on button press.

Code to reproduce

Add the relevant code snippets here.

The code block(s) should be between ```c and ``` tags:

#include <Adafruit_RA8875.h>
#include <lvgl.h>
//#include "lv_conf.h"

#define RA8875_INT 3
#define RA8875_CS     10
#define RA8875_RESET  9
#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 480

uint16_t tx, ty;

Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);

// Framebuffer: 40 lines for partial rendering
#define BUFFER_LINES 40
//static lv_color_t draw_buf1[SCREEN_WIDTH * BUFFER_LINES];
static lv_color_t draw_buf1[SCREEN_WIDTH * BUFFER_LINES];
static lv_draw_buf_t draw_buf;
static lv_display_t* disp;

lv_obj_t* TextBox;
int number = 0;
unsigned long previousMillis = 0;
const long interval = 100;

void log_print(lv_log_level_t level, const char * buf) {
  LV_UNUSED(level);
  Serial.println(buf);
  Serial.flush();
}

// -------- LVGL DRAWING --------
void my_flush_cb(lv_display_t* disp_drv, const lv_area_t* area, uint8_t* px_map) {
  lv_coord_t w = area->x2 - area->x1 + 1;
  lv_coord_t h = area->y2 - area->y1 + 1;
  tft.drawRGBBitmap(area->x1, area->y1, (uint16_t*)px_map, w, h);
  lv_display_flush_ready(disp_drv);
}

// -------- LVGL TICK FUNCTION --------
static uint32_t my_tick_get_cb(void) {
  return millis();
}

// -------- LVGL TOUCH DRIVER --------
static void my_touch_read_cb(lv_indev_t* indev, lv_indev_data_t* data) {
  if (tft.touched()) {
    LV_LOG_USER("Touched");
    uint16_t x, y;
    if (tft.touchRead(&x, &y)) {
      data->point.x = x;
      data->point.y = y;
      data->state = LV_INDEV_STATE_PRESSED;
    } else {
      //data->state = LV_INDEV_STATE_RELEASED;
    }
  } else {
    data->state = LV_INDEV_STATE_RELEASED;
  }
}

// -------- BUTTON EVENT HANDLER --------
static void btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    LV_LOG_USER("in event");
    if(code == LV_EVENT_CLICKED) {
      Serial.println("clicked");
        LV_LOG_USER("Clicked");
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Init display
  if (!tft.begin(RA8875_800x480)) {
    Serial.println("RA8875 init failed!");
    while (1);
  }

  tft.displayOn(true);
  tft.GPIOX(true);
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024);
  tft.PWM1out(255);
  tft.touchEnable(true);

  // Init LVGL
  lv_init();
  //lv_log_register_print_cb()
  lv_log_register_print_cb(log_print);
  lv_tick_set_cb(my_tick_get_cb);

  lv_draw_buf_init(&draw_buf,
                   SCREEN_WIDTH,
                   BUFFER_LINES,
                   LV_COLOR_FORMAT_RGB565,
                   SCREEN_WIDTH * sizeof(lv_color_t),
                   draw_buf1,
                   sizeof(draw_buf1));

  disp = lv_display_create(SCREEN_WIDTH, SCREEN_HEIGHT);
  lv_display_set_flush_cb(disp, my_flush_cb);
  lv_display_set_draw_buffers(disp, &draw_buf, NULL);

  // Register touchscreen
  lv_indev_t* indev_touch = lv_indev_create();
  lv_indev_set_type(indev_touch, LV_INDEV_TYPE_POINTER);
  lv_indev_set_read_cb(indev_touch, my_touch_read_cb);

  // UI: Button
  lv_obj_t* btn = lv_button_create(lv_screen_active());
  lv_obj_set_size(btn, 120, 50);
  //lv_obj_align(btn, LV_ALIGN_OUT_TOP_LEFT, 420, 10);
  lv_obj_center(btn);
  lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);
  lv_obj_t* label = lv_label_create(btn);
  lv_label_set_text(label, "Button");
  lv_obj_center(label);

  // UI: Text Box
  TextBox = lv_textarea_create(lv_screen_active());
  lv_obj_set_size(TextBox, 100, 60);
  lv_obj_align(TextBox, LV_ALIGN_OUT_TOP_LEFT, 100, 50);
  lv_textarea_set_one_line(TextBox, true);
  lv_obj_set_style_text_align(TextBox, LV_TEXT_ALIGN_CENTER, 0);
}

void loop() {
  lv_task_handler();  // let the GUI do its work
//lv_tick_inc(5);        // tell LVGL how much time has passed
 lv_tick_inc(10);
delay(5);                // let this time pass

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    number = (number + 1) % 1000;

    char buffer[8];
    itoa(number, buffer, 10);
    lv_textarea_set_text(TextBox, buffer);
  }

  float xScale = 1024.0F/tft.width();
  float yScale = 1024.0F/tft.height();
if (! digitalRead(RA8875_INT))
  {
    // if (tft.touched())
    // {
    //   Serial.println("Touch: ");
    //   tft.touchRead(&tx, &ty);
    //   Serial.print(tx); Serial.print(", "); Serial.println(ty);
    //   /* Draw a circle */
    //   tft.fillCircle((uint16_t)(tx/xScale), (uint16_t)(ty/yScale), 4, RA8875_WHITE);
    // }
  }
}

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

Hi I had a similar issue yesterday and was able to fix it myself. https://forum.lvgl.io/t/touch-doesnt-trigger-events/20822

In my case the display rotation I applied triggered a coordinate conversion during the processing of the touch position. I had to swap x and y and invert both to get LVGL to work as intended although the reported coordinates in the read_cb were matching LVGLs position coordinated of the button.

This issue on githup helped me a lot in understanding what might be the problem https://github.com/lvgl/lvgl/issues/7841

I see no display rotation in your code, but maybe it is a messed up coordinate system as well.

Hello,

Does the print always happen on touch? Or also only on startup?

I recommend also printing the coordinates in that log, perhaps it gives you an idea of the coordinates you are passing along to LVGL, and whether they fall in the hitbox of your button.

Brilliant! Thats exactly what it was I missed scaling for the xand y in th ra8875 example. Thanks!
Now I added for the button to change color when clicked the screen updates very slow painting from top to bottom this occurs when it starts up as well is there any way to speed this up?