/**
* @file ALGO_Bat_ATY.c
*
* @param Project ALGO_Algorithm_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2025 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief Familiar functions of Bat calc
*
* @version
* - 1_01_230107 > ATY
* -# Preliminary version, first Release
********************************************************************************
*/
#ifndef __ALGO_Bat_ATY_C
#define __ALGO_Bat_ATY_C
#include "ALGO_Bat_ATY.h"
/******************************* For user *************************************/
/******************************************************************************/
/**
* @brief
*
* @param vin bat vol now
* @param lastLevel last bat level
* @param vmax bat vol when power full
* @param vmin bat vol when power empty or limitted low power
* @param levels whole levels to split, start from 1(1 for empty)
* @param vFilter max vol for adc noise, must lower than ((vmax - vmin) / levels)
* @return uint8_t
*/
uint8_t Bat_AD_Levels(float vin, uint8_t lastLevel, float vmax, float vmin, uint8_t levels, float vFilter)
{
float vUnit = (vmax - vmin) / levels;
uint8_t resultLevel = lastLevel;
// input limit judgement
if(vmax <= vmin)
return 1;
if(vin >= vmax)
return levels;
else if(vin <= vmin)
return 1;
if(vFilter >= vUnit)
vFilter = vUnit / 2;
// calc
if(vin > (vmin + (vUnit * lastLevel) + (vFilter / 2))){
resultLevel++;
}
else if(vin < (vmin + (vUnit * (lastLevel - 1)) - (vFilter / 2))){
resultLevel--;
}
else{}
// output limit
if(resultLevel > levels)
resultLevel = levels;
else if(resultLevel < 1)
resultLevel = 1;
return resultLevel;
}
// mbP_CVBV = 3.2;//ADC_Get(&hadc1, ADC_CHANNEL_VREFINT) * VOL_PER_AD_ATY;
// mbP_CVPV = Bat_AD_Levels(mbP_CVBV, mbP_CVPV, 4.18, 3.20, 5, 0.096);
#endif /* __ALGO_Bat_ATY_C */
/******************************** End Of File *********************************/