


/**
  ******************************************************************************
  * @file    BLV_485_Dev_BLWMusicFun.c
  * @author  BLW RCU  development Team
  * @version SongHan
  * @date    2022年03月17日
  * @brief  
	*    宝来威背景音乐  有5个通道
  ******************************************************************************
  */



#include "BLV_485_Dev_BLWMusicFun.h"



#include "string.h"
#include "SPI_SRAM.h"
#include "usart.h"
#include "check_fun.h"
#include "DBG.h"
#include "rw_logging.h"
#include "SRAM_Variable_Address.h"

#include "includes.h"


/*BLWMusic调光驱动代码*/

#define REPEATSENDTIMEMAX         0x03  //重发次数上限

#define MUSICVOICEVALUEMAX          30    //实际背景音乐音量最大值

#define BLWMusicSnMin               0      //最小Sn号
#define BLWMusicSnMax               15     //最大Sn号

#define DEVMUSICTYPE              21     //设备音乐类型


/*本地函数调用开始*/
uint8_t BLW_BLWMusicCycleCtrl(uint32_t dev_addr);           //发送
uint8_t BLW_Rs485_BLWMusic_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);  //接收
void BLW_Rs485_Music_Mode_Ctrl(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo, uint16_t DevOutputLoop);  //模式控制
void BLW_Rs485_Music_Voice_Ctrl(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo, uint16_t DevOutputLoop);  
void BLW_Rs485_Music_Voice_Read(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo);
void BLW_Rs485_Music_Default_Voice_Set(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo);

/*本地函数调用结束*/

