#include "RGB_Controller.h"

void RGB_Response_Func(uint8_t* data);
uint8_t RGB_BufCal(uint8_t srcid);
void RGB_Alarm_Operate(void);
void RGB_Data_Stack(uint8_t* rgb_buf);
uint16_t RGB_Location_Address_Check(uint8_t id);

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,};
uint8_t LED_Alarm[9] = {0,};
uint8_t RGB_Location_Buf[9][50] = {0};

void RGB_Data_Init(void){
    MyControllerID = M24C32_Data_Read(&hi2c2,MY_ID_ADDRESS);
  
    for(uint8_t i = 0; i < 8; i++){
        RGB_SensorRedLimit_Buf[i + 1] = (M24C32_Data_Read(&hi2c2,RGB1_LIMIT_RED_H_ADDRESS + (6 * i)) << 8);
        RGB_SensorRedLimit_Buf[i + 1] |= M24C32_Data_Read(&hi2c2,RGB1_LIMIT_RED_L_ADDRESS + (6 * i));
    }
    for(uint8_t i = 0; i < 8; i++){
        RGB_SensorGreenLimit_Buf[i + 1] = (M24C32_Data_Read(&hi2c2,RGB1_LIMIT_GREEN_H_ADDRESS + (6 * i)) << 8);
        RGB_SensorGreenLimit_Buf[i + 1] |= M24C32_Data_Read(&hi2c2,RGB1_LIMIT_GREEN_L_ADDRESS + (6 * i));
    }
    for(uint8_t i = 0; i < 8; i++){
        RGB_SensorBlueLimit_Buf[i + 1] = (M24C32_Data_Read(&hi2c2,RGB1_LIMIT_BLUE_H_ADDRESS + (6 * i)) << 8);
        RGB_SensorBlueLimit_Buf[i + 1] |= M24C32_Data_Read(&hi2c2,RGB1_LIMIT_BLUE_L_ADDRESS + (6 * i));
    }
    for(uint8_t i = 0; i < 8; i++){
        for(uint8_t aa= 0; aa < 50; aa++)
            RGB_Location_Buf[i + 1][aa] = M24C32_Data_Read(&hi2c2,RGB_Location_Address_Check(i + 1) + aa);
    }
    printf("MY id is %d \n",MyControllerID);
    for(uint8_t i = 1; i <= 8; i++){
        printf("RGB_SensorRedLimit_Buf[%d]    :  %04x\n",i,RGB_SensorRedLimit_Buf[i]);
        printf("RGB_SensorGreenLimit_Buf[%d]  :  %04x\n",i,RGB_SensorGreenLimit_Buf[i]);
        printf("RGB_SensorBlueLimit_Buf[%d]   :  %04x\n",i,RGB_SensorBlueLimit_Buf[i]);    
    }

}
uint16_t RGB_Limit_Address_Check(uint8_t id){
    uint16_t ret = 0;
    switch(id){
        case 1: ret = RGB1_LIMIT_RED_H_ADDRESS;break;
        case 2: ret = RGB2_LIMIT_RED_H_ADDRESS;break;
        case 3: ret = RGB3_LIMIT_RED_H_ADDRESS;break;
        case 4: ret = RGB4_LIMIT_RED_H_ADDRESS;break;
        case 5: ret = RGB5_LIMIT_RED_H_ADDRESS;break;
        case 6: ret = RGB6_LIMIT_RED_H_ADDRESS;break;
        case 7: ret = RGB7_LIMIT_RED_H_ADDRESS;break;
        case 8: ret = RGB8_LIMIT_RED_H_ADDRESS;break;        
    }
    return ret;
}
uint16_t RGB_Location_Address_Check(uint8_t id){
    uint16_t ret = 0;
    switch(id){
        case 1: ret = RGB1_LOCATION_ADDRESS;break;
        case 2: ret = RGB2_LOCATION_ADDRESS;break;
        case 3: ret = RGB3_LOCATION_ADDRESS;break;
        case 4: ret = RGB4_LOCATION_ADDRESS;break;
        case 5: ret = RGB5_LOCATION_ADDRESS;break;
        case 6: ret = RGB6_LOCATION_ADDRESS;break;
        case 7: ret = RGB7_LOCATION_ADDRESS;break;
        case 8: ret = RGB8_LOCATION_ADDRESS;break;        
    }
    return ret;
}

