#include "includes.h"
#include <string.h>
#include <stdlib.h>

#define PowerBus_SendData(data,len)		UARTTransmit(UART0,data,len)		//PowerBus总线控制 数据发送
#define RCU_PB_SendData(data,len)		UARTTransmit(UART1,data,len)		//RCU与PB通讯 数据发送

PB_INFO_T g_PB;
PB_DETECTION_FILTER_INFO curr_monitoring;		//电流监视

void PB_Init(void){
	
	memset(&g_PB,0,sizeof(PB_INFO_T));
	memset(&curr_monitoring,0,sizeof(PB_DETECTION_FILTER_INFO));
	

	//PB 功能
	g_PB.enable = g_eeprom.powerbus_enable;
	g_PB.protect_curr = g_eeprom.save_curr;
    g_PB.protect_time = 5000;       //5S
	
	PB_Set_SaveCurrent(g_PB.protect_curr);		//设置保护电流
}

/*********************************************************************
 * @fn      PB_Set_SaveCurrent
 * @brief   PB - 设置PB 保护电流
 * @para	val:1~165
 */
void PB_Set_SaveCurrent(U8_T val){
	
	U32_T curr_val = 0;
	
	if( (val < PB_SaveCurrent_Min) || (val > PB_SaveCurrent_Max) )	return ;
	
	curr_val = val*4095/PB_SaveCurrent_Max;
	
	BT_Period_CMP_Write(BT0,4095,curr_val);
}

/*********************************************************************
 * @fn      PB_Set_Power_State
 * @brief   PB - 设置PB电源状态
 */
void PB_Set_Power_State(U8_T state){
    switch(state){
    case TRUE:
        g_PB.enable = TRUE;
    break;
    case FALSE:
        g_PB.enable = FALSE;
        g_PB.power_status = FALSE;
        DRV_PB_DISABLE;
    break;
    }
}

/*********************************************************************
 * @fn      PB_Soft_Boot_Task
 * @brief   PB - 软启动任务
 */
void PB_Soft_Boot_Task(void){
	
	if(g_PB.meas_hv_PB_State != 0x01)	return;		//PB输出电源未打开
	
    if(g_PB.enable != TRUE) return ;

    switch(g_PB.soft_boot){
    case 0x00:  //开启启动
        g_PB.soft_boot_high = 0x01;
        g_PB.soft_boot_low = PB_SOFTBOOT_PERIOD - g_PB.soft_boot_high;
        g_PB.soft_boot_tick = SysTick_100us;
        g_PB.soft_boot_cnt = 0;
        DRV_PB_DISABLE;
        g_PB.soft_boot = 0x01;

        Dbg_Println(DBG_BIT_SYS_STATUS, "PB_Soft_Boot_Task Start");
        break;
    case 0x01:  //软启动中 - 低电平输出时间
        if(SysTick_100us - g_PB.soft_boot_tick >= g_PB.soft_boot_low){
            g_PB.soft_boot_tick = SysTick_100us;

            DRV_PB_ENABLE;
            g_PB.soft_boot = 0x02;
            //Dbg_Println(DBG_BIT_SYS_STATUS, "PWM DutyLow %d",g_PB.soft_boot_low);
        }
        break;
    case 0x02:  //软启动中 - 高电平输出时间
        if(SysTick_100us - g_PB.soft_boot_tick >= g_PB.soft_boot_high){
            g_PB.soft_boot_tick = SysTick_100us;

            g_PB.soft_boot_cnt++;

            if(g_PB.soft_boot_cnt >= 3){
                g_PB.soft_boot_cnt = 0;

                g_PB.soft_boot_high++;
                g_PB.soft_boot_low = PB_SOFTBOOT_PERIOD - g_PB.soft_boot_high;
            }

            if(g_PB.soft_boot_high >= PB_SOFTBOOT_PERIOD){
                DRV_PB_ENABLE;
                g_PB.enable = FALSE;
                g_PB.power_status = TRUE;
                g_PB.soft_boot = 0x00;

                Dbg_Println(DBG_BIT_SYS_STATUS, "PB_Soft_Boot_Task End");

            }else {
                DRV_PB_DISABLE;
                g_PB.soft_boot = 0x01;
            }

            //Dbg_Println(DBG_BIT_SYS_STATUS, "PWM DutyHigh %d",g_PB.soft_boot_high);
        }
        break;
    }
}

/*PB 编码对照表*/
const U8_T PowerBus_Code_Comparative[16] = {0xEF,0xEE,0xED,0xB7,0xEB,0x77,0xD7,0x7D,0xE7,0xBB,0x7B,0xDB,0xBD,0xDD,0xDE,0xFF};
/*********************************************************************
 * @fn      PB_Data_Send
 * @brief   PB - 数据发送函数
 * @return  0x00:发送没问题，0x01:发送失败
 */
U8_T PowerBus_Data_Encoding(U8_T *CodeData,U8_T *Sourdata,U16_T SourLen){
    U8_T temp_num = 0;
    U8_T i=0;

    for(i=0;i<SourLen;i++){
        temp_num = (Sourdata[i] >> 4) & 0x0F;
        CodeData[i*2] = PowerBus_Code_Comparative[temp_num];

        temp_num = (Sourdata[i]) & 0x0F;
        CodeData[i*2 + 1] = PowerBus_Code_Comparative[temp_num];
    }

    //Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Encoding Buff:", CodeData, SourLen*2);

    return 0x00;
}

U8_T Uart_Intp_Send_Buff[PB_INTP_BUFFER_SIZE];		//中断发送buff
volatile U8_T UTS_send_Complete = 0;
volatile U8_T UTS_send_Length_temp = 0;
volatile U8_T UTS_send_Length = 0;

//中断发送  - 串口中断调用
void PB_Send_String_INT(void){
	if(UTS_send_Complete != 0x00){
		if(UTS_send_Length_temp >= UTS_send_Length){
			UTS_send_Complete = 0;
			UTS_send_Length_temp = 0;
		}else{
			if(UTS_send_Length_temp >= PB_INTP_BUFFER_SIZE){
				UTS_send_Complete = 0;
				UTS_send_Length_temp = 0;
			}else{
				//UARTTxByte(UART0,Uart_Intp_Send_Buff[UTS_send_Length_temp++]);
				CSP_UART_SET_DATA(UART0,Uart_Intp_Send_Buff[UTS_send_Length_temp++]);
				
				g_PB.send_tick = SysTick_1ms;	//更新发送时间戳
			}
		}
	}
}

/*********************************************************************
 * @fn      PowerBus_Data_Send
 * @brief   PowerBus - 总线数据发送函数
 * @return  0x00:发送没问题，0x01:发送失败
 */
U8_T PowerBus_Data_Send(U8_T *data,U16_T len){
    U8_T send_buff[500];
    memset(send_buff,0,sizeof(send_buff));
	U16_T send_len = len;
	
	if(send_len >= PB_BUFFER_SIZE)	send_len = PB_BUFFER_SIZE;

    //if(g_PB.power_status == 0x01)
	{
//        Dbg_Println(DBG_BIT_SYS_STATUS, "PB_Data_Send %d",len);
//        Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "PB_Data_Send Buff:", data, len);

		/*PowerBus - 总线通讯采用新编码格式*/
//        PowerBus_Data_Encoding(send_buff,data,len);
//		PowerBus_SendData(send_buff,len*2);

		/*PowerBus - 总线通讯 中断发送*/
		if(UTS_send_Complete == 0x00){
			memset((void *)Uart_Intp_Send_Buff,0,sizeof(PB_INTP_BUFFER_SIZE));
			PowerBus_Data_Encoding(Uart_Intp_Send_Buff,data,send_len);
			UTS_send_Complete = 0x01;
			UTS_send_Length = send_len*2;
			UTS_send_Length_temp = 0x01;
			UARTTxByte(UART0,Uart_Intp_Send_Buff[0]);
			g_PB.send_tick = SysTick_1ms;	//更新发送时间戳
		}else{
			//上次发送还未结束
			
			return 0xFF;
		}
		
        return 0x00;
    }

    return 0x01;
}

/*********************************************************************
 * @fn      PB_CheckSum
 * @brief   PB - 数据和校验取反
 * @return  0x00:发送没问题，0x01:发送失败
 */
U8_T PB_CheckSum(U8_T *buff,U16_T len){
    U8_T sum = 0;

    for(U16_T i=0;i<len;i++){
        sum+=buff[i];
    }

    return ~(sum);
}

/*********************************************************************
 * @fn      PB_OVERCURR_PWR_BUS_INT_Processing
 * @brief   PB - 过流保护中断 引脚中断服务函数中调用
 * @return  NULL
 */
void PB_OVERCURR_PWR_BUS_INT_Processing(void){
	//if(PB_OVERCURR_PWR_BUS == 0x01){
		/*检测OverCurr引脚状态  上升沿中断信号 High:过流保护中，Low:正常工作中*/
		PB_Set_Power_State(FALSE);
        g_PB.protect_flag = TRUE;
        g_PB.protect_tick = SysTick_1ms;
	//}
}

/*********************************************************************
 * @fn      PB_Protect_Task
 * @brief   PB - PB保护任务
 */
void PB_Protect_Task(void){
    if(g_PB.protect_flag == TRUE){
        Dbg_Println(DBG_BIT_SYS_STATUS, "PB Protect ....");
        if(PB_OVERCURR_PWR_BUS == 0x00){
            if(SysTick_1ms - g_PB.protect_tick >= g_PB.protect_time){
                g_PB.protect_flag = FALSE;

                PB_Set_Power_State(TRUE);

                //Dbg_Println(DBG_BIT_SYS_STATUS, "重新开启PB电源");
            }
        }
    }
}