/*******************************************************************************
* Function Name  : BLW_RS485_BLWMusic_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLW_RS485_BLWMusic_Data_Init(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO *Rs485Music)
{
	BUS_Public->polling_cf = (uint32_t)&BLW_BLWMusicCycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLW_Rs485_BLWMusic_Check;
	
	Rs485Music->MusicLoopValidNum = 0x05;     //2023-04-14
	
	Rs485Music->BackMusicState[0].CtrlCont.CtrlVoice = 12;
	Rs485Music->BackMusicState[1].CtrlCont.CtrlVoice = 12;
	
	Rs485Music->room_def_volume = Rs485Music->BackMusicState[0].CtrlCont.CtrlVoice*2;            //房间音乐上电默认音量
	Rs485Music->room_max_volume = 30;            //房间音乐最大音量
	Rs485Music->room_min_volume = 3;             //房间音乐最小音量
	Rs485Music->bathroom_def_volume = Rs485Music->BackMusicState[0].CtrlCont.CtrlVoice*2;        //洗手间音乐上电默认音量
	Rs485Music->bathroom_max_volume = 30;        //洗手间音乐最大音量
	Rs485Music->bathroom_min_volume = 3;         //洗手间音乐最小音量
	Rs485Music->doorbell_def_volume = 25;
	
	Rs485Music->music_volume.now_volume[0] = Rs485Music->room_def_volume;
	Rs485Music->music_volume.now_volume[1] = Rs485Music->bathroom_def_volume;
	Rs485Music->music_volume.now_volume[2] = Rs485Music->doorbell_def_volume;
	
	Rs485Music->BackMusicVoiceSetFlag = 0x01;
	Rs485Music->BackMusicVoiceSetSendCnt = 0x03;
	
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"添加BLWMusic设备");

}



/*******************************************************************************
* Function Name  : BLW_MusicCycleCtrl 
* Description    : BLWMusic调光轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_BLWMusicCycleCtrl(uint32_t dev_addr) 
{
	uint8_t i;     //用于遍历所有回路
	uint8_t Ret = RS485OCCUPYNOTIME;
	Device_Public_Information_G BUS_Public;
	RS485_MUSIC_INFO Rs485MusicInfo;
	uint8_t KeepFlag = 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485MusicInfo,sizeof(RS485_MUSIC_INFO),dev_addr+Dev_Privately);
	
	for(i = 0; i < Rs485MusicInfo.MusicLoopValidNum; i++)	   //遍历所有通道
	{

		if(0x01 == Rs485MusicInfo.MusicStateCtrlFlag[i].MusicModeCtrlVar)    //背景音乐模式控制标志
		{
			if(0x00 != Rs485MusicInfo.MusicStateCtrlCnt[i].MusicModeCtrlVar)    
			{
				Rs485MusicInfo.MusicStateCtrlCnt[i].MusicModeCtrlVar--; 
			}
			else
			{
				Rs485MusicInfo.MusicStateCtrlFlag[i].MusicModeCtrlVar = 0x00;
			}	
			Rs485MusicInfo.MusicChannelNo = i;
			BLW_Rs485_Music_Mode_Ctrl(&BUS_Public, &Rs485MusicInfo, i);  //背景音乐控制	
			Ret = RS485OCCUPYTIME;
			break;	
		}
		else if(0x00 != Rs485MusicInfo.MusicStateCtrlFlag[i].MusicVoiceCtrlVar)
		{
			if(0x00 != Rs485MusicInfo.MusicStateCtrlCnt[i].MusicVoiceCtrlVar)
			{
				Rs485MusicInfo.MusicStateCtrlCnt[i].MusicVoiceCtrlVar--; 
				Rs485MusicInfo.MusicChannelNo = i;
				BLW_Rs485_Music_Voice_Ctrl(&BUS_Public, &Rs485MusicInfo, i);  //背景音乐音量控制
				Ret = RS485OCCUPYTIME;
			}
			else
			{
				Rs485MusicInfo.MusicStateCtrlFlag[i].MusicVoiceCtrlVar = 0x00;
			}	
			break;	
		} 
		else if(0x01 == Rs485MusicInfo.MusicStateCtrlFlag[i].MusicReadFlagVar)
		{
			if(0x00 != Rs485MusicInfo.MusicStateCtrlCnt[i].MusicReadFlagVar)
			{
				Rs485MusicInfo.MusicStateCtrlCnt[i].MusicReadFlagVar--; 
			}
			else
			{
				Rs485MusicInfo.MusicStateCtrlFlag[i].MusicReadFlagVar = 0x00;
			}
			BLW_Rs485_Music_Voice_Read(&BUS_Public, &Rs485MusicInfo);           //背景音乐音量读取
			Ret = RS485OCCUPYTIME;
		}	
	}
	
	if(Rs485MusicInfo.BackMusicVoiceSetFlag == 0x01)       //2023-04-14   设定默认音量
	{
		if(0x00 != Rs485MusicInfo.BackMusicVoiceSetSendCnt)
		{
			Rs485MusicInfo.BackMusicVoiceSetSendCnt--; 
		}
		else
		{
			Rs485MusicInfo.BackMusicVoiceSetFlag = 0x00;
			Rs485MusicInfo.BackMusicVoiceSetSendCnt = 0x03;
		}		
		BLW_Rs485_Music_Default_Voice_Set(&BUS_Public, &Rs485MusicInfo);
		Ret = RS485OCCUPYTIME;
	}
	if(RS485OCCUPYTIME == Ret)
	{
		KeepFlag = 0x01;
		if(Rs485MusicInfo.DevSendCnt > REPEATSENDTIMEMAX)
		{
			if(Rs485MusicInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485_Music DEV_IS_OFFLINE");
				LOG_Device_Online_Record(DEVMUSICTYPE,BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
			}
			Rs485MusicInfo.DevOffline = DEV_IS_OFFLINE;   //离线
			if(Rs485MusicInfo.DevOffline != Rs485MusicInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485MusicInfo.DevOfflineLast = Rs485MusicInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}
		else
		{
			Rs485MusicInfo.DevSendCnt++;   //
		}
		
		/*通讯统计*/
		BLV_Communication_Record(&Rs485MusicInfo.comm_record,0x01,0x00);
		Rs485MusicInfo.inquire_tick = SysTick_1ms;
	}
	
	if(0x01 == KeepFlag)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485MusicInfo, sizeof(RS485_MUSIC_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485MusicInfo,sizeof(RS485_MUSIC_INFO),dev_addr+Dev_Privately);
				
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"Music crc_val:%02X\r\n",crc_val);
	}

	return Ret;
}


#define BLW_MUSIC_Mode_Ctrl_LEN   0x0B     

