Merge pull request #1105 from Amanieu/fix_power_calc

Fix power calculation when battery units are in μA instead of μW
This commit is contained in:
Alex 2021-07-25 15:07:27 +02:00 committed by GitHub
commit bad72de960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 17 deletions

View File

@ -162,25 +162,36 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
uint32_t energy_full_design; uint32_t energy_full_design;
std::string _status; std::string _status;
std::ifstream(bat / "status") >> _status; std::ifstream(bat / "status") >> _status;
auto rate_path = fs::exists(bat / "current_now") ? "current_now" : "power_now";
std::ifstream(bat / rate_path) >> power_now;
std::string now_path; // Some battery will report current and charge in μA/μAh.
if (fs::exists(bat / "charge_now")) { // Scale these by the voltage to get μW/μWh.
now_path = "charge_now"; if (fs::exists(bat / "current_now")) {
} else if (fs::exists(bat / "energy_now")) { uint32_t voltage_now;
now_path = "energy_now"; uint32_t current_now;
} else { uint32_t charge_now;
now_path = "capacity"; uint32_t charge_full;
uint32_t charge_full_design;
std::ifstream(bat / "voltage_now") >> voltage_now;
std::ifstream(bat / "current_now") >> current_now;
std::ifstream(bat / "charge_full") >> charge_full;
std::ifstream(bat / "charge_full_design") >> charge_full_design;
if (fs::exists(bat / "charge_now"))
std::ifstream(bat / "charge_now") >> charge_now;
else {
// charge_now is missing on some systems, estimate using capacity.
uint32_t capacity;
std::ifstream(bat / "capacity") >> capacity;
charge_now = (capacity * charge_full) / 100;
} }
std::ifstream(bat / now_path) >> energy_now; power_now = ((uint64_t)current_now * (uint64_t)voltage_now) / 1000000;
auto full_path = fs::exists(bat / "charge_full") ? "charge_full" : "energy_full"; energy_now = ((uint64_t)charge_now * (uint64_t)voltage_now) / 1000000;
std::ifstream(bat / full_path) >> energy_full; energy_full = ((uint64_t)charge_full * (uint64_t)voltage_now) / 1000000;
auto full_design_path = fs::exists(bat / "charge_full_design") ? "charge_full_design" : "energy_full_design"; energy_full_design = ((uint64_t)charge_full_design * (uint64_t)voltage_now) / 1000000;
std::ifstream(bat / full_design_path) >> energy_full_design; } else {
std::ifstream(bat / "power_now") >> power_now;
if (now_path == "capacity") { std::ifstream(bat / "energy_now") >> energy_now;
energy_now = energy_now * energy_full / 100; std::ifstream(bat / "energy_full") >> energy_full;
std::ifstream(bat / "energy_full_design") >> energy_full_design;
} }
// Show the "smallest" status among all batteries // Show the "smallest" status among all batteries