 /*
  * adc.c
  *
  *  Created on: 2020. 8. 3.
  *      Author: parkyj
  */
#include "main.h"
#include "adc.h"
#include "NessLab.h"
 
uint8_t ADC_Convert_Temperature(double val);

 extern ADC_HandleTypeDef hadc1;
 extern DMA_HandleTypeDef hdma_adc1;
 extern Nesslab_Prot Currstatus;
 
 
 //volatile uint16_t ADC1valuearray[ADC1_CNT][ADC_AVERAGECNT];
 volatile uint16_t ADC1value[ADC1_CNT];
 volatile uint16_t ADC1_Arrage[ADC_AVERAGECNT] = {0,};
 volatile uint32_t ADC1_Arrage_Ret[ADC_AVERAGECNT] = {0,};
 
 uint16_t adc1cnt = 0 ;
 uint32_t TotalCnt = 0;
 
 
 extern volatile uint32_t TDD_125ms_Cnt;
 
 
 /*
  *
  * ADC 0 :DL TX
  * ADC 1 :DL RX
  * ADC 2 :TEMP
  * */
 
 void ADC_Value_Get(){
     uint16_t CalcRet = 0 ;
     uint16_t Tx_Det_Volt = ((ADC1value[0] * (3.3 / 4095))* 1000);
     uint16_t Rx_Det_Volt = ((ADC1value[1] * (3.3 / 4095))* 1000);
     int8_t Real_Temperature = ADC_Convert_Temperature((ADC1value[2] * (3.3 / 4095)));
     /*DL TX Calc*/
     Currstatus.DownLink_Forward_Det_H = ((Tx_Det_Volt & 0xFF00) >> 8);
     Currstatus.DownLink_Forward_Det_L = (Tx_Det_Volt & 0x00FF);
     printf("Tx_Det_Volt : %d \r\n",Tx_Det_Volt);
 
     /*DL RX Calc*/
     Currstatus.DownLink_Reverse_Det_H = ((Rx_Det_Volt & 0xFF00) >> 8);
     Currstatus.DownLink_Reverse_Det_L = (Rx_Det_Volt & 0x00FF);
 
     /*Temp Calc*/
     Currstatus.Temp_Monitor = Real_Temperature;
 
 
 }
 void ADC_Initialize(){
     while(!(HAL_ADCEx_Calibration_Start(&hadc1)==HAL_OK));
     HAL_ADC_Start_DMA(&hadc1, (uint16_t*)ADC1value,(uint32_t) 3);
 }
 
 uint8_t ADC_Convert_Temperature(double val){
     int16_t ref_0temp = 500;
     int16_t ret = val * 1000;
     int8_t cnt = 0;
 
     printf("ret : %d \r\n", ret);
     if( ret - ref_0temp > 0){
         for(int i = 0; i < ret - ref_0temp; i += 10){
             cnt++;
         }
     }else{
         for(int i = 0; i > ret - ref_0temp; i -= 10){
             cnt--;
         }
     }
     printf("Temp : %d\r\n",cnt);
     return cnt;
 }
 uint32_t SumFunc(uint32_t* data,uint16_t size){
     uint32_t ret = 0;
     for (uint16_t i = 0; i < size; i++)    // 배열의 요소 개수만큼 반복
     {
         ret += data[i];    // sum과 배열의 요소를 더해서 다시 sum에 저장
 //        printf("ret : %d  data[%d] \r\n",ret,i,data[i]);
     }
     return ret;
 }
 
 
 void DascendigFunc(int32_t* src,uint32_t size ){
   int32_t temp;
 
   
   for(int i = 0 ; i < size - 1 ; i ++) {
       for(int j = i+1 ; j < size ; j ++) {
       if(src[i] < src[j]) {
               temp = src[j];
               src[j] = src[i];
               src[i] = temp;
 //              printf("temp");
         }
       }
   }
 }
