Button remains pressed

Description

My problem is that if I press a key briefly, the key remains pressed and the letters repeat themselves on the keyboard, for example, without the key being pressed.
If I actually hold the key down, only a single entry is registered as desired.

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

Arduino Due/ Arduino IDE

What LVGL version are you using?

latest

What do you want to achieve?

That the buttons do not stay pressed when pressed briefly

What have you tried so far?

The logic in the touch function is reversed.

Code to reproduce

#include <lvgl.h>
//#include <lv_examples.h>
#include <UTFT.h>
#include <URTouch.h>
#include <Ticker.h>
#include <arduino-timer.h>


////////////////Globals/////////////////////77
uint32_t state_roller;
float value_spinbox1;




static void controls_create(lv_obj_t * parent);
static void visuals_create(lv_obj_t * parent);
static lv_obj_t * spinbox;
static lv_obj_t * gauge1;


static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 54];

void lv__tick_handler(void);

UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
URTouch  myTouch( 6, 5, 4, 3, 2);


static void lv_spinbox_increment_event_cb(lv_obj_t * btn, lv_event_t e)
{
  if (e == LV_EVENT_SHORT_CLICKED || e == LV_EVENT_LONG_PRESSED_REPEAT)
  {
    lv_spinbox_increment(spinbox);
    value_spinbox1 = lv_spinbox_get_value(spinbox);
    lv_gauge_set_value(gauge1, 0, value_spinbox1);
    Serial.println(value_spinbox1);
  }
}

static void lv_spinbox_decrement_event_cb(lv_obj_t * btn, lv_event_t e)
{
  if (e == LV_EVENT_SHORT_CLICKED || e == LV_EVENT_LONG_PRESSED_REPEAT)
  {
    lv_spinbox_decrement(spinbox);
    value_spinbox1 = lv_spinbox_get_value(spinbox);
    lv_gauge_set_value(gauge1, 0, value_spinbox1);
    Serial.println(value_spinbox1);
  }
}



