lv_port_indev.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-18 Meco Man The first version
  9. */
  10. #include <lvgl.h>
  11. #include <rtdevice.h>
  12. #include <drv_gpio.h>
  13. #define BUTTON0_PIN GET_PIN(D, 10)
  14. #define BUTTON1_PIN GET_PIN(D, 9)
  15. #define BUTTON2_PIN GET_PIN(D, 8)
  16. #define BUTTON_WKUP_PIN GET_PIN(C, 13)
  17. lv_indev_t * button_indev;
  18. /*Test if `id` button is pressed or not*/
  19. static bool button_is_pressed(uint8_t id)
  20. {
  21. switch(id)
  22. {
  23. case 0:
  24. if(rt_pin_read(BUTTON0_PIN) == PIN_LOW)
  25. return true;
  26. break;
  27. case 1:
  28. if(rt_pin_read(BUTTON1_PIN) == PIN_LOW)
  29. return true;
  30. break;
  31. case 2:
  32. if(rt_pin_read(BUTTON2_PIN) == PIN_LOW)
  33. return true;
  34. break;
  35. case 3:
  36. if(rt_pin_read(BUTTON_WKUP_PIN) == PIN_HIGH)
  37. return true;
  38. break;
  39. }
  40. return false;
  41. }
  42. static int8_t button_get_pressed_id(void)
  43. {
  44. uint8_t i;
  45. /*Check to buttons see which is being pressed*/
  46. for(i = 0; i < 4; i++)
  47. {
  48. /*Return the pressed button's ID*/
  49. if(button_is_pressed(i))
  50. {
  51. return i;
  52. }
  53. }
  54. /*No button pressed*/
  55. return -1;
  56. }
  57. void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
  58. {
  59. static uint32_t last_btn = 0; /*Store the last pressed button*/
  60. int btn_pr = button_get_pressed_id(); /*Get the ID (0,1,2...) of the pressed button*/
  61. if(btn_pr >= 0)
  62. { /*Is there a button press? (E.g. -1 indicated no button was pressed)*/
  63. last_btn = btn_pr; /*Save the ID of the pressed button*/
  64. data->state = LV_INDEV_STATE_PRESSED; /*Set the pressed state*/
  65. }
  66. else
  67. {
  68. data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/
  69. }
  70. data->btn_id = last_btn; /*Save the last button*/
  71. }
  72. void lv_port_indev_init(void)
  73. {
  74. static lv_indev_drv_t indev_drv;
  75. /*assign buttons to coordinates*/
  76. static lv_point_t points_array[] = {{200,35},{0,0},{70,35},{0,0}};
  77. /* Initialize the on-board buttons */
  78. rt_pin_mode(BUTTON0_PIN, PIN_MODE_INPUT);
  79. rt_pin_mode(BUTTON1_PIN, PIN_MODE_INPUT);
  80. rt_pin_mode(BUTTON2_PIN, PIN_MODE_INPUT);
  81. rt_pin_mode(BUTTON_WKUP_PIN, PIN_MODE_INPUT);
  82. lv_indev_drv_init(&indev_drv); /*Basic initialization*/
  83. indev_drv.type = LV_INDEV_TYPE_BUTTON;
  84. indev_drv.read_cb = button_read;
  85. /*Register the driver in LVGL and save the created input device object*/
  86. button_indev = lv_indev_drv_register(&indev_drv);
  87. lv_indev_set_button_points(button_indev, points_array);
  88. }