keypad.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include "FreeRTOS.h"
  5. #include "chip.h"
  6. #include "board.h"
  7. #include "keypad.h"
  8. typedef int16_t lv_coord_t;
  9. typedef uint8_t lv_indev_state_t;
  10. typedef struct {
  11. lv_coord_t x;
  12. lv_coord_t y;
  13. } lv_point_t;
  14. enum { LV_INDEV_STATE_REL = 0, LV_INDEV_STATE_PR };
  15. enum {
  16. LV_KEY_UP = 17, /*0x11*/
  17. LV_KEY_DOWN = 18, /*0x12*/
  18. LV_KEY_RIGHT = 19, /*0x13*/
  19. LV_KEY_LEFT = 20, /*0x14*/
  20. LV_KEY_ESC = 27, /*0x1B*/
  21. LV_KEY_DEL = 127, /*0x7F*/
  22. LV_KEY_BACKSPACE = 8, /*0x08*/
  23. LV_KEY_ENTER = 10, /*0x0A, '\n'*/
  24. LV_KEY_NEXT = 9, /*0x09, '\t'*/
  25. LV_KEY_PREV = 11, /*0x0B, '*/
  26. LV_KEY_HOME = 2, /*0x02, STX*/
  27. LV_KEY_END = 3, /*0x03, ETX*/
  28. };
  29. typedef struct {
  30. lv_point_t point; /**< For LV_INDEV_TYPE_POINTER the currently pressed point*/
  31. uint32_t key; /**< For LV_INDEV_TYPE_KEYPAD the currently pressed key*/
  32. uint32_t btn_id; /**< For LV_INDEV_TYPE_BUTTON the currently pressed button*/
  33. int16_t enc_diff; /**< For LV_INDEV_TYPE_ENCODER number of steps since the previous read*/
  34. lv_indev_state_t state; /**< LV_INDEV_STATE_REL or LV_INDEV_STATE_PR*/
  35. } lv_indev_data_t;
  36. #define KEYAD_0 100
  37. #define KEYAD_1 622
  38. #define KEYAD_2 1132
  39. #define KEYAD_3 1652
  40. #define KEYAD_4 2260
  41. #define KEYAD_5 2845
  42. #define KEYAD_6 100
  43. #define KEYAD_7 600
  44. #define KEYAD_8 1119
  45. #define KEYAD_9 1621
  46. #define KEYAD_NUM 10
  47. #define KEYAD_RANGE 100
  48. static int KeyAdcValue[KEYAD_NUM] =
  49. {
  50. KEYAD_0,
  51. KEYAD_1,
  52. KEYAD_2,
  53. KEYAD_3,
  54. KEYAD_4,
  55. KEYAD_5,
  56. KEYAD_6,
  57. KEYAD_7,
  58. KEYAD_8,
  59. KEYAD_9,
  60. };
  61. static unsigned int KeypadMap[KEYAD_NUM] =
  62. {
  63. LV_KEY_HOME,
  64. LV_KEY_ENTER,
  65. LV_KEY_ESC,
  66. LV_KEY_UP,
  67. LV_KEY_DOWN,
  68. LV_KEY_LEFT,
  69. LV_KEY_RIGHT,
  70. };
  71. #define NULL_KEY 0xFFFF
  72. #define KEY_STATE_IDLE 0
  73. #define KEY_STATE_START 1
  74. #define KEY_STATE_PRESS 2
  75. static UINT32 lg_ulKeyMachineState = KEY_STATE_IDLE;
  76. static UINT32 lg_ulLastKey = NULL_KEY;
  77. #define KEY_POOL_SIZE 8
  78. static UINT32 lg_arrKeyAvgPool[KEY_POOL_SIZE];
  79. static UINT32 lg_ulKeyIndex = 0;
  80. char test_key_value = 0;
  81. static TaskHandle_t key_task;
  82. uint32_t key_value = 0;
  83. uint32_t key_value_status = 0;
  84. extern void SendKeypadInputEventFromISR(lv_indev_data_t *indata);
  85. static void PushKeyToAVGPool(UINT32 ulKeyValue)
  86. {
  87. if(lg_ulKeyIndex < KEY_POOL_SIZE)
  88. {
  89. lg_arrKeyAvgPool[lg_ulKeyIndex] = ulKeyValue;
  90. lg_ulKeyIndex++;
  91. }
  92. }
  93. static UINT32 AVGPoolIsFull(void)
  94. {
  95. return lg_ulKeyIndex == KEY_POOL_SIZE;
  96. }
  97. static UINT32 GetKeyAVGValue(void)
  98. {
  99. UINT32 i;
  100. UINT32 ulSum;
  101. ulSum = 0;
  102. for(i=0;i<lg_ulKeyIndex;i++)
  103. {
  104. ulSum += lg_arrKeyAvgPool[i];
  105. }
  106. return ulSum/lg_ulKeyIndex;
  107. }
  108. static void ResetKeyAVGPool(void)
  109. {
  110. lg_ulKeyIndex = 0;
  111. }
  112. static UINT32 CheckKey(UINT32 id, UINT32 ulSampleValue)
  113. {
  114. UINT32 i;
  115. for(i = 0; i < (sizeof(KeyAdcValue) / sizeof(KeyAdcValue[0])); i++ )
  116. {
  117. if((ulSampleValue >= KeyAdcValue[i] - KEYAD_RANGE) && (ulSampleValue <= KeyAdcValue[i] + KEYAD_RANGE))
  118. {
  119. return i + (id * 6);
  120. }
  121. }
  122. return NULL_KEY;
  123. }
  124. static void SendKeyPressToMcu(void *param)
  125. {
  126. uint32_t ulNotifiedValue;
  127. for(;;){
  128. xTaskNotifyWait(0x00, 0xffffffff, &ulNotifiedValue, portMAX_DELAY);
  129. if(mailbox_msg_send(MB_MSG_T_DCIC_DETECT, NULL, 0, portMAX_DELAY))
  130. printf("%s, mailbox send fail.\n", __func__);
  131. }
  132. }
  133. static void SendKeyPress(UINT32 key)
  134. {
  135. if(key == 9)
  136. xTaskNotifyFromISR(key_task, 0, eSetValueWithOverwrite, 0);
  137. key_value = key+1;
  138. key_value_status = 1;
  139. }
  140. static void SendKeyRelease(UINT32 key)
  141. {
  142. key_value = key+1;
  143. key_value_status = 0;
  144. }
  145. static void KeyEventHandler(UINT32 adc_id, UINT32 ulEvent, UINT32 lpParam, UINT32 wParam)
  146. {
  147. switch(lg_ulKeyMachineState)
  148. {
  149. case KEY_STATE_IDLE:
  150. if(ulEvent == KEY_START_EVENT)
  151. {
  152. lg_ulKeyMachineState = KEY_STATE_START;
  153. }
  154. break;
  155. case KEY_STATE_START:
  156. test_key_value = !test_key_value;
  157. if(ulEvent == KEY_SAMPLE_EVENT)
  158. {
  159. ResetKeyAVGPool();
  160. PushKeyToAVGPool(lpParam);
  161. lg_ulKeyMachineState = KEY_STATE_PRESS;
  162. }
  163. else if(ulEvent == KEY_STOP_EVENT)
  164. {
  165. lg_ulKeyMachineState = KEY_STATE_IDLE;
  166. lg_ulLastKey = NULL_KEY;
  167. }
  168. break;
  169. case KEY_STATE_PRESS:
  170. if(ulEvent == KEY_SAMPLE_EVENT)
  171. {
  172. PushKeyToAVGPool(lpParam);
  173. if(AVGPoolIsFull())
  174. {
  175. UINT32 ulKeySampleValue;
  176. UINT32 ulNewKeyCode;
  177. ulKeySampleValue = GetKeyAVGValue();
  178. ulNewKeyCode = CheckKey(adc_id, ulKeySampleValue);
  179. ResetKeyAVGPool();
  180. if(ulNewKeyCode != NULL_KEY)
  181. {
  182. if(lg_ulLastKey != ulNewKeyCode && lg_ulLastKey != NULL_KEY)
  183. {
  184. lg_ulKeyMachineState = KEY_STATE_IDLE;
  185. //send key release event;
  186. SendKeyRelease(lg_ulLastKey);
  187. lg_ulLastKey = NULL_KEY;
  188. }
  189. else if (lg_ulLastKey == ulNewKeyCode)
  190. {
  191. //Send repeat key event;
  192. SendKeyPress(lg_ulLastKey);
  193. }
  194. else if (lg_ulLastKey == NULL_KEY)
  195. {
  196. //Send key press event;
  197. SendKeyPress(ulNewKeyCode);
  198. }
  199. lg_ulLastKey = ulNewKeyCode;
  200. }
  201. else
  202. {
  203. lg_ulKeyMachineState = KEY_STATE_IDLE;
  204. if(lg_ulLastKey != NULL_KEY)
  205. {
  206. //send key release event
  207. SendKeyRelease(lg_ulLastKey);
  208. lg_ulLastKey = NULL_KEY;
  209. }
  210. }
  211. }
  212. }
  213. else if(ulEvent == KEY_STOP_EVENT)
  214. {
  215. lg_ulKeyMachineState = KEY_STATE_IDLE;
  216. if(lg_ulLastKey != NULL_KEY)
  217. {
  218. //send key release event
  219. SendKeyRelease(lg_ulLastKey);
  220. lg_ulLastKey = NULL_KEY;
  221. }
  222. }
  223. break;
  224. }
  225. }
  226. static void adc_cb(uint32_t adc_id, uint32_t adc_int_flag, uint32_t adc_value, void *user_param)
  227. {
  228. if (adc_int_flag & ADC_START_INT) {
  229. KeyEventHandler(adc_id, KEY_START_EVENT, 0, 0);
  230. }
  231. if (adc_int_flag & ADC_STOP_INT) {
  232. KeyEventHandler(adc_id, KEY_STOP_EVENT, 0, 0);
  233. }
  234. if (adc_int_flag & ADC_VALUE_INT) {
  235. KeyEventHandler(adc_id, KEY_SAMPLE_EVENT, adc_value, 0);
  236. }
  237. }
  238. void KeypadInit(void)
  239. {
  240. for(int i = ADC0; i < ADC_ID_MAX; i++){
  241. adc_int_threshold_sel(i, 1);
  242. adc_interrupt_enable(i, ADC_CH_AUX0, ADC_START_INT \
  243. | ADC_STOP_INT | ADC_VALUE_INT, 1);
  244. adc_register_int_cb(i, ADC_CH_AUX0, adc_cb, NULL);
  245. adc_enable(i, ADC_CH_AUX0, 1);
  246. }
  247. if (xTaskCreate(SendKeyPressToMcu, "SendKeyPressToMcu", configMINIMAL_STACK_SIZE, NULL,
  248. configMAX_PRIORITIES - 2, &key_task) != pdPASS) {
  249. printf("create SendKeyPressToMcu task fail.\n");
  250. }
  251. }