lv_port_indev.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. lv_indev_t * button_indev;
  23. /*Test if `id` button is pressed or not*/
  24. static bool button_is_pressed(uint8_t id)
  25. {
  26. switch(id)
  27. {
  28. case 0:
  29. if(rt_pin_read(BUTTON0_PIN) == PIN_LOW)
  30. return true;
  31. break;
  32. case 1:
  33. if(rt_pin_read(BUTTON1_PIN) == PIN_LOW)
  34. return true;
  35. break;
  36. case 2:
  37. if(rt_pin_read(BUTTON2_PIN) == PIN_LOW)
  38. return true;
  39. break;
  40. case 3:
  41. if(rt_pin_read(BUTTON_WKUP_PIN) == PIN_LOW)
  42. return true;
  43. break;
  44. }
  45. return false;
  46. }
  47. static int8_t button_get_pressed_id(void)
  48. {
  49. uint8_t i;
  50. /*Check to buttons see which is being pressed*/
  51. for(i = 0; i < 4; i++)
  52. {
  53. /*Return the pressed button's ID*/
  54. if(button_is_pressed(i))
  55. {
  56. return i;
  57. }
  58. }
  59. /*No button pressed*/
  60. return -1;
  61. }
  62. void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
  63. {
  64. static uint32_t last_btn = 0; /*Store the last pressed button*/
  65. int btn_pr = button_get_pressed_id(); /*Get the ID (0,1,2...) of the pressed button*/
  66. if(btn_pr >= 0)
  67. { /*Is there a button press? (E.g. -1 indicated no button was pressed)*/
  68. last_btn = btn_pr; /*Save the ID of the pressed button*/
  69. data->state = LV_INDEV_STATE_PRESSED; /*Set the pressed state*/
  70. }
  71. else
  72. {
  73. data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/
  74. }
  75. data->btn_id = last_btn; /*Save the last button*/
  76. }
  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. }