From da2895d355651bf16774e9d21896de6dab266998 Mon Sep 17 00:00:00 2001 From: penglei <1624952103@qq.com> Date: Sat, 7 Dec 2024 16:26:41 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96debug=E7=95=8C=E9=9D=A2?= =?utf8?q?=E7=94=B5=E6=B1=A0=E4=BF=A1=E6=81=AF=E4=B8=8E=E4=BD=A9=E6=88=B4?= =?utf8?q?=E4=BF=A1=E6=81=AF=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../battery/service/battery_service.cpp | 36 +++++++++++-------- .../debug_output_notification.cpp | 7 +++- .../drivers/peripheral/cywee/src/main_dml.c | 30 ++++++++-------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/code/sdk/application/battery/service/battery_service.cpp b/code/sdk/application/battery/service/battery_service.cpp index 0e19fd5bf..9096afe1d 100644 --- a/code/sdk/application/battery/service/battery_service.cpp +++ b/code/sdk/application/battery/service/battery_service.cpp @@ -45,6 +45,7 @@ extern "C" { #include "cywee_service.h" +#define BAT_FILE_COUNT 180 bool batteryservice_get_voltage_by_channel(float *voltage, uint8_t channel); battery_info_t *get_current_battery_info(void); @@ -103,41 +104,39 @@ static uint32_t len = 0; void battery_file_check(BATTERY_DATA_T newStruct, const char *filename) { FILE *fp; - BATTERY_DATA_T data[200]; - uint8_t count = 0; + uint16_t count = 0; // uint8_t oldest = 0; fp = fopen(filename, "r+b"); if(!fp) + { fp = fopen(filename, "wb"); + } fseek(fp, 0, SEEK_END); count = ftell(fp) / sizeof(BATTERY_DATA_T); fseek(fp, 0, SEEK_SET); - // PRINT("battery data count %d \n",count); - if (count >= 180) + + if (count >= BAT_FILE_COUNT) { - fread(data, sizeof(BATTERY_DATA_T), count, fp); + BATTERY_DATA_T data[BAT_FILE_COUNT] = {0}; - for (int i = 0; i < count - 1; i++) + fseek(fp,(count - BAT_FILE_COUNT + 1) * sizeof(BATTERY_DATA_T), SEEK_SET); + fread(data, sizeof(BATTERY_DATA_T), BAT_FILE_COUNT - 1, fp); + data[BAT_FILE_COUNT - 1] = newStruct; + + if(count > BAT_FILE_COUNT) { - // if (data[i].tm < data[oldest].tm) - // { - // oldest = i; - // } - data[i] = data[i + 1]; + fp = freopen(filename, "wb", fp); } - data[count - 1] = newStruct; - fseek(fp, 0, SEEK_SET); - fwrite(data, sizeof(BATTERY_DATA_T), count, fp); + fwrite(data, sizeof(BATTERY_DATA_T), BAT_FILE_COUNT, fp); } else { fseek(fp, 0, SEEK_END); fwrite(&newStruct, sizeof(BATTERY_DATA_T), 1, fp); } - fclose(fp); } @@ -148,6 +147,10 @@ void battery_get_data_t(BATTERY_DATA_T **data,uint16_t *length) return; *length = len; *data = (BATTERY_DATA_T *)malloc(len); + + if(*data == NULL) + return; + memcpy_s(*data,len,bat_t,len); free(bat_t); } @@ -159,6 +162,9 @@ void battery_get_data(void) return; bat_t = (BATTERY_DATA_T *)malloc(len); + if(bat_t == NULL) + return; + FILE *fp; fp = fopen("/user/userdata/bat_data.bin", "rb"); diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp index 0bf26fa02..fe5e7a6de 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp @@ -507,7 +507,12 @@ namespace OHOS } else if(get_battery_percent_flag == 2) { - if(len >= 120) + if(len >= 180) + { + index = 120; + len = 180; + } + else if(len >= 120 && len < 180) index = 120; else return result; diff --git a/code/sdk/drivers/peripheral/cywee/src/main_dml.c b/code/sdk/drivers/peripheral/cywee/src/main_dml.c index 489c24dc4..f90ba62a5 100644 --- a/code/sdk/drivers/peripheral/cywee/src/main_dml.c +++ b/code/sdk/drivers/peripheral/cywee/src/main_dml.c @@ -34,6 +34,8 @@ #define SLEEP_STATUS_LENGTH 50 +#define WEAR_FILE_COUNT 50 + static gsenson_t gxyz_t; static uint8_t gsenson_falg=0; @@ -1914,9 +1916,7 @@ void cywee_custom_save_nap_data(void) 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; + uint16_t count = 0; fp = fopen(filename, "r+b"); if(!fp) fp = fopen(filename, "wb"); @@ -1924,24 +1924,24 @@ void wear_file_check(SLEEP_STAT_T newStruct, const char *filename) fseek(fp, 0, SEEK_END); count = ftell(fp) / sizeof(SLEEP_STAT_T); fseek(fp, 0, SEEK_SET); - // PRINT("battery data count %d \n",count); - if (count >= 50) + + if (count >= WEAR_FILE_COUNT) { - fread(data, sizeof(SLEEP_STAT_T), count, fp); + SLEEP_STAT_T data[WEAR_FILE_COUNT] = {0}; + + fseek(fp,(count - WEAR_FILE_COUNT + 1) * sizeof(SLEEP_STAT_T), SEEK_SET); + fread(data, sizeof(SLEEP_STAT_T), WEAR_FILE_COUNT - 1, fp); - for (int i = 0; i < count - 1; i++) + data[WEAR_FILE_COUNT - 1] = newStruct; + + if(count > WEAR_FILE_COUNT) { - // if (data[i].tm < data[oldest].tm) - // { - // oldest = i; - // } - data[i] = data[i + 1]; + fp = freopen(filename, "wb", fp); } - data[count - 1] = newStruct; - fseek(fp, 0, SEEK_SET); - fwrite(data, sizeof(SLEEP_STAT_T), count, fp); + + fwrite(data, sizeof(SLEEP_STAT_T), WEAR_FILE_COUNT, fp); } else { -- 2.17.1