/*********************************************************************
 * @fn      PB_Scan_State_Task
 * @brief   PB - 扫描状态 - 1ms一次，确保状态可以及时更新
 */
void PB_Scan_State_Task(void){

    /*检测OverCurr引脚状态，High:过流保护中，Low:正常工作中*/
    if(PB_OVERCURR_PWR_BUS == 0x01){
        PB_Set_Power_State(FALSE);
        g_PB.protect_flag = TRUE;
        g_PB.protect_tick = SysTick_1ms;

        //Dbg_Println(DBG_BIT_SYS_STATUS, "检测到 OverCurr High状态");
    }
}

/*********************************************************************
 * @fn      PowerBus_FillSendBuff
 * @brief   PowerBus - 写入发送缓冲区中
 * @return  NULL
 */
void PowerBus_FillSendBuff(U8_T resendNum,U8_T *data,U8_T len){
    if(len >= PB_BUFFER_SIZE) len = PB_BUFFER_SIZE;

	g_PB.resendNum = resendNum;
    g_PB.sendbufferlen[g_PB.sendWriteCnt] = len;
    memcpy(g_PB.sendbuffer[g_PB.sendWriteCnt], data, len);

    g_PB.sendWriteCnt++;
    if(g_PB.sendWriteCnt >= PB_BUFFER_NUM){
        g_PB.sendWriteCnt = 0;
    }
}

/*********************************************************************
 * @fn      PowerBus_PackFillBuff
 * @brief   PowerBus - 数据打包并写入发生缓冲区中
 * @return  NULL
 */
void PowerBus_PackFillBuff(U8_T resendNum,U8_T cmd,U8_T *data,U8_T len){

    g_PB.sendSN++;
    if(g_PB.sendSN >= 0xFA) g_PB.sendSN = 0x01;     //SN号范围 1~250

    g_PB.resendNum = resendNum;

    if((len + PowerBUS_FMT_PARAM) >= PB_BUFFER_SIZE) len = PB_BUFFER_SIZE - PowerBUS_FMT_PARAM;      //参数加上包头长度大于上限

    g_PB.sendbufferlen[g_PB.sendWriteCnt] = len + PowerBUS_FMT_PARAM;
    g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_LEN] = g_PB.sendbufferlen[g_PB.sendWriteCnt];
    g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_SN] = g_PB.sendSN;
    g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_CKS] = 0x00;
    g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_CMD] = cmd;

    memcpy(&g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_PARAM], data, len);

    g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_CKS] = PB_CheckSum(&g_PB.sendbuffer[g_PB.sendWriteCnt][PowerBUS_FMT_LEN], g_PB.sendbufferlen[g_PB.sendWriteCnt]);

//	Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Data Buff:", data, len);
//	Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Send Buff:", g_PB.sendbuffer[g_PB.sendWriteCnt], g_PB.sendbufferlen[g_PB.sendWriteCnt]);

    g_PB.sendWriteCnt++;
    if(g_PB.sendWriteCnt >= PB_BUFFER_NUM){
        g_PB.sendWriteCnt = 0;
    }

}

/*********************************************************************
 * @fn      PowerBus_SendBuffer_Task
 * @brief   BLV PowerBus通讯协议发送任务
 * @return  none
 */
void PowerBus_SendBuffer_Task(void){
	U8_T send_rev = 0;
    if(g_PB.sendWriteCnt != g_PB.sendReadCnt){
        if(SysTick_1ms - g_PB.send_tick >= POWERBUS_SEND_DELAY){

            if(g_PB.sendCnt < g_PB.resendNum){
                //Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "PowerBus_Data_Send",g_PB.sendbuffer[g_PB.sendReadCnt],g_PB.sendbufferlen[g_PB.sendReadCnt]);

                send_rev = PowerBus_Data_Send(g_PB.sendbuffer[g_PB.sendReadCnt], g_PB.sendbufferlen[g_PB.sendReadCnt]);
				if(send_rev != 0xFF){
					Set_PB_CurrMonitoring_Dead();
					g_PB.sendCnt++;
				}
				
            }else {
                g_PB.sendCnt = 0;

                g_PB.sendReadCnt++;
                if(g_PB.sendReadCnt >= PB_BUFFER_NUM){
                    g_PB.sendReadCnt = 0;
                }
				
				return ;	//直接退出不更新时间
            }

            //g_PB.send_tick = SysTick_1ms;
        }
    }
}

/*********************************************************************
 * @fn      PowerBUS_GetCommState
 * @brief   BLV PowerBus 获取当前PowerBUS总线发送状态
 * @return  none
 */
U8_T PowerBUS_GetCommState(void){
	
	if(g_PB.sendWriteCnt != g_PB.sendReadCnt){
		return 0x01;
	}
	
	return 0x00;
}

/*********************************************************************
 * @fn      PB_FilACkPacket
 * @brief   BLV PB控制协议 - 数据打包函数
 * @return  none
 */
void PB_FilACkPacket(U8_T *ackBuff,U8_T ackLen){
    ackBuff[PB_FMT_ADDR_TX] = PB_DEV_Addr;
    ackBuff[PB_FMT_TYPE] = (g_PB.recv_sn & 0x0F);
    ackBuff[PB_FMT_DEV_TYPE] = PB_DEV_TYPE;
    ackBuff[PB_FMT_ADDR_RX] = g_PB.recv_addr;
    ackBuff[PB_FMT_LEN] = ackLen;
    ackBuff[PB_FMT_CKS] = 0x00;
    ackBuff[PB_FMT_CMD] = g_PB.recv_cmd;

    ackBuff[PB_FMT_CKS] = PB_CheckSum(ackBuff, ackLen);
}

/*********************************************************************
 * @fn      PB_RS485_ReplyAck
 * @brief   BLV PB控制协议 - 应答回复
 * @return  none
 */
void PB_RS485_ReplyAck(void){
    g_PB.ackLen += PB_FMT_PARA;

    PB_FilACkPacket(g_PB.ackbuff,g_PB.ackLen);
    RCU_PB_SendData(g_PB.ackbuff,g_PB.ackLen);
	
	g_PB.ackLen = 0;
}

/*********************************************************************
 * @fn      PB_Set_CH_SaveBrightnessInfo
 * @brief   PB - 设置灯带回路保存灯光参数
 * @return  0x00:处理成功，其他值：处理失败
 */
U8_T PB_Set_CH_SaveBrightnessInfo(U8_T loop,U8_T brightness,U16_T gradualtime){
	
	switch(loop){
		case PWM_OUT_CH1:
			g_eeprom.brightness[PWM_OUT_CH1] = brightness;
			g_eeprom.gradialTime[PWM_OUT_CH1] = gradualtime;
			break;
		case PWM_OUT_CH2:
			g_eeprom.brightness[PWM_OUT_CH2] = brightness;
			g_eeprom.gradialTime[PWM_OUT_CH2] = gradualtime;
			break;
		case PWM_OUT_CH3:
			g_eeprom.brightness[PWM_OUT_CH3] = brightness;
			g_eeprom.gradialTime[PWM_OUT_CH3] = gradualtime;
			break;
		case PWM_OUT_CH4:
			g_eeprom.brightness[PWM_OUT_CH4] = brightness;
			g_eeprom.gradialTime[PWM_OUT_CH4] = gradualtime;
			break;
		default:
			return 0x01;
			break;
	}
	
	return 0x00;
}

/*********************************************************************
 * @fn      PB_Set_CH_SaveSwitchInfo
 * @brief   PB - 设置灯带回路保存开关状态参数
 * @return  0x00:处理成功，其他值：处理失败
 */
U8_T PB_Set_CH_SaveSwitchInfo(U8_T loop,U8_T switch_state){
	
	switch(loop){
		case PWM_OUT_CH1:
			g_eeprom.swithcState[PWM_OUT_CH1] = switch_state;
			break;
		case PWM_OUT_CH2:
			g_eeprom.swithcState[PWM_OUT_CH2] = switch_state;
			break;
		case PWM_OUT_CH3:
			g_eeprom.swithcState[PWM_OUT_CH3] = switch_state;
			break;
		case PWM_OUT_CH4:
			g_eeprom.swithcState[PWM_OUT_CH4] = switch_state;
			break;
		default:
			return 0x01;
			break;
	}
	
	return 0x00;
}

/*********************************************************************
 * @fn      PB_ACK_GET_STATE
 * @brief   BLV PB控制协议 - 查询当前状态   因为目前无法获取PB状态，只能回复0x00
 * @return  none
 */
