key.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <rtthread.h>
  2. #include <stm32f10x.h>
  3. #include <rtgui/event.h>
  4. #include <rtgui/rtgui_server.h>
  5. /*
  6. key_enter PA0
  7. key_down PA1
  8. key_up PA2
  9. key_right PC2
  10. key_left PC3
  11. */
  12. #define key_enter_GETVALUE() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
  13. #define key_down_GETVALUE() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)
  14. #define key_up_GETVALUE() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)
  15. #define key_right_GETVALUE() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)
  16. #define key_left_GETVALUE() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)
  17. static void key_thread_entry(void *parameter)
  18. {
  19. rt_time_t next_delay;
  20. struct rtgui_event_kbd kbd_event;
  21. GPIO_InitTypeDef GPIO_InitStructure;
  22. /* init gpio configuration */
  23. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC,ENABLE);
  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
  27. GPIO_Init(GPIOA,&GPIO_InitStructure);
  28. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  29. GPIO_Init(GPIOC,&GPIO_InitStructure);
  30. /* init keyboard event */
  31. RTGUI_EVENT_KBD_INIT(&kbd_event);
  32. kbd_event.mod = RTGUI_KMOD_NONE;
  33. kbd_event.unicode = 0;
  34. while (1)
  35. {
  36. next_delay = 20;
  37. kbd_event.key = RTGUIK_UNKNOWN;
  38. kbd_event.type = RTGUI_KEYDOWN;
  39. if ( key_enter_GETVALUE() == 0 )
  40. {
  41. rt_thread_delay(next_delay);
  42. if (key_enter_GETVALUE() == 0)
  43. {
  44. /* HOME key */
  45. rt_kprintf("key_home\n");
  46. kbd_event.key = RTGUIK_HOME;
  47. }
  48. else
  49. {
  50. rt_kprintf("key_enter\n");
  51. kbd_event.key = RTGUIK_RETURN;
  52. }
  53. }
  54. if ( key_down_GETVALUE() == 0 )
  55. {
  56. rt_kprintf("key_down\n");
  57. kbd_event.key = RTGUIK_DOWN;
  58. }
  59. if ( key_up_GETVALUE() == 0 )
  60. {
  61. rt_kprintf("key_up\n");
  62. kbd_event.key = RTGUIK_UP;
  63. }
  64. if ( key_right_GETVALUE() == 0 )
  65. {
  66. rt_kprintf("key_right\n");
  67. kbd_event.key = RTGUIK_RIGHT;
  68. }
  69. if ( key_left_GETVALUE() == 0 )
  70. {
  71. rt_kprintf("key_left\n");
  72. kbd_event.key = RTGUIK_LEFT;
  73. }
  74. if (kbd_event.key != RTGUIK_UNKNOWN)
  75. {
  76. /* post down event */
  77. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  78. next_delay = 10;
  79. /* delay to post up event */
  80. rt_thread_delay(next_delay);
  81. /* post up event */
  82. kbd_event.type = RTGUI_KEYUP;
  83. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  84. }
  85. /* wait next key press */
  86. rt_thread_delay(next_delay);
  87. }
  88. }
  89. void rt_hw_key_init()
  90. {
  91. rt_thread_t key_tid;
  92. key_tid = rt_thread_create("key",
  93. key_thread_entry, RT_NULL,
  94. 512, 30, 5);
  95. if (key_tid != RT_NULL) rt_thread_startup(key_tid);
  96. }