去掉电量变化阈值与开机读历史电量,优化debug界面电池信息与佩戴信息显示 1497/1 1497/head
authorpenglei <1624952103@qq.com>
Fri, 6 Dec 2024 01:20:19 +0000 (09:20 +0800)
committerpenglei <1624952103@qq.com>
Fri, 6 Dec 2024 01:20:19 +0000 (09:20 +0800)
code/sdk/application/battery/service/battery_driver.cpp
code/sdk/application/battery/service/battery_service.cpp
code/sdk/drivers/peripheral/cywee/src/main_dml.c

index d2cfbbd..a6a4869 100644 (file)
@@ -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)
index 14a54aa..0e19fd5 100644 (file)
@@ -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};
 
index 50899c1..489c24d 100644 (file)
@@ -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);