/** ##########################################################################
**     Filename  : system_voltage_manage.c
**     Project   :
**     Module    :
**     Processor :
**     Version   : 1.0
**     Compiler  :
**     Date/Time :
**     Abstract  :
**     Contents  :
**     Note      :
**     (c) Copyright dmdz Co.,Ltd
**  --------------------------------------------------------------------------
**               R E V I S I O N   H I S T O R Y
**  --------------------------------------------------------------------------
**  Date        Ver      Author             Description

**  -20230620-  V01-- --Linboyi---            --初版--
** #########################################################################*/



/*****************************************************************************
* I N C L U D E   F I L E S
*****************************************************************************/
#include "system_voltage_manage.h"
#include "system_voltage_manage_cfg.h"



/*****************************************************************************
* D E F I N E S / M A C R O S
*****************************************************************************/


/*****************************************************************************
* T Y P E   D E F I N I T I O N S
*****************************************************************************/
typedef struct
{
	uint8_t  u8mode;
	uint16_t u16timer;
	uint8_t  u8new_mode;
	uint8_t  u8old_mode;
}system_voltage_manage_s;



/*****************************************************************************
* S T A T I C   V A R I A B L E S
*****************************************************************************/
system_voltage_manage_s gs_system_voltage_manage[NUM_OF_SYSTEM_VOLTAGE_MANAGE];



/*****************************************************************************
* G L O B A L   V A R I A B L E S
*****************************************************************************/


/*****************************************************************************
* C O N S T A N T S
*****************************************************************************/

/*****************************************************************************
* F U N C T I O N   P R O T O T Y P E
*****************************************************************************/


