key.c 3.4 KB

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