SY8837_Demo_For_OurSelf/UsrSrc/key/key.c
rb_peng c743af0aa6 1、将双向通信的具体场景提到vox_Comm_Handle函数中
2、增加VOX持续输出0V以唤醒耳机的情况;
3、解决超长按后,双向通讯一直发码的问题;(松开按键后Key_Press_ll_irq设为FALSE)
4、解决关盖上电时,不亮灯和不发码的问题(即把Hall_Sta_bk初始化);
5、长按发配对码时,主副耳的选择提取到config.h中;
2025-02-21 17:24:44 +08:00

337 lines
7.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
******************************************************************************
*
* @file key.c
* @brief key module
* @ic sy8837/8
*
* @version 1.0
* @date 2024/12/02 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/12/02 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"
#include "discharge_module.h"
#include "display_ui.h"
#include "vox_comm.h"
#if KEY_ENABLE
/******************************************************************************\
Macro definitions
\******************************************************************************/
#define KEY_PRESS_500MS_CNT 30 //按键按下500ms(调用周期15ms)
#define KEY_PRESS_2S_CNT 135 //按键按下计数2s(调用周期15ms)
#define KEY_PRESS_5S_CNT 335
#define KEY_PRESS_8S_CNT 535 //按键按下计数8s(调用周期15ms)
#define KEY_TWS_PAIR_CLEAR_CNT 180
#define KEY_LL_TX_Interval 8 //Vox定时发送指令时间间隔
#define KEY_LL_TX_Interval_2 16
#define KEY_LIFT_CNT 35 //调用周期15ms
/******************************************************************************\
Variables definitions
\******************************************************************************/
bit Key_Press_l_irq;
bit Key_Press_ll_irq;
KEY_EVENT_E Event_key;
#if 0
uint8_t Key_TWS_Pair_Tim;
uint8_t Key_TWS_Clear_Pair_Tim;
uint8_t Key_TWS_Pair_Cnt;
#endif
uint16_t Key_Press_Debounce;
uint8_t Key_Lift_Debounce;
uint8_t Key_Voltage_Hold_Timer;
/******************************************************************************\
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_Vol_Up_Flag;
if( !(CHIP_STA0 & KEY_STAT) ) //CHIP_STA0 的bit1为Key的状态值0按下
{
Key_Press_Debounce++;
if( Key_Press_Debounce >= KEY_PRESS_8S_CNT ) //长按8sVOX掉电
{
Key_Press_ll_irq = TRUE;
#ifdef _DEBUG_KEY
printf("supper long 8s press.(line:%d)\r\n",(u16)__LINE__);
#endif
}
else
if( Key_Press_Debounce >= KEY_PRESS_2S_CNT ) //长按2s需要抬键判断
{
if( !nKey_Press_2s_Flag )
{
nKey_Press_2s_Flag = TRUE;
}
}
Key_Press_Flag = TRUE;
Key_Lift_Flag = FALSE;
}
else
{
if( Key_Press_Flag )
{
#if SLEEP_ENABLE
Enter_Sleep_Cnt_Restart_Flag = TRUE;
#endif
Event_key = KEY_EVENT_Empty;
Key_Press_Flag = FALSE;
Key_Lift_Flag = TRUE;
Key_Lift_Debounce = 0;
Key_Voltage_Hold_Timer = 0;
if( (Key_Press_Debounce <= KEY_PRESS_500MS_CNT) ) //松开按键按下时间小于500ms则识别为短按显示电量
{
Key_Press_short_irq_led = TRUE;
nKey_Press_short_irq = TRUE;
#ifdef LED_DISPLAY
LED_On_Flag = TRUE;
#endif
}
else
if( (Key_Press_Debounce < KEY_PRESS_5S_CNT) ) //按键时间大于2s小于5s则判断为长按2s。
{
if( nKey_Press_2s_Flag )
{
Key_Press_l_irq = TRUE;
Key_Press_ll_irq = FALSE;
}
}
nKey_Press_2s_Flag = FALSE;
#ifdef _DEBUG_KEY
printf("Key lift(line:%d).\r\n",(u16)__LINE__);
#endif
}
Key_Press_Debounce = 0;
}
/* 用于按键发码VOX先升压一段时间用于唤醒耳机后发码。 */
if( Key_Lift_Flag && gBoost_Prepared_Flag )
{
if( Key_Press_l_irq )
{
if( !Boost_Open_Flag )
{
DisCharge_Boost_Open(OFF,BOOST_VOUT_MAX);
Boost_Open_Flag = TRUE;
nKey_Vol_Up_Flag = TRUE;
#ifdef _DEBUG_KEY
printf("Boost Open Key.(line:%d)\r\n",(u16)__LINE__);
#endif
return;
}
#if VOX_ENABLE
if( nKey_Vol_Up_Flag )
{
nKey_Vol_Up_Flag = FALSE;
#ifdef VOX_ADT_ENABLE
Vol_State = VOX_GET_INTO_BOX;
Vor_State = VOX_GET_INTO_BOX;
#else
VOX_EN_Type(VOX_VOUT_Mode); //VOX Enable 5V
#endif
}
#endif
if( Key_Lift_Debounce <= KEY_LIFT_CNT )
{
Key_Lift_Debounce++;
return;
}
#if VOX_0V_WAKEUP_EAR /*长按后5V持续N00ms后0V持续N00ms保证耳机能被唤醒之后发长按码。*/
if( Key_Voltage_Hold_Timer < Key_VOLTAGE_HOLD_CNT ) //转0V N00ms
{
VOX_EN_Type(VOX_PD1K_Mode);
Key_Voltage_Hold_Timer++;
return;
}
#endif
Key_Press_l_irq = FALSE;
/*todo*/
Event_key = KEY_EVENT_L_2S;
Vox_Comm_Cfg_Clear();
gVox_Comm.Key_L_2S_Flag = TRUE; //按键2s发码。
#ifdef VOX_TX
/*耳机配对*/
Vox_Get_BES_Addr_Flag[VOL_TYPE] = 0x00;
Vox_Get_BES_Addr_Flag[VOR_TYPE] = 0x00;
#endif
#ifdef _DEBUG_KEY
printf("long 2s press\r\n");
#endif
}
if( nKey_Press_short_irq )
{
nKey_Press_short_irq = FALSE;
/*todo*/
// Event_key = KEY_EVENT_SHORT;
#ifdef _DEBUG_KEY
printf("short press\r\n");
#endif
}
Key_Lift_Flag = FALSE;
Boost_Open_Flag = FALSE;
}
/*长按8sVOX先升压。*/
if( Key_Press_ll_irq )
{
if( !Boost_Open_Flag )
{
DisCharge_Boost_Open(OFF,BOOST_VOUT_MAX);
Boost_Open_Flag = TRUE;
nKey_Vol_Up_Flag = TRUE;
#ifdef _DEBUG_KEY
printf("Boost Open Key.(line:%d)\r\n",(u16)__LINE__);
#endif
return;
}
#if VOX_ENABLE
if( nKey_Vol_Up_Flag )
{
nKey_Vol_Up_Flag = FALSE;
#ifdef VOX_ADT_ENABLE
Vol_State = VOX_GET_INTO_BOX;
Vor_State = VOX_GET_INTO_BOX;
#else
VOX_EN_Type(VOX_VOUT_Mode); //VOX Enable 5V
#endif
}
#endif
if( Key_Lift_Debounce <= KEY_LIFT_CNT )
{
Key_Lift_Debounce++;
return;
}
#if VOX_0V_WAKEUP_EAR /*长按后5V持续N00ms后0V持续N00ms保证耳机能被唤醒之后发长按码。*/
if( Key_Voltage_Hold_Timer < Key_VOLTAGE_HOLD_CNT ) //转0V N00ms
{
VOX_EN_Type(VOX_PD1K_Mode);
Key_Voltage_Hold_Timer++;
return;
}
#endif
Boost_Open_Flag = FALSE;
Vox_Comm_Cfg_Clear();
gVox_Comm.Key_LL_8s_Flag = TRUE;
Key_Press_ll_irq = FALSE;
/*todo*/
Event_key = KEY_EVENT_LL_8S;
#ifdef _DEBUG_KEY
printf("supper long 8s press.(line:%d)\r\n",(u16)__LINE__);
#endif
}
}
#endif