/******************************************************************************
*
* @file    battery_voltage.c
* @author  ECS, Falko Jahn
* @version V1.0.0
* @date    2020-05-01
* @brief
*
******************************************************************************/

//	--- INCLUDES -----------------------------------------------------------------
#include "battery_voltage.h"
#include "main.h"
#include "sysdata.h"
#include "stdlib.h"
#include "stdio.h"

//	--- EXTERNE VARIABLEN --------------------------------------------------------

//	--- LOKALE DEFINES - bitte hier dokumentieren --------------------------------

#define BATTERY_VOLTAGE_FILTER  32


//	--- LOKALE TYPE DEFS - bitte hier dokumentieren-------------------------------

//	--- DEFINITIONEN GLOBALER VARIABLEN - Bitte in Header dokumentieren ----------
extern ADC_HandleTypeDef hadc2;
//	--- LOKALE VARIABLEN - bitte hier dokumentieren ------------------------------
 int32_t BATTERY_VOLTAGE_adc_offset =0;
 int32_t last_adc_value = 0 ;
//	--- LOKALE FUNKTIONS PROTOTYPEN ----------------------------------------------
void ADC2_SetOffset(uint32_t channel, int16_t offset);
//	--- LOKALE FUNKTIONEN - bitte hier dokumentieren -----------------------------

//	--- GLOBALE FUNKTIONEN - bitte in Header dokumentieren------------------------


void BATTERY_VOLTAGE_Exec(int32_t newVal)
{
  static int measCounter;
  static unsigned long avgSum = 0;
  int32_t avgVal;
  last_adc_value = newVal;
  if (measCounter < INT32_MAX) measCounter++;


  // Filterlängen in 2er-Potenzen --> Compiler optimiert
  //avgSum -= avgSum / BATTERY_VOLTAGE_FILTER;
  //avgSum += newVal;
  //avgVal =  avgSum / BATTERY_VOLTAGE_FILTER;

  


  //Umrechung auf Eingangsspannung am Gerät mit Teiler
  sys_data.s.values.batteryVoltage = ((newVal-BATTERY_VOLTAGE_ADC_OFFSET) * VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER ) / BATTERY_VOLTAGE_ADC_RESOLUTION;

  


  //Max und Minwert
  if (measCounter > BATTERY_VOLTAGE_FILTER * 10)
  {
    if (sys_data.s.values.batteryVoltage > sys_data.s.values.batteryVoltageMax)
    {
      sys_data.s.values.batteryVoltageMax = sys_data.s.values.batteryVoltage;
    }
    if (sys_data.s.values.batteryVoltage < sys_data.s.values.batteryVoltageMin)
    {
      sys_data.s.values.batteryVoltageMin = sys_data.s.values.batteryVoltage;
    }
  }



  //Berechnung schnelle Wert ohne Glättung:
  //Umrechung auf Eingangsspannung am Gerät mit Teiler
  //sys_data.s.values.fast_voltage = ((newVal-BATTERY_VOLTAGE_ADC_OFFSET) * VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER ) / BATTERY_VOLTAGE_ADC_RESOLUTION;



}

    ADC_ChannelConfTypeDef sConfig = {0};

void BATTERY_VOLTAGE_ZeroCal(void)
{
  BATTERY_VOLTAGE_adc_offset = last_adc_value - BATTERY_VOLTAGE_ADC_OFFSET;
  




    // 1. ADC2 stoppen und sicherstellen, dass er deaktiviert ist
    HAL_ADC_Stop(&hadc2);

  uint16_t offset_value = abs(BATTERY_VOLTAGE_adc_offset);

  if (BATTERY_VOLTAGE_adc_offset < 0 )
  {
   ADC2->OFR1 = (ADC_OFR1_OFFSET1_EN | 
                 (3 << ADC_OFR1_OFFSET1_CH_Pos) | 
				 (1 << ADC_OFR1_OFFSETPOS_Pos) |
                 (offset_value  ));
  }
  else 
  {
   ADC2->OFR1 = (ADC_OFR1_OFFSET1_EN | 
                 (3 << ADC_OFR1_OFFSET1_CH_Pos) | 
				 (0 << ADC_OFR1_OFFSETPOS_Pos) |
                 (offset_value  ));
  }

  // 5. ADC2 wieder für die Wandlung starten
  HAL_ADC_Start(&hadc2);
}



/*************************** End of file ****************************/