/*
 * BatteryManagement.c
 *
 *  Created on: 29.01.2022
 *      Author: max
 */

#include "BatteryManagement.h"
#include "BQ_Abstraction_Layer.h"

extern uint16_t cell_voltages[N_CELLS];

void ams_init(UART_HandleTypeDef* uarthandle, DMA_HandleTypeDef* uartdma)
{
	afe_init(uarthandle);
}

void ams_loop_discharging()
{
	afe_measure();

	if(bq_status == BQ_ERROR)
	{
		ams_status = AMS_TIMEOUT_ERROR;
	}
	else
	{
		ams_check_cell_voltages();
	}
}

void ams_loop_charging()
{

}

void ams_check_cell_voltages()
{
	modulesumvoltage = 0;
	highestcell = 0;
	lowestcell = 0;
	uint16_t lowestvoltage = cell_voltages[0];
	uint16_t highestvoltage = cell_voltages[0];

	for(int n = 0; n < N_CELLS; n++)
	{
		modulesumvoltage += cell_voltages[n];

		if(cell_voltages[n] > highestvoltage)
			highestvoltage = cell_voltages[n];

		if(cell_voltages[n] < lowestvoltage)
			lowestvoltage = cell_voltages[n];

		if(cell_voltages[n] > OVERVOLTAGE_LIMIT)
			ams_status = AMS_OV_ERROR;

		if(cell_voltages[n] < UNDERVOLTAGE_LIMIT)
			ams_status = AMS_UV_ERROR;
	}
}

void ams_check_cell_temperaures()
{

}

void ams_step_soc_model()
{

}

void ams_step_balancing_model()
{

}

void ams_fan_control()
{

}
