From 37d87be3c115ab9556be9ab51f57b96c1ce2bc5e Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Mon, 14 Mar 2022 17:53:19 +0300 Subject: [PATCH] Add supporting of the gamepads batteries --- src/modules/battery.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 7ea9b55..c2dc389 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -154,6 +154,7 @@ const std::tuple waybar::modules::Battery::g uint32_t total_energy = 0; // μWh uint32_t total_energy_full = 0; uint32_t total_energy_full_design = 0; + uint32_t total_capacity{0}; std::string status = "Unknown"; for (auto const& item : batteries_) { auto bat = item.first; @@ -161,6 +162,7 @@ const std::tuple waybar::modules::Battery::g uint32_t energy_full; uint32_t energy_now; uint32_t energy_full_design; + uint32_t capacity{0}; std::string _status; std::getline(std::ifstream(bat / "status"), _status); @@ -188,11 +190,18 @@ const std::tuple waybar::modules::Battery::g energy_now = ((uint64_t)charge_now * (uint64_t)voltage_now) / 1000000; energy_full = ((uint64_t)charge_full * (uint64_t)voltage_now) / 1000000; energy_full_design = ((uint64_t)charge_full_design * (uint64_t)voltage_now) / 1000000; + } // Gamepads such as PS Dualshock provide the only capacity + else if (fs::exists(bat / "energy_now") && fs::exists(bat / "energy_full")) { + std::ifstream(bat / "power_now") >> power_now; + std::ifstream(bat / "energy_now") >> energy_now; + std::ifstream(bat / "energy_full") >> energy_full; + std::ifstream(bat / "energy_full_design") >> energy_full_design; } else { - std::ifstream(bat / "power_now") >> power_now; - std::ifstream(bat / "energy_now") >> energy_now; - std::ifstream(bat / "energy_full") >> energy_full; - std::ifstream(bat / "energy_full_design") >> energy_full_design; + std::ifstream(bat / "capacity") >> capacity; + power_now = 0; + energy_now = 0; + energy_full = 0; + energy_full_design = 0; } // Show the "smallest" status among all batteries @@ -203,6 +212,7 @@ const std::tuple waybar::modules::Battery::g total_energy += energy_now; total_energy_full += energy_full; total_energy_full_design += energy_full_design; + total_capacity += capacity; } if (!adapter_.empty() && status == "Discharging") { bool online; @@ -222,7 +232,12 @@ const std::tuple waybar::modules::Battery::g time_remaining = 0.0f; } } - float capacity = ((float)total_energy * 100.0f / (float) total_energy_full); + float capacity{0.0f}; + if(total_energy_full > 0.0f) { + capacity = ((float)total_energy * 100.0f / (float) total_energy_full); + } else { + capacity = (float)total_capacity; + } // Handle design-capacity if (config_["design-capacity"].isBool() ? config_["design-capacity"].asBool() : false) { capacity = ((float)total_energy * 100.0f / (float) total_energy_full_design);