//背景音乐内容发送函数
void BLW_Rs485_Music_Mode_Ctrl(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo, uint16_t DevOutputLoop)
{													
                                                 //己方地址 发送类型 设备类型    收方地址 LEN                                校验和   CMD  指定播放器 播放状态
	uint8_t MusicData[BLW_MUSIC_Mode_Ctrl_LEN]; // = {0x00, 	0x00, DEVMUSICTYPE,	0x01, 	MUSIC_BLW_Audio1802_Fun_Ctrl_LEN, 	0x88, 	0x22, 	0x01, 	0x00     ,0x00, 0x00};    //模式控制  包括音量加和减
	              	
//	RS485_MUSIC_INFOP Rs485MusicInfo;       //背景音乐局部变量    

//  Rs485MusicInfo = CfgDevAdd->DevDataStruct;  
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"执行背景音乐模式发送函数，地址:%d 回路%d 状态:%08X",BUS_Public->addr,DevOutputLoop, Rs485MusicInfo->DevMusicCtrlState[DevOutputLoop].DevMusicCtrlWay.CtrlDir);	
	
	MusicData[0] = 0x00;
	MusicData[1] = BLV_SN_Get(&Rs485MusicInfo->MusicSn, BLWMusicSnMin, BLWMusicSnMax);  
	MusicData[2] = DEVMUSICTYPE;   
	MusicData[3] = BUS_Public->addr;
	MusicData[4] = BLW_MUSIC_Mode_Ctrl_LEN;   //数据长度
	MusicData[6] = 0x22;                //命令
	MusicData[7] = DevOutputLoop+1;   //当前通道
	MusicData[10] = 0x00;             //

#if LOGIC_FILE_EN
	switch(Rs485MusicInfo->DevMusicCtrlState[DevOutputLoop].DevMusicCtrlWay.CtrlDir)   //功能
#else
	switch(Rs485MusicInfo->DevMusicCtrlState[DevOutputLoop].CtrlCont.CtrlMode)   //backmusic_data[DevOutputLoop]
#endif		
	{
		case MUSIC_PLAY:  //播放
			MusicData[8] = 0x00 ;//2??   //EA  指定播放   B7    进入播放模式
		    MusicData[9] = 0x00;  //
			break;
		case MUSIC_STOP:  //暂停
			MusicData[8] = 0x01;//?Y? //B8   E4   退出播放模式
			MusicData[9] = 0x00; 
//			backmusic_data[8] = 0xFF; 
			break;
		case MUSIC_PRE:  //上一曲
			MusicData[8] = 0x04;//
			MusicData[9] = 0x00; 
			break;
		case MUSIC_NEX:  //下一曲
			MusicData[8] = 0x03;//
			MusicData[9] = 0x00; 
			break;
		case MUSIC_PLAY_SPE_1:  //指定播放第1首
			if(BUS_Public->Protocol==0x02)
			{
				MusicData[8] = 0x07;     //小宝开启模块反馈音
				MusicData[9] = 0x00;  
			}
			else
			{
				MusicData[8] = 0x00;  //
				MusicData[9] = 0x01;  // 第一首
			}
			break;
		case MUSIC_PLAY_SPE_2: //指定播放第2首
			if(BUS_Public->Protocol==0x02)
			{
				MusicData[8] = 0x08;    //小宝关闭模块反馈音
				MusicData[9] = 0x00;  
			}
			else
			{
				MusicData[8] = 0x00;  //
				MusicData[9] = 0x02;  //
			}
			break;
		case MUSIC_PLAY_SPE_3://指定播放第3首
			if(BUS_Public->Protocol==0x02)
			{
				MusicData[7] = 0x05;      //小宝播放欢迎词
				MusicData[8] = 0x00;
				MusicData[9] = 0x00;  
			}
			else
			{
				MusicData[8] = 0x00;  //
				MusicData[9] = 0x03;  //
			}
			break;
		case MUSIC_PLAY_SPE_4://指定播放第4首
			if(BUS_Public->Protocol==0x02)
			{
				MusicData[7] = 0x04;     //小宝播放门铃
				MusicData[8] = 0x00;
				MusicData[9] = 0x00;  
			}
			else
			{
				MusicData[8] = 0x00;  //
				MusicData[9] = 0x04;  //
			}
			break;
		case MUSIC_PLAY_SPE_5://指定播放第5首
			MusicData[8] = 0x00 ;//
			MusicData[9] = 0x05;  //
			break;
		case MUSIC_PLAY_SPE_6:  //指定播放第6首    
			MusicData[8] = 0x00 ;//
			MusicData[9] = 0x06;  //			
			break;
		
	}
	MusicData[5] = CheckSum_Overlook_Check(MusicData, BLW_MUSIC_Mode_Ctrl_LEN, 5);
	MCU485_SendString(BUS_Public->port,MusicData,BLW_MUSIC_Mode_Ctrl_LEN);
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,MusicData,BLW_MUSIC_Mode_Ctrl_LEN);
	
}

