key.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "common.h"
  2. HSKey Key[4];
  3. void KeyInit(void)
  4. {
  5. Key[KEY_SELECT].longPressTimeOne = 2;
  6. Key[KEY_UP].longPressTimeKeep = 2;
  7. //Key[KEY_COUNTER].longPressTimeKeep = 4;
  8. }
  9. //50ms key polling
  10. void ReadKEY(void)
  11. {
  12. if( SystemTimer.TIM_KEY < 45 )
  13. return;
  14. SystemTimer.TIM_KEY = 0;
  15. //////////////////////////////////////////////////////////////////////////////////////////
  16. if( nrf_gpio_pin_read(BSP_BUTTON_2) == false )
  17. {
  18. if( Key[KEY_SELECT].softRelease == 1 )
  19. {
  20. return;
  21. }
  22. if( Key[KEY_SELECT].onFlag == 0 )
  23. {
  24. #ifdef ENABLE_USART
  25. //printf("KEY_NUMUP\r\n");
  26. #endif
  27. KeySelectPressed();
  28. }else{
  29. if( Key[KEY_SELECT].longPressTimeKeep != 0 )
  30. {
  31. if( Key[KEY_SELECT].pressedSec >= Key[KEY_SELECT].longPressTimeKeep )
  32. {
  33. KeySelectLongPressedKeep();
  34. }
  35. }
  36. if( Key[KEY_SELECT].pressTime%11 == 0 ) // count up 1 sec
  37. {
  38. #ifdef ENABLE_USART
  39. //printf("KEY_NUMUP Press Time_%d\r\n", (unsigned int)Key[0].pressedSec);
  40. #endif
  41. Key[KEY_SELECT].pressedSec++;
  42. if( Key[KEY_SELECT].longPressTimeOne != 0 )
  43. {
  44. if( Key[KEY_SELECT].pressedSec >= Key[KEY_SELECT].longPressTimeOne )
  45. {
  46. KeySelectLongPressedOne();
  47. Key[KEY_SELECT].softRelease = 1;
  48. }
  49. }
  50. }
  51. }
  52. Key[KEY_SELECT].onFlag = 1;
  53. Key[KEY_SELECT].pressTime++;
  54. }else{
  55. if( Key[KEY_SELECT].onFlag == 1 )
  56. {
  57. //printf("KEY_NUMUP_%d\r\n", (unsigned int)Key[0].pressTime);
  58. KeySelectReleased();
  59. }
  60. Key[KEY_SELECT].pressTime = 0;
  61. Key[KEY_SELECT].onFlag = 0;
  62. Key[KEY_SELECT].pressedSec = 0;
  63. Key[KEY_SELECT].softRelease = 0;
  64. Key[KEY_SELECT].prevTime = 0;
  65. }
  66. //////////////////////////////////////////////////////////////////////////////////////////
  67. if( nrf_gpio_pin_read(BSP_BUTTON_1) == false )
  68. {
  69. if( Key[KEY_UP].softRelease == 1 )
  70. {
  71. return;
  72. }
  73. if( Key[KEY_UP].onFlag == 0 )
  74. {
  75. #ifdef ENABLE_USART
  76. //printf("KEY_NUMUP\r\n");
  77. #endif
  78. KeyUpPressed();
  79. }else{
  80. if( Key[KEY_UP].longPressTimeKeep != 0 )
  81. {
  82. if( Key[KEY_UP].pressedSec >= Key[KEY_UP].longPressTimeKeep )
  83. {
  84. KeyUpLongPressedKeep();
  85. }
  86. }
  87. if( Key[KEY_UP].pressTime%11 == 0 ) // count up 1 sec
  88. {
  89. #ifdef ENABLE_USART
  90. //printf("KEY_NUMUP Press Time_%d\r\n", (unsigned int)Key[0].pressedSec);
  91. #endif
  92. Key[KEY_UP].pressedSec++;
  93. if( Key[KEY_UP].longPressTimeOne != 0 )
  94. {
  95. if( Key[KEY_UP].pressedSec >= Key[KEY_UP].longPressTimeOne )
  96. {
  97. KeyUpLongPressedOne();
  98. Key[KEY_UP].softRelease = 1;
  99. }
  100. }
  101. }
  102. }
  103. Key[KEY_UP].onFlag = 1;
  104. Key[KEY_UP].pressTime++;
  105. }else{
  106. if( Key[KEY_UP].onFlag == 1 )
  107. {
  108. //printf("KEY_NUMUP_%d\r\n", (unsigned int)Key[0].pressTime);
  109. KeyUpReleased();
  110. }
  111. Key[KEY_UP].pressTime = 0;
  112. Key[KEY_UP].onFlag = 0;
  113. Key[KEY_UP].pressedSec = 0;
  114. Key[KEY_UP].softRelease = 0;
  115. Key[KEY_UP].prevTime = 0;
  116. }
  117. }
  118. void KeySelectPressed(void)
  119. {
  120. DBGPrint("KeySelectPressed\r\n" );
  121. }
  122. void KeySelectReleased(void)
  123. {
  124. DBGPrint("KeySelectReleased\r\n" );
  125. }
  126. void KeySelectLongPressedOne(void)
  127. {
  128. DBGPrint("KeySelectLongPressedOne\r\n" );
  129. }
  130. void KeySelectLongPressedKeep(void)
  131. {
  132. DBGPrint("KeySelectLongPressedKeep\r\n" );
  133. }
  134. void KeyUpPressed(void)
  135. {
  136. DBGPrint("KeyUpPressed\r\n" );
  137. }
  138. void KeyUpReleased(void)
  139. {
  140. DBGPrint("KeyUpReleased\r\n" );
  141. }
  142. void KeyUpLongPressedOne(void)
  143. {
  144. DBGPrint("KeyUpLongPressedOne\r\n" );
  145. }
  146. void KeyUpLongPressedKeep(void)
  147. {
  148. DBGPrint("KeyUpLongPressedKeep\r\n" );
  149. }