/**
  ******************************************************************************
  * @file    BLV_Nor_Dev_GlobalSet.c
  * @author  BLW RCU  YYW
  * @version V1.0.0
  * @date    2025年07月14日
  * @brief   全局设置，用于设置包括全局亮度、全局色温等全局属性  
  ******************************************************************************
  */


#include "includes.h"

void Dev_GlobalSet_Dis(uint32_t DevAddr);
void BLV_GlobalSet_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start);
uint16_t BLV_GlobalSet_Read_State(uint32_t devaddr, uint16_t DevOutputLoop);

void BLV_Nor_Dev_GlobalSet_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len){
	Device_Public_Information_G BUS_Public;
	NOR_GlobalSet_INFO GlobalSet_Inf;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&GlobalSet_Inf,0,sizeof(NOR_GlobalSet_INFO));
    
	BUS_Public.addr = dev_info->addr;		//设备地址
	BUS_Public.type = Dev_Virtual_GlobalSet;	//设备类型，2025-07-14，YYW
	BUS_Public.port = dev_info->type;		//设备发送端口
	BUS_Public.baud = 0000;			        //设备发送波特率

	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_GlobalSet_Dis;                  
//	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_SceneRestore_InType_Get;      
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_GlobalSet_Control_State;         
//	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLV_GlobalSet_Read_State;    
    
	/*RCU根据MCU复位状态进行设备初始化 - 
		保存标志位成立的情况下，如果是看门狗复位、软件复位、外部手动复位，将之前保存的状态进行设备初始化
		否则采用默认状态初始化(默认状态可通过RCU_POWER_Deivce_State_Init 宏定义 修改默认初始化状态)
	*/
//	if( (DevActionGlobal.sram_save_flag == 0xA8) && ((SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x01) || (SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x03) || (SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x05)) )
//	{
//		GlobalSet_Inf.global_light = DevActionGlobal.DimGlobalValue;
//		GlobalSet_Inf.global_light_last = DevActionGlobal.DimGlobalValue;
//	}else {
//		GlobalSet_Inf.global_light = 80;
//		GlobalSet_Inf.global_light_last = 80;
//	}
	
    Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&GlobalSet_Inf,sizeof(NOR_GlobalSet_INFO));
}