#define BLW_MUSIC_Voice_Ctrl_LEN   0x0B

/*相对调节音量控制函数   2023-04-15*/
void BLW_Rs485_Music_Voice_Ctrl(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo, uint16_t DevOutputLoop)
{
	uint8_t MusicVoice[BLW_MUSIC_Voice_Ctrl_LEN]; // = {0x00, 0x00, DEVMUSICTYPE, 0x01, MUSIC_BLW_Audio1802_Voi_Ctrl_LEN, 0x7E, 0x23, 0x02, 0x02, 0x03, 0x00};                    //音量控制
	
	memset(MusicVoice,0,sizeof(MusicVoice)/sizeof(uint8_t));

	MusicVoice[0] = 0x00;
	MusicVoice[1] = BLV_SN_Get(&Rs485MusicInfo->MusicSn, BLWMusicSnMin, BLWMusicSnMax);  
	MusicVoice[2] = DEVMUSICTYPE;   
	MusicVoice[3] = BUS_Public->addr;   //地址
	
	MusicVoice[4] = BLW_MUSIC_Voice_Ctrl_LEN;   //数据长度
	MusicVoice[6] = 0x23;                //命令
	MusicVoice[7] = DevOutputLoop+1;
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"485music 控制模式%d\r\n",Rs485MusicInfo->MusicStateCtrlFlag[DevOutputLoop].MusicVoiceCtrlVar);
	
	if(Rs485MusicInfo->MusicStateCtrlFlag[DevOutputLoop].MusicVoiceCtrlVar==0x01)
	{
		MusicVoice[8] = 0x02;    //相对调节
		MusicVoice[9] = 0x00;
		MusicVoice[10] = 0x02;   //音量加1
	}
	else if(Rs485MusicInfo->MusicStateCtrlFlag[DevOutputLoop].MusicVoiceCtrlVar==0x02)
	{
		MusicVoice[8] = 0x02;    //相对调节
		MusicVoice[9] = 0x00;
		MusicVoice[10] = 0x01;   //音量减1
	}
	else if(Rs485MusicInfo->MusicStateCtrlFlag[DevOutputLoop].MusicVoiceCtrlVar==0x03)
	{
		MusicVoice[8] = 0x01;    //绝对调节
		MusicVoice[9] = Rs485MusicInfo->music_volume.now_volume[DevOutputLoop];
		MusicVoice[10] = 0x00;
	}
	
	MusicVoice[5] = CheckSum_Overlook_Check(MusicVoice, BLW_MUSIC_Voice_Ctrl_LEN, 5);
	
//	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"485music 音量控制数据\r\n",MusicVoice,BLW_MUSIC_Voice_Ctrl_LEN);
	
	MCU485_SendString(BUS_Public->port,MusicVoice,BLW_MUSIC_Voice_Ctrl_LEN);
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,MusicVoice,BLW_MUSIC_Voice_Ctrl_LEN);
}
///*背景音乐音量控制函数*/
//void BLW_Rs485_Music_Voice_Ctrl(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo, uint16_t DevOutputLoop)
//{
//	uint8_t MusicVoice[BLW_MUSIC_Voice_Ctrl_LEN]; // = {0x00, 0x00, DEVMUSICTYPE, 0x01, MUSIC_BLW_Audio1802_Voi_Ctrl_LEN, 0x7E, 0x23, 0x02, 0x02, 0x03, 0x00};                    //音量控制