void RGB_Response_Func(uint8_t* data){

	RGB_CMD_T type = data[bluecell_type];
    uint16_t temp  = 0;
    LoraDataRequest_t Lora_Tempdata;
#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);
            M24C32_Data_Write(&hi2c2,&MyControllerID,MY_ID_ADDRESS,1); // EEPROM Controller ID Save 
            break;
        case RGB_SensorID_SET:  
            Uart2_Data_Send(data,RGB_SensorIDAutoSetRequest_Length);
            break;        
        case RGB_Status_Data_Response:
            Uart1_Data_Send(data,data[bluecell_length] + 3);
            break;
        case RGB_ControllerLimitSet:
            Uart1_Data_Send(data,data[bluecell_length] + 3);
            M24C32_Data_Write(&hi2c2,&data[bluecell_red_H],RGB_Limit_Address_Check(data[bluecell_dstid]),6); // EEPROM Controller ID Save 
            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[bluecell_length] + 3);
            break;
        case RGB_Reset:
        case RGB_SensorID_SET_Success:
        case RGB_ID_Allocate_Request:
        case RGB_Lora_ControllerIDAmount_Report:
            break;
        case RGB_Location_Report:
            M24C32_Data_Write(&hi2c2,&data[Location_stx],RGB_Location_Address_Check(data[bluecell_srcid]),data[bluecell_length] + 3); // EEPROM Controller ID Save 
            break;
        case RGB_Location_Response:
            data[bluecell_length] = M24C32_Data_Read(&hi2c2,RGB_Location_Address_Check(data[bluecell_dstid]) + 2); // EEPROM Controller ID Save 
            temp = RGB_Location_Address_Check(data[bluecell_srcid]);
            for(uint8_t i = 0; i < (data[bluecell_length] + 3); i++){
                data[i] = M24C32_Data_Read(&hi2c2,(temp + i)); // EEPROM Controller ID Save 
            }
            data[bluecell_type] = RGB_Location_Response;
            data[data[bluecell_length] + 1] = STH30_CreateCrc(&data[bluecell_type],data[bluecell_length]);
            Uart1_Data_Send(data,data[bluecell_length] + 3);    
            break;
         case RGB_ControllerID_GET:
             Uart1_Data_Send(data,data[bluecell_length] + 3);
             break;
         case RGB_ControllerLimitGet:
             Uart1_Data_Send(data,data[bluecell_length] + 3);
             break;
         case RGB_Lora_DataResponse:
             memcpy(&Lora_Tempdata.Request_stx,&data[bluecell_stx],data[bluecell_length] + 3);
             if(Lora_Tempdata.Request_dstid == MyControllerID)
                 LoraDataSendSet(1);
             else
                 return;
             break;
         
         default:break;
    }
}

void RGB_Sensor_LED_Alarm_ON(uint8_t id ){
    switch(id){
        case 0:// 모든 LED의 전원을 ON
            HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_RESET);            
            HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_RESET);            
            HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_RESET);               
            break;
        case 1:
            HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_RESET);
            break;
        case 2:
            HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_RESET);
            break;
        case 3:
            HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_RESET);
            break;
        case 4:
            HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_RESET);
            break;
        case 5:
            HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_RESET);
            break;
        case 6:
            HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_RESET);
            break;
        case 7:
            HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_RESET);
            break;
        case 8:
            HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_RESET);
            break;
    }
}
void RGB_Sensor_LED_Alarm_OFF(uint8_t id ){
    switch(id){
        case 0:// 모든 LED의 전원을 OFF
            HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_SET);
            HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_SET);            
            HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_SET);            
            HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_SET);
            HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_SET);
            HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_SET);
            HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_SET);
            HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_SET);               
            break;
        case 1:
            HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_SET);
            break;
        case 2:
            HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_SET);
            break;
        case 3:
            HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_SET);
            break;
        case 4:
            HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_SET);
            break;
        case 5:
            HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_SET);
            break;
        case 6:
            HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_SET);
            break;
        case 7:
            HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_SET);
            break;
        case 8:
            HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_SET);
            break;
    }
}

