lv_port_indev.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <stdbool.h>
  12. #include <rtdevice.h>
  13. #define UP_KEY 2
  14. #define DOWN_KEY 18
  15. #define LEFT_KEY 16
  16. #define RIGHT_KEY 20
  17. #define CRTL_KEY 3
  18. #define BUTTON0_PIN 2
  19. #define BUTTON1_PIN 18
  20. #define BUTTON2_PIN 16
  21. #define BUTTON_WKUP_PIN 20
  22. /*Test if `id` button is pressed or not*/
  23. static bool button_is_pressed(uint8_t id)
  24. {
  25. switch(id)
  26. {
  27. case 0:
  28. if(rt_pin_read(BUTTON0_PIN) == PIN_LOW)
  29. return true;
  30. break;
  31. case 1:
  32. if(rt_pin_read(BUTTON1_PIN) == PIN_LOW)
  33. return true;
  34. break;
  35. case 2:
  36. if(rt_pin_read(BUTTON2_PIN) == PIN_LOW)
  37. return true;
  38. break;
  39. case 3:
  40. if(rt_pin_read(BUTTON_WKUP_PIN) == PIN_LOW)
  41. return true;
  42. break;
  43. }
  44. return false;
  45. }
  46. static int8_t button_get_pressed_id(void)
  47. {
  48. uint8_t i;
  49. /*Check to buttons see which is being pressed*/
  50. for(i = 0; i < 4; i++)
  51. {
  52. /*Return the pressed button's ID*/
  53. if(button_is_pressed(i))
  54. {
  55. return i;
  56. }
  57. }
  58. /*No button pressed*/
  59. return -1;
  60. }
  61. void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
  62. {
  63. static uint32_t last_btn = 0; /*Store the last pressed button*/
  64. int btn_pr = button_get_pressed_id(); /*Get the ID (0,1,2...) of the pressed button*/
  65. if(btn_pr >= 0)
  66. { /*Is there a button press? (E.g. -1 indicated no button was pressed)*/
  67. last_btn = btn_pr; /*Save the ID of the pressed button*/
  68. data->state = LV_INDEV_STATE_PRESSED; /*Set the pressed state*/
  69. }
  70. else
  71. {
  72. data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/
  73. }
  74. data->btn_id = last_btn; /*Save the last button*/
  75. }
  76. lv_indev_t * button_indev;
  77. void lv_port_indev_init(void)
  78. {
  79. static lv_indev_drv_t indev_drv;
  80. /* Initialize the on-board buttons */
  81. rt_pin_mode(BUTTON0_PIN, PIN_MODE_INPUT);
  82. rt_pin_mode(BUTTON1_PIN, PIN_MODE_INPUT);
  83. rt_pin_mode(BUTTON2_PIN, PIN_MODE_INPUT);
  84. rt_pin_mode(BUTTON_WKUP_PIN, PIN_MODE_INPUT);
  85. lv_indev_drv_init(&indev_drv); /*Basic initialization*/
  86. indev_drv.type = LV_INDEV_TYPE_BUTTON;
  87. indev_drv.read_cb = button_read;
  88. /*Register the driver in LVGL and save the created input device object*/
  89. button_indev = lv_indev_drv_register(&indev_drv);
  90. }