key.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * 2011-03-03 lgnq
  9. */
  10. #include <rtthread.h>
  11. #include "key.h"
  12. #include "lcd.h"
  13. #ifdef RT_USING_RTGUI
  14. #include <rtgui/event.h>
  15. #include <rtgui/rtgui_server.h>
  16. #endif
  17. static void key_io_init(void)
  18. {
  19. /*Select CPIO function*/
  20. KEY_PFR &= ~KEY_MASK;
  21. /*Set CPIO Pull-Up function*/
  22. KEY_PCR |= KEY_MASK;
  23. /*Make button pins inputs*/
  24. KEY_DDR &= ~KEY_MASK;
  25. }
  26. static void key_thread_entry(void *parameter)
  27. {
  28. #ifdef RT_USING_RTGUI
  29. rt_time_t next_delay;
  30. rt_uint8_t i;
  31. struct rtgui_event_kbd kbd_event;
  32. key_io_init();
  33. /* init keyboard event */
  34. RTGUI_EVENT_KBD_INIT(&kbd_event);
  35. kbd_event.mod = RTGUI_KMOD_NONE;
  36. kbd_event.unicode = 0;
  37. while (1)
  38. {
  39. next_delay = RT_TICK_PER_SECOND/10;
  40. kbd_event.key = RTGUIK_UNKNOWN;
  41. kbd_event.type = RTGUI_KEYDOWN;
  42. if (KEY_ENTER_GETVALUE() == 0 )
  43. {
  44. for(i=0; ; i++)
  45. {
  46. rt_thread_delay( next_delay );
  47. if (KEY_ENTER_GETVALUE() == 0)
  48. {
  49. if (i>=4)
  50. {
  51. /* HOME key */
  52. kbd_event.key = RTGUIK_HOME;
  53. next_delay = RT_TICK_PER_SECOND/5;
  54. break;
  55. }
  56. }
  57. else
  58. {
  59. kbd_event.key = RTGUIK_RETURN;
  60. break;
  61. }
  62. }
  63. }
  64. if (KEY_DOWN_GETVALUE() == 0)
  65. {
  66. kbd_event.key = RTGUIK_DOWN;
  67. }
  68. if (KEY_UP_GETVALUE() == 0)
  69. {
  70. kbd_event.key = RTGUIK_UP;
  71. }
  72. if (KEY_RIGHT_GETVALUE() == 0)
  73. {
  74. kbd_event.key = RTGUIK_RIGHT;
  75. }
  76. if (KEY_LEFT_GETVALUE() == 0)
  77. {
  78. kbd_event.key = RTGUIK_LEFT;
  79. }
  80. if (kbd_event.key != RTGUIK_UNKNOWN)
  81. {
  82. /* post down event */
  83. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  84. }
  85. else
  86. {
  87. kbd_event.type = RTGUI_KEYUP;
  88. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  89. }
  90. /* wait next key press */
  91. rt_thread_delay(next_delay);
  92. }
  93. #else
  94. extern struct rt_messagequeue mq;
  95. rt_time_t next_delay;
  96. struct lcd_msg msg;
  97. msg.type = KEY_MSG;
  98. key_io_init();
  99. while (1)
  100. {
  101. msg.key = NO_KEY;
  102. next_delay = RT_TICK_PER_SECOND/10;
  103. if (KEY_ENTER_GETVALUE() == 0 )
  104. {
  105. msg.key = KEY_ENTER;
  106. }
  107. if (KEY_DOWN_GETVALUE() == 0)
  108. {
  109. msg.key = KEY_DOWN;
  110. }
  111. if (KEY_UP_GETVALUE() == 0)
  112. {
  113. msg.key = KEY_UP;
  114. }
  115. if (KEY_RIGHT_GETVALUE() == 0)
  116. {
  117. msg.key = KEY_RIGHT;
  118. }
  119. if (KEY_LEFT_GETVALUE() == 0)
  120. {
  121. msg.key = KEY_LEFT;
  122. }
  123. rt_mq_send(&mq, &msg, sizeof(msg));
  124. /* wait next key press */
  125. rt_thread_delay(next_delay);
  126. }
  127. #endif
  128. }
  129. static rt_thread_t key_thread;
  130. void rt_hw_key_init(void)
  131. {
  132. key_thread = rt_thread_create("key", key_thread_entry, RT_NULL, 384, 28, 5);
  133. if (key_thread != RT_NULL)
  134. rt_thread_startup(key_thread);
  135. }