/*
 * NessLab.c
 *
 *  Created on: Aug 3, 2020
 *      Author: parkyj
 */


#include "main.h"
#include "NessLab.h"
#include "adc.h"
#include "crc.h"
#include "flash.h"
#include "NessLab.h"
//extern uint8_t NessLab_Checksum(uint8_t* data,uint8_t size);


uint8_t NessLab_Adc_Convert_db();

#define NESSLAB_DATARESPONSE_INDEXSIZE  23
#define NESSLAB_TABLE_LENGTH 110
Nesslab_Prot Currstatus;
extern volatile uint16_t ADC1value[ADC1_CNT];

extern volatile uint32_t UartRxTimerCnt ;
extern volatile uint32_t DC_FAIL_ALARM_CNT ;
extern volatile uint32_t OVER_INPUT_ALARM_CNT ;
extern volatile uint32_t OVER_TEMP_ALARM_CNT ;
extern volatile uint32_t ALC_ALARM_CNT ;
extern volatile uint32_t OVER_POWER_ALARM_CNT ;
extern volatile uint32_t VSWR_ALARM_CNT ;

volatile uint8_t NessLab_TxData[200] = {0,};
uint8_t Flash_DataArray[200] = {0,};
uint8_t DB_Define[100];

void NessLab_Init(){
    FLASH_Read_Func(FLASH_USER_USE_START_ADDR + 2,&DB_Define[0],104);
    HAL_GPIO_WritePin(PAU_RESET_GPIO_Port,PAU_RESET_Pin, GPIO_PIN_SET);
}
double Round_Function(double value){
    double val = value * 100;

    val = (int)(val + 0.5);
    val *= 0.1;
    val = (int)(val + 0.5);
    val *= 0.1;

    return val;
}
uint16_t Absolute_value_Convert(int16_t val){
    if(val < 0)
        val *= -1;
    return val;
}
uint8_t NessLab_Adc_Convert_db() // ?占쏙옙湲고븿?占쏙옙
{
    double CurrAdc = (float)((Currstatus.DownLink_Forward_Det_H << 8 | Currstatus.DownLink_Forward_Det_L)*0.001);
    double TableVal = 0;
    float ret = 0;
    int16_t calc_val = 0,Prev_calc_val = 3300 ;
    uint8_t Curr_DB = 0 ;
    uint16_t CurrAdc_Temp = 0,TableVal_Temp = 0;
    ret = Round_Function(CurrAdc);
//    CurrAdc *= 1000;
    CurrAdc_Temp = CurrAdc * 1000;
    for(int i = 0; i <= 50; i++){
        TableVal_Temp = ((DB_Define[i * 2] << 8 | DB_Define[(i * 2)+ 1]));
        if(TableVal_Temp == 0)
            continue;
        calc_val = CurrAdc_Temp - TableVal_Temp;
        calc_val = Absolute_value_Convert(calc_val);
//        printf("%d - %d calc_val : %d \r\n",CurrAdc_Temp,TableVal_Temp,calc_val);
        if(Prev_calc_val > calc_val && TableVal_Temp != 0){
           Prev_calc_val = calc_val;
           Curr_DB = i + 2;
//           printf("Curr_DB : %d \r\n",Curr_DB);
        }else{
//            printf("%d  %d \r\n",Prev_calc_val , calc_val);
        }
    }
//    DB_Define[]

    printf("Curr Db : %d \r\n",Curr_DB);

    return Curr_DB;

}

