/*
 * 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);

#define NESSLAB_DATARESPONSE_INDEXSIZE  23
#define NESSLAB_TABLE_LENGTH 110

Nesslab_Prot Currstatus;
extern volatile uint16_t ADC1value[ADC1_CNT];
volatile uint8_t NessLab_TxData[200] = {0,};
uint8_t Flash_DataArray[200] = {0,};



void NessLab_TalbleFlash_Read(uint8_t* data,uint8_t size) // ?占쏙옙湲고븿?占쏙옙
{

  

}

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_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;
            NessLab_Table_Frame_Set(NessLab_TxData,100);
            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_DataArray[0],data[NessLab_DataLength]);
            UartLength = NESSLAB_TABLE_LENGTH;
            NessLab_Table_Frame_Set(NessLab_TxData,100);
            printf("\r\nNessLab_TableSet_REQ \r\n");            
            break;
	}
    Uart1_Data_Send(&NessLab_TxData[NessLab_Header0], UartLength);
}



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_L + 1]++;
    if( data[NessLab_DC_FAIL_ALARM] == 0)
        data[NessLab_DC_FAIL_ALARM] = 1;
    else
        data[NessLab_DC_FAIL_ALARM] = 0;
    
    if( data[NessLab_DownLink_Status] == 0)
        data[NessLab_DownLink_Status] = 1;
    else
        data[NessLab_DownLink_Status] = 0;


    if( data[NessLab_Over_Power_Alarm] == 0)
        data[NessLab_Over_Power_Alarm] = 1;
    else
        data[NessLab_Over_Power_Alarm] = 0;        
//    data[NessLab_Over_Power_Alarm] = 00;
    if( data[NessLab_VSWR_ALARM] == 0)
        data[NessLab_VSWR_ALARM] = 1;
    else
        data[NessLab_VSWR_ALARM] = 0;  

//    data[NessLab_VSWR_ALARM] = 0;
    if( data[NessLab_Over_Input_Alarm] == 0)
        data[NessLab_Over_Input_Alarm] = 1;
    else
        data[NessLab_Over_Input_Alarm] = 0;  
    
//    data[NessLab_Over_Input_Alarm] = 0;
    if( data[NessLab_Over_Temp_Alarm] == 0)
        data[NessLab_Over_Temp_Alarm] = 1;
    else
        data[NessLab_Over_Temp_Alarm] = 0; 

//    data[NessLab_Over_Temp_Alarm] = 0;
    if( data[NessLab_Temp_Monitor] == 0)
        data[NessLab_Temp_Monitor] = 1;
    else
        data[NessLab_Temp_Monitor] = 0; 

//    data[NessLab_Temp_Monitor] = 0;
    if( data[NessLab_ALC_ALARM] == 0)
        data[NessLab_ALC_ALARM] = 1;
    else
        data[NessLab_ALC_ALARM] = 0; 

//    data[NessLab_ALC_ALARM] = 0;
    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; 
}


void NessLab_Table_Frame_Set(uint8_t* data,uint8_t size){
    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++] = NessLab_STATUS_RES;// 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(){
	HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin));
	//HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, HAL_GPIO_ReadPin(PAU_RESET_GPIO_Port, PAU_RESET_Pin));
}
void NessLab_GPIO_Operate(){
	NessLab_PAU_Enable();
}

