| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include "FreeRTOS.h"
- #include "chip.h"
- #include "board.h"
- #include "keypad.h"
- typedef int16_t lv_coord_t;
- typedef uint8_t lv_indev_state_t;
- typedef struct {
- lv_coord_t x;
- lv_coord_t y;
- } lv_point_t;
- enum { LV_INDEV_STATE_REL = 0, LV_INDEV_STATE_PR };
- enum {
- LV_KEY_UP = 17, /*0x11*/
- LV_KEY_DOWN = 18, /*0x12*/
- LV_KEY_RIGHT = 19, /*0x13*/
- LV_KEY_LEFT = 20, /*0x14*/
- LV_KEY_ESC = 27, /*0x1B*/
- LV_KEY_DEL = 127, /*0x7F*/
- LV_KEY_BACKSPACE = 8, /*0x08*/
- LV_KEY_ENTER = 10, /*0x0A, '\n'*/
- LV_KEY_NEXT = 9, /*0x09, '\t'*/
- LV_KEY_PREV = 11, /*0x0B, '*/
- LV_KEY_HOME = 2, /*0x02, STX*/
- LV_KEY_END = 3, /*0x03, ETX*/
- };
- typedef struct {
- lv_point_t point; /**< For LV_INDEV_TYPE_POINTER the currently pressed point*/
- uint32_t key; /**< For LV_INDEV_TYPE_KEYPAD the currently pressed key*/
- uint32_t btn_id; /**< For LV_INDEV_TYPE_BUTTON the currently pressed button*/
- int16_t enc_diff; /**< For LV_INDEV_TYPE_ENCODER number of steps since the previous read*/
- lv_indev_state_t state; /**< LV_INDEV_STATE_REL or LV_INDEV_STATE_PR*/
- } lv_indev_data_t;
- #define KEYAD_0 100
- #define KEYAD_1 622
- #define KEYAD_2 1132
- #define KEYAD_3 1652
- #define KEYAD_4 2260
- #define KEYAD_5 2845
- #define KEYAD_6 100
- #define KEYAD_7 600
- #define KEYAD_8 1119
- #define KEYAD_9 1621
- #define KEYAD_NUM 10
- #define KEYAD_RANGE 100
- static int KeyAdcValue[KEYAD_NUM] =
- {
- KEYAD_0,
- KEYAD_1,
- KEYAD_2,
- KEYAD_3,
- KEYAD_4,
- KEYAD_5,
- KEYAD_6,
- KEYAD_7,
- KEYAD_8,
- KEYAD_9,
- };
- static unsigned int KeypadMap[KEYAD_NUM] =
- {
- LV_KEY_HOME,
- LV_KEY_ENTER,
- LV_KEY_ESC,
- LV_KEY_UP,
- LV_KEY_DOWN,
- LV_KEY_LEFT,
- LV_KEY_RIGHT,
- };
- #define NULL_KEY 0xFFFF
- #define KEY_STATE_IDLE 0
- #define KEY_STATE_START 1
- #define KEY_STATE_PRESS 2
- static UINT32 lg_ulKeyMachineState = KEY_STATE_IDLE;
- static UINT32 lg_ulLastKey = NULL_KEY;
- #define KEY_POOL_SIZE 8
- static UINT32 lg_arrKeyAvgPool[KEY_POOL_SIZE];
- static UINT32 lg_ulKeyIndex = 0;
- char test_key_value = 0;
- static TaskHandle_t key_task;
- uint32_t key_value = 0;
- uint32_t key_value_status = 0;
- extern void SendKeypadInputEventFromISR(lv_indev_data_t *indata);
- static void PushKeyToAVGPool(UINT32 ulKeyValue)
- {
- if(lg_ulKeyIndex < KEY_POOL_SIZE)
- {
- lg_arrKeyAvgPool[lg_ulKeyIndex] = ulKeyValue;
- lg_ulKeyIndex++;
- }
- }
- static UINT32 AVGPoolIsFull(void)
- {
- return lg_ulKeyIndex == KEY_POOL_SIZE;
- }
- static UINT32 GetKeyAVGValue(void)
- {
- UINT32 i;
- UINT32 ulSum;
- ulSum = 0;
- for(i=0;i<lg_ulKeyIndex;i++)
- {
- ulSum += lg_arrKeyAvgPool[i];
- }
- return ulSum/lg_ulKeyIndex;
- }
- static void ResetKeyAVGPool(void)
- {
- lg_ulKeyIndex = 0;
- }
- static UINT32 CheckKey(UINT32 id, UINT32 ulSampleValue)
- {
- UINT32 i;
- for(i = 0; i < (sizeof(KeyAdcValue) / sizeof(KeyAdcValue[0])); i++ )
- {
- if((ulSampleValue >= KeyAdcValue[i] - KEYAD_RANGE) && (ulSampleValue <= KeyAdcValue[i] + KEYAD_RANGE))
- {
- return i + (id * 6);
- }
- }
- return NULL_KEY;
- }
- static void SendKeyPressToMcu(void *param)
- {
- uint32_t ulNotifiedValue;
- for(;;){
- xTaskNotifyWait(0x00, 0xffffffff, &ulNotifiedValue, portMAX_DELAY);
- if(mailbox_msg_send(MB_MSG_T_DCIC_DETECT, NULL, 0, portMAX_DELAY))
- printf("%s, mailbox send fail.\n", __func__);
- }
- }
- static void SendKeyPress(UINT32 key)
- {
- if(key == 9)
- xTaskNotifyFromISR(key_task, 0, eSetValueWithOverwrite, 0);
- key_value = key+1;
- key_value_status = 1;
- }
- static void SendKeyRelease(UINT32 key)
- {
- key_value = key+1;
- key_value_status = 0;
- }
- static void KeyEventHandler(UINT32 adc_id, UINT32 ulEvent, UINT32 lpParam, UINT32 wParam)
- {
- switch(lg_ulKeyMachineState)
- {
- case KEY_STATE_IDLE:
- if(ulEvent == KEY_START_EVENT)
- {
- lg_ulKeyMachineState = KEY_STATE_START;
- }
- break;
- case KEY_STATE_START:
- test_key_value = !test_key_value;
- if(ulEvent == KEY_SAMPLE_EVENT)
- {
- ResetKeyAVGPool();
- PushKeyToAVGPool(lpParam);
- lg_ulKeyMachineState = KEY_STATE_PRESS;
- }
- else if(ulEvent == KEY_STOP_EVENT)
- {
- lg_ulKeyMachineState = KEY_STATE_IDLE;
- lg_ulLastKey = NULL_KEY;
- }
- break;
- case KEY_STATE_PRESS:
- if(ulEvent == KEY_SAMPLE_EVENT)
- {
- PushKeyToAVGPool(lpParam);
- if(AVGPoolIsFull())
- {
- UINT32 ulKeySampleValue;
- UINT32 ulNewKeyCode;
- ulKeySampleValue = GetKeyAVGValue();
- ulNewKeyCode = CheckKey(adc_id, ulKeySampleValue);
- ResetKeyAVGPool();
- if(ulNewKeyCode != NULL_KEY)
- {
- if(lg_ulLastKey != ulNewKeyCode && lg_ulLastKey != NULL_KEY)
- {
- lg_ulKeyMachineState = KEY_STATE_IDLE;
- //send key release event;
- SendKeyRelease(lg_ulLastKey);
- lg_ulLastKey = NULL_KEY;
- }
- else if (lg_ulLastKey == ulNewKeyCode)
- {
- //Send repeat key event;
- SendKeyPress(lg_ulLastKey);
- }
- else if (lg_ulLastKey == NULL_KEY)
- {
- //Send key press event;
- SendKeyPress(ulNewKeyCode);
- }
- lg_ulLastKey = ulNewKeyCode;
- }
- else
- {
- lg_ulKeyMachineState = KEY_STATE_IDLE;
- if(lg_ulLastKey != NULL_KEY)
- {
- //send key release event
- SendKeyRelease(lg_ulLastKey);
- lg_ulLastKey = NULL_KEY;
- }
- }
- }
- }
- else if(ulEvent == KEY_STOP_EVENT)
- {
- lg_ulKeyMachineState = KEY_STATE_IDLE;
- if(lg_ulLastKey != NULL_KEY)
- {
- //send key release event
- SendKeyRelease(lg_ulLastKey);
- lg_ulLastKey = NULL_KEY;
- }
- }
- break;
- }
- }
- static void adc_cb(uint32_t adc_id, uint32_t adc_int_flag, uint32_t adc_value, void *user_param)
- {
- if (adc_int_flag & ADC_START_INT) {
- KeyEventHandler(adc_id, KEY_START_EVENT, 0, 0);
- }
- if (adc_int_flag & ADC_STOP_INT) {
- KeyEventHandler(adc_id, KEY_STOP_EVENT, 0, 0);
- }
- if (adc_int_flag & ADC_VALUE_INT) {
- KeyEventHandler(adc_id, KEY_SAMPLE_EVENT, adc_value, 0);
- }
- }
- void KeypadInit(void)
- {
- for(int i = ADC0; i < ADC_ID_MAX; i++){
- adc_int_threshold_sel(i, 1);
- adc_interrupt_enable(i, ADC_CH_AUX0, ADC_START_INT \
- | ADC_STOP_INT | ADC_VALUE_INT, 1);
- adc_register_int_cb(i, ADC_CH_AUX0, adc_cb, NULL);
- adc_enable(i, ADC_CH_AUX0, 1);
- }
- if (xTaskCreate(SendKeyPressToMcu, "SendKeyPressToMcu", configMINIMAL_STACK_SIZE, NULL,
- configMAX_PRIORITIES - 2, &key_task) != pdPASS) {
- printf("create SendKeyPressToMcu task fail.\n");
- }
- }
|