void PB_ACK_GET_STATE(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T i=0;

	U16_T temp_val = Get_PB_CURR_VAL();
	U32_T temp_power = 0;

    //g_PB.recv_cmd = para[PB_FMT_CMD];

    ackPara[i++] = g_pwm.allBrightness;        			//全局亮度
	ackPara[i++] = g_pwm.allBrightnessUpLimit;        	//全局亮度可调上限
	ackPara[i++] = g_pwm.allBrightnessDownLimit;        //全局亮度可调下限
	
    ackPara[i++] = g_pwm.brightnessCurr[PWM_OUT_CH1];        //灯亮度 - CH4
    ackPara[i++] = g_pwm.brightnessCurr[PWM_OUT_CH2];        //灯亮度 - CH3
    ackPara[i++] = g_pwm.brightnessCurr[PWM_OUT_CH3];        //灯亮度 - CH2
    ackPara[i++] = g_pwm.brightnessCurr[PWM_OUT_CH4];        //灯亮度 - CH1
    ackPara[i++] = (g_PB.meas_powerstrip_volt >> 8) & 0xFF;        	//当前灯带电压 H
    ackPara[i++] = (g_PB.meas_powerstrip_volt) & 0xFF;;        		//当前灯带电压 L
    ackPara[i++] = 0x00;        //当前灯带CH1 电流 H
    ackPara[i++] = 0x00;        //当前灯带CH1 电流 L
    ackPara[i++] = 0x00;        //当前灯带CH2 电流 H
    ackPara[i++] = 0x00;        //当前灯带CH2 电流 L
    ackPara[i++] = 0x00;        //当前灯带CH3 电流 H
    ackPara[i++] = 0x00;        //当前灯带CH3 电流 L
    ackPara[i++] = 0x00;        //当前灯带CH4 电流 H
    ackPara[i++] = 0x00;        //当前灯带CH4 电流 L
    ackPara[i++] = 0x00;		//g_PB.switchState[group];        	//Bit7~Bit4:灯带开关状态MASK ,Bit3~Bit0:灯带故障状态
    ackPara[i++] = (g_PB.meas_powerbus_volt >> 8) & 0xFF;         //当前PB BUS电压 H
    ackPara[i++] = (g_PB.meas_powerbus_volt) & 0xFF;        //当前PB BUS电压 L
    ackPara[i++] = (temp_val >> 8) & 0xFF;        //当前PB BUS电流 H
    ackPara[i++] = (temp_val) & 0xFF;        //当前PB BUS电流 L
	temp_power = temp_val * g_PB.meas_powerbus_volt / 1000;
	ackPara[i++] = (temp_power >> 24) & 0xFF;        //当前PB BUS功率 H
    ackPara[i++] = (temp_power >> 16) & 0xFF;        //当前PB BUS功率 L
	ackPara[i++] = (temp_power >> 8) & 0xFF;        //当前PB BUS功率 H
    ackPara[i++] = temp_power & 0xFF;        		//当前PB BUS功率 L
    ackPara[i++] = g_PB.protect_flag;        		//PB BUS故障状态MASK
	
	ackPara[i++] = g_PB.enable;        				//PB 输出使能状态
	
	ackPara[i++] = Project_Software_Ver;        		//PB 软件版本
	ackPara[i++] = Project_Hardware_Ver;        		//PB 硬件版本
	ackPara[i++] = PowerBUS_GetCommState();				//PowerBus 当前通讯状态
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PS Voltage:%dmV",g_PB.meas_powerstrip_volt);
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB CURR:%dmA Voltage:%dmV %dmW",temp_val,g_PB.meas_powerbus_volt,temp_power);
	Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "ACK Buff:", ackPara, i);
	
    *ackLen = i;
}

/*********************************************************************
 * @fn      PB_ACK_SET_LED_BRIGHTNESS
 * @brief   BLV PB控制协议 - 设定灯带亮度
 * @return  none
 */
void PB_ACK_SET_LED_BRIGHTNESS(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	U8_T chnMask = para[PB_FMT_PARA];
	U8_T i = 0,save_flag = 0;
	U16_T temp_val = 0x00;
	
	//修改后的协议控制内容
	for(i=0;i<PWM_OUT_CH_MAX;i++){
		if((chnMask & (0x01 << i)) != 0x00){

			temp_val = para[PB_FMT_PARA+6+i]*100;
			PWM_SetCHGradualTime(i, temp_val);
			PWM_SetOutBrightness(i, para[PB_FMT_PARA+2+i]);
			
			Dbg_Println(DBG_BIT_SYS_STATUS,"Set PWM%d - %d %d",i,para[PB_FMT_PARA+2+i],para[PB_FMT_PARA+6]);
		
			//是否保存参数
			if( para[PB_FMT_PARA+1] == 0xF1){
				PB_Set_CH_SaveBrightnessInfo(i,para[PB_FMT_PARA+2+i],temp_val);
				save_flag = 0x01;
			}
		}
	}

	//目前展会使用的控制内容，旧协议内容
//	for(i=0;i<PWM_OUT_CH_MAX;i++){
//		if((chnMask & (0x01 << i)) != 0x00){
//
//			PWM_SetCHGradualTime(i, temp_val);
//			PWM_SetOutBrightness(i, para[PB_FMT_PARA+3+i]);
//			
//			Dbg_Println(DBG_BIT_SYS_STATUS,"Set PWM%d - %d %d",i,para[PB_FMT_PARA+3+i],temp_val);
//		}
//	}
	
	if(save_flag == 0x01){
		EEPROM_WriteParaInfo(&g_eeprom);		//保存参数
	}
	
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
}

void PB_ACK_SET_STRIP_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	U8_T chnMask = para[PB_FMT_PARA];
	U8_T i = 0,save_flag = 0;;

	for(i=0;i<PWM_OUT_CH_MAX;i++){
		if((chnMask & (0x01 << i)) != 0x00){

			Pwm_SetOnOffState(i, para[PB_FMT_PARA+2+i]);
			
			Dbg_Println(DBG_BIT_SYS_STATUS,"Set PWM%d - %d ",i,para[PB_FMT_PARA+2+i]);
			
			//是否保存参数
			if( para[PB_FMT_PARA+1] == 0xF1){
				PB_Set_CH_SaveSwitchInfo(i,para[PB_FMT_PARA+2+i]);
				save_flag = 0x01;
			}
		}
	}
	
	if(save_flag == 0x01){
		EEPROM_WriteParaInfo(&g_eeprom);		//保存参数
	}
	
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
}

void PB_ACK_SET_STRIP_ADJUST(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	U8_T chnMask = para[PB_FMT_PARA];
	U8_T i = 0;
	U16_T temp_val = 0;

	for(i=0;i<PWM_OUT_CH_MAX;i++){
		if((chnMask & (0x01 << i)) != 0x00){
			temp_val = para[PB_FMT_PARA+3+i*3];
			temp_val = temp_val * 100;
			PWM_SetAutoAdjust(i, para[PB_FMT_PARA+1+i*3],para[PB_FMT_PARA+2+i*3],temp_val);
			
			Dbg_Println(DBG_BIT_SYS_STATUS,"Set PWM%d - %d %d %dms",i,para[PB_FMT_PARA+1+i*3],para[PB_FMT_PARA+2+i*3],temp_val);
		}
	}
	
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
}

/*********************************************************************
 * @fn      PB_ACK_SET_PIRTIGGLE_LED
 * @brief   BLV PB控制协议 - 设定PB总线 设定LD PIR触发LED
 * @return  none
 */
void PB_ACK_SET_PIRTIGGLE_LED(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
    PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_PIR_ENABLE,&para[PB_FMT_PARA + 1],para_len);	
}

/*********************************************************************
 * @fn      PB_ACK_SET_CurrTiggleStrip
 * @brief   BLV PB控制协议 - 设定PB总线 PB总线电流触发灯带调光
 * @return  none
 */
void PB_ACK_SET_CurrTiggleStrip(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	//测试PB电流触发灯带调光

	U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA;
	U16_T temp_para = 0;
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
	
	if(para_len < 15) return ; 
	
	if((curr_monitoring.fun_enable == 0x00) && (para[PB_FMT_PARA] == 0x01)){
		//之前是关的，现在开启；需要先稳定，在开始判断
		Set_PB_CurrMonitoring_Dead();
	}
	
	curr_monitoring.fun_enable = para[PB_FMT_PARA];					//使能状态
	temp_para = para[PB_FMT_PARA + 1];
	temp_para = temp_para * 10 * g_PB.meas_powerbus_volt / 1000;
	curr_monitoring.trigger_threshold = temp_para;						//触发阈值
	curr_monitoring.trigger_debounce_num = para[PB_FMT_PARA + 2];		//触发消抖次数
	temp_para = para[PB_FMT_PARA + 3];
	temp_para = temp_para * 10 * g_PB.meas_powerbus_volt / 1000;
	curr_monitoring.release_threshold = temp_para;						//释放阈值
	curr_monitoring.release_debounce_num = para[PB_FMT_PARA + 4];		//释放消抖次数
	curr_monitoring.strip_relation = para[PB_FMT_PARA + 5];
	curr_monitoring.strip_gradient_time = para[PB_FMT_PARA + 6];
	curr_monitoring.strip_gradient_time = curr_monitoring.strip_gradient_time * 100;
	
	curr_monitoring.triggle_strip_bright[PWM_OUT_CH1] = para[PB_FMT_PARA + 7];
	curr_monitoring.triggle_strip_bright[PWM_OUT_CH2] = para[PB_FMT_PARA + 8];
	curr_monitoring.triggle_strip_bright[PWM_OUT_CH3] = para[PB_FMT_PARA + 9];
	curr_monitoring.triggle_strip_bright[PWM_OUT_CH4] = para[PB_FMT_PARA + 10];
	curr_monitoring.release_strip_bright[PWM_OUT_CH1] = para[PB_FMT_PARA + 11];
	curr_monitoring.release_strip_bright[PWM_OUT_CH2] = para[PB_FMT_PARA + 12];
	curr_monitoring.release_strip_bright[PWM_OUT_CH3] = para[PB_FMT_PARA + 13];
	curr_monitoring.release_strip_bright[PWM_OUT_CH4] = para[PB_FMT_PARA + 14];
	
	
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"fun_enable %d",curr_monitoring.fun_enable);
	Dbg_Println(DBG_BIT_SYS_STATUS,"trigger_threshold %dmW",curr_monitoring.trigger_threshold);
	Dbg_Println(DBG_BIT_SYS_STATUS,"trigger_debounce_num %d",curr_monitoring.trigger_debounce_num);
	Dbg_Println(DBG_BIT_SYS_STATUS,"release_threshold %dmW",curr_monitoring.release_threshold);
	Dbg_Println(DBG_BIT_SYS_STATUS,"release_debounce_num %d",curr_monitoring.release_debounce_num);
	Dbg_Println(DBG_BIT_SYS_STATUS,"strip_relation %d",curr_monitoring.strip_relation);
	Dbg_Println(DBG_BIT_SYS_STATUS,"strip_gradient_time %d",curr_monitoring.strip_gradient_time);
	Dbg_Println(DBG_BIT_SYS_STATUS,"triggle_strip_bright CH1 %d",curr_monitoring.triggle_strip_bright[PWM_OUT_CH1]);
	Dbg_Println(DBG_BIT_SYS_STATUS,"triggle_strip_bright CH2 %d",curr_monitoring.triggle_strip_bright[PWM_OUT_CH2]);
	Dbg_Println(DBG_BIT_SYS_STATUS,"triggle_strip_bright CH3 %d",curr_monitoring.triggle_strip_bright[PWM_OUT_CH3]);
	Dbg_Println(DBG_BIT_SYS_STATUS,"triggle_strip_bright CH4 %d",curr_monitoring.triggle_strip_bright[PWM_OUT_CH4]);
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"release_strip_bright CH1 %d",curr_monitoring.release_strip_bright[PWM_OUT_CH1]);
	Dbg_Println(DBG_BIT_SYS_STATUS,"release_strip_bright CH2 %d",curr_monitoring.release_strip_bright[PWM_OUT_CH2]);
	Dbg_Println(DBG_BIT_SYS_STATUS,"release_strip_bright CH3 %d",curr_monitoring.release_strip_bright[PWM_OUT_CH3]);
	Dbg_Println(DBG_BIT_SYS_STATUS,"release_strip_bright CH4 %d",curr_monitoring.release_strip_bright[PWM_OUT_CH4]);
	
	
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_SaveCurrInfo
 * @brief   BLV PB控制协议 - 设定PB总线 保护电流
 * @return  none
 */
