——来自迪文开发者论坛
本期为大家推送迪文开发者论坛获奖开源案例——??迪文屏与安卓手机双向联动控制音乐播放??。工程师通过蓝牙模块实现了迪文与安卓手机的数据交互,用户可以轻松控制安卓手机、U盘中的歌曲播放、暂停、换曲及音效设置,体验智能屏与安卓手机的双向便捷操控。
【演示视频】
完整开发资料含迪文屏DGUS工程资料与C51代码,获取方式:
1. 前往迪文开发者论坛获取:http://inforum.dwin.com.cn:20080/forum.php?mod=viewthread&tid=9992&extra=page%3D1
2. 微信公众号中回复“音乐播放”获取。
【UI开发示例】
【C51工程设计】
T5L 串口与蓝牙模块进行数据交互,修改播放状态、设置音量、读取蓝牙状态,部分参考代码如下:
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编写,主要实现与智能屏进行蓝牙连接、通信控制,部分参考代码如下:
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("搜索连接中"); }
}
下一篇:【开源获奖案例】多功能称重器