void RGB_Alarm_Operate(void){
    uint8_t temp_warning = 0;
    for(uint8_t i = 1; i <= (SensorID_Cnt); i++){
        if(LED_Alarm[SensorID_buf[i]] == 1){
            HAL_GPIO_WritePin(LED_ALARM_GPIO_Port, LED_ALARM_Pin, GPIO_PIN_SET); //표지 LED
            RGB_Sensor_LED_Alarm_ON(SensorID_buf[i]);
            temp_warning = 1; 
        }else{
            RGB_Sensor_LED_Alarm_OFF(SensorID_buf[i]);
        }
    }
    if(temp_warning == 0){ // 8개의 Sensor가 전부 정상일 때 만 동작
        HAL_GPIO_WritePin(LED_ALARM_GPIO_Port, LED_ALARM_Pin, GPIO_PIN_RESET); //표지 LED
        RGB_Sensor_LED_Alarm_OFF(0); //모든 Sensor가 정상일 때는 LED 가 켜지지 않는다.
    }
}
void RGB_Alarm_Check(uint8_t* data){
    static uint16_t Sensor_red[9] = {0,};
    static uint16_t Sensor_green[9] = {0,};
    static uint16_t Sensor_blue[9] = {0,};    
    static uint8_t Prev_Alarm_occur;
    uint8_t Alarm_occur = 0;
    Sensor_red[data[bluecell_srcid]] = ((data[bluecell_red_H + 2] << 8)| data[bluecell_red_L + 2]);
    Sensor_green[data[bluecell_srcid]] = ((data[bluecell_green_H + 2] << 8)| data[bluecell_green_L + 2]);
    Sensor_blue[data[bluecell_srcid]] = ((data[bluecell_blue_H + 2] << 8)| data[bluecell_blue_L + 2]);  

    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[SensorID_buf[i]] = 1;
            Alarm_occur = 1;
        }else{
            LED_Alarm[SensorID_buf[i]] = 0;
        }
    }
    RGB_Data_Stack(&LED_Alarm[1]);
    if(Prev_Alarm_occur != Alarm_occur){
//         LoraDataSendSet(1);
         Prev_Alarm_occur = Alarm_occur;
    }
}

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 Lora_Buf[100] = {0,};

#if 0 // PYJ.2019.04.14_BEGIN --  //Uart Value Data 
void RGB_Data_Stack(uint8_t* rgb_buf){
    uint8_t mynumcnt = RGB_BufCal(rgb_buf[bluecell_srcid]);
    Lora_Buf[bluecell_stx]         = 0xbe;
    Lora_Buf[bluecell_type]        = RGB_Lora_Data_Report;
    Lora_Buf[bluecell_length]      = Lora_Max_Amount + 2; //length 1byte + type 1byte + RGB Data 60byte
    Lora_Buf[bluecell_srcid]       = MyControllerID;
    Lora_Buf[mynumcnt]             = rgb_buf[bluecell_srcid];
    Lora_Buf[mynumcnt + 1]         = rgb_buf[bluecell_red_H + 2];
    Lora_Buf[mynumcnt + 2]         = rgb_buf[bluecell_red_L + 2];
    Lora_Buf[mynumcnt + 3]         = rgb_buf[bluecell_green_H + 2];
    Lora_Buf[mynumcnt + 4]         = rgb_buf[bluecell_green_L + 2];
    Lora_Buf[mynumcnt + 5]         = rgb_buf[bluecell_blue_H + 2];
    Lora_Buf[mynumcnt + 6]         = rgb_buf[bluecell_blue_L + 2];
    LoraDataSendSet(1);
}