/*******************************************************************************
* Function Name  : Dev_GlobalSet_Dis 
* Description    : 回路扫描函数
* Input          : 
* devaddr	     : 设备信息地址
* Return         : 无
* attention      : 拿到回路的状态，并转换为动作
*******************************************************************************/
void Dev_GlobalSet_Dis(uint32_t DevAddr)
{
//	Device_Public_Information_G BUS_Public;   //共用	
//	NOR_GlobalSet_INFO GlobalSet_Inf;        //私有属性局部变量
    
//    static uint32_t GlobalSet_light_tick = 0;  
//	uint16_t ctrl_loop_state[LED_BUFF_Size];
//	uint32_t ctrl_flag = 0;
//	uint8_t ctrl_num = 0;
//	
//	uint8_t KeepFlag = 0x00;
//	
//	if(NULL == DevAddr)
//	{
//		return ;
//	}
	
//	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
//	SRAM_DMA_Read_Buff((uint8_t *)&GlobalSet_Inf,sizeof(NOR_GlobalSet_INFO),DevAddr+Dev_Privately); 
    
    
    //1、判断条件
//    if( (SysTick_10ms - GlobalSet_light_tick >= 50)&&(GlobalSet_Inf.global_light != GlobalSet_Inf.global_light_last) )      //全局亮度改变
//    if( (GlobalSet_Inf.global_light != GlobalSet_Inf.global_light_last) )
//    {
//        GlobalSet_Inf.global_light_last = GlobalSet_Inf.global_light;
//        
//        Dbg_Println(DBG_BIT_Debug_STATUS_bit,"GlobalSet_Inf Global_light:%d",GlobalSet_Inf.global_light);

//        KeepFlag = 0x01;
//    }    
    
    
//	if(GlobalSet_Inf.global_light != GlobalSet_Inf.global_light_last)      //全局亮度改变
//	{
//		GlobalSet_Inf.global_light_last = GlobalSet_Inf.global_light;
//		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"GlobalSet_Inf Global_light:%d",GlobalSet_Inf.global_light);
//		
//		for(uint8_t n = 0; n < GlobalSet_Inf.MapDev_Inf.MapDevNum; n++)
//		{
//			ctrl_flag = 0x00;
//			ctrl_num = 0x00;
//			memset(ctrl_loop_state,0,sizeof(ctrl_loop_state));
//			
//			ctrl_flag |= (0x01 << 0);
//			ctrl_num = 1;
//			
//			for(uint8_t i = 0;i < DNOR_GlobalSet_LoopNumMax; i++)
//			{
////				if(ColorTemp_Inf.MapDev_Inf.MapDevAddr[n] == ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[0])
//				{
//					switch(GlobalSet_Inf.Map_Loop_Info[i].Map_Type[0])
//					{
//						case DEV_RS485_PWM:      //C12等485类型调光
//							ctrl_loop_state[0] = (0x30 + (GlobalSet_Inf.global_light << 8)); 
//							BLW_LED_Group_Ctrl(0x00, 0x00,GlobalSet_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
//                            //2025-07-14,YYW,c12需要所有回路控制一次
//							break;
//						#if C8_TYPE
//						case Dev_Rs485_PB20:
//						case Dev_Rs485_PB20_LS:	
//							ctrl_loop_state[0] = (0x0A + (GlobalSet_Inf.global_light << 8));
//							uint32_t tempaddr = Find_AllDevice_List_Information2(Bus_port,Dev_Rs485_PB20,0x01);
//							BLV_PB20_Group_Control_State(0x00, 0x00,tempaddr,ctrl_flag,ctrl_num,ctrl_loop_state);
//							n = GlobalSet_Inf.MapDev_Inf.MapDevNum;      
//                            //2025-07-14,YYW,C8中所有PB20-LS都挂载在一个总线，只控制一次
//							break;
//						#endif
//					}
//					break;
//				}
//			}
//		}
//		KeepFlag = 0x01;
//	}	
	
    //3、保存数据
//	if(0x01 == KeepFlag)
//	{
//		BUS_Public.check = 0x00;
//		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&GlobalSet_Inf, sizeof(NOR_GlobalSet_INFO));
//		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
//		SRAM_DMA_Write_Buff((uint8_t *)&GlobalSet_Inf,sizeof(NOR_GlobalSet_INFO),DevAddr+Dev_Privately);		
//	}
	
}

