本帖最后由 大明哥 于 2024-4-9 14:26 编辑
音频播放器迪文屏加安卓手机 联动控制音乐播放,通过串口,BLE 先看下演示效果,地址如下
这次的主要控制结构是 迪文屏通过串口4和播放芯片通信,播放芯片通过蓝牙和安卓手机通信,从而达到双向联动控制 迪文屏幕大家比较熟悉,看起来比较简单,其它的代码,整个比较大,看起来可能会吃力点。
屏幕的界面控件如下
屏幕的源码如下:
int main(void) { INIT_CPU(); T2_Init(); UART4_Init(); EA=1; // UART4_SendStr("hello",sizeof("hello")); // WDT_ON();//打开开门狗
while(1) { // WDT_RST();//喂狗 Process(); } }
void UART4_Init(void) { //UART4 波特率设置: //BODE2_DIV_H =CPU 主频/(8*波特率) 。 //206438400/8/115200=224=0xe0 //206438400/8/921600=28=0x1C //206438400/8=25804800/230400=112=0x70
SCON2T=0x80; SCON2R=0x80; BODE2_DIV_H=0x00; //FCLK/(8*DIV) BODE2_DIV_L=0xe0;//; 921600 波特率 // BODE2_DIV_H=0x00; //FCLK/(8*DIV) // BODE2_DIV_L=0x70;
//ES3T=1; ES2R=1; EA=1; }
void TenMsHandle(void) { if(!TenmsFlag) return; TenmsFlag=0;
SourcePress();//音源改变 PlayCtrPress();//播放状态改变 VolCtrSlider();//音量改变 VolLogSlider();//音量改变
ReadBtState();//读取蓝牙状态 }
void SourcePress(void) { charTouchKey[2]={0,0}; u8Sdata[2];
read_dgusii_vp(0x3000,TouchKey,1); if(TouchKey[1]==0) return;
Sdata[1]=0; switch(TouchKey[1]) { case1://U盘 Sdata[0]=1; SendDataToBT(Write_Run_Mode,Sdata,1); break;
case2://外部LineIn输入 Sdata[0]=2; SendDataToBT(Write_Run_Mode,Sdata,1); break;
case3://蓝牙 Sdata[0]=3; SendDataToBT(Write_Run_Mode,Sdata,1); break; }
write_dgusii_vp(0x3003,TouchKey,1); memset(TouchKey,0,sizeof(TouchKey)); write_dgusii_vp(0x3000,TouchKey,1); ReadBtStatecount=0; }
void PlayCtrPress(void) { charTouchKey[2]={0,0}; u8Sdata[2];
read_dgusii_vp(0x3001,TouchKey,1); if(TouchKey[1]==0) return; Sdata[1]=0;
switch(TouchKey[1]) { case1://上一曲 Sdata[0]=1; SendDataToBT(Write_Next_Pre_PT,Sdata,1); break;
case2://播放暂停 Sdata[0]=2; SendDataToBT(Write_Next_Pre_PT,Sdata,1); break;
case3://下一曲 Sdata[0]=3; SendDataToBT(Write_Next_Pre_PT,Sdata,1); break; }
memset(TouchKey,0,sizeof(TouchKey)); write_dgusii_vp(0x3001,TouchKey,1); ReadBtStatecount=0; }
。。。。。。。 就不全部列出,感兴趣自已去看工程
安卓是用QT 写的,主要是写BLE的连接,和通信控制,和界面,其中一个代码如下
ui->setupUi(this); bluetooth_ble_Tool=new Bluetooth_ble_Tool("dwin_bt(BLE)"); ui->label_localBT_Name->setText(bluetooth_ble_Tool->getLocalName()); connect(bluetooth_ble_Tool,&Bluetooth_ble_Tool::BlueSearchConnectState, this,&BLE_PairPage::BlueSearchConnectState); //btchat=new BtChat(); //btchat->set_ble_Tool(bluetooth_ble_Tool); //btchat->init_Page(); //btchat->hide(); devicecommHandle=new DeviceComm(); devicecommHandle->set_ble_Tool(bluetooth_ble_Tool); devicecommHandle->InitDevice(); mwin=new mainwin(); mwin->hide(); connect(mwin,&mainwin:layChangeMode, devicecommHandle,&DeviceComm:layChangeMode);//写模式改变下发 connect(mwin,&mainwin:layCtrPreNextSt, devicecommHandle,&DeviceComm:layCtrPreNextSt);//写上下曲 connect(mwin,&mainwin:layMusicVolChange, devicecommHandle,&DeviceComm:layMusicVolChange);//写音量改变下发 connect(devicecommHandle,&DeviceComm::ReadPlayMode, this,[=](int mode){//设置目前的模式 qDebug()<<"mode:"<<mode; mwin->B_Upan->setChecked(false); mwin->B_AUX->setChecked(false); mwin->B_BT->setChecked(false); if(mode==1) mwin->B_Upan->setChecked(true); else if(mode==2) mwin->B_AUX->setChecked(true); else if(mode==3) mwin->B_BT->setChecked(true); }); connect(devicecommHandle,&DeviceComm::ReadPlayMusicVol, this,[=](int vol){//设置音量显示 mwin->QS_MainVol->setValue(vol); }); connect(bluetooth_ble_Tool,&Bluetooth_ble_Tool::BLE_Link_error, this,[=](){ this->show(); mwin->hide(); devicecommHandle->TimerStopReadDeviceST(); this->ui->progressBar->setValue(0); });
void BLE_PairPage::on_pushButton_clicked(){ bluetooth_ble_Tool->SetOperUuidServer(QBluetoothUuid(serviceUuid)); //ui->label_localBT->clear(); //ui->label_localBT_Name->setText(bluetooth_ble_Tool->getLocalName()); bluetooth_ble_Tool->startFindConnectBle(); //bluetooth_ble_Tool->startStateprogressBar(); ui->pushButton->setEnabled(false); ui->pushButton->setText("正在搜索"); connect(bluetooth_ble_Tool,&Bluetooth_ble_Tool::BlueBLE_FindDevicefinished, this,[=](bool ok){ ui->pushButton->setEnabled(true); ui->pushButton->setText("开始检测连接蓝牙"); if(ok==false) { QMessageBox::information(this,tr("搜索状态"),"搜索不到设备,请查看设备是否已经开机,或者有其它手机已经连接上?"); } });} void BLE_PairPage::BlueSearchConnectState(Bluetooth_ble_Tool::startStateprogressBar_em Linkst, int progress){ ui->progressBar->setValue(progress); if(Linkst>=Bluetooth_ble_Tool::Ble_OtherError) { QMessageBox::information(this,tr("连接提示"),bluetooth_ble_Tool->getErrorinfo()); ui->label_tips->setText(bluetooth_ble_Tool->getErrorinfo()); } else if(Linkst>=Bluetooth_ble_Tool::Ble_Deconnect_device) { QMessageBox::information(this,tr("连接提示"),"有错误"); ui->label_tips->setText("有错误"); } else if(Linkst==Bluetooth_ble_Tool::Ble_Connect_characterOk) {// QMessageBox::information(this,tr("成功连接"),bluetooth_ble_Tool->getErrorinfo()); ui->label_tips->setText("已成功连接"); this->hide(); mwin->show(); devicecommHandle->TimerStartReadDeviceST(100); //btchat->show(); // disconnect(bluetooth_ble_Tool,&Bluetooth_ble_Tool::BlueSearchConnectState,// this,&BLE_PairPage::BlueSearchConnectState); } else { ui->label_tips->setText("搜索连接中"); }}
蓝牙代码 列出一点点吧
extern void app_audio_direct_set_volume(u8 value);extern int user_app_send_user_data(u16 handle, u8 *data, u16 len, u8 handle_type); u8 Add_crc(u8 *data,u16 datalen){ u16 count=0; u8 ret=0; for(count=0;count<datalen;count++) { ret+=data[count]; } return ret;}
void UserBleRetRec(u16 retcmd,u8 *data,u16 datalen){ u8 senddata[250]; u16 i=0; memset(senddata,0,sizeof(senddata)); senddata[i++]=Target_addr; senddata[i++]=0; senddata[i++]=0; senddata[i++]=retcmd>>8; senddata[i++]=retcmd; memcpy(&senddata,data,datalen); i+=datalen; senddata[1]=(i+1)>>8; senddata[2]=i+1; senddata=Add_crc(senddata,i); i++; user_app_send_user_data(ATT_CHARACTERISTIC_ae05_01_VALUE_HANDLE, senddata, i, ATT_OP_AUTO_READ_CCC);} void UserBleRecHandle(u8 *data,u16 len){ u16 cmd=0; int ret=0; u8 retok=1; if(data[0]!=Local_addr) return; cmd=data[3]<<8|data[4]; switch(cmd) { case Write_Run_Mode: log_info("UserBleRecHandle %d\r\n",data[5]); if(data[5]==1)//U盘模式 {#if TCFG_APP_MUSIC_EN ret = app_task_switch_to(APP_MUSIC_TASK);#endif } else if(data[5]==2)//外部输入 {#if TCFG_APP_LINEIN_EN ret = app_task_switch_to(APP_LINEIN_TASK);#endif } else if(data[5]==3)//蓝牙 {#if TCFG_APP_BT_EN ret = app_task_switch_to(APP_BT_TASK);#endif } if(TRUE==ret) retok=1; else retok=0; UserBleRetRec(Write_Run_Mode,&retok,1); break; case Write_Next_Pre_PT: if(data[5]==1)//上一曲 { app_task_put_key_msg(KEY_MUSIC_PREV,0); } else if(data[5]==2)//播放暂停 { app_task_put_key_msg(KEY_MUSIC_PP,0); } else if(data[5]==3)//下一曲 { app_task_put_key_msg(KEY_MUSIC_NEXT,0); } retok=1; UserBleRetRec(Write_Next_Pre_PT,&retok,1); break; case Write_VolChange: app_audio_direct_set_volume(data[5]); retok=1; UserBleRetRec(Write_VolChange,&retok,1); break; case Read_device_ST: u8 retst[10]; retst[0]=app_var.music_volume; retst[1]=app_get_curr_task(); if(retst[1]==APP_MUSIC_TASK) retst[1]=1; else if(retst[1]==APP_LINEIN_TASK) retst[1]=2; else if(retst[1]==APP_BT_TASK) retst[1]=3; else retst[1]=0; UserBleRetRec(Read_device_ST,retst,2); break; }}
|