//	MusicVoice[0] = 0x00;
//	MusicVoice[1] = BLV_SN_Get(&Rs485MusicInfo->MusicSn, BLWMusicSnMin, BLWMusicSnMax);  
//	MusicVoice[2] = DEVMUSICTYPE;   
//	MusicVoice[3] = BUS_Public->addr;   //地址
//	
//	MusicVoice[4] = BLW_MUSIC_Voice_Ctrl_LEN;   //数据长度
//	MusicVoice[6] = 0x23;                //命令
//	MusicVoice[7] = DevOutputLoop+1;
//	
//	switch(Rs485MusicInfo->DevMusicCtrlState[DevOutputLoop].CtrlCont.CtrlMode)    //存储的模式
//	{
//		case MUSIC_VOICE_ALW_UP://音量一直加
//			MusicVoice[8] = 0x02;
//			MusicVoice[9] = 0x03;
//			MusicVoice[10] = 0x08;						
//			break;
//		case MUSIC_VOICE_ALW_DOWN: //音量一直减
//			MusicVoice[8] = 0x02;
//			MusicVoice[9] = 0x03;			
//			MusicVoice[10] = 0x04;			
//			break;
//		case MUSIC_VOICE_STOP:   
//			MusicVoice[8] = 0x02;
//			MusicVoice[9] = 0x03;			
//			MusicVoice[10] = 0x10;		//音量停止
//			break; 
//		default:                   //其他情况皆转换为音量控制
//			MusicVoice[8] = 0x01;                                               //指定音量
//			MusicVoice[9] = Rs485MusicInfo->BackMusicState[DevOutputLoop].CtrlCont.CtrlVoice*MUSICVOICEVALUEMAX/MUSICVOICELEVELMAX;     //实际音量控制
//			MusicVoice[10] = 0x00;
//			break;
//	}
//	

//	MusicVoice[5] = CheckSum_Overlook_Check(MusicVoice, BLW_MUSIC_Voice_Ctrl_LEN, 5);
//	
//	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"485music 音量控制数据\r\n",MusicVoice,BLW_MUSIC_Voice_Ctrl_LEN);
//	
//	MCU485_SendString(BUS_Public->port,MusicVoice,BLW_MUSIC_Voice_Ctrl_LEN);
//	/*日志记录*/
//	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,MusicVoice,BLW_MUSIC_Voice_Ctrl_LEN);
//}

#define MUSIC_VOICE_READ_LEN  7    //
/*背景音乐音量读取*/
void BLW_Rs485_Music_Voice_Read(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo)
{
	uint8_t MusicData[MUSIC_VOICE_READ_LEN];// = {0x00,  0x00,    DEVMUSICTYPE,  0x01,  MUSIC_VOICE_READ_LEN,  0x21,  0x20};

	
	MusicData[0] = 0x00;
	MusicData[1] = BLV_SN_Get(&Rs485MusicInfo->MusicSn, BLWMusicSnMin, BLWMusicSnMax);  
	MusicData[2] = DEVMUSICTYPE;   
	MusicData[3] = BUS_Public->addr;
	MusicData[4] = 	MUSIC_VOICE_READ_LEN;
  MusicData[5] = CheckSum_Overlook_Check(MusicData, MUSIC_VOICE_READ_LEN, 5);
	MusicData[6] = 0x20;
	
	MCU485_SendString(BUS_Public->port,MusicData,MUSIC_VOICE_READ_LEN);
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,MusicData,MUSIC_VOICE_READ_LEN);
}




#define RECDATALENMAX   24   //接收数据最大长度