void PB_ACK_SET_SaveCurrInfo(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	
	U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA;
	//*ackLen = 0;
	
	if(para_len < 0x02)	return ;
	
	g_PB.protect_curr =  para[PB_FMT_PARA];
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"protect_curr %d",g_PB.protect_curr);
	
	/*不在范围内，设置为默认值10A*/
	if((g_PB.protect_curr < PB_SaveCurrent_Min) || (g_PB.protect_curr > PB_SaveCurrent_Max)){
		g_PB.protect_curr = EEPROM_ParaDefault_SaveCurr;

		ackPara[0] = 0x01;		//设置失败
	}else{
		
		ackPara[0] = 0x00;		//设置成功
	}
	
	PB_Set_SaveCurrent(g_PB.protect_curr);
	
	if(para[PB_FMT_PARA + 1] == 0xF1){
		if(g_eeprom.save_curr != g_PB.protect_curr){
			g_eeprom.save_curr = g_PB.protect_curr;
			EEPROM_WriteParaInfo(&g_eeprom);		//保存参数
		}
	}
	
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
}

/*********************************************************************
 * @fn      PB_ACK_GET_SaveCurrInfo
 * @brief   BLV PB控制协议 - 获取PB总线 保护电流信息
 * @return  none
 */
void PB_ACK_GET_SaveCurrInfo(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	*ackLen = 1;
	
	ackPara[0] = g_PB.protect_curr;
}

/*********************************************************************
 * @fn      PB_ACK_SET_UniversPara
 * @brief   BLV PB控制协议 - 设定整体参数，设定PB整体亮度和PowerBus 总线整体亮度
 * @return  none
 */
void PB_ACK_SET_UniversPara(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	
	S8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	U8_T mask_bit = 0,rev = 0;
	U32_T temp_val = 0;
	
	if( para_len < 5 ) return ;
	
	mask_bit = para[PB_FMT_PARA + 1];
	
	if((mask_bit & 0x80) != 0x00){
		//设定全局亮度
		if( para[PB_FMT_PARA + 3] <= 100){
			g_eeprom.allBrightness = para[PB_FMT_PARA + 3];        			//全局亮度
			Pwm_SetAllBrightness(g_eeprom.allBrightness);
		}else{
			rev = 0x01;		//参数错误
		}
		
		Dbg_Println(DBG_BIT_SYS_STATUS,"Set allBrightness:%d",g_pwm.allBrightness);
	}
	if((mask_bit & 0x40) != 0x00){
		//设定全局可调上限
		if( para[PB_FMT_PARA + 4] <= 100){
			g_eeprom.allBrightnessUpLimit = para[PB_FMT_PARA + 4];        	//全局亮度可调上限
			PWM_SetUpLimitVal(g_eeprom.allBrightnessUpLimit);
		}else{
			rev = 0x01;		//参数错误
		}
		
		Dbg_Println(DBG_BIT_SYS_STATUS,"Set allBrightnessUpLimit:%d - %d",g_pwm.allBrightnessUpLimit,g_pwm.allPwmUpLimit);
	}
	if((mask_bit & 0x20) != 0x00){
		//设定全局可调下限
		if( (para[PB_FMT_PARA + 5] <= 100) && (para[PB_FMT_PARA + 5] < g_pwm.allBrightnessUpLimit) ){
			g_eeprom.allBrightnessDownLimit = para[PB_FMT_PARA + 5];        	//全局亮度可调上限
			PWM_SetDownLimitVal(g_eeprom.allBrightnessDownLimit);
		}else{
			rev = 0x01;		//参数错误
		}
		
		Dbg_Println(DBG_BIT_SYS_STATUS,"Set allBrightnessDownLimit:%d - %d",g_pwm.allBrightnessDownLimit,g_pwm.allPwmDownLimit);
	}
	
	if(para[PB_FMT_PARA + 2] == 0xF1){
		/*保存参数*/

		EEPROM_WriteParaInfo(&g_eeprom);		//保存参数
	}
	
	/*同时下发PowerBus*/
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_UniversPara");
    PowerBus_PackFillBuff(0x03,PowerBUS_CMD_SET_UniversPara,&para[PB_FMT_PARA + 1],para_len);
	
	*ackLen = 1;
	
	ackPara[0] = rev;
}

/*********************************************************************
 * @fn      PB_ACK_SET_PowerBus_EnablePara
 * @brief   BLV PB控制协议 - PB使能状态控制
 * @return  none
 */
void PB_ACK_SET_PowerBus_EnablePara(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	
	U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA;
	//*ackLen = 0;
	
	if(para_len < 0x02)	return ;
	
	PB_Set_Power_State(para[PB_FMT_PARA]);
	Dbg_Println(DBG_BIT_SYS_STATUS, "Power Enable %d",para[PB_FMT_PARA]);
	
	if(para[PB_FMT_PARA + 1] == 0xF1){
		
		if(g_PB.enable != g_eeprom.powerbus_enable){
			g_eeprom.powerbus_enable = g_PB.enable;
			EEPROM_WriteParaInfo(&g_eeprom);		//保存参数
		}
	}
	
	*ackLen = 1;
	ackPara[0] = PB_CMD_Reply_Succ;
}

/*********************************************************************
 * @fn      PB_ACK_PassThroug_Data
 * @brief   BLV PB控制协议 - PB透传命令
 * @return  none
 */
