key.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.type = RTGUI_KEYDOWN;
  38. if ( key_enter_GETVALUE() == 0 )
  39. {
  40. rt_thread_delay(next_delay);
  41. if (key_enter_GETVALUE() == 0)
  42. {
  43. /* HOME key */
  44. rt_kprintf("key_home\n");
  45. kbd_event.key = RTGUIK_HOME;
  46. }
  47. else
  48. {
  49. rt_kprintf("key_enter\n");
  50. kbd_event.key = RTGUIK_RETURN;
  51. }
  52. }
  53. if ( key_down_GETVALUE() == 0 )
  54. {
  55. rt_kprintf("key_down\n");
  56. kbd_event.key = RTGUIK_DOWN;
  57. }
  58. if ( key_up_GETVALUE() == 0 )
  59. {
  60. rt_kprintf("key_up\n");
  61. kbd_event.key = RTGUIK_UP;
  62. }
  63. if ( key_right_GETVALUE() == 0 )
  64. {
  65. rt_kprintf("key_right\n");
  66. kbd_event.key = RTGUIK_RIGHT;
  67. }
  68. if ( key_left_GETVALUE() == 0 )
  69. {
  70. rt_kprintf("key_left\n");
  71. kbd_event.key = RTGUIK_LEFT;
  72. }
  73. /* post down event */
  74. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  75. /* wait next key press */
  76. rt_thread_delay(next_delay);
  77. /* post up event */
  78. kbd_event.type = RTGUI_KEYUP;
  79. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  80. }
  81. }
  82. void rt_hw_key_init()
  83. {
  84. rt_thread_t key_tid;
  85. key_tid = rt_thread_create("key",
  86. key_thread_entry, RT_NULL,
  87. 512, 30, 5);
  88. if (key_tid != RT_NULL) rt_thread_startup(key_tid);
  89. }