平台:AB1565M
SDK版本:V2.11.0
开发环境:windows10
络达的SDK中有默认的参考工程,其中包含默认的按钮的事件定义。这些定义是基于官方的评估板进行设计的。通常用户在开发自己的产品时,都是需要对其进行修改和定制。UI 定义的源码文件的路径如下(以EVB为例):
bta_sdk\mcu\project\ab1565_ab1568_evk\apps\headset_ref_design\src\boards\ab1565_evb\customerized_key_config.c
对于TWS工程,还有左右两侧独立定义的区别。对于headset来说,只有单侧的按钮。笔者在此以Headset类型的工程为例来进行说明。
对于headset,在上面提到的源码文件中,用到的定义有:
- temp_key_short_click_configs[] ------ 定义按钮单击的事件
-
- temp_key_double_click_configs[] --- 定义按钮双击的事件
-
- temp_key_triple_click_configs[] ----- 定义三击的事件(正常情况下不用该定义,否则UI会太复杂)
-
- temp_key_long_press1_configs[] --- 定义1类长按事件;
-
- temp_key_long_press2_configs[] ---定义2类长按事件;
-
- temp_key_long_press3_configs[] --- 定义3类长按事件;
上面提到长按的类型有三种,这三种长按可以对应于不同的长按时长,默认的时长定义在工程下面文件中:
bta_sdk\mcu\project\ab1565_ab1568_evk\apps\headset_ref_design\inc\boards\ab1565_evb\airo_key_config.h
如下代码所示,其中时间单位为ms:
- /*the default time,uint:ms*/
-
- #define default_t_p 300 //the multiple click press time
-
- #define default_t_r 200 //the multiple click release time for waiting next click
-
- #define default_t_end 300 //the silence time when only support someone click
-
- #define default_t_repeat 200 //the repeat time inlcude slong repeat and long repeat
-
- #define default_t_l_p1 500 //the longpress 1 time , long1 = default_t_l_p1
-
- #define default_t_l_p2 1000 //the longpress 1 -> longpress 2 time, long2 = default_t_l_p1 + default_t_l_p2
-
- #define default_t_l_p3 1500 //the longpress 2 -> longpress 3 time, long3 = default_t_l_p1 + default_t_l_p2 + default_t_l_p3
-
- #define default_t_slong 500 //the slong time
当然,时间也可以通过用ATK配置工具来调整,可以参见笔者如下连接的另一篇文章,在此不在重复:
笔者对于单击的UI的定义如下所示:
- const apps_config_key_event_map_t temp_key_short_click_configs[] = {
- #if GHP_UI_DEF
- {
- DEVICE_KEY_POWER,
- KEY_ACCEPT_CALL,
- (1 << APP_HFP_INCOMING)
- },
- {
- DEVICE_KEY_POWER,
- KEY_3WAY_HOLD_ACTIVE_ACCEPT_OTHER,
- (1 << APP_HFP_TWC_INCOMING)
- },
- {
- DEVICE_KEY_POWER,
- KEY_AVRCP_PLAY,
- (1 << APP_CONNECTED)
- },
- {
- DEVICE_KEY_POWER,
- KEY_AVRCP_PAUSE,
- (1 << APP_A2DP_PLAYING)
- },
- {
- DEVICE_KEY_POWER,
- KEY_END_CALL,
- (1 << APP_HFP_CALLACTIVE) | (1 << APP_HFP_CALLACTIVE_WITHOUT_SCO) | (1 << APP_HFP_MULTITPART_CALL)
- | (1 << APP_HFP_OUTGOING) | (1 << APP_STATE_HELD_ACTIVE) | (1 << APP_HFP_TWC_OUTGOING)
- },
- {
- EINT_KEY_0, //GPIO2---EINT2 --- KEY2
- KEY_ANC_GAIN_UP, //KEY_VOICE_UP,
- (1 << APP_CONNECTED) | (1 << APP_HFP_INCOMING) | (1 << APP_HFP_OUTGOING) | (1 << APP_HFP_CALLACTIVE)
- | (1 << APP_HFP_TWC_INCOMING) | (1 << APP_HFP_TWC_OUTGOING) | (1 << APP_HFP_MULTITPART_CALL) | (1 << APP_A2DP_PLAYING)
- | (1 << APP_STATE_HELD_ACTIVE) | (1 << APP_ULTRA_LOW_LATENCY_PLAYING) | (1 << APP_WIRED_MUSIC_PLAY)
- },
- {
- EINT_KEY_1, //GPIO8---EINT8 --- KEY8
- KEY_ANC_GAIN_DOWN, //KEY_VOICE_DN,
- (1 << APP_CONNECTED) | (1 << APP_HFP_INCOMING) | (1 << APP_HFP_OUTGOING) | (1 << APP_HFP_CALLACTIVE)
- | (1 << APP_HFP_TWC_INCOMING) | (1 << APP_HFP_TWC_OUTGOING) | (1 << APP_HFP_MULTITPART_CALL) | (1 << APP_A2DP_PLAYING)
- | (1 << APP_STATE_HELD_ACTIVE) | (1 << APP_ULTRA_LOW_LATENCY_PLAYING) | (1 << APP_WIRED_MUSIC_PLAY)
- },
- #if defined(AIR_BT_ULTRA_LOW_LATENCY_ENABLE)
- {
- EINT_KEY_2, //GPIO8 --- EINT9 ---KEY9
- KEY_ULL_RECONNECT,
- (1 << APP_DISCONNECTED) | (1 << APP_CONNECTED) | (1 << APP_A2DP_PLAYING) | (1 << APP_ULTRA_LOW_LATENCY_PLAYING)
- },
- #endif
-
- #endif
- };
可以看到,代码中的事件<KEY_ANC_GAIN_UP>和<KEY_ANC_GAIN_DOWN>,这两个为笔者添加的事件类型,原SDK中没有该定义,其目的是实现通过按钮来实时调整ANC的增益。那么,这个定义添加在哪里呢?如下路径所示:
bta_sdk\mcu\project\ab1565_ab1568_evk\apps\headset_ref_design\inc\apps\config\apps_config_event_list.h
文件中的枚举定义:apps_config_key_action_t中就是用来声名事件的类型。如笔者添加的类型:
- KEY_AUDIO_PEQ_SWITCH = 0x0220, /** < For PEQ switch key event >*/
-
- KEY_TEST_MODE_ENTER_DUT_MODE = 0x0300, /* For test mode, press key to enter DUT mode. */
- KEY_TEST_MODE_ENTER_RELAY_MODE = 0x0301, /* For test mode, press key to enter Relay mode. */
-
- KEY_CUSTOMER_DEFINE_ACTIONS = 0xF000, /**< Value larger than it is for customization. */
-
- KEY_MS_TEAMS_BTN_INVOKE = 0x0140, /* Teams BTN press. */
- KEY_MS_TEAMS_BTN_RELEASE = 0x0141, /* Teams BTN release. */
- /*------------------add by cczy-----------------*/
- KEY_ANC_GAIN_UP = 0xF010,
- KEY_ANC_GAIN_DOWN = 0xF011,
- } apps_config_key_action_t;
事件类型添加并定义好之后,还要去事件处理器里面去handle这个事件,相关代码文件在如下路径:
bta_sdk\mcu\project\ab1565_ab1568_evk\apps\headset_ghp\src\apps\app_idle\app_home_screen_idle_activity.c
上面文件中的函数:
- static bool _proc_key_event_group(ui_shell_activity_t *self,
- uint32_t event_id,
- void *extra_data,
- size_t data_len)
即为处理各按钮事件的函数;
因为笔者添加的是和ANC相关的事件,所以在该函数处理中,会调用函数:
app_home_screen_process_anc_and_pass_through()
并在其中继续识别事件类型;相关代码如下所示:
- static bool _proc_key_event_group(......){
- ......
- #ifdef MTK_ANC_ENABLE
- case KEY_PASS_THROUGH:
- case KEY_ANC:
- case KEY_SWITCH_ANC_AND_PASSTHROUGH:
- case KEY_BETWEEN_ANC_PASSTHROUGH:
- case KEY_ANC_GAIN_UP:
- case KEY_ANC_GAIN_DOWN:
- /* Handle ANC key event. */
- ret = app_home_screen_process_anc_and_pass_through(self, action);
- break;
- #endif
-
- ......
- }
-
- static bool app_home_screen_process_anc_and_pass_through(......)
- {
- ......
- }else if(KEY_ANC_GAIN_UP == key_action){
- if(anc_enable)
- {
- bool control_ret = FALSE;
- int16_t newGain = anc_runtime_gain;
- if(newGain < 500)
- {
- newGain += 100;
- if(newGain > 500) newGain = 500;
- control_ret = app_anc_service_set_runtime_gain(target_anc_type,newGain);
- }
- GHP_LOG_MSGID_I("KEY_ANC_GAIN_UP[%d],oldG:%d, newG:%d",3,control_ret,anc_runtime_gain, newGain);
- }
- else
- {
- GHP_LOG_MSGID_I("gain up:ANC not enable",0);
- }
- return true;
- } else if(KEY_ANC_GAIN_DOWN == key_action){
- if(anc_enable)
- {
- bool control_ret = FALSE;
- int16_t newGain = anc_runtime_gain;
-
- if(anc_runtime_gain > -500)
- {
- newGain -= 100;
- if(newGain < -500) newGain = -500;
- control_ret = app_anc_service_set_runtime_gain(target_anc_type,newGain);
- }
- GHP_LOG_MSGID_I("KEY_ANC_GAIN_DOWN[%d],oldG:%d, newG:%d",3,control_ret,anc_runtime_gain, newGain);
- }
- else
- {
- GHP_LOG_MSGID_I("gain down:ANC not enable",0);
- }
-
- return true;
- }else {
- return false;
- }
- ......
- }
至此,自定义按钮事件和UI的关联已经完成建立。