void PB_ACK_PassThroug_Data(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_PassThroug_Data");
	
	if(PowerBUS_GetCommState() == 0x00){
		
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
	
		PowerBus_FillSendBuff(para[PB_FMT_PARA] + 0x01,&para[PB_FMT_PARA + 2],para[PB_FMT_PARA + 1]);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
}


/*********************************************************************
 * @fn      PB_ACK_SET_PB_LEDS_BRIGHTNESS
 * @brief   BLV PB控制协议 - 设定PB总线 LED亮度				--按照地址段控制 每个回路可单独设定亮度
 * @return  none
 */
void PB_ACK_SET_PB_LEDS_BRIGHTNESS(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LEDS_BRIGHTNESS");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LEDS_BRIGHTNESS,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_LEDS_ADJUST
 * @brief   BLV PB控制协议 - 设定PB总线 LED亮度递增递减调节	--按照地址段控制
 * @return  none
 */
void PB_ACK_SET_PB_LEDS_ADJUST(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LEDS_ADJUST");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LEDS_ADJUST,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_LEDS_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 LED单开关状态			--按照单个回路控制
 * @return  none
 */
void PB_ACK_SET_PB_LEDS_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LEDS_SWITCH");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LEDS_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_LEDS_BRIGHT
 * @brief   BLV PB控制协议 - 设定PB总线 LED亮度				--按照地址段控制 
 * @return  none
 */
void PB_ACK_SET_PB_LEDS_BRIGHT(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LEDS_BRIGHT");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LEDS_BRIGHT,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_LED_BRIGHTNESS
 * @brief   BLV PB控制协议 - 设定PB总线 LED单回路亮度			--按照单个回路控制
 * @return  none
 */
void PB_ACK_SET_PB_LED_BRIGHTNESS(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LED_BRIGHTNESS");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LED_BRIGHTNESS,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
		
	}

}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_LED_Adjust
 * @brief   BLV PB控制协议 - 设定PB总线 LED单回路亮度调节			--按照单独回路控制
 * @return  none
 */
void PB_ACK_SET_PB_LED_Adjust(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LED_Adjust");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LED_ADJUST,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
		
	}
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_LED_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 LED单回路开关状态			--按照单独回路控制
 * @return  none
 */
void PB_ACK_SET_PB_LED_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_LED_SWITCH");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_LED_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_ALLLED_Switch
 * @brief   BLV PB控制协议 - 设定PB总线 LED全部设备开关状态
 * @return  none
 */
void PB_ACK_SET_PB_ALLLED_Switch(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "PB_ACK_SET_PB_ALLLED_Switch");
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_Succ;
		
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_ALLLED_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIPS_BRIGHTNESS
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_STRIPS_BRIGHTNESS(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	U8_T addr_field = 0;
	U8_T pack_buff[60];
	U8_T pack_len = 0;
	U32_T ch_mask = 0;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s %d",__func__,para_len);
	
	/*2024-06-20 修改主机这边组地址 按照64回路地址下发，而PowerBus还是按照32回路一组控制，因此需要手动分包*/
	if(para_len == 0x4B){
		
		if(PowerBUS_GetCommState() == 0x00){
			addr_field = para[PB_FMT_PARA + 1];		//地址段 0~3：正常地址段范围 ，大于4：属于群控地址
			ch_mask = para[PB_FMT_PARA + 5];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 4];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 3];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 2];
			Dbg_Println(DBG_BIT_SYS_STATUS, "L ch_mask :%08x",ch_mask);
			if(ch_mask != 0x00){
				//判断前32回路是否有控制状态
				
				//Dbg_Println(DBG_BIT_SYS_STATUS, "L ch_mask :%08x",ch_mask);
				
				memset(pack_buff, 0, sizeof(pack_buff));
				pack_len = 0;
				
				pack_buff[pack_len++] = addr_field*2;					//组地址
				pack_buff[pack_len++] = para[PB_FMT_PARA + 2];			//回路Mask
				pack_buff[pack_len++] = para[PB_FMT_PARA + 3];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 4];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 5];
				
				pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//是否保存
				pack_buff[pack_len++] = para[PB_FMT_PARA + 11];				//渐变时间
				
				memcpy(&pack_buff[pack_len], &para[PB_FMT_PARA + 12], 32);	//拷贝32回路的控制亮度
				pack_len+=32;
				
				Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
				
				PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_BRIGHTNESS,pack_buff,pack_len);
			}
			
			ch_mask = para[PB_FMT_PARA + 9];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 8];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 7];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 6];
			Dbg_Println(DBG_BIT_SYS_STATUS, "L ch_mask :%08x",ch_mask);
			if(ch_mask != 0x00){
				//判断后32回路是否有控制状态
				memset(pack_buff, 0, sizeof(pack_buff));
				pack_len = 0;
				
				//Dbg_Println(DBG_BIT_SYS_STATUS, "L ch_mask :%08x",ch_mask);
				
				pack_buff[pack_len++] = addr_field*2+1;					//组地址
				pack_buff[pack_len++] = para[PB_FMT_PARA + 6];			//回路Mask
				pack_buff[pack_len++] = para[PB_FMT_PARA + 7];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 8];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 9];
				
				pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//是否保存
				pack_buff[pack_len++] = para[PB_FMT_PARA + 11];				//渐变时间
				
				memcpy(&pack_buff[pack_len], &para[PB_FMT_PARA + 44], 32);	//拷贝32回路的控制亮度
				pack_len+=32;
				
				Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
				
				PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_BRIGHTNESS,pack_buff,pack_len);
			}
			
			//回复当前PowerBus 通讯状态空闲，处理成功
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_Succ;
		}else{
			//回复当前PowerBus 通讯状态繁忙
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_CommStateBusy;
		}
	}else{
		//回复当前PowerBus 参数错误
		*ackLen = 1;

		ackPara[0] = PB_CMD_Reply_ParaError;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIPS_ADJUST
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_STRIPS_ADJUST(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	U8_T addr_field = 0;
	U8_T pack_buff[60];
	U8_T pack_len = 0;
	U32_T ch_mask = 0;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	/*2024-06-20 修改主机这边组地址 按照64回路地址下发，而PowerBus还是按照32回路一组控制，因此需要手动分包*/
	if(para_len == 12){
		
		if(PowerBUS_GetCommState() == 0x00){
			addr_field = para[PB_FMT_PARA + 1];		//地址段 0~3：正常地址段范围 ，大于4：属于群控地址
			ch_mask = para[PB_FMT_PARA + 5];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 4];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 3];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 2];
			if(ch_mask != 0x00){
				//判断前32回路是否有控制状态
				
				memset(pack_buff, 0, sizeof(pack_buff));
				pack_len = 0;
				
				pack_buff[pack_len++] = addr_field*2;					//组地址
				pack_buff[pack_len++] = para[PB_FMT_PARA + 2];			//回路Mask
				pack_buff[pack_len++] = para[PB_FMT_PARA + 3];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 4];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 5];
				
				pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//模式
				pack_buff[pack_len++] = para[PB_FMT_PARA + 11];				//调节方向
				pack_buff[pack_len++] = para[PB_FMT_PARA + 12];				//调节时间
				
				Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
				PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_SWITCH,pack_buff,pack_len);
			}
			
			ch_mask = para[PB_FMT_PARA + 9];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 8];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 7];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 6];
			if(ch_mask != 0x00){
				//判断后32回路是否有控制状态
				memset(pack_buff, 0, sizeof(pack_buff));
				pack_len = 0;
				
				pack_buff[pack_len++] = addr_field*2+1;					//组地址
				pack_buff[pack_len++] = para[PB_FMT_PARA + 6];			//回路Mask
				pack_buff[pack_len++] = para[PB_FMT_PARA + 7];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 8];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 9];
				
				pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//模式
				pack_buff[pack_len++] = para[PB_FMT_PARA + 11];				//调节方向
				pack_buff[pack_len++] = para[PB_FMT_PARA + 12];				//调节时间
				
				Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
				PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_SWITCH,pack_buff,pack_len);
			}
			//回复当前PowerBus 通讯状态空闲，处理成功
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_Succ;
			
		}else{
			//回复当前PowerBus 通讯状态繁忙
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_CommStateBusy;
		}
	}else{
		//回复当前PowerBus 参数错误
		*ackLen = 1;

		ackPara[0] = PB_CMD_Reply_ParaError;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIPS_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_STRIPS_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	U8_T addr_field = 0;
	U8_T pack_buff[60];
	U8_T pack_len = 0;
	U32_T ch_mask = 0,off_mask = 0,on_mask = 0;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s %02x",__func__,para_len);
	
	/*2024-06-20 修改主机这边组地址 按照64回路地址下发，而PowerBus还是按照32回路一组控制，因此需要手动分包*/
	if(para_len == 0x4A){
		if(PowerBUS_GetCommState() == 0x00){
			addr_field = para[PB_FMT_PARA + 1];		//地址段 0~3：正常地址段范围 ，大于4：属于群控地址
			ch_mask = para[PB_FMT_PARA + 5];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 4];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 3];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 2];
			
			Dbg_Println(DBG_BIT_SYS_STATUS, "L ch_mask :%08x",ch_mask);
			if(ch_mask != 0x00){
				//判断前32回路是否有控制状态
				
				for(U8_T ch = 0;ch<32;ch++){
					if( (ch_mask & (0x01<<ch)) != 0x00 ) {
						//有控制状态 - 检查控制
						if(para[PB_FMT_PARA + 11 + ch] == 0x01){
							
							on_mask |= 0x01 << ch;
						}else if(para[PB_FMT_PARA + 11 + ch] == 0x02){
							
							off_mask |= 0x01 << ch;
						}
					}
				}
				
				if(on_mask != 0x00){
					memset(pack_buff, 0, sizeof(pack_buff));
					pack_len = 0;
				
					pack_buff[pack_len++] = addr_field*2;					//组地址
					
					pack_buff[pack_len++] = (on_mask & 0xFF);				//回路Mask
					pack_buff[pack_len++] = ((on_mask>>8) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((on_mask>>16) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((on_mask>>24) & 0xFF);			//回路Mask
					
					
					pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//是否保存
					
					pack_buff[pack_len++] = 0x01;				//开
					Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
					PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_ADJUST,pack_buff,pack_len);
				}
				
				if(off_mask != 0x00){
					memset(pack_buff, 0, sizeof(pack_buff));
					pack_len = 0;
					
					pack_buff[pack_len++] = addr_field*2;					//组地址
					
					pack_buff[pack_len++] = (off_mask & 0xFF);				//回路Mask
					pack_buff[pack_len++] = ((off_mask>>8) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((off_mask>>16) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((off_mask>>24) & 0xFF);			//回路Mask
					
					pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//是否保存
					
					pack_buff[pack_len++] = 0x00;				//关
					Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
					PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_ADJUST,pack_buff,pack_len);
				}
				
			}
			
			//前32个回路发送完成后，清除当前开关状态数据，然后进行后32个回路开关状态数据的装填
			on_mask = 0x00000000;
			off_mask = 0x00000000;
			
			ch_mask = para[PB_FMT_PARA + 9];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 8];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 7];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 6];
			if(ch_mask != 0x00){
				//判断后32回路是否有控制状态
				for(U8_T ch = 0;ch<32;ch++){
					if( (ch_mask & (0x01<<ch)) != 0x00 ) {
						//有控制状态 - 检查控制
						if(para[PB_FMT_PARA + 43 + ch] == 0x01){
							
							on_mask |= 0x01 << ch;
						}else if(para[PB_FMT_PARA + 43 + ch] == 0x02){
							
							off_mask |= 0x01 << ch;
						}
					}
				}
				
				if(on_mask != 0x00){
					memset(pack_buff, 0, sizeof(pack_buff));
					pack_len = 0;
				
					pack_buff[pack_len++] = addr_field*2+1;					//组地址
					
					pack_buff[pack_len++] = (on_mask & 0xFF);				//回路Mask
					pack_buff[pack_len++] = ((on_mask>>8) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((on_mask>>16) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((on_mask>>24) & 0xFF);			//回路Mask
					
					pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//是否保存
					
					pack_buff[pack_len++] = 0x01;				//开
					Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
					PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_ADJUST,pack_buff,pack_len);
				}
				
				if(off_mask != 0x00){
					memset(pack_buff, 0, sizeof(pack_buff));
					pack_len = 0;
					
					pack_buff[pack_len++] = addr_field*2+1;					//组地址
					
					pack_buff[pack_len++] = (off_mask & 0xFF);				//回路Mask
					pack_buff[pack_len++] = ((off_mask>>8) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((off_mask>>16) & 0xFF);			//回路Mask
					pack_buff[pack_len++] = ((off_mask>>24) & 0xFF);			//回路Mask
					
					pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//是否保存
					
					pack_buff[pack_len++] = 0x00;				//关
					Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
					PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIPS_ADJUST,pack_buff,pack_len);
				}
			}
			//回复当前PowerBus 通讯状态空闲，处理成功
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_Succ;
		}else{
			//回复当前PowerBus 通讯状态繁忙
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_CommStateBusy;
		}
	}else{
		//回复当前PowerBus 参数错误
		*ackLen = 1;

		ackPara[0] = PB_CMD_Reply_ParaError;
	}
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIP_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_STRIP_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIP_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIP_BRIGHTNESS
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_STRIP_BRIGHTNESS(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIP_BRIGHTNESS,&para[PB_FMT_PARA + 1],para_len);
	}else{
		
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIP_Adjust
 * @brief   BLV PB控制协议 - 设定PB总线 LS 递增、递减 - 指定回路控制
 * @return  none
 */
void PB_ACK_SET_PB_STRIP_Adjust(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIP_ADJUST,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIP_RELATIVE_GROUP_ADJUST
 * @brief   BLV PB控制协议 - 设定PB总线 LS 相对亮度调节 - 组地址控制
 * @return  none
 */
void PB_ACK_SET_PB_STRIP_RELATIVE_GROUP_ADJUST(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	U8_T addr_field = 0;
	U8_T pack_buff[60];
	U8_T pack_len = 0;
	U32_T ch_mask = 0;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	/*2024-07-02 修改主机这边组地址 按照64回路地址下发，而PowerBus还是按照32回路一组控制，因此需要手动分包*/
	if(para_len == 12){
		
		if(PowerBUS_GetCommState() == 0x00){
			addr_field = para[PB_FMT_PARA + 1];		//地址段 0~3：正常地址段范围 ，大于4：属于群控地址
			ch_mask = para[PB_FMT_PARA + 5];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 4];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 3];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 2];
			if(ch_mask != 0x00){
				//判断前32回路是否有控制状态
				
				memset(pack_buff, 0, sizeof(pack_buff));
				pack_len = 0;
				
				pack_buff[pack_len++] = addr_field*2;					//组地址
				pack_buff[pack_len++] = para[PB_FMT_PARA + 2];			//回路Mask
				pack_buff[pack_len++] = para[PB_FMT_PARA + 3];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 4];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 5];
				
				pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//模式
				pack_buff[pack_len++] = para[PB_FMT_PARA + 11];				//相对调节亮度值
				pack_buff[pack_len++] = para[PB_FMT_PARA + 12];				//只调亮的回路
				
				Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
				PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIP_RELATIVE_GROUP_ADJUST,pack_buff,pack_len);
			}
			
			ch_mask = para[PB_FMT_PARA + 9];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 8];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 7];
			ch_mask <<= 8;
			ch_mask |= para[PB_FMT_PARA + 6];
			if(ch_mask != 0x00){
				//判断后32回路是否有控制状态
				memset(pack_buff, 0, sizeof(pack_buff));
				pack_len = 0;
				
				pack_buff[pack_len++] = addr_field*2+1;					//组地址
				pack_buff[pack_len++] = para[PB_FMT_PARA + 6];			//回路Mask
				pack_buff[pack_len++] = para[PB_FMT_PARA + 7];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 8];
				pack_buff[pack_len++] = para[PB_FMT_PARA + 9];
				
				pack_buff[pack_len++] = para[PB_FMT_PARA + 10];				//模式
				pack_buff[pack_len++] = para[PB_FMT_PARA + 11];				//相对调节亮度值
				pack_buff[pack_len++] = para[PB_FMT_PARA + 12];				//只调亮的回路
				
				Dbg_Print_Buff(DBG_BIT_SYS_STATUS, "Pack BUFF",pack_buff,pack_len);
				PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIP_RELATIVE_GROUP_ADJUST,pack_buff,pack_len);
			}
			//回复当前PowerBus 通讯状态空闲，处理成功
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_Succ;
			
		}else{
			//回复当前PowerBus 通讯状态繁忙
			*ackLen = 1;
	
			ackPara[0] = PB_CMD_Reply_CommStateBusy;
		}
	}else{
		//回复当前PowerBus 参数错误
		*ackLen = 1;

		ackPara[0] = PB_CMD_Reply_ParaError;
	}
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_STRIP_RELATIVE_ADJUST
 * @brief   BLV PB控制协议 - 设定PB总线 LS 相对亮度调节 - 指定回路控制
 * @return  none
 */
