From 8849c3f6f2a5112e35561c1298b53f2ab9213b45 Mon Sep 17 00:00:00 2001 From: penglei <1624952103@qq.com> Date: Tue, 3 Dec 2024 16:54:02 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96debug=E7=95=8C=E9=9D=A2?= =?utf8?q?=E4=BD=A9=E6=88=B4=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 --- .../drivers/peripheral/cywee/src/main_dml.c | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/code/sdk/drivers/peripheral/cywee/src/main_dml.c b/code/sdk/drivers/peripheral/cywee/src/main_dml.c index 39895b1b5..50899c18a 100644 --- a/code/sdk/drivers/peripheral/cywee/src/main_dml.c +++ b/code/sdk/drivers/peripheral/cywee/src/main_dml.c @@ -1911,18 +1911,60 @@ void cywee_custom_save_nap_data(void) uapi_dev_runtime_set_sleep_storage_status(0); } -void cywee_custom_save_wear_data(void) +void wear_file_check(SLEEP_STAT_T newStruct, const char *filename) { - int len = uapi_get_file_size("user/userdata/wear.bin"); - if(len > sizeof(SLEEP_STAT_T) * 50) + FILE *fp; + SLEEP_STAT_T data[50]; + uint8_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(SLEEP_STAT_T); + fseek(fp, 0, SEEK_SET); + // PRINT("battery data count %d \n",count); + if (count >= 50) + { + fread(data, sizeof(SLEEP_STAT_T), count, fp); + + for (int i = 1; i < count; i++) + { + if (data[i].timestamp < data[oldest].timestamp) + { + oldest = i; + } + } + + data[oldest] = newStruct; + + fseek(fp, 0, SEEK_SET); + fwrite(data, sizeof(SLEEP_STAT_T), count, fp); + } + else { - uapi_delete_files("user/userdata/wear.bin"); + fseek(fp, 0, SEEK_END); + fwrite(&newStruct, sizeof(SLEEP_STAT_T), 1, fp); } + + fclose(fp); +} + + +void cywee_custom_save_wear_data(void) +{ + // int len = uapi_get_file_size("user/userdata/wear.bin"); + // if(len > sizeof(SLEEP_STAT_T) * 50) + // { + // uapi_delete_files("user/userdata/wear.bin"); + // } SLEEP_STAT_T data; uint32_t tm = 0; uapi_get_timestampe_ss(&tm); data.timestamp = tm; data.stat = wear_status; // CWM_OS_dbgPrintf("data.timestamp : %d , data.stat : %d\n", data.timestamp,data.stat); - uapi_save_file_data("user/userdata/wear.bin",&data,sizeof(SLEEP_STAT_T),"ab"); + // uapi_save_file_data("user/userdata/wear.bin",&data,sizeof(SLEEP_STAT_T),"ab"); + wear_file_check(data,"user/userdata/wear.bin"); } -- 2.17.1