#define IsMyData     0x00
#define IsNotMyData  0x01
/*******************************************************************************
* Function Name  : BLW_Rs485_Music_Check 
* Description    : BLWMusic调光接收数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLW_Rs485_BLWMusic_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"BLW_Rs485_Music_Check\r\n");
	uint8_t rev = IsNotMyData;
//	uint16_t temp;  //保存命令类型和数据长度
	
	Device_Public_Information_G BUS_Public;
	RS485_MUSIC_INFO Rs485Music;
	uint8_t data[RECDATALENMAX];
	
//	uint8_t Music_addr = SRAM_Read_Byte(dev_addr+Dev_Addr);  //485地址
//	uint8_t crc_val = SRAM_Read_Word(data_addr+5);        //保存校验
	if(len > RECDATALENMAX)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Len:%d Err",__func__,len);
		return rev;     //长度
	}
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝482数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	
	if((data[0] != BUS_Public.addr) || (DEVMUSICTYPE != (data[2]))
		||(len != (data[4]))
	   )	   //地址不对
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Addr:%d 格式错误",__func__,BUS_Public.addr);
		return rev;				//校验文件头
	}

	
	if(data[5] == CheckSum_Overlook_Check(data, len, 5))   //校验通过
	{
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485Music,sizeof(RS485_MUSIC_INFO),dev_addr+Dev_Privately);
//		temp = SRAM_Read_Byte(data_addr + 6);
		
		switch(data[6])  
		{
			case 0x31:      //上电音量设置回复
			case 0x32:			//模式回复
			case 0x30:			//读取音量回复
			case 0x33:      //音量回复
			case 0x22:
			case 0x23:
				rev = IsMyData;
			
				if(Rs485Music.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485Music DEV_IS_ONLINE");
					LOG_Device_Online_Record(DEVMUSICTYPE,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
				}
				Rs485Music.DevSendCnt = 0x00;
				Rs485Music.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485Music.DevOffline != Rs485Music.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485Music.DevOfflineLast = Rs485Music.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				BLV_Communication_Record(&Rs485Music.comm_record,0x02,0x01);		//记录通讯成功
				break;			
		}
		switch(data[6])
		{
			case 0x31:
//				Rs485Music.MusicCtrlFlag = 0x00;
				Rs485Music.MusicStateCtrlFlag[Rs485Music.MusicChannelNo].MusicDefaultVoiceSetVar = 0x00;
				Rs485Music.BackMusicVoiceSetFlag = 0x00;
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record3(dev_addr+Dev_port,dev_addr+Dev_baud,data_addr,len);
				break;
			case 0x32:
			case 0x22:
//				Rs485Music.MusicWayCtrlFlag = 0x00;
				Rs485Music.MusicStateCtrlFlag[Rs485Music.MusicChannelNo].MusicModeCtrlVar = 0x00;
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record3(dev_addr+Dev_port,dev_addr+Dev_baud,data_addr,len);
				break;
			case 0x30:
//			BLW_Rs485_BLWMusic_Lightness_Get(data, CfgDevAdd);
				/*检测到状态改变，才保存*/
//				Rs485Music.MusicLightnessReadFlag = 0x00;		
				Rs485Music.MusicStateCtrlFlag[Rs485Music.MusicChannelNo].MusicReadFlagVar = 0x00;			
				break;
			case 0x33:  //设置音量回复
			case 0x23:
				Rs485Music.MusicStateCtrlFlag[Rs485Music.MusicChannelNo].MusicVoiceCtrlVar = 0x00;
				break;
		}
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485Music, sizeof(RS485_MUSIC_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485Music,sizeof(RS485_MUSIC_INFO),dev_addr+Dev_Privately);
			
	}
//	else 
//		{
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"SRAM_CRC16 Fail ,crc_val:%02X SRAM_CRC16:%02X!!\r\n",data[5],SRAM_CRC16(data_addr,len - 2));
		
//	}
	
	return rev;
}

//设定默认音量
void BLW_Rs485_Music_Default_Voice_Set(Device_Public_Information_G *BUS_Public, RS485_MUSIC_INFO* Rs485MusicInfo)
{
	uint8_t MusicData[17];
	
	MusicData[0] = 0x00;
	MusicData[1] = BLV_SN_Get(&Rs485MusicInfo->MusicSn, BLWMusicSnMin, BLWMusicSnMax);  
	MusicData[2] = DEVMUSICTYPE;   
	MusicData[3] = BUS_Public->addr;
	MusicData[4] = 0x11;     //Lens
	MusicData[6] = 0x21;     //CMD
	MusicData[7] = Rs485MusicInfo->room_def_volume;
	MusicData[8] = Rs485MusicInfo->room_max_volume;
	MusicData[9] = Rs485MusicInfo->room_min_volume;
	MusicData[10] = 10;
	MusicData[11] = Rs485MusicInfo->bathroom_def_volume;
	MusicData[12] = Rs485MusicInfo->bathroom_max_volume;
	MusicData[13] = Rs485MusicInfo->bathroom_min_volume;
	MusicData[14] = 10;
	MusicData[15] = Rs485MusicInfo->doorbell_def_volume;
	MusicData[16] = 0x01;
	
	MusicData[5] = CheckSum_Overlook_Check(MusicData, 0x11, 5);
	
	MCU485_SendString(BUS_Public->port,MusicData,0x11);
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,MusicData,0x11);

}





