#include "RGB_Sensor.h"



#define     RGB_ControllerID_SET_Length 10 
#define     RGB_SensorID_SET_Length     10 
#define     RGB_SensorDataRequest_Length 7
#define     RGB_SensorIDAutoSetRequest_Length 7

uint8_t My_RGB_ID = 0;
uint8_t AutoSetRequestCnt = 0;
uint8_t* RGB_Sensor_Func(uint8_t* data){
	RGB_CMD_T type = data[Bluecell_Type];
    switch(type){
        case RGB_Status_Data_Request:      
            if(data[blucell_srcid + 1] == My_RGB_ID){
                AutoSetRequestCnt= 0;
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET);
                printf("MY RGB Status Send");
                for(uint8_t i = 0; i<15; i++)
                    printf("%02x ",RGB_Data[i]);
                printf("\r\n");
                Uart2_Data_Send(&RGB_Data[0],15);
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET);
            }else{
                printf("NO MY ID : %02x  AutoSet Request : %d\r\n",My_RGB_ID,AutoSetRequestCnt);
                AutoSetRequestCnt++;
                if(AutoSetRequestCnt > 32){
                    printf("Auto Set Request \n");
                    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET);
                    AutoSetRequestCnt= 0;
                    data[blucell_stx]        = 0xBE; //0
                    data[blucell_type]       = RGB_SensorID_SET;//1
                    data[blucell_length]     = 4;//2 // 4 byte
                    data[blucell_srcid + 1]  = data[blucell_srcid + 0];
                    data[blucell_srcid + 0]  = My_RGB_ID;
                    data[blucell_srcid + 2]  = STH30_CreateCrc(&data[blucell_type],data[blucell_length]);
                    data[blucell_srcid + 3]  = 0xEB;
                    Uart2_Data_Send(&data[blucell_stx],data[blucell_length] + 3);
                    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET);
                }

            }
            break;
        case RGB_ControllerID_SET:                             break;
        case RGB_SensorID_SET:    
            if(My_RGB_ID == 0 || My_RGB_ID == data[blucell_srcid + 1]){
                 HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET);
                data[blucell_type] = RGB_SensorID_SET_Success;
                data[blucell_srcid] = My_RGB_ID = data[blucell_srcid + 1];
                data[5]   = STH30_CreateCrc(&data[blucell_type],data[blucell_length]);
                printf("My ID SET Success %02x \r\n",My_RGB_ID);
                Uart2_Data_Send(&data[blucell_stx],RGB_SensorIDAutoSetRequest_Length);
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET);
            }else{
                printf("My ID aleady exist %02x \r\n",My_RGB_ID);
            }
            break;
         case RGB_Sensor_Check:
             if(My_RGB_ID == data[blucell_srcid + 1]){
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET);
                data[blucell_stx]        = 0xBE; //0
                data[blucell_type]       = type + 1;//1
                data[blucell_length]     = 4;//2 // 4 byte
                data[blucell_srcid + 1]  = data[blucell_srcid + 0];
                data[blucell_srcid + 0]  = My_RGB_ID;
                data[blucell_srcid + 2]  = STH30_CreateCrc(&data[blucell_type],data[blucell_length]);
                data[blucell_srcid + 3]  = 0xEB;
                Uart2_Data_Send(&data[blucell_stx],data[blucell_length] + 3);
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET);
            }
            break;
        default:break;
    }
    return data;
}
void RGB_Value_Func(void){
    
}