void NessLab_Operate(uint8_t* data){
	uint8_t datatype = data[NessLab_MsgID0];
    uint8_t UartLength = 0;
    static uint16_t MSG_SNCnt = 0;
	switch(datatype){
		case NessLab_STATUS_REQ:
			ADC_Check();
            
            UartLength = NessLab_MAX_INDEX + 1;
            MSG_SNCnt = data[NessLab_Req_MsgSN0] << 8 | data[NessLab_Req_MsgSN1];
            MSG_SNCnt++;
//            if(data[NessLab_Req_Data_Cnt0] > 0)
//                NessLab_TxData[NessLab_Over_Power_Alarm] = 1;
//            else
//                NessLab_TxData[NessLab_Over_Power_Alarm] = 0;
//            if(data[NessLab_Req_Data_Cnt1] > 0)                
//                NessLab_TxData[NessLab_VSWR_ALARM] = 1;
//            else
//                NessLab_TxData[NessLab_VSWR_ALARM] = 0;
                
            NessLab_TxData[NessLab_MsgSN0] = (uint8_t)((MSG_SNCnt & 0xFF00) >>8);//data[NessLab_Req_MsgSN0];
            NessLab_TxData[NessLab_MsgSN1] = (uint8_t)((MSG_SNCnt & 0x00FF));//data[NessLab_Req_MsgSN1] ;    
            NessLab_Frame_Set(NessLab_TxData,12,NessLab_STATUS_RES);
//            NessLab_TxData[10] = 1;
//            NessLab_TxData[11] = 0;
//            NessLab_TxData[12] = 1;
//            NessLab_TxData[13] = 0;
//            NessLab_TxData[14] = 1;
//            NessLab_TxData[15] = 0;
//            NessLab_TxData[16] = 1;
//            NessLab_TxData[17] = 0;            
                
			break;
            
        case NessLab_Table_REQ:
            UartLength = NESSLAB_TABLE_LENGTH;
            FLASH_Read_Func(FLASH_USER_USE_START_ADDR,&NessLab_TxData[NessLab_Req_Data_Cnt0],data[NessLab_DataLength]);
            NessLab_Table_Frame_Set(NessLab_TxData,102,NessLab_Table_RES);
            printf("NessLab_Table_REQ \r\n");
            break;
        case NessLab_TableSet_REQ:
            DataErase_Func(FLASH_USER_USE_START_ADDR,200);
            printf("Ram Data Display \r\n");
            for(int i = 0; i < data[NessLab_DataLength]; i++){
                Flash_DataArray[i] = data[NessLab_Data_ADC1_H + i];
                printf("%x ",Flash_DataArray[i]);
            }
            FLASH_Write_Func(FLASH_USER_USE_START_ADDR,&Flash_DataArray[0],data[NessLab_DataLength]);
            UartLength = NESSLAB_TABLE_LENGTH;
            NessLab_Table_Frame_Set(NessLab_TxData,104,NessLab_TableSet_RES);
            FLASH_Read_Func(FLASH_USER_USE_START_ADDR + 2,&DB_Define[0],104);
//            NessLab_Init();
            printf("\r\nNessLab_TableSet_REQ \r\n");            
            break;
        case NessLab_PAU_Enable_Req:
            HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, GPIO_PIN_SET);
            break;
        case NessLab_PAU_Disable_Req:
            HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, GPIO_PIN_RESET);
            break;            
	}
    Uart1_Data_Send(&NessLab_TxData[NessLab_Header0], UartLength);
}

/*
Rx Total Byte
7e 7e 65 00 01 ff 02 00 00 99 7e 7e


Tx Total Byte : 

7e 7e 66 00 02 00 0c 04 20 00 00 00 00 00 00 00 00 00 68 7e 7e 0a

*/

