key.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(GPIOF,GPIO_Pin_11)
  13. #define key_down_GETVALUE() GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_15)
  14. #define key_up_GETVALUE() GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_11)
  15. #define key_right_GETVALUE() GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_14)
  16. #define key_left_GETVALUE() GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_13)
  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_GPIOG | RCC_APB2Periph_GPIOE,ENABLE);
  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  27. GPIO_Init(GPIOG,&GPIO_InitStructure);
  28. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  29. GPIO_Init(GPIOF,&GPIO_InitStructure);
  30. /* PE2 LED */
  31. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  33. GPIO_Init(GPIOE,&GPIO_InitStructure);
  34. GPIO_SetBits(GPIOE,GPIO_Pin_2);
  35. /* init keyboard event */
  36. RTGUI_EVENT_KBD_INIT(&kbd_event);
  37. kbd_event.mod = RTGUI_KMOD_NONE;
  38. kbd_event.unicode = 0;
  39. while (1)
  40. {
  41. next_delay = 10;
  42. kbd_event.key = RTGUIK_UNKNOWN;
  43. kbd_event.type = RTGUI_KEYDOWN;
  44. if ( key_enter_GETVALUE() == 0 )
  45. {
  46. rt_thread_delay( next_delay*4 );
  47. if (key_enter_GETVALUE() == 0)
  48. {
  49. /* HOME key */
  50. rt_kprintf("key_home\n");
  51. kbd_event.key = RTGUIK_HOME;
  52. }
  53. else
  54. {
  55. rt_kprintf("key_enter\n");
  56. kbd_event.key = RTGUIK_RETURN;
  57. }
  58. }
  59. if ( key_down_GETVALUE() == 0 )
  60. {
  61. rt_kprintf("key_down\n");
  62. kbd_event.key = RTGUIK_DOWN;
  63. }
  64. if ( key_up_GETVALUE() == 0 )
  65. {
  66. rt_kprintf("key_up\n");
  67. kbd_event.key = RTGUIK_UP;
  68. }
  69. if ( key_right_GETVALUE() == 0 )
  70. {
  71. rt_kprintf("key_right\n");
  72. kbd_event.key = RTGUIK_RIGHT;
  73. }
  74. if ( key_left_GETVALUE() == 0 )
  75. {
  76. rt_kprintf("key_left\n");
  77. kbd_event.key = RTGUIK_LEFT;
  78. }
  79. if (kbd_event.key != RTGUIK_UNKNOWN)
  80. {
  81. /* post down event */
  82. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  83. next_delay = 10;
  84. /* delay to post up event */
  85. rt_thread_delay(next_delay);
  86. /* post up event */
  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. }
  94. void rt_hw_key_init()
  95. {
  96. rt_thread_t key_tid;
  97. key_tid = rt_thread_create("key",
  98. key_thread_entry, RT_NULL,
  99. 512, 30, 5);
  100. if (key_tid != RT_NULL) rt_thread_startup(key_tid);
  101. }