key.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <rtthread.h>
  2. #include <stm32f10x.h>
  3. #include <rtgui/event.h>
  4. #include <rtgui/rtgui_server.h>
  5. /*
  6. key_enter PF11
  7. key_down PG15
  8. key_up PG11
  9. key_right PG14
  10. key_left PG13
  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. /* from remote.c */
  18. extern void rem_start(void);
  19. extern void rem_encoder(struct rtgui_event_kbd * p);
  20. extern unsigned int rem_mode;
  21. /* from remote.c */
  22. static void GPIO_Configuration(void)
  23. {
  24. GPIO_InitTypeDef GPIO_InitStructure;
  25. /* init gpio configuration */
  26. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG | RCC_APB2Periph_GPIOE,ENABLE);
  27. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  30. GPIO_Init(GPIOG,&GPIO_InitStructure);
  31. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  32. GPIO_Init(GPIOF,&GPIO_InitStructure);
  33. /* PE2 LED */
  34. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  36. GPIO_Init(GPIOE,&GPIO_InitStructure);
  37. GPIO_SetBits(GPIOE,GPIO_Pin_2);
  38. }
  39. static void key_thread_entry(void *parameter)
  40. {
  41. rt_time_t next_delay;
  42. struct rtgui_event_kbd kbd_event;
  43. GPIO_Configuration();
  44. /* start remote */
  45. rem_start();
  46. /* init keyboard event */
  47. RTGUI_EVENT_KBD_INIT(&kbd_event);
  48. kbd_event.mod = RTGUI_KMOD_NONE;
  49. kbd_event.unicode = 0;
  50. while (1)
  51. {
  52. next_delay = 10;
  53. kbd_event.key = RTGUIK_UNKNOWN;
  54. kbd_event.type = RTGUI_KEYDOWN;
  55. if( rem_mode== 2 )
  56. {
  57. rem_encoder(&kbd_event);
  58. }
  59. if ( key_enter_GETVALUE() == 0 )
  60. {
  61. rt_thread_delay( next_delay*4 );
  62. if (key_enter_GETVALUE() == 0)
  63. {
  64. /* HOME key */
  65. rt_kprintf("key_home\n");
  66. kbd_event.key = RTGUIK_HOME;
  67. }
  68. else
  69. {
  70. rt_kprintf("key_enter\n");
  71. kbd_event.key = RTGUIK_RETURN;
  72. }
  73. }
  74. if ( key_down_GETVALUE() == 0 )
  75. {
  76. rt_kprintf("key_down\n");
  77. kbd_event.key = RTGUIK_DOWN;
  78. }
  79. if ( key_up_GETVALUE() == 0 )
  80. {
  81. rt_kprintf("key_up\n");
  82. kbd_event.key = RTGUIK_UP;
  83. }
  84. if ( key_right_GETVALUE() == 0 )
  85. {
  86. rt_kprintf("key_right\n");
  87. kbd_event.key = RTGUIK_RIGHT;
  88. }
  89. if ( key_left_GETVALUE() == 0 )
  90. {
  91. rt_kprintf("key_left\n");
  92. kbd_event.key = RTGUIK_LEFT;
  93. }
  94. if (kbd_event.key != RTGUIK_UNKNOWN)
  95. {
  96. /* post down event */
  97. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  98. next_delay = 10;
  99. /* delay to post up event */
  100. rt_thread_delay(next_delay);
  101. /* post up event */
  102. kbd_event.type = RTGUI_KEYUP;
  103. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  104. }
  105. /* wait next key press */
  106. rt_thread_delay(next_delay);
  107. }
  108. }
  109. static rt_thread_t key_tid;
  110. void rt_hw_key_init(void)
  111. {
  112. key_tid = rt_thread_create("key",
  113. key_thread_entry, RT_NULL,
  114. 768, 30, 5);
  115. if (key_tid != RT_NULL) rt_thread_startup(key_tid);
  116. }