void PB_ACK_SET_PB_STRIP_RELATIVE_ADJUST(U8_T *para,U8_T *ackPara,U16_T *ackLen){
	U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_STRIP_RELATIVE_ADJUST,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_ALLSTRIP_Switch
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_ALLSTRIP_Switch(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_ALLSTRIP_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_RELAYS_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_RELAYS_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_RELAYS_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
    
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_ALLRELAY_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_ALLRELAY_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_ALLRELAY_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_RELAY_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_RELAY_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_RELAY_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      PB_ACK_SET_PB_RELAY_DELAY_SWITCH
 * @brief   BLV PB控制协议 - 设定PB总线 
 * @return  none
 */
void PB_ACK_SET_PB_RELAY_DELAY_SWITCH(U8_T *para,U8_T *ackPara,U16_T *ackLen){
    U8_T para_len = para[PB_FMT_LEN] - PB_FMT_PARA - 1;
	
	Dbg_Println(DBG_BIT_SYS_STATUS, "%s",__func__);
	
	if(PowerBUS_GetCommState() == 0x00){
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_Succ;
		PowerBus_PackFillBuff(para[PB_FMT_PARA]+1,PowerBUS_CMD_SET_RELAY_DELAY_SWITCH,&para[PB_FMT_PARA + 1],para_len);
	}else{
		*ackLen = 1;
	
		ackPara[0] = PB_CMD_Reply_CommStateBusy;
	}
	
}

/*********************************************************************
 * @fn      BLV_PB_Control_Protocol_Processing
 * @brief   BLV PB控制协议处理函数(BLV_A8 PB控制协议)
 * @return  none
 */
U8_T BLV_PB_Control_Protocol_Processing(U8_T *data,U16_T len){

    U8_T ack_flag = 0;
	
	//目前RCU地址为0x00
	if(data[PB_FMT_ADDR_TX] != 0x00){
		Dbg_Println(DBG_BIT_SYS_STATUS, "PB Tx Addr Error!");
		return 0x01;
	}
	
	//目前PB地址为0x01
	if(data[PB_FMT_ADDR_RX] != 0x01){
		Dbg_Println(DBG_BIT_SYS_STATUS, "PB Rx Addr Error!");
		return 0x01;
	}

    if(PB_CheckSum(data,len) != 0x00){
        Dbg_Println(DBG_BIT_SYS_STATUS, "PB Check Fail!!!!");
        return 0x01;
    }

    if(data[PB_FMT_LEN] != len){
        Dbg_Println(DBG_BIT_SYS_STATUS, "PB Len Fail %d - %d!!!!",data[PB_FMT_LEN],len);
        return 0x01;
    }
	
	if(data[PB_FMT_DEV_TYPE] != PB_DEV_TYPE){
		Dbg_Println(DBG_BIT_SYS_STATUS, "PB TYPE Fail %d - %d!!!!",data[PB_FMT_DEV_TYPE],PB_DEV_TYPE);
		return 0x02;
	}

	g_PB.recv_cmd = data[PB_FMT_CMD];
	if((data[PB_FMT_TYPE] & 0x0F) != g_PB.recv_sn){
		g_PB.recv_sn = data[PB_FMT_TYPE] & 0x0F;
		g_PB.recv_addr = data[PB_FMT_ADDR_TX];
		
		switch(data[PB_FMT_CMD]){
		case PB_CMD_GET_STATE:				            //查询当前状态
			PB_ACK_GET_STATE(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_STRIP_BRIGHTNESS:		        //设定灯带亮度
			PB_ACK_SET_LED_BRIGHTNESS(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_STRIP_SWITCH:	                //设定灯带开关状态
			PB_ACK_SET_STRIP_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_STRIP_ADJUST:
			PB_ACK_SET_STRIP_ADJUST(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PIRTIGGLE_LED:
			PB_ACK_SET_PIRTIGGLE_LED(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_CurrTiggleStrip:
			PB_ACK_SET_CurrTiggleStrip(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_SaveCurrInfo:
			PB_ACK_SET_SaveCurrInfo(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_GET_SaveCurrInfo:
			PB_ACK_GET_SaveCurrInfo(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_UniversPara:
			PB_ACK_SET_UniversPara(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PowerBus_Enable:
			PB_ACK_SET_PowerBus_EnablePara(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_PassThroug_Data:
			PB_ACK_PassThroug_Data(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LEDS_BRIGHTNESS:		        //设定PB总线 LED亮度				--按照地址段控制 单独控制回路亮度
			PB_ACK_SET_PB_LEDS_BRIGHTNESS(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LEDS_ADJUST:				    //设定PB总线 LED亮度递增递减调节	--按照地址段控制
			PB_ACK_SET_PB_LEDS_ADJUST(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LEDS_SWITCH:			        //设定PB总线 LED开关状态			--按照地址段控制
			PB_ACK_SET_PB_LEDS_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LEDS_BRIGHT:					//设定PB总线 LED亮度				--按照地址段控制 
			PB_ACK_SET_PB_LEDS_BRIGHT(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LED_SWITCH:			        //设定PB总线 LED单开关状态			--按照单个回路控制
			PB_ACK_SET_PB_LED_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LED_BRIGHTNESS:				//设定PB总线 LED单回路亮度			--按照单个回路控制
			PB_ACK_SET_PB_LED_BRIGHTNESS(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_LED_Adjust:					//设定PB总线 LED单回路亮度			--按照单个回路控制
			PB_ACK_SET_PB_LED_Adjust(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_ALLLED_Switch:				//设定PB总线 LED单回路亮度			--按照单个回路控制
			PB_ACK_SET_PB_ALLLED_Switch(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIPS_BRIGHTNESS:			//PB 恒压调光 设定灯带亮度			--按照地址段控制
			PB_ACK_SET_PB_STRIPS_BRIGHTNESS(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIPS_ADJUST:				//PB 恒压调光 设定灯带递增、递减	--按照地址段控制
			PB_ACK_SET_PB_STRIPS_ADJUST(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIPS_SWITCH:				//PB 恒压调光 设定灯带开关状态		--按照地址段控制
			PB_ACK_SET_PB_STRIPS_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_ALLSTRIP_Switch:				//PB 恒压调光 设定灯带全部开关状态		
			PB_ACK_SET_PB_ALLSTRIP_Switch(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIP_SWITCH:				//PB 恒压调光 设定灯带开关状态		--按照指定回路
			PB_ACK_SET_PB_STRIP_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIP_BRIGHTNESS:			//PB 恒压调光 设定灯带亮度			--按照指定回路
			PB_ACK_SET_PB_STRIP_BRIGHTNESS(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIP_Adjust:				//PB 恒压调光 设定灯带递增、递减	--按照指定回路
			PB_ACK_SET_PB_STRIP_Adjust(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIP_RELATIVE_GROUP_ADJUST:	//PB 恒压调光 设定灯带相对亮度		--按照地址段控制
			PB_ACK_SET_PB_STRIP_RELATIVE_GROUP_ADJUST(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_STRIP_RELATIVE_ADJUST:		//PB 恒压调光 设定灯带相对亮度		--按照指定回路
			PB_ACK_SET_PB_STRIP_RELATIVE_ADJUST(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		
		case PB_CMD_SET_PB_RELAYS_SWITCH:
			PB_ACK_SET_PB_RELAYS_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_ALLRELAY_SWITCH:
			PB_ACK_SET_PB_ALLRELAY_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_RELAY_SWITCH:
			PB_ACK_SET_PB_RELAY_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		case PB_CMD_SET_PB_RELAY_DELAY_SWITCH:
			PB_ACK_SET_PB_RELAY_DELAY_SWITCH(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			ack_flag = 0x01;
			break;
		
		}
	}else if((data[PB_FMT_TYPE] & 0x40) == 0x40){
		//重发标志位置位 - 回复应答数据，但是不处理
		switch(data[PB_FMT_CMD]){
			case PB_CMD_GET_STATE:
			PB_ACK_GET_STATE(data,&g_PB.ackbuff[PB_FMT_PARA],&g_PB.ackLen);
			break;
		}
		PB_RS485_ReplyAck();
	}

    if(ack_flag == 0x01){
        PB_RS485_ReplyAck();
    }

    return 0x00;
}

/*********************************************************************
 * @fn      Get_ADC_Val_Filter
 * @brief   获取ADC平滑滤波后的值
 */
U32_T Get_ADC_Val_Filter(U16_T *data,U8_T data_len){
	U32_T sum_val = 0;
	U16_T age_val = 0;
	U8_T sum_cnt = 0;
	
	for(sum_cnt = 0;sum_cnt < data_len;sum_cnt++){
		sum_val += data[sum_cnt];
	}
	
	age_val = (sum_val / data_len) & 0xFFFF;
	
	return age_val;
}

/*获取电流值*/
U16_T Get_PB_CURR_VAL(void){
	
	U16_T Temp_val = Get_ADC_Val_Filter(g_PB.meas_hv_CURRPWR_Buff,MEAS_Debounce_Num);
	g_PB.meas_powerbus_curr = (Temp_val * 16500 ) / 4096;
	return g_PB.meas_powerbus_curr;
}

/*********************************************************************
 * @fn      PMU_MEAS_Task
 * @brief   PB - 检测输入电压与负载任务，通过4路ADC检测
 */
void PMU_MEAS_Task(void){
	
	static U32_T meas_tick = 0;
	U32_T temp_val = 0,convert_val = 0;
	
	if(SysTick_1ms - meas_tick >= 30){
		meas_tick = SysTick_1ms;
		
		if(g_PB.meas_cnt >= MEAS_BUFFER_SIZE)	{
			g_PB.meas_fill_flag = 0x01;
			g_PB.meas_cnt = 0;
		}
		
		ADC12_SEQEND_wait(0);
		g_PB.meas_hv_PB_val[g_PB.meas_cnt] = ADC12_DATA_OUPUT(0);
		
		ADC12_SEQEND_wait(1);
		g_PB.meas_hv_PS_val[g_PB.meas_cnt] = ADC12_DATA_OUPUT(1);
		
		ADC12_SEQEND_wait(2);
		g_PB.meas_hv_CURRPWRx5_val = ADC12_DATA_OUPUT(2);
		
		ADC12_SEQEND_wait(3);
		g_PB.meas_hv_CURRPWRx50_val = ADC12_DATA_OUPUT(3);
		
		/* 检测PB输出 - 负载电流
		 * 2023 - 10 - 21 目前只使用X5挡位的检测 精度可达标 可以不使用x50挡位
		 * 
		 * */
//		if(g_PB.meas_hv_curr_ward == 0x01){
//			if(g_PB.meas_hv_CURRPWRx5_val <= 500){
//				g_PB.meas_hv_curr_toggle++;
//				if(g_PB.meas_hv_curr_toggle >= 3){
//					g_PB.meas_hv_curr_toggle = 0;
//					g_PB.meas_hv_curr_ward = 0x00;		//切换至低挡位
//				}
//			}else{
//				g_PB.meas_hv_curr_toggle = 0;
//			}
//			
//			g_PB.meas_hv_CURRPWR_Buff[g_PB.meas_hv_curr_cnt] = g_PB.meas_hv_CURRPWRx5_val;
//			
//		}else {
//			if(g_PB.meas_hv_CURRPWRx50_val >= 3500){
//				g_PB.meas_hv_curr_toggle++;
//				if(g_PB.meas_hv_curr_toggle >= 3){
//					g_PB.meas_hv_curr_toggle = 0;
//					g_PB.meas_hv_curr_ward = 0x01;		//切换至高挡位
//				}
//			}else{
//				g_PB.meas_hv_curr_toggle = 0;
//			}
//			
//			g_PB.meas_hv_CURRPWR_Buff[g_PB.meas_hv_curr_cnt] = g_PB.meas_hv_CURRPWRx50_val;
//		}
		
		if(g_PB.meas_hv_curr_cnt >= MEAS_Debounce_Num){
			g_PB.meas_hv_curr_cnt = 0;
		}
		g_PB.meas_hv_CURRPWR_Buff[g_PB.meas_hv_curr_cnt++] = g_PB.meas_hv_CURRPWRx5_val;
		Get_PB_CURR_VAL();
		PB_Current_Monitoring_Meas();
		
		//Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PB Curr %dmA",Get_PB_CURR_VAL());
		
		//以下内容为输出电源检测
		g_PB.meas_cnt++;
		if(g_PB.meas_fill_flag != 0x01)	return ;		//检测BUFF未满,直接退出
		g_PB.meas_fill_flag = 0x00;	//重新开始计数
		
		/*Meas PB Voltage 检测OK后，才打开PB输出电源*/
		temp_val = Get_ADC_Val_Filter(g_PB.meas_hv_PB_val,MEAS_BUFFER_SIZE);
		g_PB.meas_powerbus_volt = (temp_val * 3300 * 16) / 4096;			//输入电压 = 检测电压 * 16
//		Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PB Voltage:%dmv",g_PB.meas_powerbus_volt);
		
		if( (g_PB.meas_powerbus_volt >= MEAS_InputVoltage_Min) && (g_PB.meas_powerbus_volt <= MEAS_InputVoltage_Max) ){
			g_PB.meas_hv_PB_ONcnt++;
			g_PB.meas_hv_PB_OFFcnt = 0x00;
			
			if(g_PB.meas_hv_PB_ONcnt >= MEAS_Debounce_Num){
				g_PB.meas_hv_PB_ONcnt = 0x00;
				g_PB.meas_hv_PB_State = 0x01;
				
				//Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PB Voltage ON %dmV",g_PB.meas_powerbus_volt);
			}
		}else {
			g_PB.meas_hv_PB_ONcnt = 0x00;
			g_PB.meas_hv_PB_OFFcnt++;
			
			if(g_PB.meas_hv_PB_OFFcnt >= MEAS_Debounce_Num){
				g_PB.meas_hv_PB_OFFcnt = 0x00;
				g_PB.meas_hv_PB_State = 0x00;
				
				//Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PB Voltage OFF %dmV",g_PB.meas_powerbus_volt);
			}
		}
		
		/*Meas PS Voltage 检测输入OK后，才打开PB输出电源*/
		temp_val = Get_ADC_Val_Filter(g_PB.meas_hv_PS_val,MEAS_BUFFER_SIZE);
		g_PB.meas_powerstrip_volt = (temp_val * 3300 * 16) / 4096;
//		Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PS Voltage:%dmv",g_PB.meas_powerstrip_volt);
		
		if( (g_PB.meas_powerstrip_volt >= MEAS_InputVoltage_Min) && (g_PB.meas_powerstrip_volt <= MEAS_InputVoltage_Max)){
			g_PB.meas_hv_PS_ONcnt++;
			g_PB.meas_hv_PS_OFFcnt = 0x00;
			
			if(g_PB.meas_hv_PS_ONcnt >= MEAS_Debounce_Num){
				g_PB.meas_hv_PS_ONcnt = 0x00;
				g_PB.meas_hv_PS_State = 0x01;
				
				//Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PS Voltage ON %dmV",g_PB.meas_powerstrip_volt);
			}
		}else {
			g_PB.meas_hv_PS_ONcnt = 0x00;
			g_PB.meas_hv_PS_OFFcnt++;
			
			if(g_PB.meas_hv_PS_OFFcnt >= MEAS_Debounce_Num){
				g_PB.meas_hv_PS_OFFcnt = 0x00;
				g_PB.meas_hv_PS_State = 0x00;
				
				//Dbg_Println(DBG_BIT_SYS_STATUS,"Meas PS Voltage OFF %dmV",g_PB.meas_powerstrip_volt);
			}
		}
		
	}

	if(g_PB.meas_hv_PB_State != g_PB.last_meas_hv_PB_State) {
		g_PB.last_meas_hv_PB_State = g_PB.meas_hv_PB_State;

		Dbg_Println(DBG_BIT_SYS_STATUS,"HV PB State Change : %d",g_PB.last_meas_hv_PB_State);
	    switch(g_PB.last_meas_hv_PB_State) {
			case 0x01:		//开启PB输出
				DRV_PB36V_ON;
				break;
			case 0x00:		//关闭PB输出
				DRV_PB36V_OFF;
				break;
	    }
	}
	
	if(g_PB.meas_hv_PS_State != g_PB.last_meas_hv_PS_State){
		g_PB.last_meas_hv_PS_State = g_PB.meas_hv_PS_State;
		
		Dbg_Println(DBG_BIT_SYS_STATUS,"HV PS State Change : %d",g_PB.last_meas_hv_PS_State);
		
		switch(g_PB.last_meas_hv_PS_State) {
			case 0x01:		//开启PS输出
				DRV_PS36V_ON;
				break;
			case 0x00:		//关闭PS输出
				DRV_PS36V_OFF;
				break;
	    }
	}
	
}

//检测电流突变功能
U16_T Current_Coarse_Filter(U16_T vsens){
	U8_T i=0;
	U16_T rev = 0;
	U32_T filter_sum = 0;
	curr_monitoring.coarse_filter_buff[Filter_Coarse_Size] = vsens;
	for(i=0;i<Filter_Coarse_Size;i++){
		curr_monitoring.coarse_filter_buff[i] = curr_monitoring.coarse_filter_buff[i+1];
		filter_sum += curr_monitoring.coarse_filter_buff[i];
	}
	rev = (U16_T)(filter_sum / Filter_Coarse_Size);
	return rev;
}

U16_T Current_Fine_Filter(U16_T vsens){
	U8_T i=0;
	U16_T rev = 0;
	U32_T filter_sum = 0;
	curr_monitoring.fine_filter_buff[Filter_Fine_Size] = vsens;
	for(i=0;i<Filter_Fine_Size;i++){
		curr_monitoring.fine_filter_buff[i] = curr_monitoring.fine_filter_buff[i+1];
		filter_sum += curr_monitoring.fine_filter_buff[i];
	}
	rev = (U16_T)(filter_sum / Filter_Fine_Size);
	return rev;
}

void PB_Current_Monitoring_Meas(void){
	U16_T coarse_val = 0,fine_val = 0;
	int temp_diff_val = 0,temp_power = 0;
	
	if(curr_monitoring.fun_enable != 0x01)	return ;
	
	coarse_val = Current_Coarse_Filter(g_PB.meas_powerbus_curr);
	fine_val = Current_Fine_Filter(g_PB.meas_powerbus_curr);
	
	curr_monitoring.curr_diff_val = coarse_val - fine_val;
	temp_diff_val = abs(curr_monitoring.curr_diff_val);
	temp_power = temp_diff_val * g_PB.meas_powerbus_volt / 1000;
	
	if(curr_monitoring.dead_switch != 0x00){
		//根据控制时间来决定短期失效多久
//		if(SysTick_1ms - curr_monitoring.dead_tick < curr_monitoring.dead_time){
//			/*短期失效时间内*/
//			return ;
//		}else{
//			curr_monitoring.dead_switch = 0x00;
//			curr_monitoring.dead_tick = SysTick_1ms;
//		}
		
		//根据当前是否处于稳定范围
		
		if(temp_power <= 1000){
			
			curr_monitoring.release_debounce_cnt++;
			if(curr_monitoring.release_debounce_cnt >= 20){
				curr_monitoring.dead_switch = 0x00;
				curr_monitoring.release_debounce_cnt = 0;
			}
		}else{
			curr_monitoring.release_debounce_cnt = 0;
		}
		Dbg_Println(DBG_BIT_SYS_STATUS,"Curr Monit: %d - %dmW  JUMP %d",temp_diff_val,temp_power,curr_monitoring.release_debounce_cnt);
		return ;
	}
	
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"Curr Monit: %d - %dmW",temp_diff_val,temp_power);
	/*触发/消抖阈值是以功率为标准，所以需要转化一下*/
	if(curr_monitoring.curr_diff_val >= 0){
		if(temp_power >= curr_monitoring.trigger_threshold){
			curr_monitoring.trigger_debounce_cnt++;
			if(curr_monitoring.trigger_debounce_cnt >= curr_monitoring.trigger_debounce_num){
				curr_monitoring.pb_state = 0x01;
				curr_monitoring.trigger_debounce_cnt = 0;
			}
		}else{
			curr_monitoring.trigger_debounce_cnt = 0;
		}
		
	}else if(curr_monitoring.curr_diff_val < 0){
		if(temp_power >= curr_monitoring.release_threshold){
			curr_monitoring.release_debounce_cnt++;
			if(curr_monitoring.release_debounce_cnt >= curr_monitoring.release_debounce_num){
				curr_monitoring.pb_state = 0x00;
				curr_monitoring.release_debounce_cnt = 0;
			}
		}else{
			curr_monitoring.release_debounce_cnt = 0;
		}
	}
	
	if(curr_monitoring.pb_state != curr_monitoring.last_pb_state){
		
		curr_monitoring.last_pb_state = curr_monitoring.pb_state;
		
		Dbg_Println(DBG_BIT_SYS_STATUS,"curr_monitoring state: %d",curr_monitoring.pb_state);
		
		if(curr_monitoring.pb_state == 0x01){
			PWM_SetOutBrightness(PWM_OUT_CH1,curr_monitoring.triggle_strip_bright[PWM_OUT_CH1]);
			PWM_SetOutBrightness(PWM_OUT_CH2,curr_monitoring.triggle_strip_bright[PWM_OUT_CH2]);
			PWM_SetOutBrightness(PWM_OUT_CH3,curr_monitoring.triggle_strip_bright[PWM_OUT_CH3]);
			PWM_SetOutBrightness(PWM_OUT_CH4,curr_monitoring.triggle_strip_bright[PWM_OUT_CH4]);
		}else{
			PWM_SetOutBrightness(PWM_OUT_CH1,curr_monitoring.release_strip_bright[PWM_OUT_CH1]);
			PWM_SetOutBrightness(PWM_OUT_CH2,curr_monitoring.release_strip_bright[PWM_OUT_CH2]);
			PWM_SetOutBrightness(PWM_OUT_CH3,curr_monitoring.release_strip_bright[PWM_OUT_CH3]);
			PWM_SetOutBrightness(PWM_OUT_CH4,curr_monitoring.release_strip_bright[PWM_OUT_CH4]);
		}
	}
	
	//Dbg_NoTick_Println(DBG_BIT_Debug_STATUS,"%d,%d,%d,%d",coarse_val,fine_val,curr_monitoring.curr_diff_val,curr_monitoring.pb_state);
}

/*让PB触发电流失效一段时间*/
void Set_PB_CurrMonitoring_Dead(void){
	curr_monitoring.dead_switch = 0x01;
	
	//根据时间
//	curr_monitoring.dead_time = time;
//	curr_monitoring.dead_tick = SysTick_1ms;
	
	//根据稳定
	curr_monitoring.release_debounce_cnt = 0;
}

void PB_Task(void){
    PB_Soft_Boot_Task();

    PB_Protect_Task();

    PowerBus_SendBuffer_Task();

    PMU_MEAS_Task();
}





