#ifdef ENABLE_HP4570
#include "hp4570.h"
#endif
+#include "custom_utils.h"
+
+#define AVG_VOLTAGE_COUNT 7
+
+#pragma pack(1)
+typedef struct
+{
+ // float avg_vol = 0;
+ float sum_vol = 0;
+ uint8_t vol_num = 0;
+ float history_data[AVG_VOLTAGE_COUNT] = {0};
+ int left = 0;
+ int right = -1;
+ uint32_t time = 0;
+ // uint8_t percent = 0;
+} BAT_VOL_T;
+#pragma pack()
+
#define HAL_ADC_SAMPLE_NUMBER_CONFIG_0 10 /* Channel with buffer manual sample average number. */
#define HAL_ADC_SAMPLE_NUMBER_CONFIG_1 10 /* Channel without buffer manual sample average number. */
#define TESTADC_DISACRD_DELAY_MS 3
#define MINIMUM_VOLTAGE_RANGE 2
#define MAXIMUM_VOLTAGE_RANGE 17
-#define AVG_VOLTAGE_COUNT 7
+
+static BAT_VOL_T bat_vol;
static int history_percent = 0;
-static float sum_vol = 0;
-static int vol_num=0;
+// static float sum_vol = 0;
+// static int vol_num=0;
-static float history_data[AVG_VOLTAGE_COUNT]={0};
-static int left = 0;
-static int right = -1;
+// static float history_data[AVG_VOLTAGE_COUNT]={0};
+// static int left = 0;
+// static int right = -1;
static uint32_t history_time = 0;
static uint32_t sample_time = 0;
static float test_temp = 0;
+bool batteryservice_get_voltage_by_channel(float *voltage, uint8_t channel);
+
+void battery_save_vol(void)
+{
+ uint32_t time = 0;
+ uapi_get_timestampe_ss(&time);
+ for(int i = 0; i < bat_vol.vol_num;i++)
+ {
+ // printf("history_data[%d]:%.3f\n",i,bat_vol.history_data[i]);
+ if(bat_vol.history_data[i] < 3.3 || bat_vol.history_data[i] > 4.45)
+ bat_vol.history_data[i] = 0;
+ }
+
+ bat_vol.time = time;
+ // bat_vol.percent = cur_percent;
+ uapi_save_file_data("/user/userdata/battery.bin",(uint8_t *)&bat_vol,sizeof(BAT_VOL_T),"wb");
+ // uapi_save_file_data("/user/userdata/battery_time.bin",(uint8_t *)&time,sizeof(uint32_t),"wb");
+}
+
+void battery_restore_vol(void)
+{
+ // float cur_vol = 0;
+ uint32_t time = 0;
+ uint16_t len = sizeof(BAT_VOL_T);
+
+ uapi_get_file_data("/user/userdata/battery.bin",(uint8_t *)&bat_vol,&len);
+
+ uapi_get_timestampe_ss(&time);
+
+ printf("time:%d,l_time:%d\n",time,bat_vol.time );
+ if(time - bat_vol.time >= 24 * 60 * 60)
+ {
+ memset_s(&bat_vol,sizeof(BAT_VOL_T),0,sizeof(BAT_VOL_T));
+ bat_vol.right = -1;
+ return;
+ }
+
+ if(bat_vol.vol_num < 0 || bat_vol.vol_num > AVG_VOLTAGE_COUNT ||
+ bat_vol.right < -1 || bat_vol.right >= AVG_VOLTAGE_COUNT ||
+ bat_vol.left < -1 || bat_vol.left >= AVG_VOLTAGE_COUNT ||
+ bat_vol.sum_vol < bat_vol.vol_num * 3.25 || bat_vol.sum_vol > (bat_vol.vol_num * 4.45))
+ {
+ memset_s(&bat_vol,sizeof(BAT_VOL_T),0,sizeof(BAT_VOL_T));
+ bat_vol.right = -1;
+ return;
+ }
+
+ // batteryservice_get_voltage_by_channel(&cur_vol,ADC_CHANNEL_BAT_VOLTAGE);
+
+ // if(bat_vol.percent > 0 && bat_vol.percent<=100 && (fabsf(cur_vol - bat_vol.avg_vol) < 0.15))
+ // history_percent = bat_vol.percent;
+ // for(int i = 0; i < AVG_VOLTAGE_COUNT;i++)
+ // {
+ // printf("history_data[%d]:%.3f\n",i,bat_vol.history_data[i]);
+ // if(bat_vol.history_data[i] < 3.3 || bat_vol.history_data[i] > 4.45)
+ // {
+ // bat_vol.history_data[i] = 0;
+ // }
+
+ // }
+ // printf("bat_vol.percent:%d,cur_vol:%.3f,bat_vol.avg_vol:%.3f\n",bat_vol.vol_num,cur_vol,bat_vol.avg_vol);
+
+ // printf("vol_num:%d\n",bat_vol.vol_num);
+}
+
void battery_percent_init(uint8_t percent)
{
percernt_installized = 1;
sample_time = current_time - history_time;
history_time = current_time;
- if(sample_time >= 10)
+ if(sample_time >= 10 && *vol >= 3.25 && *vol <= 4.45)
{
sample_time = 0;
- if(vol_num == AVG_VOLTAGE_COUNT)
+ if(bat_vol.vol_num == AVG_VOLTAGE_COUNT)
{
- sum_vol -= history_data[left];
- history_data[left] = 0;
- left++;
- left = left % AVG_VOLTAGE_COUNT;
- vol_num--;
+ bat_vol.sum_vol -= bat_vol.history_data[bat_vol.left];
+ bat_vol.history_data[bat_vol.left] = 0;
+ bat_vol.left++;
+ bat_vol.left = bat_vol.left % AVG_VOLTAGE_COUNT;
+ bat_vol.vol_num--;
}
- vol_num++;
- right++;
- right = right % AVG_VOLTAGE_COUNT;
- history_data[right] = *vol;
- sum_vol += history_data[right];
+ bat_vol.vol_num++;
+ bat_vol.right++;
+ bat_vol.right = bat_vol.right % AVG_VOLTAGE_COUNT;
+ bat_vol.history_data[bat_vol.right] = *vol;
+ bat_vol.sum_vol += bat_vol.history_data[bat_vol.right];
// PRINT("history_data[right] : %.3f\n",history_data[right]);
}
- if(vol_num > 0)
+
+ if(bat_vol.vol_num > 0)
{
- if(vol_num > 2)
+ if(bat_vol.vol_num > 2)
{
- min_vol = history_data[0];
- max_vol = history_data[0];
- for (int i = 0;i < vol_num; i++)
+ min_vol = bat_vol.history_data[0];
+ max_vol = bat_vol.history_data[0];
+ for (int i = 0;i < bat_vol.vol_num; i++)
{
- // PRINT("history_data[%d] : %.3f\n",i,history_data[i]);
- if (history_data[i] < min_vol)
- min_vol = history_data[i];
- if (history_data[i] > max_vol)
- max_vol = history_data[i];
+
+ // PRINT("333history_data[%d] : %.3f\n",i,bat_vol.history_data[i]);
+ if (bat_vol.history_data[i] < min_vol)
+ min_vol = bat_vol.history_data[i];
+ if (bat_vol.history_data[i] > max_vol)
+ max_vol = bat_vol.history_data[i];
}
- *vol = (sum_vol - max_vol - min_vol) / (vol_num - 2);
+
+
+ // bat_vol.avg_vol = (bat_vol.sum_vol - max_vol - min_vol) / (bat_vol.vol_num - 2);
+ // printf("avg_vol : %.3f\n",bat_vol.avg_vol);
+ *vol = (bat_vol.sum_vol - max_vol - min_vol) / (bat_vol.vol_num - 2);
+ // printf("avg_vol : %.3f\n",*vol);
}
else
- *vol = sum_vol / vol_num;
+ *vol = bat_vol.sum_vol / bat_vol.vol_num;
+ // bat_vol.avg_vol = bat_vol.sum_vol / bat_vol.vol_num;
}
}
int DecimalPart_1 = (int)(DecimalPart / 100);
int DecimalPart_2 = (int)((DecimalPart % 100) / 10);
int DecimalPart_3 = (int)(DecimalPart % 10);
- // PRINT("*voltage : %.3f \n",*voltage);
+ // PRINT("222*voltage : %.3f \n",*voltage);
// PRINT("DecimalPart_1 : %d , DecimalPart_2 : %d DecimalPart_3 : %d \n",DecimalPart_1,DecimalPart_2,DecimalPart_3);
uint16_t value = DecimalPart_1 << 8 | DecimalPart_2 << 4 | DecimalPart_3 << 0;
}
else
{
+ // cur_percent = battery_get_percent_by_voltage(bat_vol.avg_vol);
cur_percent = battery_get_percent_by_voltage(*voltage);
}