/*! * \file key.c * * \brief Target key implementation * * \copyright Revised BSD License, see section \ref LICENSE. * * \code * * _ __ _ ________ _ * | |/ /(_)___ / ____/ /_ (_)___ * | // / __ \/ / / __ \/ / __ \ * / |/ / / / / /___/ / / / / /_/ / * /_/|_/_/_/ /_/\____/_/ /_/_/ .___/ * /_/ * (C) 2022-2025 XinChip * * \endcode * * \author ( XinChip ) Alex-J * * \author ( XinChip ) */ /*----------------------------------------------------------------------------------- INCLUDE HEADE FILES ------------------------------------------------------------------------------------*/ #include "xc_drv_gpio.h" #include "xc6xxx.h" #include "key.h" #include "message.h" /*------------------------------------------------------------------------------------ Macros -------------------------------------------------------------------------------------*/ #define IS_KEY_DOWN(n) (!(xc_gpio_read_pin(n))) #define KEY_GPIO GPIO_13 /*------------------------------------------------------------------------------------ Global Variables -------------------------------------------------------------------------------------*/ static uint16_t last_key_val = NO_KEY; static uint16_t key_down_count = 0; static uint16_t key_up_count = 0; /*------------------------------------------------------------------------------------ Func Prototype -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Functions -------------------------------------------------------------------------------------*/ /** * @brief app_key_hold_detection * @details * @param curren_key_val * @retval key_num */ __RAM_CODE uint16_t app_key_hold_detection(uint16_t curren_key_val) { uint16_t key_num = NO_KEY; if (key_down_count < 0xff) key_down_count++; if (key_down_count == KEY_DOWN_TIMER) { key_num = (curren_key_val | KEY_SHORT_DOWN); // shor key } else if(key_down_count == KEY_LONG_TIMER) { key_num = (curren_key_val | KEY_LONG_DOWN); // loog key } key_up_count = 0; return key_num; } /** * @brief app_new_key_detection * @details key up or new key detection * @param curren_key_val * @retval key_num */ __RAM_CODE uint16_t app_new_key_detection(uint16_t curren_key_val) { uint16_t key_num = NO_KEY; key_up_count++; if (key_up_count >= KEY_UP_TIMER) { if (key_down_count >= KEY_LONG_TIMER) { key_num = (last_key_val | KEY_LONG_UP); // long key up last_key_val = 0; } else if (key_down_count >= KEY_DOWN_TIMER && key_down_count < KEY_LONG_TIMER) { key_num = (last_key_val | KEY_SHORT_UP); // shor key up last_key_val = 0; } key_down_count = 0; key_up_count = 0; } if ((curren_key_val && curren_key_val != last_key_val)) { if (key_down_count >= KEY_LONG_TIMER) { key_num = (last_key_val | KEY_LONG_UP); // long key up } else if (key_down_count >= KEY_DOWN_TIMER && key_down_count < KEY_LONG_TIMER) { key_num = (last_key_val | KEY_SHORT_UP); // shor key up } last_key_val = curren_key_val; key_down_count = 0; key_up_count = 0; } return key_num; } /** * @brief key_check_process * @details * @param curren_key_val * @retval key_num */ __RAM_CODE uint16_t key_check_process(uint16_t curren_key_val) { uint16_t key_num = NO_KEY; if ( curren_key_val == last_key_val && last_key_val != NO_KEY ) { key_num = app_key_hold_detection(curren_key_val); } else { key_num = app_new_key_detection(curren_key_val); } return key_num; } /** * @brief gpio_key_check * @details * @param void * @retval key_num */ uint16_t gpio_key_check(void) { uint16_t key_val = NO_KEY; if ( IS_KEY_DOWN(KEY_GPIO) ) { key_val = REC_KEY; } return key_val; } /** * @brief key_check_process * @details key scan and send * @param void * @retval void */ void app_key_scan(void) { uint16_t key_num = NO_KEY; uint16_t key_val = NO_KEY; key_val = gpio_key_check(); key_num = key_check_process(key_val); if (key_num != NO_KEY) { app_key_message_send( key_num ); } } /** * @brief app_key_init * @details key init * @param void * @retval void */ void app_key_init(void) { GPIO_InitCfg_t GPIO_InitCfg = {0}; GPIO_InitCfg.Mux = GPIO_Mux1; GPIO_InitCfg.FunSel = GPIO_Dx; GPIO_InitCfg.Pull = GPIO_PULLUP; GPIO_InitCfg.Dir = GPIO_DIR_INPUT; GPIO_InitCfg.Int = NOT_INT; GPIO_InitCfg.Pin = KEY_GPIO; xc_gpio_init(&GPIO_InitCfg); }