void gui(void)
{
  lv_obj_t *tabview;
  tabview = lv_tabview_create(lv_scr_act(), NULL);


  lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Home");
  lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Auto");
  lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Einstel.");

  ///////////////////////////Page_1(Home)//////////////////////////////////////

  static lv_color_t needle_colors[3];
    needle_colors[0] = LV_COLOR_BLUE;
    needle_colors[1] = LV_COLOR_ORANGE;
    needle_colors[2] = LV_COLOR_PURPLE;


  gauge1 = lv_gauge_create(tab1, NULL);
  lv_obj_set_size(gauge1, 150, 150);
  lv_obj_align(gauge1, NULL, LV_ALIGN_CENTER, 70, 0);


  //lv_gauge_set_value(gauge1, 0, value_spinbox1);
  lv_gauge_set_value(gauge1, 1, 20);


  //////////SPINBOX////////////

  spinbox = lv_spinbox_create(tab1, NULL);
  lv_spinbox_set_range(spinbox, 0, 100);
  lv_spinbox_set_digit_format(spinbox,3, 3);
  lv_spinbox_step_prev(spinbox);
  lv_obj_set_width(spinbox, 60);
  lv_obj_align(spinbox, NULL, LV_ALIGN_CENTER, -90, -50);

  lv_coord_t h = lv_obj_get_height(spinbox);
  lv_obj_t * btn = lv_btn_create(tab1, NULL);
  lv_obj_set_size(btn, h, h);
  lv_obj_align(btn, spinbox, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
  lv_theme_apply(btn, LV_THEME_SPINBOX_BTN);
  lv_obj_set_style_local_value_str(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_PLUS);
  lv_obj_set_event_cb(btn, lv_spinbox_increment_event_cb);

  btn = lv_btn_create(tab1, btn);
  lv_obj_align(btn, spinbox, LV_ALIGN_OUT_LEFT_MID, -5, 0);
  lv_obj_set_event_cb(btn, lv_spinbox_decrement_event_cb);
  lv_obj_set_style_local_value_str(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_MINUS);

  //value_spinbox1=lv_spinbox_get_value(spinbox);


  //////////////////////PAGE_2////////////////////////////

  //////////////////Roller///////////////////////////////


  lv_obj_t *roller1 = lv_roller_create(tab2, NULL);
  lv_roller_set_options(roller1,
                        "Daimler\n"
                        "GM",
                        LV_ROLLER_MODE_NORMAL);

  lv_roller_set_visible_row_count(roller1, 4);
  lv_obj_align(roller1, NULL, LV_ALIGN_CENTER, -100, 0);
  lv_obj_set_event_cb(roller1, event_handler);

  state_roller = lv_roller_get_selected(roller1);

  //lv_obj_t *roller2 = lv_roller_create(tab2, NULL);
  //lv_obj_t *roller3 = lv_roller_create(tab2, NULL);
  if (state_roller == 0)
  {
    lv_obj_t *roller2 = lv_roller_create(tab2, NULL);
    lv_roller_set_options(roller2,
                          "Model 1\n"
                          "Model 2\n"
                          "Model 3",
                          LV_ROLLER_MODE_NORMAL);

    lv_roller_set_visible_row_count(roller2, 4);
    lv_obj_align(roller2, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(roller2, event_handler);
  }

  if (state_roller == 1)
  {
    //lv_obj_del(roller2);
    lv_obj_t *roller3 = lv_roller_create(tab2, NULL);
    lv_roller_set_options(roller3,
                          "ModelGM1\n"
                          "ModelGM2\n"
                          "ModelGM3",
                          LV_ROLLER_MODE_NORMAL);

    lv_roller_set_visible_row_count(roller3, 4);
    lv_obj_align(roller3, NULL, LV_ALIGN_CENTER, 100, 0);
    lv_obj_set_event_cb(roller3, event_handler);
  }




  ///////////////////////////////////////Tastatur/////////////////////////////

  /*Create a keyboard and apply the styles*/
  lv_obj_t *kb = lv_keyboard_create(tab3, NULL);
  lv_keyboard_set_cursor_manage(kb, true);

  /*Create a text area. The keyboard will write here*/
  lv_obj_t *ta = lv_textarea_create(tab3, NULL);
  lv_obj_align(ta, NULL, LV_ALIGN_IN_TOP_MID, 0, LV_DPI / 16);
  lv_textarea_set_text(ta, "");
  lv_coord_t max_h = LV_VER_RES / 2 - LV_DPI / 8;
  if (lv_obj_get_height(ta) > max_h) lv_obj_set_height(ta, max_h);

  /*Assign the text area to the keyboard*/
  lv_keyboard_set_textarea(kb, ta);
}







/* Display flushing */
/* 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);

  myGLCD.drawBitmap(area->x1, area->y1, w , h, (bitmapdatatype)color_p, 1);

  lv_disp_flush_ready(disp);
}


/*Read the touchpad*/
bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
{
  uint16_t touchX, touchY;


  myTouch.read();
  int x = myTouch.getX();
  int y = myTouch.getY();

  bool touched = false;


  if ((x != -1) and (y != -1)) touched = true;

  if (!touched)
  {
    data->state = LV_INDEV_STATE_PR;
  }
  else
  {
    data->state = LV_INDEV_STATE_REL;
    data->point.x = x;
    data->point.y = y;
  }

  return false; //Return `false` because we are not buffering and no more data to read
}


/* Reading input device (simulated encoder here) */
bool read_encoder(lv_indev_drv_t * indev, lv_indev_data_t * data)
{
  static int32_t last_diff = 0;
  int32_t diff = 0; /* Dummy - no movement */
  int btn_state = LV_INDEV_STATE_REL; /* Dummy - no press */

  data->enc_diff = diff - last_diff;;
  data->state = btn_state;

  last_diff = diff;

  return false;
}



void setup()
{

  Serial.begin(115200); /* prepare for possible serial debug */

  lv_init();
#if USE_LV_LOG != 0
  lv_log_register_print_cb(my_print); /* register print function for debugging */
#endif

  myGLCD.InitLCD();
  // -------------------------------------------------------------
  pinMode(8, OUTPUT);  //backlight
  digitalWrite(8, HIGH);//on
  // -------------------------------------------------------------
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);



  lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);

  /*Initialize the display*/
  lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  disp_drv.hor_res = 320;
  disp_drv.ver_res = 240;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.buffer = &disp_buf;
  lv_disp_drv_register(&disp_drv);


  /*Initialize the touch pad*/
  lv_indev_drv_t indev_drv;
  lv_indev_drv_init(&indev_drv);
  indev_drv.type = LV_INDEV_TYPE_POINTER;
  indev_drv.read_cb = my_touchpad_read;
  lv_indev_drv_register(&indev_drv);


  ///////////////////////////////////////////////////////////////////////////////////////////////////
  gui();
}

void timer_1(void)
{
  if (millis() % 5 > 1) //Every 5 ms
  {
    lv_tick_inc(5);
  }
}



void loop()
{
  timer_1();
  lv_task_handler(); /* let the GUI do its work */
  delay(4);
}


static void event_handler(lv_obj_t * obj, lv_event_t event)
{
  if (event == LV_EVENT_VALUE_CHANGED) {
    char buf[32];
    lv_roller_get_selected_str(obj, buf, sizeof(buf));
    state_roller = lv_roller_get_selected(obj);
    printf("Selected Brand: %s\n", buf);
    printf("Selected at Roller1: %d\n", state_roller);

  }
}
  if (!touched)
  {
    data->state = LV_INDEV_STATE_PR;
  }
  else
  {
    data->state = LV_INDEV_STATE_REL;
    data->point.x = x;
    data->point.y = y;
  }

You need to provide the coordinates with LV_INDEV_STATE_PR, not LV_INDEV_STATE_REL.

1 Like

Yes and data->state = LV_INDEV_STATE_PR musst be data->state = LV_INDEV_STATE_REL.

Thank you very much embedded!