/*******************************************************************************
* Function Name  : BLW_GlobalSet_Control_State 
* Description    : 状态控制函数
* Input          : 
* devaddr	     : 设备信息地址
* Return         : 无
* attention      : temp1非0才会进行保存
*******************************************************************************/
void BLV_GlobalSet_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start)
{
	Device_Public_Information_G BUS_Public;     //共用	
//	NOR_GlobalSet_INFO GlobalSet_Inf;           //私有
    
	uint8_t ctr_loop = DevOutputLoop & 0xFF;    //控制的回路号	
	uint8_t ctr_mode = start & 0xff;            //控制模式
    uint8_t ctr_state = (start >> 8) & 0xff;      //控制内容
	uint8_t keep_t = 0; 
    
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
//	SRAM_DMA_Read_Buff((uint8_t *)&GlobalSet_Inf,sizeof(NOR_GlobalSet_INFO),devaddr+Dev_Privately);

	if(DevOutputLoop >= DNOR_GlobalSet_LoopNumMax){           //有效回路
		return ;
	}
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"GlobalSet-Mode:%02X State:%2X ctr_loop:%2x",ctr_mode,ctr_state,ctr_loop);
	
	switch(ctr_mode)
	{
		case 0x01:       //设置全局亮度
//            if(ctr_loop == 0x01)
            {
//                GlobalSet_Inf.global_light = ctr_state;
                //DevActionGlobal.DimGlobalValue = GlobalSet_Inf.global_light;
                Dev_DimGlobalValue_Set(ctr_state);
                Dbg_Println(DBG_BIT_Debug_STATUS_bit,"GlobalSet_Inf Global_light:%d,ctr_mode:%d,",ctr_state,ctr_mode);
                keep_t++;
            }
			break;
        case 0x02:

            break;
		case 0x03:

            break;
		case 0x04:

            break;
		case 0x05:
//          if(ctr_loop == 0x05)
            {
//                GlobalSet_Inf.global_light = ctr_state;

				//增加方法 - 退出睡眠模式，跳过执行起夜动作，直接进入起夜模式 （前提是当前处于睡眠模式）
				if(  (ctr_state == 0x01)
				  && (DevActionGlobal.SleepMode_State == 0x01)					//且在睡眠模式
				  && (DevActionGlobal.SleepLight_State == 0x00) )
				{
					Dev_SleepState_Set();
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"GlobalSet_Inf SleepState:%d,ctr_mode:%d,",ctr_state,ctr_mode);
					keep_t++;					
				}

            }
            break;
        default:
            break;
	}
	
	
	if(keep_t != 0x00)
	{
		Udp_Addtion_Roomstate(Dev_Virtual_GlobalSet,BUS_Public.addr,DevOutputLoop,start);
//        BUS_Public.check = 0x00;
//        BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&GlobalSet_Inf, sizeof(NOR_GlobalSet_INFO));
//        SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
//        SRAM_DMA_Write_Buff((uint8_t *)&GlobalSet_Inf,sizeof(NOR_GlobalSet_INFO),devaddr+Dev_Privately);
//        
	}	
}


/*******************************************************************************
* Function Name  : BLV_GlobalSet_Read_State 
* Description    : 回路状态得到函数
* Input          :  
* devaddr	     : 设备信息地址
* DevOutputLoop  : 需要查找的回路 从0开始
* ret            : 回路状态返回 ：
* attention      : 回路状态得到
*******************************************************************************/
uint16_t BLV_GlobalSet_Read_State(uint32_t devaddr, uint16_t DevOutputLoop)
{
	NOR_GlobalSet_INFO GlobalSet_Inf;           //私有
	
	if(devaddr == 0x00)
		return 0x00;
    
	SRAM_DMA_Read_Buff((uint8_t *)&GlobalSet_Inf,sizeof(NOR_GlobalSet_INFO),devaddr+Dev_Privately);	

	
    
    
    //返回，
	return 0x00;
}


//2025-07-26,YYW,设置全局亮度,带全局亮度上、下限
uint8_t Dev_DimGlobalValue_Set(uint16_t lit)
{   
    uint8_t ret = 0x00;
    
    if(lit > DevActionGlobal.Dim_Upper_limit)  {
        DevActionGlobal.DimGlobalValue = DevActionGlobal.Dim_Upper_limit; 
        ret = 0x03;
    }else if(lit < DevActionGlobal.Dim_Lower_limit){
        DevActionGlobal.DimGlobalValue = DevActionGlobal.Dim_Lower_limit; 
        ret = 0x02;
    }else{
        DevActionGlobal.DimGlobalValue = lit;  
        ret = 0x01;
    } 
    
    return ret;
}
uint16_t Dev_DimGlobalValue_Get(void)
{
    return DevActionGlobal.DimGlobalValue;
}


//2026-03-26,李世杰 增加方法 - 退出睡眠模式，跳过执行起夜动作，直接进入起夜模式
uint8_t Dev_SleepState_Set(void)//uint8_t SleepState)
{   
    uint8_t ret = 0x00;	
	
	DevActionGlobal.SleepMode_State = 0x00;					//退出睡眠模式 2026-03-26
	DevActionGlobal.SleepLight_State = 0x01;
	DevActionGlobal.DevActionU64Cond.NeightState = NightModeOpen;	//跳过起夜 2026-03-26
	
    return ret;
}