uint8_t RGB_BufCal(uint8_t srcid){
    uint8_t ret = 0;
    switch(srcid){
        case 1:ret = 4;break;
        case 2:ret = 11;break;
        case 3:ret = 18;break;
        case 4:ret = 25;break;
        case 5:ret = 32;break;
        case 6:ret = 39;break;
        case 7:ret = 46;break;
        case 8:ret = 53;break;      
    }
    return ret;
}
#else //Uart Flag Data
/*
    현재 Controller 가지고 있는 RGB Sensor ID Check
    현재 비정상적인 동작을 하는 Sensor 에대한 Flag 정보
*/
void RGB_Data_Stack(uint8_t* rgb_buf){
    memset(&Lora_Buf[0],0x00,8);
    /*********************FIX DATA*************************************/
    Lora_Buf[bluecell_stx]         = 0xbe;
    Lora_Buf[bluecell_srcid + 4]   = 0xeb;
    Lora_Buf[bluecell_type]        = RGB_Lora_DataResponse;
    Lora_Buf[bluecell_length]      = Lora_Max_Amount;// RGB Data 5byte
    Lora_Buf[bluecell_srcid]       = MyControllerID;
    /*********************FIX DATA*************************************/
    if(RGB_BufCal(SensorID_buf[1]) == 0){//아무런 Device가 존재 하지않을 때
        printf("Not Exist Device \n");
        return;
    }
    for(uint8_t i = 1; i <= (SensorID_Cnt); i++){
        Lora_Buf[bluecell_srcid + 1] |= 0x01  << (SensorID_buf[i] - 1);
    }
    for(uint8_t i = 0; i < 8; i++){
        Lora_Buf[bluecell_srcid + 2] |= rgb_buf[i] << i ;
    }
    Lora_Buf[bluecell_srcid + 3]= STH30_CreateCrc(&Lora_Buf[bluecell_type],Lora_Buf[bluecell_length]);
   
}
/*
    RGB_Data_Stack에 Lora에 Data를 보내기 위해 Buffer에 Data를 쌓을 때
    ID 마다 Location Cnt
*/
uint8_t RGB_BufCal(uint8_t srcid){
    uint8_t ret = 0;
    switch(srcid){
        case 1:ret = 4;break;
        case 2:ret = 7;break;
        case 3:ret = 10;break;
        case 4:ret = 13;break;
        case 5:ret = 16;break;
        case 6:ret = 29;break;
        case 7:ret = 32;break;
        case 8:ret = 35;break;      
    }
    return ret;
}
uint8_t RGB_LimitData_Get(uint8_t id){

    switch(id){


    }


}
#endif // PYJ.2019.04.14_END -- 
uint8_t datalosscnt[9] = {0,};