/*****************************************************************************
|Prototype			: system_voltage_manage_init
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void system_voltage_manage_init(void)
{
	uint8_t i;

	for(i = 0u; i < NUM_OF_SYSTEM_VOLTAGE_MANAGE; i ++)
	{
		gs_system_voltage_manage[i].u8mode     = SYSTEM_VOLTAGE_MODE_UNKNOWN;
		gs_system_voltage_manage[i].u16timer   = 0u;
		gs_system_voltage_manage[i].u8new_mode = SYSTEM_VOLTAGE_MODE_UNKNOWN;
		gs_system_voltage_manage[i].u8old_mode = SYSTEM_VOLTAGE_MODE_UNKNOWN;
	}
}



/*****************************************************************************
|Prototype			: system_voltage_manage_compare
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void system_voltage_manage_compare(uint8_t index)
{
	uint16_t adc_result_mv;
	uint16_t system_voltage_mode_low_max;
	uint16_t system_voltage_mode_high_min;
	uint16_t system_voltage_mode_normal_min;
	uint16_t system_voltage_mode_normal_max;

	adc_result_mv = SYSTEM_VOLTAGE_MANAGE_GET_ADC_MV();

	switch(gs_system_voltage_manage[index].u8mode)
	{
		case SYSTEM_VOLTAGE_MODE_NORMAL:
			system_voltage_mode_low_max  = system_voltage_cfg_max_get(index, SYSTEM_VOLTAGE_MODE_LOW_CFG_ID);
			system_voltage_mode_high_min = system_voltage_cfg_min_get(index, SYSTEM_VOLTAGE_MODE_HIGH_CFG_ID);
			if(adc_result_mv <= system_voltage_mode_low_max)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_LOW;
			}
			else if(adc_result_mv >= system_voltage_mode_high_min)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_HIGH;
			}
			else
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_NORMAL;
			}
			break;
		case SYSTEM_VOLTAGE_MODE_LOW:
			system_voltage_mode_normal_min = system_voltage_cfg_min_get(index, SYSTEM_VOLTAGE_MODE_NORMAL_CFG_ID);
			system_voltage_mode_high_min   = system_voltage_cfg_min_get(index, SYSTEM_VOLTAGE_MODE_HIGH_CFG_ID);
			if(adc_result_mv >= system_voltage_mode_high_min)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_HIGH;
			}
			else if(adc_result_mv >= system_voltage_mode_normal_min)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_NORMAL;
			}
			else
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_LOW;
			}
			break;
		case SYSTEM_VOLTAGE_MODE_HIGH:
			system_voltage_mode_low_max    = system_voltage_cfg_max_get(index, SYSTEM_VOLTAGE_MODE_LOW_CFG_ID);
			system_voltage_mode_normal_max = system_voltage_cfg_max_get(index, SYSTEM_VOLTAGE_MODE_NORMAL_CFG_ID);
			if(adc_result_mv <= system_voltage_mode_low_max)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_LOW;
			}
			else if(adc_result_mv <= system_voltage_mode_normal_max)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_NORMAL;
			}
			else
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_HIGH;
			}
			break;
		case SYSTEM_VOLTAGE_MODE_UNKNOWN:
			system_voltage_mode_low_max = system_voltage_cfg_max_get(index, SYSTEM_VOLTAGE_MODE_LOW_CFG_ID);
			system_voltage_mode_high_min = system_voltage_cfg_min_get(index, SYSTEM_VOLTAGE_MODE_HIGH_CFG_ID);
			if(adc_result_mv <= system_voltage_mode_low_max)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_LOW;
			}
			else if(adc_result_mv >= system_voltage_mode_high_min)
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_HIGH;
			}
			else
			{
				gs_system_voltage_manage[index].u8new_mode = SYSTEM_VOLTAGE_MODE_NORMAL;
			}
			break;
		default:
			while(1)
			{
				;
			}
	}
}

/*****************************************************************************
|Prototype			: system_voltage_manage_decode
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void system_voltage_manage_decode(uint8_t index)
{

	uint16_t debounce_time;


	if(gs_system_voltage_manage[index].u8new_mode != gs_system_voltage_manage[index].u8old_mode)
	{
		gs_system_voltage_manage[index].u16timer = 0;
	}
	else
	{
		if(gs_system_voltage_manage[index].u16timer < 0xffffu)
		{
			gs_system_voltage_manage[index].u16timer ++;
		}


		if(gs_system_voltage_manage[index].u8new_mode != gs_system_voltage_manage[index].u8mode)
		{
			if(gs_system_voltage_manage[index].u8mode == SYSTEM_VOLTAGE_MODE_UNKNOWN)
			{
				debounce_time = SYSTEM_VOLTAGE_MODE_UNKNOWN_DEBOUNCE_TIME;
			}
			else
			{
				debounce_time = system_voltage_cfg_debounce_time_get(index, gs_system_voltage_manage[index].u8new_mode);
			}

			if(gs_system_voltage_manage[index].u16timer >= debounce_time)
			{
				gs_system_voltage_manage[index].u8mode = gs_system_voltage_manage[index].u8new_mode;
			}
		}
	}
	gs_system_voltage_manage[index].u8old_mode = gs_system_voltage_manage[index].u8new_mode;



}


/*****************************************************************************
|Prototype			: system_voltage_manage_task
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void system_voltage_manage_task(void)
{
	uint8_t i;


	for(i = 0; i < NUM_OF_SYSTEM_VOLTAGE_MANAGE; i ++)
	{
		system_voltage_manage_compare(i);
		system_voltage_manage_decode(i);
	}
}



/*****************************************************************************
|Prototype			: system_voltage_manage_mode_get
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
uint8_t system_voltage_manage_mode_get(uint8_t system_voltage_manage_id)
{
	uint8_t system_voltage_mode;

	switch(gs_system_voltage_manage[system_voltage_manage_id].u8mode)
	{
		case SYSTEM_VOLTAGE_MODE_UNKNOWN:
			system_voltage_mode = SYSTEM_VOLTAGE_MODE_UNKNOWN;
		break;
		case SYSTEM_VOLTAGE_MODE_NORMAL:
			system_voltage_mode = SYSTEM_VOLTAGE_MODE_NORMAL;
		break;
		case SYSTEM_VOLTAGE_MODE_LOW:
			system_voltage_mode = SYSTEM_VOLTAGE_MODE_LOW;
		break;
		case SYSTEM_VOLTAGE_MODE_HIGH:
			system_voltage_mode = SYSTEM_VOLTAGE_MODE_HIGH;
		break;
		default:
			system_voltage_mode = SYSTEM_VOLTAGE_MODE_UNKNOWN;
			gs_system_voltage_manage[system_voltage_manage_id].u8mode = SYSTEM_VOLTAGE_MODE_UNKNOWN;
		break;
	}
	return(system_voltage_mode);
}