void NessLab_Frame_Set(uint8_t* data,uint8_t size){
    data[NessLab_Header0] = 0x7E;
    data[NessLab_Header1] = 0x7E;

    data[NessLab_MsgID0] = NessLab_STATUS_RES;// ID
//    data[NessLab_MsgSN0] = 0; // SEQ NUMBER
//    data[NessLab_MsgSN1] = 0; // SEQ NUMBER   
    data[NessLab_Reserve0] = 0; // NessLab_Reserve0
    data[NessLab_DataLength] = size; // Nesslab Size
    data[NessLab_Data_ADC1_H] =  Currstatus.DownLink_Forward_Det_H;//(uint8_t)((ADC1value[0] & 0xFF00) >> 8);
    data[NessLab_Data_ADC1_L] =  Currstatus.DownLink_Forward_Det_L;//(uint8_t)(ADC1value[0] & 0x00FF);
    data[NessLab_Data_ADC1_Table_Value] = NessLab_Adc_Convert_db();
    if(DC_FAIL_ALARM_CNT > 3000)
        data[NessLab_DC_FAIL_ALARM] = 1;
    else
        data[NessLab_DC_FAIL_ALARM] = 0;
    
    if(OVER_INPUT_ALARM_CNT > 3000)
        data[NessLab_Over_Input_Alarm] = 1;  
    else
        data[NessLab_Over_Input_Alarm] = 0;  
    if(OVER_TEMP_ALARM_CNT > 3000)
        data[NessLab_Over_Temp_Alarm] = 1; 
    else
        data[NessLab_Over_Temp_Alarm] = 0; 
        
    if(ALC_ALARM_CNT > 3000)
        data[NessLab_ALC_ALARM] = 1; 
    else
        data[NessLab_ALC_ALARM] = 0; 
    if(OVER_POWER_ALARM_CNT > 3000)
        data[NessLab_Over_Power_Alarm] = 1;        
    else
        data[NessLab_Over_Power_Alarm] = 0;        
    if(VSWR_ALARM_CNT > 3000)
        data[NessLab_VSWR_ALARM] = 1;  
    else
        data[NessLab_VSWR_ALARM] = 0;  
        
    
    data[NessLab_DownLink_Status] = 0;
    data[NessLab_Temp_Monitor] = Currstatus.Temp_Monitor; 
    data[NessLab_ChecksumVal] = NessLab_Checksum(&data[NessLab_MsgID0], NessLab_MAX_INDEX - 5);
    /* Exception Header Tail Checksum */
    data[NessLab_Tail0] = 0x7E;
    data[NessLab_Tail1] = 0x7E; 
    data[NessLab_Tail1 + 1] = 0x0A; 
    NessLab_Protocol_LastCheck(&data[NessLab_MsgID0],16);
}
void NessLab_Protocol_LastCheck(uint8_t* data,uint8_t size){
    int cnt = NessLab_MsgID0;
    for(int i = cnt; i < 17; i++){

        if(data[i] == 0x7e){
            data[cnt++] = 0x7d;
            data[cnt++] = 0x5e;
        }
        else if(data[i] == 0x7d){
            data[cnt++] = 0x7d;
            data[cnt++] = 0x5d;

        }else{
            data[i++] = data[i];
        }

    }


}

void NessLab_Table_Frame_Set(uint8_t* data,uint8_t size,uint8_t mode){
    uint32_t i = 0;
    uint32_t  CurrApiAddress = 0;
    CurrApiAddress = FLASH_USER_USE_START_ADDR;
    uint8_t* Currdata = (uint8_t*)CurrApiAddress;
    uint8_t* pdata;



    data[i++] = 0x7E;
    data[i++] = 0x7E;

    data[i++] = mode;// ID
    data[i++] = 0; // SEQ NUMBER
    data[i++] = 0; // SEQ NUMBER   
    data[i++] = 0; // NessLab_Reserve0
    data[i++] = size; // Nesslab Size
    //    NessLab_TalbleFlash_Read(&data[NessLab_DataLength + 1],100);              

    for(int a = 0; a < size; a++){
        data[i++] = Currdata[a];
//        printf("%02x ",Currdata[i]);
    }
    data[i++] = NessLab_Checksum(&data[NessLab_MsgID0], 100 + 5);
    /* Exception Header Tail Checksum */
    data[i++] = 0x7E;
    data[i++] = 0x7E; 
}

void NessLab_Status_Check(){
	//HAL_GPIO_ReadPin(, GPIO_Pin)
}
void NessLab_PAU_Enable(){
#if 1
    if( HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin) != HAL_GPIO_ReadPin(AMP_EN_GPIO_Port, AMP_EN_Pin) ){
        HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin));
        printf("HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin) : %d \r\n",HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin));
        printf("AMP_EN_GPIO_Port : %d \r\n",HAL_GPIO_ReadPin(AMP_EN_GPIO_Port, AMP_EN_Pin));
//        HAL_Delay(1000);
    }
	//HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, HAL_GPIO_ReadPin(PAU_RESET_GPIO_Port, PAU_RESET_Pin));
#else
    HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, 0);
    printf("AMP_EN_GPIO_Port : %d \r\n",HAL_GPIO_ReadPin(AMP_EN_GPIO_Port, AMP_EN_Pin));

#endif
}
void NessLab_GPIO_Operate(){
	NessLab_PAU_Enable();
}



void NessLab_ADC_Convert_Table(){
    

}
