优化debug界面佩戴信息存储 1489/3 1489/head
authorpenglei <1624952103@qq.com>
Tue, 3 Dec 2024 08:54:02 +0000 (16:54 +0800)
committerpenglei <1624952103@qq.com>
Tue, 3 Dec 2024 08:54:02 +0000 (16:54 +0800)
code/sdk/drivers/peripheral/cywee/src/main_dml.c

index 39895b1..50899c1 100644 (file)
@@ -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");
 }