From 2e41389fadc8111d7527a3e0d1d8218add956beb Mon Sep 17 00:00:00 2001 From: penglei <1624952103@qq.com> Date: Fri, 6 Dec 2024 09:20:19 +0800 Subject: [PATCH] =?utf8?q?=E5=8E=BB=E6=8E=89=E7=94=B5=E9=87=8F=E5=8F=98?= =?utf8?q?=E5=8C=96=E9=98=88=E5=80=BC=E4=B8=8E=E5=BC=80=E6=9C=BA=E8=AF=BB?= =?utf8?q?=E5=8E=86=E5=8F=B2=E7=94=B5=E9=87=8F=EF=BC=8C=E4=BC=98=E5=8C=96d?= =?utf8?q?ebug=E7=95=8C=E9=9D=A2=E7=94=B5=E6=B1=A0=E4=BF=A1=E6=81=AF?= =?utf8?q?=E4=B8=8E=E4=BD=A9=E6=88=B4=E4=BF=A1=E6=81=AF=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../battery/service/battery_driver.cpp | 38 ++++++++++++------- .../battery/service/battery_service.cpp | 17 +++++---- .../drivers/peripheral/cywee/src/main_dml.c | 15 ++++---- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/code/sdk/application/battery/service/battery_driver.cpp b/code/sdk/application/battery/service/battery_driver.cpp index d2cfbbd9d..a6a4869b2 100644 --- a/code/sdk/application/battery/service/battery_driver.cpp +++ b/code/sdk/application/battery/service/battery_driver.cpp @@ -263,13 +263,14 @@ static uint32_t g_adc_sample_stick = 0; #define TESTADC_DISACRD_DELAY_MS 3 #define MINIMUM_VOLTAGE_RANGE 2 #define MAXIMUM_VOLTAGE_RANGE 17 +#define AVG_VOLTAGE_COUNT 7 static int history_percent = 0; static float sum_vol = 0; static int vol_num=0; -static float history_data[5]={0}; +static float history_data[AVG_VOLTAGE_COUNT]={0}; static int left = 0; static int right = -1; static uint32_t history_time = 0; @@ -431,37 +432,44 @@ static int battery_get_percent_by_voltage(float voltage) if(get_charge_stat()) { - if(history_percent > percent && abs(history_percent - percent) < 10) - // if(history_percent > percent) + // if(history_percent > percent && abs(history_percent - percent) < 10) + if(history_percent > percent) { + + // PRINT("1111 charge percent[%d], history_percent[%d] \n",percent,history_percent); percent = history_percent; + // PRINT("adjust percent[%d]\n",percent); break; } //电量均匀增加 - else if(percent > history_percent && history_percent && abs(history_percent - percent) < 10) - // else if(percent > history_percent && history_percent) + // else if(percent > history_percent && history_percent && abs(history_percent - percent) < 10) + else if(percent > history_percent && history_percent) { + // PRINT("2222 charge percent[%d], history_percent[%d] \n",percent,history_percent); history_percent++; percent = history_percent; - // PRINT("charge percent[%d], history_percent[%d] \n",percent,history_percent); + // PRINT("adjust percent[%d]\n",percent); break; } } else { - if(history_percent < percent && history_percent != 0 && abs(history_percent - percent) < 10) - // if(history_percent < percent && history_percent != 0) + // if(history_percent < percent && history_percent != 0 && abs(history_percent - percent) < 10) + if(history_percent < percent && history_percent != 0) { + // PRINT("1111 uncharge percent[%d], history_percent[%d] \n",percent,history_percent); percent = history_percent; + // PRINT("adjust percent[%d]\n",percent); break; } //电量均匀减少 - else if(history_percent > percent && abs(history_percent - percent) < 10) - // else if(history_percent > percent) + // else if(history_percent > percent && abs(history_percent - percent) < 10) + else if(history_percent > percent) { + // PRINT("2222 uncharge percent[%d], history_percent[%d] \n",percent,history_percent); history_percent--; percent = history_percent; - // PRINT("uncharge percent[%d], history_percent[%d] \n",percent,history_percent); + // PRINT("adjust percent[%d]\n",percent); break; } @@ -515,20 +523,21 @@ static void get_avg_voltage(float *vol) if(sample_time >= 10) { sample_time = 0; - if(vol_num == 7) + if(vol_num == AVG_VOLTAGE_COUNT) { sum_vol -= history_data[left]; history_data[left] = 0; left++; - left = left % 7; + left = left % AVG_VOLTAGE_COUNT; vol_num--; } vol_num++; right++; - right = right % 7; + right = right % AVG_VOLTAGE_COUNT; history_data[right] = *vol; sum_vol += history_data[right]; + // PRINT("history_data[right] : %.3f\n",history_data[right]); } if(vol_num > 0) { @@ -538,6 +547,7 @@ static void get_avg_voltage(float *vol) max_vol = history_data[0]; for (int i = 0;i < 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) diff --git a/code/sdk/application/battery/service/battery_service.cpp b/code/sdk/application/battery/service/battery_service.cpp index 14a54aab8..0e19fd5bf 100644 --- a/code/sdk/application/battery/service/battery_service.cpp +++ b/code/sdk/application/battery/service/battery_service.cpp @@ -105,7 +105,7 @@ void battery_file_check(BATTERY_DATA_T newStruct, const char *filename) FILE *fp; BATTERY_DATA_T data[200]; uint8_t count = 0; - uint8_t oldest = 0; + // uint8_t oldest = 0; fp = fopen(filename, "r+b"); if(!fp) fp = fopen(filename, "wb"); @@ -118,15 +118,16 @@ void battery_file_check(BATTERY_DATA_T newStruct, const char *filename) { fread(data, sizeof(BATTERY_DATA_T), count, fp); - for (int i = 1; i < count; i++) + for (int i = 0; i < count - 1; i++) { - if (data[i].tm < data[oldest].tm) - { - oldest = i; - } + // if (data[i].tm < data[oldest].tm) + // { + // oldest = i; + // } + data[i] = data[i + 1]; } - data[oldest] = newStruct; + data[count - 1] = newStruct; fseek(fp, 0, SEEK_SET); fwrite(data, sizeof(BATTERY_DATA_T), count, fp); @@ -666,7 +667,7 @@ void uapi_battery_create_queue(void) errcode_t uapi_battery_init(void) { battery_check_voltage(); - battery_percent_init(battery_get_percent_onstart()); //先读历史电量 + battery_percent_init(0); //先读历史电量 app_task_definition_t taskcfg={0}; diff --git a/code/sdk/drivers/peripheral/cywee/src/main_dml.c b/code/sdk/drivers/peripheral/cywee/src/main_dml.c index 50899c18a..489c24dc4 100644 --- a/code/sdk/drivers/peripheral/cywee/src/main_dml.c +++ b/code/sdk/drivers/peripheral/cywee/src/main_dml.c @@ -1916,7 +1916,7 @@ void wear_file_check(SLEEP_STAT_T newStruct, const char *filename) FILE *fp; SLEEP_STAT_T data[50]; uint8_t count = 0; - uint8_t oldest = 0; + // uint8_t oldest = 0; fp = fopen(filename, "r+b"); if(!fp) fp = fopen(filename, "wb"); @@ -1929,15 +1929,16 @@ void wear_file_check(SLEEP_STAT_T newStruct, const char *filename) { fread(data, sizeof(SLEEP_STAT_T), count, fp); - for (int i = 1; i < count; i++) + for (int i = 0; i < count - 1; i++) { - if (data[i].timestamp < data[oldest].timestamp) - { - oldest = i; - } + // if (data[i].tm < data[oldest].tm) + // { + // oldest = i; + // } + data[i] = data[i + 1]; } - data[oldest] = newStruct; + data[count - 1] = newStruct; fseek(fp, 0, SEEK_SET); fwrite(data, sizeof(SLEEP_STAT_T), count, fp); -- 2.17.1