/*
 * Current_Monitoring.c
 *
 *  Created on: 24. April, 2024
 *      Author: nived
 */

#include "Current_Monitoring.h"
#include "main.h"

volatile union adc1_channels {
  struct {
    uint16_t isense12;  // acu
    uint16_t isense11;  // epsc
    uint16_t isense6;   // radiator fans 
    uint16_t isense13;  // tsac fans 
    uint16_t isense5;   // gss and lidar 
    uint16_t isense3;   // sdc and always on 
    uint16_t isense7;   // misc and inverters
  } adcbank1;

  uint16_t adcbuffer[7]; // array 7*16 bit
 
} adc_channels1;
// ADC's anpassen adc1 - 9, adc2 ist halt 5 , buffer anpassen und namen auch ( isense usw)
volatile union adc2_channels {
  struct {
    uint16_t isense2;    // ebs valve a and ebs valve b 
    uint16_t isense1;    // ebs cs valve 
    uint16_t isense10;   // hyr aggregat 
    uint16_t isense4;     // cooling pump and reserved 

  } adcbank1;

  uint16_t adcbuffer[4];

} adc_channels2;

CurrentMeasurements current_measurements_adc_val;

GPIO_PinState adcbank1 = GPIO_PIN_RESET;
GPIO_PinState adcbank2 = GPIO_PIN_RESET;

ADC_HandleTypeDef* adc1;
ADC_HandleTypeDef* adc2;

void currentMonitor_init(ADC_HandleTypeDef* hadc1, ADC_HandleTypeDef* hadc2, // init ist initilisierung 
                         TIM_HandleTypeDef* trigtim) {
  HAL_GPIO_WritePin(DSEL_3_GPIO_Port, DSEL_3_Pin, adcbank2); //DSELs zu adc's neu zuordnen 
  HAL_GPIO_WritePin(DSEL_4_GPIO_Port, DSEL_4_Pin, adcbank2);
  HAL_GPIO_WritePin(DSEL_5_GPIO_Port, DSEL_5_Pin, adcbank1);
  HAL_GPIO_WritePin(DSEL_7_GPIO_Port, DSEL_7_Pin, adcbank1);
  HAL_GPIO_WritePin(DSEL_8_GPIO_Port, DSEL_8_Pin, adcbank2);
  adc1 = hadc1;
  adc2 = hadc2;
  HAL_TIM_Base_Start(trigtim);
  HAL_ADC_Start_DMA(hadc1, (uint32_t*)adc_channels1.adcbuffer, 7);
  HAL_ADC_Start_DMA(hadc2, (uint32_t*)adc_channels2.adcbuffer, 4); // wie adc mit dma geht , red mit jasper
}

uint8_t currentMonitor_checklimits() { return 0; }

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
  if (hadc == adc2) {
    if (adcbank2 == GPIO_PIN_RESET) {
      current_measurements_adc_val.ebsvalve_a =
          adc_channels2.adcbank1.isense2 * CURR_SENSE_FACTOR_5A;
      current_measurements_adc_val.cooling_pump =
          adc_channels2.adcbank1.isense4 * CURR_SENSE_FACTOR_7_5A;
      adcbank2 = GPIO_PIN_SET;
    } else {
      current_measurements_adc_val.ebsvalve_b =
          adc_channels2.adcbank1.isense2 * CURR_SENSE_FACTOR_5A;
      current_measurements_adc_val.reserved =
          adc_channels2.adcbank1.isense4 * CURR_SENSE_FACTOR_7_5A;
      adcbank2 = GPIO_PIN_RESET;
    }
    current_measurements_adc_val.ebs_cs_valve =
        adc_channels2.adcbank1.isense1 * CURR_SENSE_FACTOR_5A;
    current_measurements_adc_val.aggregat =
        adc_channels2.adcbank1.isense10 * CURR_SENSE_FACTOR_21A;

    HAL_GPIO_WritePin(DSEL_3_GPIO_Port, DSEL_3_Pin,
                      adcbank2);
    HAL_GPIO_WritePin(DSEL_8_GPIO_Port, DSEL_8_Pin,
                      adcbank2);
  }

  if (hadc == adc1) {
    if (adcbank1 == GPIO_PIN_RESET) {
      current_measurements_adc_val.sdc=
          adc_channels1.adcbank1.isense3 * CURR_SENSE_FACTOR_5A;
      current_measurements_adc_val.misc=
          adc_channels1.adcbank1.isense7 * CURR_SENSE_FACTOR_7_5A; 
      current_measurements_adc_val.gss=
          adc_channels1.adcbank1.isense5 * CURR_SENSE_FACTOR_7_5A; 
    
      adcbank1 = GPIO_PIN_SET;
    } else {
      current_measurements_adc_val.always_on =
          adc_channels1.adcbank1.isense3 * CURR_SENSE_FACTOR_5A;
      current_measurements_adc_val.inverters =
          adc_channels1.adcbank1.isense7 * CURR_SENSE_FACTOR_7_5A; 
      current_measurements_adc_val.lidar=
          adc_channels1.adcbank1.isense5 * CURR_SENSE_FACTOR_7_5A; 
      adcbank1 = GPIO_PIN_RESET;
    }
    current_measurements_adc_val.acu=
        adc_channels1.adcbank1.isense12 * CURR_SENSE_FACTOR_31A;
    current_measurements_adc_val.epsc =
        adc_channels1.adcbank1.isense11 * CURR_SENSE_FACTOR_21A;
    current_measurements_adc_val.tsac_fans =
        adc_channels1.adcbank1.isense13 * CURR_SENSE_FACTOR_31A;
    current_measurements_adc_val.radiator_fans=
          adc_channels1.adcbank1.isense6 * CURR_SENSE_FACTOR_21A; 

    HAL_GPIO_WritePin(DSEL_5_GPIO_Port, DSEL_5_Pin,
                      adcbank1);
    HAL_GPIO_WritePin(DSEL_4_GPIO_Port, DSEL_4_Pin,
                      adcbank1);                   
    HAL_GPIO_WritePin(DSEL_7_GPIO_Port, DSEL_7_Pin,
                      adcbank1); 
   }
}
 

 // current monitoring c und h anpassen 