/*
 * @Description: 
 * @Date: 2021-03-10 10:44:19
 * @LastEditors: CK.Zh
 * @LastEditTime: 2021-03-10 10:45:36
 * @FilePath: \NaviKit_EC_stm32\App\Src\th_power.c
 */
#define LOG_TAG    "TH-Power"

#include <th_power.h>
#include "main.h"
#include "adc.h"
#include "navikit.h"

const osThreadAttr_t PowerMonitTask_attributes = {
  .name = "PowerMonitTask",
  .priority = (osPriority_t) osPriorityBelowNormal,
  .stack_size = 256 * 4
};

 //Event Flag
 const osEventFlagsAttr_t PowerMonitorEventFlags_attributes = {
   .name = "PowerMonitorEventFlags"
 };



/* USER CODE BEGIN Header_PowerMonitTask */
/**
* @brief Function: Monit som power status and PMB status
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_PowerMonitTask */
void PowerMonitTask(void *argument)
{
  /* USER CODE BEGIN PowerMonitTask */
	log_d("Start Power Monitor Task");
	HAL_ADCEx_Calibration_Start(&hadc1);
	HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT);
	osDelay(10);
  /* Infinite loop */
  for(;;)
  {
		if((NaviKit.pmb.rails.main_pwr > 30) || (NaviKit.pmb.rails.main_pwr < 19)){
			NaviKit.pmb.main_pwr_good = false;
			 log_e("Main power :%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
			 if((NaviKit.sys.next_sta == run) && (NaviKit.sys.next_sta == run)){
				 TaskBeep(200,2);
			 }
		}else{
			log_d("Main power :%dmV",(uint32_t)(NaviKit.pmb.rails.main_pwr*1000));
			NaviKit.pmb.main_pwr_good = true;
		}

		if((NaviKit.pmb.rails.bkp_bat < 8) || (NaviKit.pmb.rails.bkp_bat > 15)){
			 TaskBeep(200,2);
		}else{
			//stat1 stat2   lead-acid
			//1		1		Not Charging
			//1		0		Float Charge
			//0		1		Absorption Charge
			//0		0		Bulk Charge
			NaviKit.pmb.sta.chrg_stat1 = (bool)HAL_GPIO_ReadPin(PMB_CHRG_STAT1_GPIO_Port, PMB_CHRG_STAT1_Pin);
			NaviKit.pmb.sta.chrg_stat2 = (bool)HAL_GPIO_ReadPin(PMB_CHRG_STAT2_GPIO_Port, PMB_CHRG_STAT2_Pin);

			if(false == NaviKit.pmb.sta.chrg_stat1 && false == NaviKit.pmb.sta.chrg_stat2)
				log_d("Backup battery :%dmV Bulk Charge(Vth=12.6V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
			else if(false == NaviKit.pmb.sta.chrg_stat1 && true == NaviKit.pmb.sta.chrg_stat2)
				log_d("Backup battery :%dmV Absorption Charge(Vth=14.4V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
			else if(true == NaviKit.pmb.sta.chrg_stat1 && false == NaviKit.pmb.sta.chrg_stat2)
				log_d("Backup battery :%dmV Float Charge(Vth=13.3V)",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
			else if(true == NaviKit.pmb.sta.chrg_stat1 && true == NaviKit.pmb.sta.chrg_stat2)
				log_d("Backup battery :%dmV Not Charge",(uint32_t)(NaviKit.pmb.rails.bkp_bat*1000));
		}

		if(NaviKit.pmb.rails.out_24v > 26.4 || NaviKit.pmb.rails.out_24v < 21.6)
			 log_e("24V(±10%%) :%dmV",(uint32_t)(NaviKit.pmb.rails.out_24v*1000));
		else
			log_d("24V(±10%%) :%dmV",(uint32_t)(NaviKit.pmb.rails.out_24v*1000));

		if(NaviKit.pmb.rails.out_5v > 5.5 || NaviKit.pmb.rails.out_5v < 4.5)
			 log_e("5V(±10%%) :%dmV",(uint32_t)(NaviKit.pmb.rails.out_5v*1000));
		else
			log_d("5V(±10%%) :%dmV",(uint32_t)(NaviKit.pmb.rails.out_5v*1000));

		if(NaviKit.pmb.rails.out_12v > 13.2 || NaviKit.pmb.rails.out_12v < 10.8)
			 log_e("12V(±10%%) :%dmV",(uint32_t)(NaviKit.pmb.rails.out_12v*1000));
		else
			log_d("12V(±10%%) :%dmV",(uint32_t)(NaviKit.pmb.rails.out_12v*1000));



		osDelay(3000);
  }
  /* USER CODE END PowerMonitTask */
}
