Browse Source

添加电池电量映射

wanghechen 8 tháng trước cách đây
mục cha
commit
ef4ea03c18
2 tập tin đã thay đổi với 120 bổ sung25 xóa
  1. 116 24
      components/Decection/Decection.c
  2. 4 1
      components/Decection/include/Decection.h

+ 116 - 24
components/Decection/Decection.c

@@ -15,7 +15,107 @@ const static char *TAG = "Decection";
 
 static int adc_raw[2][10];
 static int voltage[2][10];
-
+// 0度的电池电量映射
+batt_map batt_map_0Du[] = {
+    //
+    {2225, 100},
+    {2162, 99},
+    {2156, 98},
+    {2151, 97},
+    {2146, 95},
+    {2142, 94},
+    {2137, 93},
+    {2131, 92},
+    {2125, 91},
+    //
+    {2120, 90},
+    {2113, 89},
+    {2107, 87},
+    {2101, 86},
+    {2094, 85},
+    {2088, 84},
+    {2081, 83},
+    {2075, 82},
+    //
+    {2068, 80},
+    {2061, 79},
+    {2054, 78},
+    {2047, 77},
+    {2040, 76},
+    {2033, 75},
+    {2027, 74},
+    {2020, 72},
+    {2013, 71},
+    //
+    {2006, 70},
+    {2000, 69},
+    {1993, 68},
+    {1987, 67},
+    {1981, 66},
+    {1974, 64},
+    {1967, 63},
+    {1961, 62},
+    {1955, 61},
+    //
+    {1949, 60},
+    {1943, 59},
+    {1937, 58},
+    {1931, 56},
+    {1926, 55},
+    {1920, 54},
+    {1915, 53},
+    {1910, 52},
+    {1906, 51},
+    //
+    {1902, 50},
+    {1899, 48},
+    {1894, 47},
+    {1891, 46},
+    {1887, 45},
+    {1884, 44},
+    {1880, 43},
+    {1878, 41},
+    //
+    {1875, 40},
+    {1872, 39},
+    {1869, 38},
+    {1867, 37},
+    {1864, 36},
+    {1861, 35},
+    {1859, 33},
+    {1857, 32},
+    {1854, 31},
+    //
+    {1852, 30},
+    {1850, 29},
+    {1849, 28},
+    {1847, 27},
+    {1845, 25},
+    {1843, 24},
+    {1841, 23},
+    {1838, 22},
+    {1835, 21},
+    //
+    {1832, 20},
+    {1828, 19},
+    {1823, 17},
+    {1818, 16},
+    {1813, 15},
+    {1807, 14},
+    {1801, 13},
+    {1794, 12},
+    //
+    {1789, 10},
+    {1783, 9},
+    {1778, 8},
+    {1771, 7},
+    {1762, 6},
+    {1756, 5},
+    {1691, 4},
+    {1634, 2},
+    {1541, 1},
+    {1400, 0},
+};
 #define USER_KEY_CHANNEL ADC_CHANNEL_7
 
 #define USER_ADC_CHANNEL ADC_CHANNEL_2
@@ -32,6 +132,19 @@ adc_cali_handle_t adc2_cali_handle = NULL;
 //         adc_calibration_deinit(adc2_cali_handle);
 //     }
 // }
+static int get_batt_pre(int vol)
+{
+    for (int i = 0; i <= 100; i++)
+    {
+        if (vol >= batt_map_0Du[i].vol)
+        {
+            ESP_LOGI(TAG, "vol = %d ,pre = %d", vol, batt_map_0Du[i].pre);
+            return batt_map_0Du[i].pre;
+        }
+    }
+    ESP_LOGE(TAG, "ERR : get_batt_pre vol = %d",vol);
+    return 0;
+}
 int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle)
 {
     bool do_calibration;
@@ -56,29 +169,8 @@ int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle)
         ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, voltage[0][0]);
         adc_calibration_deinit(adc2_cali_handle);
     }
-    int batt_percent;
-
-    // 电量范围映射
-    float result_voltage = (float)(voltage[0][0]) / 1000.0 /*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
-    float battery_percentage = ((result_voltage - 1.8) / (2.2 - 1.8)) * 100.0;
-    // 限制电量范围在0%到100%之间
-    if (battery_percentage <= 0)
-    {
-        battery_percentage = 0;
-    }
-    else if (battery_percentage >= 100)
-    {
-        battery_percentage = 100;
-    }
-
-    batt_percent = (int)battery_percentage;
-
-#if 0
-        ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]);
-#else
-     printf("voltage[0][0] = [%d],adc value %d\r\n",voltage[0][0],(int)battery_percentage);
-#endif
-    return batt_percent;
+    
+    return get_batt_pre(voltage[0][0]);
 }
 
 int adc_read_left_key_pin(adc_oneshot_unit_handle_t adc_handle)

+ 4 - 1
components/Decection/include/Decection.h

@@ -4,7 +4,10 @@
 #include "esp_adc/adc_oneshot.h"
 #include "esp_adc/adc_cali.h"
 #include "esp_adc/adc_cali_scheme.h"
-
+typedef struct batt_map{
+    int vol;//电压
+    int pre;//百分比
+}batt_map;
 
 int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle);
 int read_battery_voltage();