优化debug界面电池信息与佩戴信息存储 1504/1 1504/head
authorpenglei <1624952103@qq.com>
Sat, 7 Dec 2024 08:26:41 +0000 (16:26 +0800)
committerpenglei <1624952103@qq.com>
Sat, 7 Dec 2024 08:26:41 +0000 (16:26 +0800)
code/sdk/application/battery/service/battery_service.cpp
code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp
code/sdk/drivers/peripheral/cywee/src/main_dml.c

index 0e19fd5..9096afe 100644 (file)
@@ -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"); 
index 0bf26fa..fe5e7a6 100644 (file)
@@ -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;
index 489c24d..f90ba62 100644 (file)
@@ -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
     {