mcu_ab568x/app/projects/AB5682C_240/functions/func_bt.c
2025-05-30 18:03:10 +08:00

664 lines
19 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.

#include "include.h"
#include "func.h"
#include "func_bt.h"
#include "app_variable.h"
#define TRACE_EN 0
#if TRACE_EN
#define TRACE(...) printf(__VA_ARGS__)
#else
#define TRACE(...)
#endif
#define TITLE_BUF_LEN 128 //歌词buuufer长度
#define ARTIST_BUF_LEN 128 //歌词buuufer长度
enum {
COMPO_ID_BTN_PREV = 1,
COMPO_ID_BTN_NEXT,
COMPO_ID_BTN_PLAY,
COMPO_ID_BTN_VOL_UP,
COMPO_ID_BTN_VOL_DOWN,
COMPO_ID_TXT_MUSIC_NAME,
COMPO_ID_TXT_MUSIC_LYRIC,
// COMPO_ID_PIC_MUSIC_VOL,
COMPO_ID_SHAPE_MUSIC_VOL,
COMPO_ID_SHAPE_MUSIC_VOL_CIR,
};
typedef struct f_bt_t_ {
bool ams_play_sta;
bool bt_play_sta;
u8 vol;
u8 ams_vol;
u8 shape_linx_width;
u8 shape_cir_x;
u8 title_buf[TITLE_BUF_LEN];
u8 artist_buf[ARTIST_BUF_LEN];
} f_bt_t;
/*****************************************************************************
* 1.当BT连接时优先同步id3信息控制通道走BT
* 2.当只连接BLE时IOS走AMS服务安卓走私有协议
*****************************************************************************/
static void func_bt_music_title_refresh(void *buf);
static void func_bt_music_artist_refresh(void *buf);
static void func_bt_music_play_btnpic_refresh(u8 sta);
static void func_bt_music_vol_btnpic_refresh(u8 vol);
/*****************************************************************************
* BT or BLE interface
*****************************************************************************/
void func_bt_mp3_res_play(u32 addr, u32 len)
{
if (len == 0) {
return;
}
bt_cb.res_bt_bypass = true;
if (!sbc_is_bypass()) {
bt_audio_bypass();
}
mp3_res_play(addr, len);
}
void func_bt_mp3_play_restore(void)
{
if (bt_cb.res_bt_bypass) {
bt_cb.res_bt_bypass = false;
bt_audio_enable();
}
}
void func_bt_init(void)
{
if (!bt_cb.bt_is_inited) {
msg_queue_clear();
dis_auto_pwroff();
bsp_bt_init();
bt_redial_init();
bt_cb.bt_is_inited = 1;
}
}
void func_bt_chk_off(void)
{
if ((func_cb.sta != FUNC_BT) && (bt_cb.bt_is_inited)) {
bt_disconnect(1);
bt_off();
bt_cb.bt_is_inited = 0;
}
}
//播放暂停控制
static void bt_control_play_pause(void)
{
//f_bt_t *f_bt = (f_bt_t *)func_cb.f_cb;
if (bt_is_connected()) {
bt_music_play_pause();
bt_cb.music_playing = bt_cb.music_playing ? 0:1;
} else if (ble_ams_is_connected()) {
ble_ams_remote_ctrl(AMS_REMOTE_CMD_PLAY_PAUSE);
bt_cb.music_playing = bt_cb.music_playing ? 0:1;
}
else{
func_popup_new(POPUP_ID_BT_REMIND);
}
}
//切换上一曲
static void bt_control_prev(void)
{
if (bt_is_connected()) {
bt_music_prev();
} else if (ble_ams_is_connected()) {
ble_ams_remote_ctrl(AMS_REMOTE_CMD_PREV_TRACK);
}
else{
func_popup_new(POPUP_ID_BT_REMIND);
}
}
//切换下一曲
static void bt_control_next(void)
{
if (bt_is_connected()) {
bt_music_next();
} else if (ble_ams_is_connected()) {
ble_ams_remote_ctrl(AMS_REMOTE_CMD_NEXT_TRACK);
}
else{
func_popup_new(POPUP_ID_BT_REMIND);
}
}
//音量更新
static void bt_vol_update(void)
{
if (!bt_cb.bt_form_created) {
return ;
}
f_bt_t *f_bt = (f_bt_t *)func_cb.f_cb;
bool vol_uodate_flag = false;
if (bt_is_connected()) {
if (f_bt->vol != SysVariable.vol) {
f_bt->vol = SysVariable.vol;
vol_uodate_flag = true;
}
} else if (ble_ams_is_connected()) {
if (f_bt->vol != f_bt->ams_vol) {
f_bt->vol = f_bt->ams_vol;
vol_uodate_flag = true;
}
}
if (vol_uodate_flag) {
func_bt_music_vol_btnpic_refresh(f_bt->vol);
}
}
//音量控制+
static void bt_control_vol_up(void)
{
if (bt_is_connected()) {
bt_volume_up();
} else if (ble_ams_is_connected()) {
ble_ams_remote_ctrl(AMS_REMOTE_CMD_VOL_UP);
}
else{
func_popup_new(POPUP_ID_BT_REMIND);
}
bt_vol_update();
}
//音量控制-
static void bt_control_vol_down(void)
{
if (bt_is_connected()) {
bt_volume_down();
} else if (ble_ams_is_connected()) {
ble_ams_remote_ctrl(AMS_REMOTE_CMD_VOL_DOWN);
}
else{
func_popup_new(POPUP_ID_BT_REMIND);
}
bt_vol_update();
}
#if LE_AMS_CLIENT_EN
//AMS推送状态更新处理
static void ble_ams_sta_update_handle(u8 sta, void *p_data, u16 len)
{
if (bt_is_connected() || !ble_ams_is_connected() || !bt_cb.bt_form_created) {
return ;
}
f_bt_t *f_bt = (f_bt_t *)func_cb.f_cb;
switch (sta) {
case BLE_AMS_STA_UPDATE_PAUSE:
{
f_bt->ams_play_sta = false;
func_bt_music_play_btnpic_refresh(false);
}
break;
case BLE_AMS_STA_UPDATE_PLAYING:
{
f_bt->ams_play_sta = true;
func_bt_music_play_btnpic_refresh(true);
}
break;
case BLE_AMS_STA_UPDATE_VOLUME:
{
u32 volume = GET_LE32(p_data);
f_bt->ams_vol = volume / 625;
bt_vol_update();
TRACE("ams volume:%d, ams_vol:%d\n", volume, f_bt->ams_vol);
}
break;
case BLE_AMS_STA_UPDATE_TITLE:
{
char *title = (char *)p_data;
if (strlen(title)) {
memcpy(f_bt->title_buf,title,TITLE_BUF_LEN - 1);
msg_enqueue(EVT_ID3_TITLE_UPDATE);
TRACE("ams title:%s\n",title);
}
}
break;
case BLE_AMS_STA_UPDATE_ALBUM:
{
char * album = (char *)p_data;
if (strlen(album)) {
memcpy(f_bt->artist_buf,album,ARTIST_BUF_LEN - 1);
msg_enqueue(EVT_ID3_ARTIST_UPDATE);
TRACE("ams album:%s\n",album);
}
}
break;
case BLE_AMS_STA_UPDATE_ARTIST:
{
char * artist = (char *)p_data;
if (strlen(artist)) {
memcpy(f_bt->artist_buf,artist,ARTIST_BUF_LEN - 1);
msg_enqueue(EVT_ID3_ARTIST_UPDATE);
TRACE("ams artist:%s\n",artist);
}
}
break;
case BLE_AMS_STA_UPDATE_APP_NAME:
{
char * app_name = (char *)p_data;
if (strlen(app_name)) {
compo_form_set_title(func_cb.frm_main, app_name);
TRACE("ams app name:%s\n",app_name);
}
}
break;
}
}
#endif // LE_AMS_CLIENT_EN
#if BT_ID3_TAG_EN
//id3信息更新处理
static void bt_id3_tag_update_handle(u8 type, char *str)
{
if (BT_ID3_TAG_TITLE == type) {
memcpy(f_bt->title_buf,str,TITLE_BUF_LEN - 1);
msg_enqueue(EVT_ID3_TITLE_UPDATE);
} else if (BT_ID3_TAG_ARTIST == type) {
memcpy(f_bt->artist_buf,str,ARTIST_BUF_LEN - 1);
msg_enqueue(EVT_ID3_ARTIST_UPDATE);
}
}
#endif // BT_ID3_TAG_EN
/*****************************************************************************
* func_bt_music(UI)
*****************************************************************************/
//创建蓝牙音乐播放器窗体,创建窗体中不要使用功能结构体 func_cb.f_cb
compo_form_t *func_bt_form_create(void)
{
//新建窗体
compo_form_t *frm = compo_form_create(true);
//设置标题栏
compo_form_set_mode(frm, COMPO_FORM_MODE_SHOW_TITLE | COMPO_FORM_MODE_SHOW_TIME);
compo_form_set_title(frm, i18n[STR_MUSIC]);
//歌名
compo_textbox_t *name_txt = compo_textbox_create(frm, 50);
compo_textbox_set_location(name_txt, 2*GUI_SCREEN_WIDTH+5, GUI_SCREEN_CENTER_Y - 100, GUI_SCREEN_WIDTH, 50);
compo_setid(name_txt, COMPO_ID_TXT_MUSIC_NAME);
compo_textbox_set(name_txt, i18n[STR_UNKNOWN]);
//歌词
compo_textbox_t *lyric_txt = compo_textbox_create(frm, 50);
compo_textbox_set_location(lyric_txt, 2*GUI_SCREEN_WIDTH+5, GUI_SCREEN_CENTER_Y - 50, GUI_SCREEN_WIDTH, 50);
compo_setid(lyric_txt, COMPO_ID_TXT_MUSIC_LYRIC);
compo_textbox_set(lyric_txt, i18n[STR_UNKNOWN]);
//新建图片 大背景
compo_form_add_image(frm, UI_BUF_MUSIC_BG_BIN, GUI_SCREEN_CENTER_X, 108);
//新建按钮
compo_button_t *btn;
//播放或暂停按钮
if (bt_cb.music_playing) {
btn = compo_button_create_by_image(frm, UI_BUF_MUSIC_PAUSE_CLICK_BIN);
} else {
btn = compo_button_create_by_image(frm, UI_BUF_MUSIC_PAUSE_BIN);
}
compo_setid(btn, COMPO_ID_BTN_PLAY);
compo_button_set_pos(btn, GUI_SCREEN_CENTER_X, 108);
//上一曲 按钮
btn = compo_button_create_by_image(frm, UI_BUF_MUSIC_PREV_CLICK_BIN);
compo_setid(btn, COMPO_ID_BTN_PREV);
compo_button_set_pos(btn, 41, 209);
//下一曲 按钮
btn = compo_button_create_by_image(frm, UI_BUF_MUSIC_NEXT_CLICK_BIN);
compo_setid(btn, COMPO_ID_BTN_NEXT);
compo_button_set_pos(btn, 197, 209);
//音量 减 图片加按钮
compo_form_add_image(frm, UI_BUF_MUSIC_VOLUME_DOWN_BIN, 25, 268);
btn = compo_button_create(frm);
compo_setid(btn, COMPO_ID_BTN_VOL_DOWN);
compo_button_set_location(btn, 25, 268, 40, 40);
//音量 增 图片加按钮
compo_form_add_image(frm, UI_BUF_MUSIC_VOLUME_UP_BIN, 214, 268);
btn = compo_button_create(frm);
compo_setid(btn, COMPO_ID_BTN_VOL_UP);
compo_button_set_location(btn, 214, 268,40,40);
//创建音乐 底部浅色 音量 进度条 底部背景
compo_shape_t *grey_shape ;
grey_shape = compo_radius_shape_create(frm, COMPO_SHAPE_TYPE_ROUNDED_RECTANGLE,1);
compo_shape_set_color(grey_shape, make_color(143, 143, 146));
compo_shape_set_location(grey_shape, GUI_SCREEN_CENTER_X, 268, 158, 4);
//创建音乐 音量 进度条
compo_shape_t *white_shape ;
white_shape = compo_radius_shape_create(frm, COMPO_SHAPE_TYPE_ROUNDED_RECTANGLE,1);
compo_shape_set_color(white_shape, COLOR_WHITE);
compo_shape_set_align_center(white_shape,false);//不居中了,居中后进度条长度不好算
compo_setid(white_shape, COMPO_ID_SHAPE_MUSIC_VOL);
compo_shape_set_location(white_shape, 41, 266, 4, 4);
//创建音乐 音量 进度条上的圆圈
compo_shape_t *cir_shape ;
cir_shape = compo_radius_shape_create(frm, COMPO_SHAPE_TYPE_ROUNDED_RECTANGLE,6);
compo_shape_set_color(cir_shape, COLOR_WHITE);
compo_shape_set_align_center(cir_shape,false);//不居中了,居中后进度条长度不好算
compo_setid(cir_shape, COMPO_ID_SHAPE_MUSIC_VOL_CIR);
compo_shape_set_location(cir_shape, 41, 261, 12, 12);
// compo_picturebox_t *vol_pic = compo_picturebox_create(frm, UI_BUF_MUSIC_VOLUME1_BIN);
// compo_setid(vol_pic, COMPO_ID_PIC_MUSIC_VOL);
// compo_picturebox_set_pos(vol_pic, GUI_SCREEN_CENTER_X, 280);
// widget_set_align_center(vol_pic->img, false);
// compo_picturebox_set_visible(vol_pic, false);
return frm;
}
//音乐歌词刷新,没有中文暂时屏蔽
static void func_bt_music_title_refresh(void *buf)
{
if (bt_cb.bt_form_created) {
compo_textbox_t *tilte_txt = compo_getobj_byid(COMPO_ID_TXT_MUSIC_LYRIC);
compo_textbox_set(tilte_txt, buf);
}
}
//音乐 歌名刷新,没有中文暂时屏蔽
static void func_bt_music_artist_refresh(void *buf)
{
if (bt_cb.bt_form_created) {
compo_textbox_t *tilte_art_txt = compo_getobj_byid(COMPO_ID_TXT_MUSIC_NAME);
compo_textbox_set(tilte_art_txt, buf);
}
}
//音乐播放或暂停 按钮刷新
static void func_bt_music_play_btnpic_refresh(u8 sta)
{
if (!bt_cb.bt_form_created) {
return ;
}
compo_button_t *btn = compo_getobj_byid(COMPO_ID_BTN_PLAY);
if (sta) {
compo_button_set_bgimg(btn, UI_BUF_MUSIC_PAUSE_CLICK_BIN);
} else {
compo_button_set_bgimg(btn, UI_BUF_MUSIC_PAUSE_BIN);
}
}
static void func_bt_music_vol_btnpic_refresh(u8 vol)
{
f_bt_t *f_bt = (f_bt_t *)func_cb.f_cb;
compo_shape_t *vol_shape_line;
compo_shape_t *vol_shape_cir;
vol_shape_line = compo_getobj_byid(COMPO_ID_SHAPE_MUSIC_VOL);
vol_shape_cir = compo_getobj_byid(COMPO_ID_SHAPE_MUSIC_VOL_CIR);
f_bt->shape_linx_width = vol*10;
compo_shape_set_location(vol_shape_line, 41, 266, f_bt->shape_linx_width, 4);
f_bt->shape_cir_x = vol*10;
//进度条不可以不限制因为会被圆挡住,这里在圆超出两个像素的时候往回拉
if(f_bt->shape_cir_x >158){
f_bt->shape_cir_x = 158;
}
compo_shape_set_location(vol_shape_cir, 35 + f_bt->shape_cir_x, 261, 12, 12);
// switch(vol){
// case 0:
// compo_shape_set_location(vol_shape_line, 41, 266, 0, 4);
// compo_shape_set_location(vol_shape_cir, 35, 261, 12, 12);
// break;
// }
// compo_picturebox_t *vol_pic;
// vol_pic = compo_getobj_byid(COMPO_ID_PIC_MUSIC_VOL);
// switch (vol){
// case 0:
// // compo_picturebox_set(vol_pic, UI_BUF_MUSIC_VOLUME1_BIN);
// // compo_picturebox_set_visible(vol_pic, false);
// break;
// case 1 ... 3:
// // compo_picturebox_set(vol_pic, UI_BUF_MUSIC_VOLUME1_BIN);
// // compo_picturebox_set_visible(vol_pic, true);
// break;
// case 4 ... 6:
// // compo_picturebox_set(vol_pic, UI_BUF_MUSIC_VOLUME2_BIN);
// // compo_picturebox_set_visible(vol_pic, true);
// break;
// case 7 ... 9:
// // compo_picturebox_set(vol_pic, UI_BUF_MUSIC_VOLUME3_BIN);
// // compo_picturebox_set_visible(vol_pic, true);
// break;
// case 10 ... 11:
// // compo_picturebox_set(vol_pic, UI_BUF_MUSIC_VOLUME4_BIN);
// // compo_picturebox_set_visible(vol_pic, true);
// break;
// case 12 ... 16:
// // compo_picturebox_set(vol_pic, UI_BUF_MUSIC_VOLUME5_BIN);
// // compo_picturebox_set_visible(vol_pic, true);
// break;
// }
}
//单击按钮
static void func_bt_button_click(void)
{
int id = compo_get_button_id();
switch (id) {
case COMPO_ID_BTN_PREV:
bt_control_prev();
break;
case COMPO_ID_BTN_NEXT:
bt_control_next();
break;
case COMPO_ID_BTN_PLAY:
bt_control_play_pause();
break;
case COMPO_ID_BTN_VOL_UP:
bt_control_vol_up();
break;
case COMPO_ID_BTN_VOL_DOWN:
bt_control_vol_down();
break;
default:
break;
}
}
void func_bt_sub_process(void)
{
bsp_bt_status();
}
void func_bt_process(void)
{
func_process();
func_bt_sub_process();
}
//蓝牙音乐消息处理
static void func_bt_message(size_msg_t msg)
{
f_bt_t *f_bt = (f_bt_t *)func_cb.f_cb;
switch (msg) {
case MSG_CTP_CLICK:
func_bt_button_click(); //单击按钮
break;
case MSG_QDEC_FORWARD:
bt_control_vol_up();
break;
case MSG_QDEC_BACKWARD:
bt_control_vol_down();
break;
case EVT_ID3_TITLE_UPDATE:
func_bt_music_title_refresh(f_bt->title_buf);
break;
case EVT_ID3_ARTIST_UPDATE:
func_bt_music_artist_refresh(f_bt->artist_buf);
break;
case MSG_SYS_500MS:
if (bt_is_connected() && !sys_cb.gui_sleep_sta) {
#if BT_ID3_TAG_EN
bt_music_paly_status_info();
#endif
if (f_bt->bt_play_sta != bt_cb.music_playing) {
f_bt->bt_play_sta = bt_cb.music_playing;
func_bt_music_play_btnpic_refresh(bt_cb.music_playing);
}
bt_vol_update();
}
break;
default:
func_message(msg);
break;
}
}
void func_bt_enter(void)
{
f_bt_t *f_bt;
func_cb.f_cb = func_zalloc(sizeof(f_bt_t));
func_cb.frm_main = func_bt_form_create();
f_bt = (f_bt_t *)func_cb.f_cb;
func_cb.mp3_res_play = func_bt_mp3_res_play;
bt_cb.bt_form_created = 1;
f_bt->bt_play_sta = bt_cb.music_playing;
f_bt->ams_play_sta = false;
f_bt->vol = 0;
func_bt_music_play_btnpic_refresh(bt_cb.music_playing);
#if LE_AMS_CLIENT_EN
if (ble_ams_is_connected()) {
if (ble_ams_cb.play_state) {
f_bt->ams_play_sta = true;
func_bt_music_play_btnpic_refresh(1);
}
f_bt->ams_vol = ble_ams_cb.vol;
func_bt_music_vol_btnpic_refresh(f_bt->ams_vol);
if (strlen(ble_ams_cb.app_name)) {
compo_form_set_title(func_cb.frm_main, ble_ams_cb.app_name);
}
}
ble_ams_sta_update_cb_reg(ble_ams_sta_update_handle);
#endif
bt_id3_tag_update_cb_reg(bt_id3_tag_update_handle);
#if !BT_BACKSTAGE_MUSIC_EN
func_bt_init();
bt_audio_enable();
#if DAC_DNR_EN
dac_dnr_set_sta(1);
sys_cb.dnr_sta = 1;
#endif
#endif // !BT_BACKSTAGE_MUSIC_EN
}
void func_bt_exit(void)
{
#if !BT_BACKSTAGE_MUSIC_EN
dac_fade_out();
#if DAC_DNR_EN
dac_dnr_set_sta(0);
sys_cb.dnr_sta = 0;
#endif
bt_audio_bypass();
#if !BT_BACKSTAGE_EN
bt_disconnect(1);
bt_off();
bt_cb.bt_is_inited = 0;
#else
if (bt_get_status() == BT_STA_PLAYING && !bt_is_testmode()) { //蓝牙退出停掉音乐
delay_5ms(10);
if(bt_get_status() == BT_STA_PLAYING) { //再次确认play状态
u32 timeout = 850; //8.5s
bt_music_pause();
while (bt_get_status() == BT_STA_PLAYING && timeout > 0) {
timeout--;
delay_5ms(2);
}
}
}
#endif // !BT_BACKSTAGE_EN
#endif // !BT_BACKSTAGE_MUSIC_EN
ble_ams_sta_update_cb_unreg();
bt_id3_tag_update_cb_unreg();
bt_cb.bt_form_created = 0;
bt_cb.rec_pause = 0;
func_cb.last = FUNC_BT;
}
void func_bt(void)
{
printf("%s\n", __func__);
func_bt_enter();
while (func_cb.sta == FUNC_BT) {
func_bt_process();
func_bt_message(msg_dequeue());
}
func_bt_exit();
}
TJD_BT_STATUS_E tjd_get_bt_connect_status(void)
{
TJD_BT_STATUS_E status;
if (ble_is_connected() && bt_is_connected()) {
status = TJD_BT_EDR_BLE_CONNECT;
} else if (bt_is_connected()) {
status = TJD_BT_EDR_CONNECT;
} else if (ble_is_connected() ) {
status = TJD_BT_BLE_CONNECT;
} else {
status = TJD_BT_NOT_CONNECT;
}
return status;
}