(干饭人,干饭魂)
项目入耳检测要求如下
1.通过触摸检测GPIO2的电平高低来确定是否入耳,当HAL_IOMUX_PIN_P0_2为低电平时,耳机佩戴;当入耳检测为高电平时,耳机取下
入耳检测实现功能:
单耳佩戴时:开启环境音模式
双耳佩戴时:开始降噪模式
双耳组队成功,双耳机佩戴成功播放音乐,否则音乐暂停
但耳机和手机连接后,佩戴播放音乐,取下音乐暂停
佩戴耳机触摸按键播放暂停有效,取下耳机触摸按键无效
入耳检测代码主程序:
#if 1//20210104
const struct HAL_IOMUX_PIN_FUNCTION_MAP app_earphone_in_out_chcek_cfg[1]= {
{HAL_IOMUX_PIN_P0_2, HAL_IOMUX_FUNC_AS_GPIO, HAL_IOMUX_PIN_VOLTAGE_VIO, HAL_IOMUX_PIN_NOPULL},
};
enum APP_EARPHONE_INOUT_T status1;
bool TWS_RIGHT_GPIO02=0;
bool TWS_LEFT_GPIO02=0;
int ear_enter_chcek1(void)
{
TRACE(“enter in out check!!!”);
hal_iomux_init(app_earphone_in_out_chcek_cfg,1);
hal_gpio_pin_set_dir((enum HAL_GPIO_PIN_T) app_earphone_in_out_chcek_cfg[0].pin,HAL_GPIO_DIR_IN,0);
if (app_earphone_in_out_chcek_cfg[0].pin != HAL_IOMUX_PIN_NUM)
{
TRACE("status==?????");
if(app_get_ear_side()==TWS_RIGHT_SIDE)
{
if (!hal_gpio_pin_get_val((enum HAL_GPIO_PIN_T) app_earphone_in_out_chcek_cfg[0].pin))
{
TRACE("11111111111111111OK");
TWS_RIGHT_GPIO02=1;
customif_cmd_send_test4();
status1 = APP_IN_EAR_OK_RIGHT;
}
else
{
TRACE("000000000000000000OUT");
TWS_RIGHT_GPIO02=0;
customif_cmd_send_test4();
status1=APP_IN_EAR_OUT_RIGHT;
}
}
else if(app_get_ear_side()==TWS_LEFT_SIDE)
{
if (!hal_gpio_pin_get_val((enum HAL_GPIO_PIN_T) app_earphone_in_out_chcek_cfg[0].pin))
{
TRACE("11111111111111111OK");
TWS_LEFT_GPIO02=1;
customif_cmd_send_test3();
status1 = APP_IN_EAR_OK_LEFT;
}
else
{
TWS_LEFT_GPIO02=0;
TRACE("000000000000000000OUT");
customif_cmd_send_test3();
status1=APP_IN_EAR_OUT_LEFT;
}
}
}
return status1;
}
enum APP_EARPHONE_INOUT_T status2;
bool app_in_ear_ok=0;
bool anc_earphone_inout=0;
int ear_enter_chcek2(void)
{
ibrt_ctrl_t *p_ibrt_ctrl = app_ibrt_if_get_bt_ctrl_ctx();
TRACE(“ear_enter_chcek2”);
if(app_tws_ibrt_tws_link_connected())
{
if((ear_enter_chcek1()APP_IN_EAR_OK_RIGHT)&&(TWS_LEFT_GPIO021))
{
TRACE(“1APP_IN_EAR_OK”);
app_in_ear_ok=1;
anc_earphone_inout=1;
status2=APP_IN_EAR_OK;
}
else if((ear_enter_chcek1()APP_IN_EAR_OK_LEFT)&&(TWS_RIGHT_GPIO021))
{
//TRACE(“2APP_IN_EAR_OK”);
app_in_ear_ok=1;
anc_earphone_inout=1;
status2=APP_IN_EAR_OK;
}
else
{
//TRACE(“3APP_IN_EAR_OUT”);
app_in_ear_ok=0;
anc_earphone_inout=0;
status2=APP_IN_EAR_OUT;
}
}
else
{
if((p_ibrt_ctrl->current_role==IBRT_MASTER&&ear_enter_chcek1()APP_IN_EAR_OK_LEFT)||(p_ibrt_ctrl->current_roleIBRT_MASTER&&ear_enter_chcek1()==APP_IN_EAR_OK_RIGHT))
{
//TRACE(" 6ONE APP_IN_EAR_OK");
app_in_ear_ok=1;
anc_earphone_inout=0;
status2=APP_IN_EAR_OK;
}
else
{
//TRACE(“7APP_IN_EAR_OUT”);
anc_earphone_inout=0;
app_in_ear_ok=0;
status2=APP_IN_EAR_OUT;
}
}
return status2;
}
enum APP_EARPHONE_INOUT_T record_status=APP_IN_EAR_OUT;
void ear_inout_handle_process(void)
{
TRACE("in out ear chcek!!!");
if(status2==APP_IN_EAR_OK)
{
if(app_bt_device.a2dp_play_pause_flag == 0)
{
//app_in_ear_ok=1;
TRACE("earphone enter ear success");
a2dp_handleKey(AVRCP_KEY_PLAY);
//if(anc_earphone_inout==1)
//{
// app_anc_key(NULL,NULL);
//}
record_status=APP_IN_EAR_OK;
}
}
else
{
TRACE("earphone enter ear Failure");
a2dp_handleKey(AVRCP_KEY_PAUSE);
//if(anc_earphone_inout==0)
//{
// app_anc_key(NULL,NULL);
//}
record_status=APP_IN_EAR_OUT;
}
}
static void app_ear_inout_handler(void const *param);
osTimerDef (EARPHONE_INOUT, app_ear_inout_handler);
static osTimerId ear_inout_timer = NULL;
static void app_ear_inout_handler(void const *param)
{
bool current_status= ear_enter_chcek2();
TRACE(“Test whether the status1 changes”);
if(current_status!=record_status)
{
ear_inout_handle_process();
}
else
{
TRACE(“OKOKOKOKKOKOKOKOKOKOKOKOKOKOK”);
}
}
void app_ear_inoout_timer1(void)
{
TRACE(“enter ear inout timer”);
if(NULL==ear_inout_timer)
ear_inout_timer=osTimerCreate(osTimer(EARPHONE_INOUT),osTimerPeriodic,NULL );
osTimerStart(ear_inout_timer, 300);
}
void close_app_ear_inout_timer1(void)
{
TRACE(“stop ear inout timer”);
osTimerStop(ear_inout_timer);
}
#endif