From e63a5bd472774d7327a44b4994ce1682a3d8364a Mon Sep 17 00:00:00 2001 From: Alessandro Toia Date: Mon, 26 Apr 2021 16:49:50 -0700 Subject: [PATCH] Iterate over n batteries to find percent --- src/battery.cpp | 107 +++++++++++++++++++++--------------------------- src/battery.h | 2 +- 2 files changed, 47 insertions(+), 62 deletions(-) diff --git a/src/battery.cpp b/src/battery.cpp index 0b275fe7..4aee444d 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -25,88 +25,73 @@ void BatteryStats::update() { if (numBattery() > 0) { if (numBattery() == 1) { current_watt = getPower(0); - current_percent = getPercent(0) * 100; + current_percent = getPercent(); } else if (numBattery() == 2) { float bat1_power = getPower(0); float bat2_power = getPower(1); - getPercent(0); - getPercent(1); - //bat_percent[][] = [bat1 or 2] [power now or power full] - - float energy=bat_percent[0][0] + bat_percent[1][0]; - float energy_full=bat_percent[0][1] + bat_percent[1][1]; current_watt = (bat1_power + bat2_power); - current_percent = (energy / energy_full) * 100; + current_percent = getPercent(); } } } -float BatteryStats::getPercent(int num) +float BatteryStats::getPercent() { - string syspath = battPath[num]; - string charge_now = syspath + "/charge_now"; - string charge_full = syspath + "/charge_full"; - string energy_now = syspath + "/energy_now"; - string energy_full = syspath + "/energy_full"; - string capacity = syspath + "/capacity"; - - if (fs::exists(charge_now)) { - float charge_n = 0; - float charge_f = 0; - - std::ifstream input(charge_now); - std::string line; - if(std::getline(input, line)) { - charge_n = (stof(line) / 1000000); - bat_percent[num][0]=charge_n; - } + int batt_count = numBattery(); + float charge_n = 0; + float charge_f = 0; + for(int i =0; i < batt_count; i++) { + string syspath = battPath[i]; + string charge_now = syspath + "/charge_now"; + string charge_full = syspath + "/charge_full"; + string energy_now = syspath + "/energy_now"; + string energy_full = syspath + "/energy_full"; + string capacity = syspath + "/capacity"; + + if (fs::exists(charge_now)) { + + std::ifstream input(charge_now); + std::string line; + if(std::getline(input, line)) { + charge_n += (stof(line) / 1000000); + } - std::ifstream input2(charge_full); - if(std::getline(input2, line)) { - charge_f = (stof(line) / 1000000); - bat_percent[num][1]=charge_f; - } + std::ifstream input2(charge_full); + if(std::getline(input2, line)) { + charge_f += (stof(line) / 1000000); + } - return (charge_n / charge_f); - } + } - else if (fs::exists(energy_now)) { - float energy_n = 0; - float energy_f = 0; + else if (fs::exists(energy_now)) { + std::ifstream input(energy_now); + std::string line; + if(std::getline(input, line)) { + charge_n += (stof(line) / 1000000); + } - std::ifstream input(energy_now); - std::string line; - if(std::getline(input, line)) { - energy_n = (stof(line) / 1000000); - bat_percent[num][0]=energy_n; - } + std::ifstream input2(energy_full); + if(std::getline(input2, line)) { + charge_f += (stof(line) / 1000000); + } - std::ifstream input2(energy_full); - if(std::getline(input2, line)) { - energy_f = (stof(line) / 1000000); - bat_percent[num][1]=energy_f; } - return (energy_n / energy_f); - - } + else { + // using /sys/class/power_supply/BAT*/capacity + // No way to get an accurate reading just average the percents if mutiple batteries + std::ifstream input(capacity); + std::string line; + if(std::getline(input, line)) { + charge_n += stof(line) / 100; + charge_f =1; + } - else { - float percent=0; - // using /sys/class/power_supply/BAT*/capacity - // No way to get an accurate reading just average the percents if mutiple batteries - std::ifstream input(capacity); - std::string line; - if(std::getline(input, line)) { - percent = stof(line) / 100; - bat_percent[num][0]=percent; - bat_percent[num][1]=1.0; } - - return percent; } + return (charge_n / charge_f) * 100; } float BatteryStats::getPower(int batt_num) { diff --git a/src/battery.h b/src/battery.h index a15a81dc..d89a20c1 100644 --- a/src/battery.h +++ b/src/battery.h @@ -12,7 +12,7 @@ class BatteryStats{ void findFiles(); void update(); float getPower(int num); - float getPercent(int num); + float getPercent(); bool isCharging(); bool fullCharge(); std::vector battPath;