void RGB_Controller_Func(uint8_t* data){
	RGB_CMD_T type = data[bluecell_type];
  
   // static uint8_t temp_sensorid;
    uint8_t Result_buf[100] = {0,};
    uint8_t i = 0;
    switch(type){
        case RGB_Status_Data_Request:           
            datalosscnt[data[bluecell_srcid + 1]]++;
            if(datalosscnt[data[bluecell_srcid + 1]] > 3 && data[bluecell_srcid + 1] != 0){
                RGB_SensorIDAutoSet(1);
                memset(&SensorID_buf[0],0x00,8);
            }
            data[5]   = STH30_CreateCrc(&data[bluecell_type],data[bluecell_length]);
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],RGB_SensorDataRequest_Length);
            break;
        case RGB_ControllerID_SET:        
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
            MyControllerID = data[bluecell_srcid]; // �긽��諛⑹쓽 SRC ID�뒗 �굹�쓽 DST ID�씠�떎.
            break;
        case RGB_SensorID_SET:        
            RGB_SensorIDAutoSet(1);
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
            Result_buf[5]  = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
            break;
        case RGB_SensorID_SET_Success:
            SensorID_Cnt++;
            SensorID_buf[SensorID_Cnt] = data[bluecell_length + 1];
            break;
        case RGB_Status_Data_Response:
            datalosscnt[data[bluecell_srcid]] = 0;
            data[bluecell_length] += 1;
            RGB_Alarm_Check(&data[bluecell_stx]);
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
            Result_buf[Result_buf[bluecell_length] - 1]   = RGB_DeviceStatusCheck();// Device On OFF status  Send byte
            Result_buf[Result_buf[bluecell_length] + 0]   = Lora_Buf[bluecell_srcid + 2];
            Result_buf[Result_buf[bluecell_length] + 1]   = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
            Result_buf[Result_buf[bluecell_length] + 2]   = 0xeb;
            break;
        case RGB_ControllerLimitSet:
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
            RGB_SensorRedLimit_Buf[data[bluecell_dstid]]   = ((data[bluecell_red_H] << 8) |data[bluecell_red_L]);
            RGB_SensorGreenLimit_Buf[data[bluecell_dstid]] = ((data[bluecell_green_H] << 8) |data[bluecell_green_L]);
            RGB_SensorBlueLimit_Buf[data[bluecell_dstid]]  = ((data[bluecell_blue_H] << 8) |data[bluecell_blue_L]);        
            Result_buf[bluecell_crc]   = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
            break;
        case RGB_Reset:
            NVIC_SystemReset();
            break;
        case RGB_ID_Allocate_Request:
            break;
        case RGB_Location_Report:
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
            break;
        case RGB_Location_Response:
            Result_buf[bluecell_type] = RGB_Location_Response;
            Result_buf[bluecell_srcid] = data[bluecell_srcid];
            break;
        case RGB_ControllerID_GET:
            Result_buf[bluecell_stx]         = 0xbe;
            Result_buf[bluecell_type]        = RGB_ControllerID_GET;
            Result_buf[bluecell_length]      = 3;
            Result_buf[bluecell_srcid]       = MyControllerID;      
            Result_buf[bluecell_srcid + 1]   = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
            Result_buf[bluecell_srcid + 2]   = 0xeb;
            break;
            
        case RGB_ControllerLimitGet:
            Result_buf[bluecell_stx]         = 0xbe;
            Result_buf[bluecell_type]        = RGB_ControllerLimitGet;
            Result_buf[bluecell_length]      = 8;
            Result_buf[bluecell_srcid + 0]   = (RGB_SensorRedLimit_Buf[data[bluecell_srcid]]   & 0xFF00) >> 8;      
            Result_buf[bluecell_srcid + 1]   = RGB_SensorRedLimit_Buf[data[bluecell_srcid]]   & 0x00FF;  
            Result_buf[bluecell_srcid + 2]   = (RGB_SensorGreenLimit_Buf[data[bluecell_srcid]] & 0xFF00) >> 8;      
            Result_buf[bluecell_srcid + 3]   = RGB_SensorGreenLimit_Buf[data[bluecell_srcid]] & 0x00FF;
            Result_buf[bluecell_srcid + 4]   = (RGB_SensorBlueLimit_Buf[data[bluecell_srcid]]  & 0xFF00) >> 8;      
            Result_buf[bluecell_srcid + 5]   = RGB_SensorBlueLimit_Buf[data[bluecell_srcid]]  & 0x00FF;
            Result_buf[bluecell_srcid + 6]   = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
            Result_buf[bluecell_srcid + 7]   = 0xeb;
            break;
        case RGB_Lora_DataRequest:
            memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
            Result_buf[bluecell_type] = RGB_Lora_DataResponse;
            break;
        default:
            break;
    }
    RGB_Response_Func(&Result_buf[bluecell_stx]);
    return;
}

