#define MAXIMUM_VOLTAGE_RANGE 17
static int history_percent = 0;
-static int sum_percent = 0;
-static int percent_num=0;
-static int history_data[5]={0};
+
+static float sum_vol = 0;
+static int vol_num=0;
+
+static float history_data[5]={0};
static int left = 0;
static int right = -1;
static uint32_t history_time = 0;
}
+ if (voltage <= batteryservice_VOLTAGE_MIN)
+ percent = 0;
+
+ if (voltage >= batteryservice_VOLTAGE_MAX)
+ percent = 100;
+
+ return percent;
+}
+
+static void get_avg_voltage(float *vol)
+{
+ uint32_t current_time = 0;
uapi_get_timestampe_ss(¤t_time);
if(history_time == 0)
history_time = current_time;
if(sample_time >= 10)
{
sample_time = 0;
- if(percent_num == 5)
+ if(vol_num == 5)
{
- sum_percent -= history_data[left];
+ sum_vol -= history_data[left];
history_data[left] = 0;
left++;
left = left % 5;
- percent_num--;
+ vol_num--;
}
- percent_num++;
+ vol_num++;
right++;
right = right % 5;
- history_data[right] = percent;
- sum_percent += history_data[right];
- }
-
- percent = sum_percent / percent_num;
-
- if (voltage <= batteryservice_VOLTAGE_MIN)
- percent = 0;
+ history_data[right] = *vol;
+ sum_vol += history_data[right];
+ }
+ if(vol_num != 0)
+ *vol = sum_vol / vol_num;
- if (voltage >= batteryservice_VOLTAGE_MAX)
- percent = 100;
-
- return percent;
}
+
static bool adc_get_sample_result(float *voltage, uint8_t channel)
{
uint32_t sample_number = 0;
if(channel == 2)
{
*voltage = adc_trans_systick_to_battery_voltage(stick);
+ get_avg_voltage(voltage);
}
else if (channel == 0){
*voltage = adc_trans_systick_to_battery_ntc(stick);