#include "RGB_Controller.h"

void RGB_Response_Func(uint8_t* data);



void RGB_Response_Func(uint8_t* data){

	RGB_CMD_T type = data[blucell_type];
#if 0
for(uint8_t i = 0; i < 10; i++){
            printf("%02x ",data[i]);
        }
#endif
    switch(type){
        case RGB_Status_Data_Request: 
            Uart2_Data_Send(data,RGB_SensorDataRequest_Length);
            break;
        case RGB_ControllerID_SET: 
            Uart1_Data_Send(data,RGB_ControllerID_SET_Length);
            break;
        case RGB_SensorID_SET:  
            Uart2_Data_Send(data,RGB_SensorIDAutoSetRequest_Length);
            break;        
        case RGB_Status_Data_Response:
            Uart1_Data_Send(data,RGB_SensorDataResponse_Length);
            break;
        case RGB_ControllerLimitSet:
            Uart1_Data_Send(data,data[blucell_length] + 3);
            Flash_write(&data[0]);
            break;
        case RGB_Sensor_Start:    
        case RGB_Sensor_Check:
            Uart2_Data_Send(data,RGB_SensorIDAutoSetRequest_Length);
            break;
        case RGB_Sensor_Ack:
            Uart2_Data_Send(data,data[blucell_length] + 3);
            break;
        case RGB_Reset:
        case RGB_SensorID_SET_Success:
            break;
             
    }

}
uint8_t SensorID_Cnt = 0;
uint8_t SensorID_buf[8] = {0,};
uint16_t RGB_SensorRedLimit_Buf[9]={0,};
uint16_t RGB_SensorGreenLimit_Buf[9]={0,};
uint16_t RGB_SensorBlueLimit_Buf[9]={0,};


uint16_t Sensor_red[9] = {0,};
uint16_t Sensor_green[9] = {0,};
uint16_t Sensor_blue[9] = {0,};    
void RGB_Alarm_Check(uint8_t* data){
   
    Sensor_red[data[blucell_srcid]] = ((data[blucell_red_H + 2] << 8)| data[blucell_red_L + 2]);
    Sensor_green[data[blucell_srcid]] = ((data[blucell_green_H + 2] << 8)| data[blucell_green_L + 2]);
    Sensor_blue[data[blucell_srcid]] = ((data[blucell_blue_H + 2] << 8)| data[blucell_blue_L + 2]);  

    uint8_t LED_Alarm = 0;
    for(uint8_t i = 1; i <= (SensorID_Cnt); i++){
        if(RGB_SensorRedLimit_Buf[SensorID_buf[i]]  >= Sensor_red[SensorID_buf[i]]
            || RGB_SensorGreenLimit_Buf[SensorID_buf[i]]  >= Sensor_green[SensorID_buf[i]]
            || RGB_SensorBlueLimit_Buf[SensorID_buf[i]]  >= Sensor_blue[SensorID_buf[i]]) {
            LED_Alarm = 1;
            break;
        }else{
            LED_Alarm = 0;
        }
    }
    if(LED_Alarm == 1){
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_13, GPIO_PIN_RESET);       
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_SET);       
    }else{
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_13, GPIO_PIN_SET);   
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET);       
    }
}

uint8_t RGB_DeviceStatusCheck(void){
    uint8_t ret = 0;
    for(uint8_t i = 1; i <= SensorID_Cnt; i++){
        if(SensorID_buf[i] > 0){
            ret += 0x01 << (SensorID_buf[i] - 1); 
        }
    }
    return ret;
}
uint8_t datalosscnt[9] = {0,};
void RGB_Controller_Func(uint8_t* data){
	RGB_CMD_T type = data[blucell_type];
   // static uint8_t temp_sensorid;
    uint8_t Result_buf[100] = {0,};
    switch(type){
        case RGB_Status_Data_Request:           
            datalosscnt[data[blucell_srcid + 1]]++;
            if(datalosscnt[data[blucell_srcid + 1]] > 3 && data[blucell_srcid + 1] != 0){
                RGB_SensorIDAutoSet(1);
                memset(&SensorID_buf[0],0x00,8);
            }
            data[5]   = STH30_CreateCrc(&data[blucell_type],data[blucell_length]);
            memcpy(&Result_buf[blucell_stx],&data[blucell_stx],RGB_SensorDataRequest_Length);
            break;
        case RGB_ControllerID_SET:        
            memcpy(&Result_buf[blucell_stx],&data[blucell_stx],data[blucell_length] + 3);
            Result_buf[7] = data[blucell_srcid]; // �긽��諛⑹쓽 SRC ID�뒗 �굹�쓽 DST ID�씠�떎.
            MyControllerID = Result_buf[7] = data[7];//�긽��諛⑹쓽 DST ID �뒗 �굹�쓽 ID �씠�떎.
            break;
        case RGB_SensorID_SET:        
            RGB_SensorIDAutoSet(1);
            memcpy(&Result_buf[blucell_stx],&data[blucell_stx],data[blucell_length] + 3);
            Result_buf[5]   = STH30_CreateCrc(&Result_buf[blucell_type],Result_buf[blucell_length]);
            break;
        case RGB_SensorID_SET_Success:
            SensorID_Cnt++;
            SensorID_buf[SensorID_Cnt] = data[blucell_length + 1];
            break;
        case RGB_Status_Data_Response:
            datalosscnt[data[blucell_srcid]] = 0;
            data[blucell_length] += 1;// Device On OFF status  Send byte
            data[blucell_srcid + 9]  = RGB_DeviceStatusCheck();// Device On OFF status  Send byte
            memcpy(&Result_buf[blucell_stx],&data[blucell_stx],data[blucell_length] + 3);
            Result_buf[5]   = STH30_CreateCrc(&Result_buf[blucell_type],Result_buf[blucell_length]);
            RGB_Alarm_Check(&data[blucell_stx]);
            break;
        case RGB_ControllerLimitSet:
            memcpy(&Result_buf[blucell_stx],&data[blucell_stx],data[blucell_length] + 3);
            RGB_SensorRedLimit_Buf[data[blucell_dstid]]   = ((data[blucell_red_H] << 8) |data[blucell_red_L]);
            RGB_SensorGreenLimit_Buf[data[blucell_dstid]] = ((data[blucell_green_H] << 8) |data[blucell_green_L]);
            RGB_SensorBlueLimit_Buf[data[blucell_dstid]]  = ((data[blucell_blue_H] << 8) |data[blucell_blue_L]);        
            Result_buf[blucell_crc]   = STH30_CreateCrc(&Result_buf[blucell_type],Result_buf[blucell_length]);
            break;
        case RGB_Reset:
            NVIC_SystemReset();
            break;
        default:
        break;
    }
  
    RGB_Response_Func(&Result_buf[blucell_stx]);
    return;
}

