drv_key.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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-04-17 Meco Man first version
  9. */
  10. #include <rtthread.h>
  11. #ifdef BSP_USING_KEY
  12. #define DBG_TAG "KEY"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #include <rtdevice.h>
  16. #include <drv_gpio.h>
  17. #include <multi_button.h>
  18. #define KEY0_PIN GET_PIN(D, 10)
  19. #define KEY1_PIN GET_PIN(D, 9)
  20. #define KEY2_PIN GET_PIN(D, 8)
  21. #define KEY_WKUP_PIN GET_PIN(C, 13)
  22. static struct rt_timer key_timer;
  23. static struct button key0;
  24. static struct button key1;
  25. static struct button key2;
  26. static struct button key_wkup;
  27. /*---- user codes area begin ----*/
  28. /*users can modify according to needs*/
  29. static void key0_BtnCallback(void* state)
  30. {
  31. rt_kprintf("key0!\n");
  32. }
  33. static void key1_BtnCallback(void* state)
  34. {
  35. rt_kprintf("key1!\n");
  36. }
  37. static void key2_BtnCallback(void* state)
  38. {
  39. rt_kprintf("key2!\n");
  40. }
  41. static void key_wkup_BtnCallback(void* state)
  42. {
  43. rt_kprintf("key wkup!\n");
  44. }
  45. /*---- user codes area end ----*/
  46. static void _cb_key_timer(void *parameter)
  47. {
  48. button_ticks();
  49. }
  50. static uint8_t _cb_key0_pin_level(void)
  51. {
  52. return rt_pin_read(KEY0_PIN);
  53. }
  54. static uint8_t _cb_key1_pin_level(void)
  55. {
  56. return rt_pin_read(KEY1_PIN);
  57. }
  58. static uint8_t _cb_key2_pin_level(void)
  59. {
  60. return rt_pin_read(KEY2_PIN);
  61. }
  62. static uint8_t _cb_key_wkup_pin_level(void)
  63. {
  64. return rt_pin_read(KEY_WKUP_PIN);
  65. }
  66. static int onboard_key_init(void)
  67. {
  68. rt_timer_init(&key_timer,
  69. "key timer",
  70. _cb_key_timer,
  71. RT_NULL,
  72. rt_tick_from_millisecond(TICKS_INTERVAL),
  73. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  74. if(rt_timer_start(&key_timer) < 0)
  75. {
  76. LOG_E("drv_key timer initialization failed");
  77. return -1;
  78. }
  79. rt_pin_mode(KEY0_PIN, PIN_MODE_INPUT);
  80. rt_pin_mode(KEY1_PIN, PIN_MODE_INPUT);
  81. rt_pin_mode(KEY2_PIN, PIN_MODE_INPUT);
  82. rt_pin_mode(KEY_WKUP_PIN, PIN_MODE_INPUT);
  83. button_init(&key0, _cb_key0_pin_level, PIN_LOW);
  84. button_init(&key1, _cb_key1_pin_level, PIN_LOW);
  85. button_init(&key2, _cb_key2_pin_level, PIN_LOW);
  86. button_init(&key_wkup, _cb_key_wkup_pin_level, PIN_HIGH);
  87. /*---- user codes area begin ----*/
  88. /*users can modify according to needs*/
  89. button_attach(&key0, PRESS_DOWN, key0_BtnCallback);
  90. button_attach(&key1, PRESS_DOWN, key1_BtnCallback);
  91. button_attach(&key2, PRESS_DOWN, key2_BtnCallback);
  92. button_attach(&key_wkup, PRESS_UP, key_wkup_BtnCallback);
  93. /*---- user codes area end ----*/
  94. button_start(&key0);
  95. button_start(&key1);
  96. button_start(&key2);
  97. button_start(&key_wkup);
  98. return 0;
  99. }
  100. INIT_APP_EXPORT(onboard_key_init);
  101. #endif