/* ****************************************************************************** * * @file key.c * @brief key module * @ic sy8835 * * @version 1.0 * @date 2024/11/01 15:00:40 * @author Alex Xu * * Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd. * All Rights Reserved * * History: * Revision Date Author Desc * 1.0.0 2024/11/01 Alex build this file ******************************************************************************/ /*_____ I N C L U D E S ____________________________________________________*/ #include "key.h" #include "system.h" #include "led.h" #include "sleep.h" #include "adc.h" #include "bat.h" #include "vox_module.h" #include "userapp.h" #include "hall.h" #if KEY_ENABLE /******************************************************************************\ Macro definitions \******************************************************************************/ #define KEY_PRESS_500MS_CNT 25 //按键按下500ms(调用周期20ms) #define KEY_PRESS_2S_CNT 100 //按键按下计数2s(调用周期20ms) #define KEY_PRESS_5S_CNT 250 #define KEY_PRESS_8S_CNT 400 //按键按下计数8s(调用周期20ms) /******************************************************************************\ Variables definitions \******************************************************************************/ bit Key_Press_short_irq = 0; bit Key_Press_l_irq = 0; bit Key_Press_ll_irq = 0; idata KEY_EVENT_E Event_key = 0; uint8_t Key_TWS_Pair_Tim = 0; uint8_t Key_TWS_Pair_Cnt = 0; uint16_t Key_Press_Debounce = 0; #define KEY_TWS_PAIR_TIMER 20 #define KEY_TWS_PAIR_CNT 5 #define KEY_TWS_PAIR_CLEAR_CNT 30 /******************************************************************************\ Functions definitions \******************************************************************************/ /* ******************************************************************************* * void Key_Handler(void) * * Description : Key Handler --- 主循环中调用,调用周期5ms。其中长按(按键时间大于2s)需要判断抬键动作才触发。 CoverStatus --- 充电仓盖子状态。(本函数提供对应Key事件标志位,后续应用待定) (调用周期20ms) * * Arguments : NONE * Returns : NONE * Notes : NONE * ******************************************************************************* */ void Key_Handler(void) { static bit Key_Press_Flag; static bit Key_Lift_Flag; static bit nKey_Press_short_irq; static bit nKey_Press_2s_Flag; static bit nKey_Press_8s_Flag; if( pmu_Info.pmu_Chip_STA & KEY_STA ) //Key的状态值:1:按下; { Key_Press_Debounce++; if( Key_Press_Debounce >= KEY_PRESS_2S_CNT ) //长按2s,需要抬键判断 { if( !nKey_Press_2s_Flag ) { nKey_Press_2s_Flag = 1; } } else if( Key_Press_Debounce >= KEY_PRESS_8S_CNT ) //长按8s,VOX掉电 { if( !nKey_Press_8s_Flag ) { nKey_Press_8s_Flag = 1; Key_Press_ll_irq = 1; } } Key_Press_Flag = 1; Key_Lift_Flag = 0; } else { if( Key_Press_Flag ) { Key_Press_Flag = 0; Key_Lift_Flag = 1; nKey_Press_8s_Flag = 0; if( (Key_Press_Debounce <= KEY_PRESS_500MS_CNT) ) //松开按键,按下时间小于500ms,则识别为短按,显示电量 { Key_Press_short_irq = 1; nKey_Press_short_irq = 1; #ifdef LED_DISPLAY LED_On_Flag = 1; #endif } else if( (Key_Press_Debounce < KEY_PRESS_5S_CNT) ) //按键时间大于2s小于5s则判断为长按2s。 { if( nKey_Press_2s_Flag ) { nKey_Press_2s_Flag = 0; Key_Press_l_irq = 1; } } } Key_Press_Debounce = 0; } if( Key_Lift_Flag ) { Key_Lift_Flag = 0; if( Key_Press_l_irq ) { Key_Press_l_irq = 0; /*todo*/ Event_key = KEY_EVENT_L_2S; Key_TWS_Pair_Tim = 0; /*耳机配对*/ #ifdef _DEBUG_KEY printf("long 2s press\r\n"); #endif } /*长按8s,系统强制进shipmode.*/ if( Key_Press_ll_irq ) { Key_Press_ll_irq = 0; /*todo*/ Event_key = KEY_EVENT_LL_8S; #ifdef _DEBUG_KEY printf("supper long 8s press\r\n"); #endif } if( nKey_Press_short_irq ) { nKey_Press_short_irq = 0; /*todo*/ Event_key = KEY_EVENT_SHORT; #ifdef _DEBUG_KEY printf("short press\r\n"); #endif } } else { Event_key = KEY_EVENT_Empty; } #if 1 if ( CoverStatus == OPEN ) { if( Event_key == KEY_EVENT_L_2S ) //长按2s,充电仓发送配对指令。 { if( Key_TWS_Pair_Tim < 1 ) { #ifdef VOX_TX HandleTxCommand(VHOUSE_CMD_PAIR,VOR_CHAN); //右耳发配对指令,区分耳机通道。 #endif Vox_Get_BES_Addr_Flag[VOL_TYPE] = 0x00; Vox_Get_BES_Addr_Flag[VOR_TYPE] = 0x00; } else { if(Key_TWS_Pair_Cnt > KEY_TWS_PAIR_CNT) //耳机蓝牙地址置换超时10s,关闭蓝牙地址置换,开启VOX 5V。 { Key_TWS_Pair_Cnt = 0; #if VOX_ENABLE VOX_EN_Type(VOX_VOUT_Mode); #endif return; } else { if( Vox_Get_BES_Addr_Flag[VOR_TYPE] == 0x01 ) /*收到右耳回复的CMD3指令。*/ { #ifdef VOX_TX HandleTxCommand(VHOUSE_CMD_EXCH_TWS_BTADDR,VOL_CHAN); /*获取到右耳回复的CMD3指令,转发给左耳。*/ #endif Vox_Get_BES_Addr_Flag[VOR_TYPE] = 0x02; } else if( Vox_Get_BES_Addr_Flag[VOR_TYPE] == 0x02 ) { if( Vox_Get_BES_Addr_Flag[VOL_TYPE] != 0x00 ) { #ifdef VOX_TX HandleTxCommand(VHOUSE_CMD_EXCH_TWS_BTADDR,VOR_CHAN); /*获取到右耳回复的CMD3指令,转发给左耳。*/ #endif Vox_Get_BES_Addr_Flag[VOL_TYPE] = 0x00; Vox_Get_BES_Addr_Flag[VOR_TYPE] = 0x00; Key_TWS_Pair_Tim = 0; Event_key = KEY_EVENT_Empty; VOX_EN_Type(VOX_VOUT_Mode); return; } else { #ifdef VOX_TX HandleTxCommand(VHOUSE_CMD_PAIR,VOL_CHAN); #endif } } } } if(Key_TWS_Pair_Tim > KEY_TWS_PAIR_TIMER) //3s重发一次。重发5次后超时 { Key_TWS_Pair_Cnt++; Key_TWS_Pair_Tim = 0; } else { Key_TWS_Pair_Tim++; } } else if( Event_key == KEY_EVENT_LL_8S ) { if( Key_TWS_Pair_Tim < KEY_TWS_PAIR_CLEAR_CNT ) //每隔200ms发送一次,持续时间3s。 { if( Key_TWS_Pair_Tim % 2*Vox_TX_Interval ) { #ifdef VOX_TX HandleTxCommand(VHOUSE_CMD_CLEAR_PAIR,VOR_CHAN); #endif } else if( Key_TWS_Pair_Tim % Vox_TX_Interval ) { #ifdef VOX_TX HandleTxCommand(VHOUSE_CMD_CLEAR_PAIR,VOL_CHAN); #endif } Key_TWS_Pair_Tim++; } else { Event_key = KEY_EVENT_Empty; Key_TWS_Pair_Tim = 0; } } } #endif } #endif