/*
 * AIR_State_Maschine.h
 *
 *  Created on: Jun 15, 2022
 *      Author: max
 */

#ifndef INC_AIR_STATE_MASCHINE_H_
#define INC_AIR_STATE_MASCHINE_H_

#include "main.h"

#include "stm32g431xx.h"
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_gpio.h"

#define TS_INACTIVE 0
#define TS_PRECHARGE 2
#define TS_DISCHARGE 3
#define TS_ERROR 4
#define TS_ACTIVE 1
#define TS_CHARGING_CHECK 5
#define TS_CHARGING 6

#define ADC_READ_TIMEOUT 500       // in ms
#define SDC_LOWER_THRESHOLD 2500   // in ADC Values
#define PRECHARGE_95_DURATION 1000 // in ms
#define PRECHARGE_OPEN_AFTER 1000  // in ms
// FIXME
#define LOWER_VEHICLE_SIDE_VOLTAGE_LIMIT 150000 // in mV

typedef struct {

  int32_t BatteryVoltageVehicleSide;
  int32_t BatteryVoltageBatterySide;
  int32_t shuntCurrent;
  uint8_t targetTSState;
  uint8_t currentTSState;
  uint32_t precharge95ReachedTimestamp;
  uint32_t chargingCheckTimestamp;

} AIRStateHandler;
extern AIRStateHandler airstate;

void init_AIR_State_Maschine();

uint8_t Update_AIR_State();
void Activate_TS();
void Deactivate_TS();

void AIR_Precharge_Position();
void AIR_Inactive_Position();
void AIR_Discharge_Position();
void AIR_Active_Position();
void AIR_Error_Position();

typedef enum { RELAY_AIR_NEG, RELAY_AIR_POS, RELAY_PRECHARGE } Relay;

void Set_Relay_Position(Relay relay, GPIO_PinState position);

#endif /* INC_AIR_STATE_MASCHINE_H_ */