#define Percent100 5
 
 void ADC_Check(){
     float tempval = 0;

     for(int i = 0; i < 3 ; i++)
         printf("ADC1value[%d] : %d \r\n",i,ADC1value[i]);
     
 
 
     
#if 0 // PYJ.2020.09.09_BEGIN -- 
     Currstatus.DownLink_Forward_Det_H
         = (((uint16_t)tempval & 0xFF00) >> 8);
     Currstatus.DownLink_Forward_Det_L
         = (((uint16_t)tempval & 0x00FF) );
#endif // PYJ.2020.09.09_END -- 
    ADC_Value_Get();

//      Currstatus.Temp_Monitor = ADC_Convert_Temperature((ADC1value[2]/1000));
//     printf("Currstatus.DownLink_Forward_Det : %d \r\n",Currstatus.DownLink_Forward_Det_H << 8 | Currstatus.DownLink_Forward_Det_L);
 }
 void ADC_TDD_Arrange(){
     uint32_t ADC1_Average_value = 0;
     
 
     if(TDD_125ms_Cnt > 125)
     {
         
 //        printf("================TotalCnt %d  =================\r\n",TotalCnt);
#if 1 // PYJ.2020.09.09_BEGIN -- 
         DascendigFunc(&ADC1_Arrage_Ret[0],ADC_AVERAGECNT); 
 
 //        for(int i = 0; i < 100; i++){/*ADC Data Dascending Complete*/
 //            printf("ADC1_Arrage_Ret[%d] : %d \r\n",i,ADC1_Arrage_Ret[i]);
 //        }           
 
         ADC1_Average_value = SumFunc(&ADC1_Arrage_Ret[0],Percent100);
         
 //        printf("ADC1_Average_value Sum  : %d \r\n",ADC1_Average_value);
         ADC1_Average_value /=Percent100;    
 //        printf("ADC1_Average_value : %d \r\n",ADC1_Average_value);
     for(int i = 0; i < ADC_AVERAGECNT; i++){
         ADC1_Arrage_Ret[i] = 0;
     }       
 
     Currstatus.DownLink_Forward_Det_H
         = (((uint16_t)ADC1_Average_value & 0xFF00) >> 8);
     Currstatus.DownLink_Forward_Det_L
         = (((uint16_t)ADC1_Average_value & 0x00FF) );        
#else
         for(int i = 0; i < ADC_AVERAGECNT; i++){
             ADC1_Arrage_Ret[i] = ADC1_Arrage[i];
         }
#endif // PYJ.2020.09.09_END -- 
 
         
         TotalCnt = 0;
         TDD_125ms_Cnt = 0;
     }
 
 }
 
 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
 {
     if(hadc->Instance == hadc1.Instance && TDD_125ms_Cnt < 125)
     {
         
         ADC1_Arrage[adc1cnt] = ADC1value[0];
 //        for(int i = 0; i < 2; i++){
 //            printf("ADC1value[%d] : %d , %f\r\n",i,ADC1value[i],(float)((ADC1value[i]) *3.3 /4095));
 //        }
         adc1cnt++;
         
         if(adc1cnt == ADC_AVERAGECNT){
 //            DascendigFunc(&ADC1_Arrage[0],ADC_AVERAGECNT);
 
             adc1cnt = 0;
             TotalCnt++;
             if(TotalCnt > 2)
                 TotalCnt = 2;
             
             for(int i = 0; i < 100; i++){/*ADC Data Dascending Complete*/
                 if(ADC1_Arrage_Ret[i] <= ADC1_Arrage[i])
                     ADC1_Arrage_Ret[i] = ADC1_Arrage[i];
             } 
             DascendigFunc(&ADC1_Arrage_Ret[0],ADC_AVERAGECNT);              
 //            for(int i = 0; i < 100; i++){/*ADC Data Dascending Complete*/
 //                printf("ADC1_Arrage_Ret[%d] : %d \r\n",i,ADC1_Arrage_Ret[i]);
 //            }             
 //                else
 //                ADC1_Arrage_Ret[i] /= TotalCnt;
             
         }
 //        if(adc1cnt < ADC_AVERAGECNT){
 //            for(int i = 0; i < ADC1_CNT; i++){
 //                ADC1valuearray[i][adc1cnt] = ADC1value[i];
 //            }
 //            adc1cnt++;
 //        }
     }
 }
