From 120c68e014af7f379749e826bcd39e0e88d8eb3c Mon Sep 17 00:00:00 2001 From: Tom Charnock Date: Fri, 2 Sep 2022 11:42:46 +0200 Subject: [PATCH 1/5] Updated logic in battery module --- src/modules/battery.cpp | 263 ++++++++++++++++++++++++++++------------ 1 file changed, 184 insertions(+), 79 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 60194f0..b47aee9 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -157,122 +157,227 @@ const std::tuple waybar::modules::Battery::g try { uint32_t total_power = 0; // μW + bool total_power_exists = false; uint32_t total_energy = 0; // μWh + bool total_energy_exists = false; uint32_t total_energy_full = 0; + bool total_energy_full_exists = false; uint32_t total_energy_full_design = 0; + bool total_energy_full_design_exists = false; uint32_t total_capacity{0}; + bool total_capacity_exists = false; + std::string status = "Unknown"; for (auto const& item : batteries_) { auto bat = item.first; - uint32_t power_now; - 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); // Some battery will report current and charge in μA/μAh. // Scale these by the voltage to get μW/μWh. - if (fs::exists(bat / "current_now") || fs::exists(bat / "current_avg")) { - uint32_t voltage_now; - uint32_t current_now; - uint32_t charge_now; - uint32_t charge_full; - uint32_t charge_full_design; - // Some batteries have only *_avg, not *_now - if (fs::exists(bat / "voltage_now")) - std::ifstream(bat / "voltage_now") >> voltage_now; - else - std::ifstream(bat / "voltage_avg") >> voltage_now; - if (fs::exists(bat / "current_now")) - std::ifstream(bat / "current_now") >> current_now; - else - std::ifstream(bat / "current_avg") >> 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; - } - power_now = ((uint64_t)current_now * (uint64_t)voltage_now) / 1000000; - 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 { + + uint32_t capacity; + bool capacity_exists; + if (fs::exists(bat / "capacity")) { + capacity_exists = true; std::ifstream(bat / "capacity") >> capacity; + } else { + capacity_exists = false; + capacity = 0; + } + + uint32_t current_now; + bool current_now_exists; + if (fs::exists(bat / "current_now")) { + current_now_exists = true; + std::ifstream(bat / "current_now") >> current_now; + } else if (fs::exists(bat / "current_avg")) { + current_now_exists = true; + std::ifstream(bat / "current_avg") >> current_now; + } else { + current_now_exists = false; + current_now = 0; + } + + uint32_t voltage_now; + bool voltage_now_exists; + if (fs::exists(bat / "voltage_now")) { + voltage_now_exists = true; + std::ifstream(bat / "voltage_now") >> voltage_now; + } else if (fs::exists(bat / "voltage_avg")) { + voltage_now_exists = true; + std::ifstream(bat / "voltage_avg") >> voltage_now; + } else { + voltage_now_exists = false; + voltage_now = 0; + } + + uint32_t charge_full; + bool charge_full_exists; + if (fs::exists(bat / "charge_full")) { + charge_full_exists = true; + std::ifstream(bat / "charge_full") >> charge_full; + } else { + charge_full_exists = false; + charge_full = 0; + } + + uint32_t charge_full_design; + bool charge_full_design_exists; + if (fs::exists(bat / "charge_full_design")) { + charge_full_design_exists = true; + std::ifstream(bat / "charge_full_design") >> charge_full_design; + } else { + charge_full_design_exists = false; + charge_full_design = 0; + } + + uint32_t charge_now; + bool charge_now_exists; + if (fs::exists(bat / "charge_now")) { + charge_now_exists = true; + std::ifstream(bat / "charge_now") >> charge_now; + } else if (capacity_exists && charge_full_exists) { + // charge_now is missing on some systems, estimate using capacity and charge_full. + charge_now_exists = true; // this could be debatably set to false (same for all properties below) + charge_now = (capacity * charge_full) / 100; + } else { + charge_now_exists = false; + charge_now = 0; + } + + uint32_t power_now; + bool power_now_exists; + if (fs::exists(bat / "power_now")) { + power_now_exists = true; + std::ifstream(bat / "power_now") >> power_now; + } else if (current_now_exists && voltage_now_exists) { + power_now_exists = true; + power_now = ((uint64_t)current_now * (uint64_t)voltage_now) / 1000000; + } else { + power_now_exists = false; power_now = 0; + } + + uint32_t energy_now; + bool energy_now_exists; + if (fs::exists(bat / "energy_now")) { + energy_now_exists = true; + std::ifstream(bat / "energy_now") >> energy_now; + } else if (charge_now_exists && voltage_now_exists) { + energy_now_exists = true; + energy_now = ((uint64_t)charge_now * (uint64_t)voltage_now) / 1000000; + } else { + energy_now_exists = false; energy_now = 0; + } + + uint32_t energy_full; + bool energy_full_exists; + if (fs::exists(bat / "energy_full")) { + energy_full_exists = true; + std::ifstream(bat / "energy_full") >> energy_full; + } else if (charge_full_exists && voltage_now_exists) { + energy_full_exists = true; + energy_full = ((uint64_t)charge_full * (uint64_t)voltage_now) / 1000000; + } else { + energy_full_exists = false; energy_full = 0; + } + + uint32_t energy_full_design; + bool energy_full_design_exists; + if (fs::exists(bat / "energy_full_design")) { + energy_full_design_exists = true; + std::ifstream(bat / "energy_full_design") >> energy_full_design; + } else if (charge_full_design_exists && voltage_now_exists) { + energy_full_design_exists = true; + energy_full_design = ((uint64_t)charge_full_design * (uint64_t)voltage_now) / 1000000; + } else { + energy_full_design_exists = false; energy_full_design = 0; } // Show the "smallest" status among all batteries - if (status_gt(status, _status)) { + if (status_gt(status, _status)) status = _status; + + if (power_now_exists) { + total_power_exists = true; + total_power += power_now; + } + if (energy_now_exists) { + total_energy_exists = true; + total_energy += energy_now; + } + if (energy_full_exists) { + total_energy_full_exists = true; + total_energy_full += energy_full; + } + if (energy_full_design_exists) { + total_energy_full_design_exists = true; + total_energy_full_design += energy_full_design; + } + if (capacity_exists) { + total_capacity_exists = true; + total_capacity += capacity; } - total_power += power_now; - 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; - std::ifstream(adapter_ / "online") >> online; - if (online) { + if (fs::exists(adapter_ / "online")) + std::ifstream(adapter_ / "online") >> online; + else + online = false; + if (online) status = "Plugged"; - } } - float time_remaining = 0; - if (status == "Discharging" && total_power != 0) { - time_remaining = (float)total_energy / total_power; - } else if (status == "Charging" && total_power != 0) { - time_remaining = -(float)(total_energy_full - total_energy) / total_power; - if (time_remaining > 0.0f) { - // If we've turned positive it means the battery is past 100% and so - // just report that as no time remaining + + float time_remaining{0.0f}; + if (status == "Discharging" && total_power_exists && total_energy_exists) { + if (total_power != 0) + time_remaining = (float)total_energy / total_power; + } else if (status == "Charging" && total_energy_exists && total_energy_full_exists && total_power_exists) { + if (total_power != 0) + time_remaining = -(float)(total_energy_full - total_energy) / total_power; + // If we've turned positive it means the battery is past 100% and so just report that as no time remaining + if (time_remaining > 0.0f) time_remaining = 0.0f; + } + + float calculated_capacity{0.0f}; + if (total_capacity_exists) { + if (total_capacity > 0.0f) + calculated_capacity = (float)total_capacity; + else if (total_energy_full_exists && total_energy_exists) { + if (total_energy_full > 0.0f) + calculated_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); + if ((config_["design-capacity"].isBool() ? config_["design-capacity"].asBool() : false) && total_energy_exists && total_energy_full_design_exists) { + if (total_energy_full_design > 0.0f) + calculated_capacity = ((float)total_energy * 100.0f / (float)total_energy_full_design); } + // Handle full-at if (config_["full-at"].isUInt()) { auto full_at = config_["full-at"].asUInt(); - if (full_at < 100) { - capacity = 100.f * capacity / full_at; - } + if (full_at < 100) + calculated_capacity = 100.f * calculated_capacity / full_at; } - if (capacity > 100.f) { - // This can happen when the battery is calibrating and goes above 100% - // Handle it gracefully by clamping at 100% - capacity = 100.f; - } - uint8_t cap = round(capacity); - if (cap == 100 && status == "Charging") { - // If we've reached 100% just mark as full as some batteries can stay - // stuck reporting they're still charging but not yet done + + // Handle it gracefully by clamping at 100% + // This can happen when the battery is calibrating and goes above 100% + if (calculated_capacity > 100.f) + calculated_capacity = 100.f; + + uint8_t cap = round(calculated_capacity); + // If we've reached 100% just mark as full as some batteries can stay stuck reporting they're still charging but not yet done + if (cap == 100 && status == "Charging") status = "Full"; - } return {cap, time_remaining, status, total_power / 1e6}; } catch (const std::exception& e) { From 0d948536135b74393d7b09d367ad00be7d8a4c3c Mon Sep 17 00:00:00 2001 From: Tom Charnock Date: Fri, 2 Sep 2022 15:37:23 +0200 Subject: [PATCH 2/5] Added alternative variable calculations --- src/modules/battery.cpp | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index b47aee9..7cbc7b5 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -246,6 +246,13 @@ const std::tuple waybar::modules::Battery::g charge_now = 0; } + if (!capacity_exists && charge_now_exists && charge_full_exists) { + if (charge_full != 0) { + capacity_exists = true; + capacity = charge_now * 100 / charge_full; + } + } + uint32_t power_now; bool power_now_exists; if (fs::exists(bat / "power_now")) { @@ -259,6 +266,20 @@ const std::tuple waybar::modules::Battery::g power_now = 0; } + if (!current_now_exists && power_now_exists && voltage_now_exists) { + if (voltage_now != 0){ + current_now_exists = true; + current_now = (uint64_t)power_now * 1000000 / (uint64_t)voltage_now; + } + } + + if (!voltage_now_exists && power_now_exists && current_now_exists) { + if (current_now != 0) { + voltage_now_exists = true; + voltage_now = (uint64_t)power_now * 1000000 / (uint64_t)current_now; + } + } + uint32_t energy_now; bool energy_now_exists; if (fs::exists(bat / "energy_now")) { @@ -272,6 +293,20 @@ const std::tuple waybar::modules::Battery::g energy_now = 0; } + if (!charge_now_exists && energy_now_exists && voltage_now_exists) { + if (voltage_now != 0){ + charge_now_exists = true; + charge_now = (uint64_t)energy_now * 1000000 / (uint64_t)voltage_now; + } + } + + if (!voltage_now_exists && energy_now_exists && charge_now_exists) { + if (charge_now != 0) { + voltage_now_exists = true; + voltage_now = (uint64_t)energy_now * 1000000 / (uint64_t)charge_now; + } + } + uint32_t energy_full; bool energy_full_exists; if (fs::exists(bat / "energy_full")) { @@ -285,6 +320,20 @@ const std::tuple waybar::modules::Battery::g energy_full = 0; } + if (!charge_full_exists && energy_full_exists && voltage_now_exists) { + if (voltage_now != 0){ + charge_full_exists = true; + charge_full = (uint64_t)energy_full * 1000000 / (uint64_t)voltage_now; + } + } + + if (!voltage_now_exists && energy_full_exists && charge_full_exists) { + if (charge_full != 0) { + voltage_now_exists = true; + voltage_now = (uint64_t)energy_full * 1000000 / (uint64_t)charge_full; + } + } + uint32_t energy_full_design; bool energy_full_design_exists; if (fs::exists(bat / "energy_full_design")) { @@ -298,6 +347,20 @@ const std::tuple waybar::modules::Battery::g energy_full_design = 0; } + if (!charge_full_design_exists && energy_full_design_exists && voltage_now_exists) { + if (voltage_now != 0){ + charge_full_design_exists = true; + charge_full_design = (uint64_t)energy_full_design * 1000000 / (uint64_t)voltage_now; + } + } + + if (!voltage_now_exists && energy_full_design_exists && charge_full_design_exists) { + if (charge_full_design != 0) { + voltage_now_exists = true; + voltage_now = (uint64_t)energy_full_design * 1000000 / (uint64_t)charge_full_design; + } + } + // Show the "smallest" status among all batteries if (status_gt(status, _status)) status = _status; From af2a3f8bda521853548d1e4774fd6df9fba8a498 Mon Sep 17 00:00:00 2001 From: Tom Charnock Date: Sat, 3 Sep 2022 16:06:13 +0200 Subject: [PATCH 3/5] Added alternative calculations for time remaining --- src/modules/battery.cpp | 315 +++++++++++++++++++++++----------------- 1 file changed, 178 insertions(+), 137 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 7cbc7b5..1cff9f9 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -164,7 +164,7 @@ const std::tuple waybar::modules::Battery::g bool total_energy_full_exists = false; uint32_t total_energy_full_design = 0; bool total_energy_full_design_exists = false; - uint32_t total_capacity{0}; + uint32_t total_capacity = 0; bool total_capacity_exists = false; std::string status = "Unknown"; @@ -176,191 +176,232 @@ const std::tuple waybar::modules::Battery::g // Some battery will report current and charge in μA/μAh. // Scale these by the voltage to get μW/μWh. - uint32_t capacity; - bool capacity_exists; + // uint32_t capacity (bool pass) { + // uint32_t c = 0; + // if (fs::exists(bat / "capacity")) + // c = std::ifstream(bat / "capacity"); + // else if (pass) + // c = charge_now(pass) / charge_full(pass); + // return c; + // } + + // uint32_t current_now (bool pass) { + // uint32_t c = 0; + // if (fs::exists(bat / "current_now")) + // std::ifstream(bat / "current_now") >> c; + // else if (fs::exists(bat / "current_avg")) + // std::ifstream(bat / "current_avg") >> c; + // else + // c = power_now(pass) / voltage_now(pass); + // return c; + // } + + // uint32_t voltage_now () { + // uint32_t v = 0; + // if (fs::exists(bat / "voltage_now")) + // std::ifstream(bat / "voltage_now") >> v; + // else if (fs::exists(bat / "voltage_avg")) + // std::ifstream(bat / "voltage_avg") >> v; + // else + + // } + + uint32_t capacity = 0; + bool capacity_exists = false; if (fs::exists(bat / "capacity")) { - capacity_exists = true; - std::ifstream(bat / "capacity") >> capacity; - } else { - capacity_exists = false; - capacity = 0; + capacity_exists = true; + std::ifstream(bat / "capacity") >> capacity; } - uint32_t current_now; - bool current_now_exists; + uint32_t current_now = 0; + bool current_now_exists = false; if (fs::exists(bat / "current_now")) { - current_now_exists = true; - std::ifstream(bat / "current_now") >> current_now; + current_now_exists = true; + std::ifstream(bat / "current_now") >> current_now; } else if (fs::exists(bat / "current_avg")) { - current_now_exists = true; - std::ifstream(bat / "current_avg") >> current_now; - } else { - current_now_exists = false; - current_now = 0; + current_now_exists = true; + std::ifstream(bat / "current_avg") >> current_now; } - uint32_t voltage_now; - bool voltage_now_exists; + uint32_t voltage_now = 0; + bool voltage_now_exists = false; if (fs::exists(bat / "voltage_now")) { - voltage_now_exists = true; - std::ifstream(bat / "voltage_now") >> voltage_now; + voltage_now_exists = true; + std::ifstream(bat / "voltage_now") >> voltage_now; } else if (fs::exists(bat / "voltage_avg")) { - voltage_now_exists = true; - std::ifstream(bat / "voltage_avg") >> voltage_now; - } else { - voltage_now_exists = false; - voltage_now = 0; + voltage_now_exists = true; + std::ifstream(bat / "voltage_avg") >> voltage_now; } - uint32_t charge_full; - bool charge_full_exists; + uint32_t charge_full = 0; + bool charge_full_exists = false; if (fs::exists(bat / "charge_full")) { charge_full_exists = true; std::ifstream(bat / "charge_full") >> charge_full; - } else { - charge_full_exists = false; - charge_full = 0; } - uint32_t charge_full_design; - bool charge_full_design_exists; + uint32_t charge_full_design = 0; + bool charge_full_design_exists = false; if (fs::exists(bat / "charge_full_design")) { charge_full_design_exists = true; std::ifstream(bat / "charge_full_design") >> charge_full_design; - } else { - charge_full_design_exists = false; - charge_full_design = 0; } - uint32_t charge_now; - bool charge_now_exists; + uint32_t charge_now = 0; + bool charge_now_exists = false; if (fs::exists(bat / "charge_now")) { charge_now_exists = true; std::ifstream(bat / "charge_now") >> charge_now; - } else if (capacity_exists && charge_full_exists) { - // charge_now is missing on some systems, estimate using capacity and charge_full. - charge_now_exists = true; // this could be debatably set to false (same for all properties below) - charge_now = (capacity * charge_full) / 100; - } else { - charge_now_exists = false; - charge_now = 0; } - if (!capacity_exists && charge_now_exists && charge_full_exists) { - if (charge_full != 0) { - capacity_exists = true; - capacity = charge_now * 100 / charge_full; - } - } - - uint32_t power_now; - bool power_now_exists; + uint32_t power_now = 0; + bool power_now_exists = false; if (fs::exists(bat / "power_now")) { power_now_exists = true; std::ifstream(bat / "power_now") >> power_now; - } else if (current_now_exists && voltage_now_exists) { - power_now_exists = true; - power_now = ((uint64_t)current_now * (uint64_t)voltage_now) / 1000000; - } else { - power_now_exists = false; - power_now = 0; - } + } - if (!current_now_exists && power_now_exists && voltage_now_exists) { - if (voltage_now != 0){ - current_now_exists = true; - current_now = (uint64_t)power_now * 1000000 / (uint64_t)voltage_now; - } - } - - if (!voltage_now_exists && power_now_exists && current_now_exists) { - if (current_now != 0) { - voltage_now_exists = true; - voltage_now = (uint64_t)power_now * 1000000 / (uint64_t)current_now; - } - } - - uint32_t energy_now; - bool energy_now_exists; + uint32_t energy_now = 0; + bool energy_now_exists = false; if (fs::exists(bat / "energy_now")) { energy_now_exists = true; std::ifstream(bat / "energy_now") >> energy_now; - } else if (charge_now_exists && voltage_now_exists) { - energy_now_exists = true; - energy_now = ((uint64_t)charge_now * (uint64_t)voltage_now) / 1000000; - } else { - energy_now_exists = false; - energy_now = 0; } - if (!charge_now_exists && energy_now_exists && voltage_now_exists) { - if (voltage_now != 0){ - charge_now_exists = true; - charge_now = (uint64_t)energy_now * 1000000 / (uint64_t)voltage_now; - } - } - - if (!voltage_now_exists && energy_now_exists && charge_now_exists) { - if (charge_now != 0) { - voltage_now_exists = true; - voltage_now = (uint64_t)energy_now * 1000000 / (uint64_t)charge_now; - } - } - - uint32_t energy_full; - bool energy_full_exists; + uint32_t energy_full = 0; + bool energy_full_exists = false; if (fs::exists(bat / "energy_full")) { energy_full_exists = true; std::ifstream(bat / "energy_full") >> energy_full; - } else if (charge_full_exists && voltage_now_exists) { - energy_full_exists = true; - energy_full = ((uint64_t)charge_full * (uint64_t)voltage_now) / 1000000; - } else { - energy_full_exists = false; - energy_full = 0; } - if (!charge_full_exists && energy_full_exists && voltage_now_exists) { - if (voltage_now != 0){ - charge_full_exists = true; - charge_full = (uint64_t)energy_full * 1000000 / (uint64_t)voltage_now; - } - } - - if (!voltage_now_exists && energy_full_exists && charge_full_exists) { - if (charge_full != 0) { - voltage_now_exists = true; - voltage_now = (uint64_t)energy_full * 1000000 / (uint64_t)charge_full; - } - } - - uint32_t energy_full_design; - bool energy_full_design_exists; + uint32_t energy_full_design = 0; + bool energy_full_design_exists = false; if (fs::exists(bat / "energy_full_design")) { energy_full_design_exists = true; std::ifstream(bat / "energy_full_design") >> energy_full_design; - } else if (charge_full_design_exists && voltage_now_exists) { - energy_full_design_exists = true; - energy_full_design = ((uint64_t)charge_full_design * (uint64_t)voltage_now) / 1000000; - } else { - energy_full_design_exists = false; - energy_full_design = 0; } - if (!charge_full_design_exists && energy_full_design_exists && voltage_now_exists) { - if (voltage_now != 0){ - charge_full_design_exists = true; - charge_full_design = (uint64_t)energy_full_design * 1000000 / (uint64_t)voltage_now; - } - } - - if (!voltage_now_exists && energy_full_design_exists && charge_full_design_exists) { - if (charge_full_design != 0) { + if (!voltage_now_exists) { + if (power_now_exists && current_now_exists && current_now != 0) { voltage_now_exists = true; - voltage_now = (uint64_t)energy_full_design * 1000000 / (uint64_t)charge_full_design; + voltage_now = 1000000 * power_now / current_now; + } else if (energy_full_design_exists && charge_full_design_exists && charge_full_design != 0) { + voltage_now_exists = true; + voltage_now = 1000000 * energy_full_design / charge_full_design; + } else if (energy_now_exists) { + if (charge_now_exists && charge_now != 0) { + voltage_now_exists = true; + voltage_now = 1000000 * energy_now / charge_now; + } else if (capacity_exists && charge_full_exists) { + charge_now_exists = true; + charge_now = charge_full * capacity / 100; + if (charge_full != 0 && capacity != 0) { + voltage_now_exists = true; + voltage_now = 1000000 * energy_now * 100 / charge_full / capacity; + } + } + } else if (energy_full_exists) { + if (charge_full_exists && charge_full != 0) { + voltage_now_exists = true; + voltage_now = 1000000 * energy_full / charge_full; + } else if (charge_now_exists && capacity_exists) { + if (capacity != 0) { + charge_full_exists = true; + charge_full = 100 * charge_now / capacity; + } + if (charge_now != 0) { + voltage_now_exists = true; + voltage_now = 10000 * energy_full * capacity / charge_now; + } + } } } + if (!capacity_exists) { + if (charge_now_exists && charge_full_exists && charge_full != 0) { + capacity_exists = true; + capacity = 100 * charge_now / charge_full; + } else if (energy_now_exists && energy_full_exists && energy_full != 0) { + capacity_exists = true; + capacity = 100 * energy_now / energy_full; + } else if (charge_now_exists && energy_full_exists && voltage_now_exists) { + if (!charge_full_exists && voltage_now != 0) { + charge_full_exists = true; + charge_full = 1000000 * energy_full / voltage_now; + } + if (energy_full != 0) { + capacity_exists = true; + capacity = charge_now * voltage_now / 10000 / energy_full; + } + } else if (charge_full_exists && energy_now_exists && voltage_now_exists) { + if (!charge_now_exists && voltage_now != 0) { + charge_now_exists = true; + charge_now = 1000000 * energy_now / voltage_now; + } + if (voltage_now != 0 && charge_full != 0) { + capacity_exists = true; + capacity = 100 * 1000000 * energy_now / voltage_now / charge_full; + } + } + } + + if (!energy_now_exists && voltage_now_exists) { + if (charge_now_exists) { + energy_now_exists = true; + energy_now = charge_now * voltage_now / 1000000; + } else if (capacity_exists && charge_full_exists) { + charge_now_exists = true; + charge_now = capacity * charge_full / 100; + energy_now_exists = true; + energy_now = voltage_now * capacity * charge_full / 1000000 / 100; + } else if (capacity_exists && energy_full) { + if (voltage_now != 0) { + charge_full_exists = true; + charge_full = 1000000 * energy_full / voltage_now; + charge_now_exists = true; + charge_now = capacity * 10000 * energy_full / voltage_now; + } + energy_now_exists = true; + energy_now = capacity * energy_full / 100; + } + } + + if (!energy_full_exists && voltage_now_exists) { + if (charge_full_exists) { + energy_full_exists = true; + energy_full = charge_full * voltage_now / 1000000; + } else if (charge_now_exists && capacity_exists && capacity != 0) { + charge_full_exists = true; + charge_full = 100 * charge_now / capacity; + energy_full_exists = true; + energy_full = charge_now * voltage_now / capacity / 10000; + } else if (capacity_exists && energy_now) { + if (voltage_now != 0) { + charge_now_exists = true; + charge_now = 1000000 * energy_now / voltage_now; + } + if (capacity != 0) { + energy_full_exists = true; + energy_full = 100 * energy_now / capacity; + if (voltage_now != 0) { + charge_full_exists = true; + charge_full = 100 * 1000000 * energy_now / voltage_now / capacity; + } + } + } + } + + if (!power_now_exists && voltage_now_exists && charge_now_exists) { + power_now_exists = true; + power_now = voltage_now * current_now / 1000000; + } + + if (!energy_full_design_exists && voltage_now_exists && charge_full_design_exists) { + energy_full_design_exists = true; + energy_full_design = voltage_now * charge_full_design / 1000000; + } + // Show the "smallest" status among all batteries if (status_gt(status, _status)) status = _status; From 5647146ac0920df6ffc0f9e189bdc687b50d9ad9 Mon Sep 17 00:00:00 2001 From: Tom Charnock Date: Sat, 3 Sep 2022 17:52:11 +0200 Subject: [PATCH 4/5] Added Discharging clause and corrected typo --- src/modules/battery.cpp | 47 ++++++++--------------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 1cff9f9..35d41ed 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -108,7 +108,7 @@ void waybar::modules::Battery::refreshBatteries() { } auto adap_defined = config_["adapter"].isString(); if (((adap_defined && dir_name == config_["adapter"].asString()) || !adap_defined) && - fs::exists(node.path() / "online")) { + (fs::exists(node.path() / "online") || fs::exists(node.path() / "status"))) { adapter_ = node.path(); } } @@ -176,36 +176,6 @@ const std::tuple waybar::modules::Battery::g // Some battery will report current and charge in μA/μAh. // Scale these by the voltage to get μW/μWh. - // uint32_t capacity (bool pass) { - // uint32_t c = 0; - // if (fs::exists(bat / "capacity")) - // c = std::ifstream(bat / "capacity"); - // else if (pass) - // c = charge_now(pass) / charge_full(pass); - // return c; - // } - - // uint32_t current_now (bool pass) { - // uint32_t c = 0; - // if (fs::exists(bat / "current_now")) - // std::ifstream(bat / "current_now") >> c; - // else if (fs::exists(bat / "current_avg")) - // std::ifstream(bat / "current_avg") >> c; - // else - // c = power_now(pass) / voltage_now(pass); - // return c; - // } - - // uint32_t voltage_now () { - // uint32_t v = 0; - // if (fs::exists(bat / "voltage_now")) - // std::ifstream(bat / "voltage_now") >> v; - // else if (fs::exists(bat / "voltage_avg")) - // std::ifstream(bat / "voltage_avg") >> v; - // else - - // } - uint32_t capacity = 0; bool capacity_exists = false; if (fs::exists(bat / "capacity")) { @@ -392,7 +362,7 @@ const std::tuple waybar::modules::Battery::g } } - if (!power_now_exists && voltage_now_exists && charge_now_exists) { + if (!power_now_exists && voltage_now_exists && current_now_exists) { power_now_exists = true; power_now = voltage_now * current_now / 1000000; } @@ -430,11 +400,10 @@ const std::tuple waybar::modules::Battery::g if (!adapter_.empty() && status == "Discharging") { bool online; - if (fs::exists(adapter_ / "online")) - std::ifstream(adapter_ / "online") >> online; - else - online = false; - if (online) + std::string current_status; + std::ifstream(adapter_ / "online") >> online; + std::getline(std::ifstream(adapter_ / "status"), current_status); + if (online && current_status != "Discharging") status = "Plugged"; } @@ -493,11 +462,13 @@ const std::tuple waybar::modules::Battery::g const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const { if (!adapter_.empty()) { bool online; + std::string status; std::ifstream(adapter_ / "online") >> online; + std::getline(std::ifstream(adapter_ / "status"), status); if (capacity == 100) { return "Full"; } - if (online) { + if (online && status != "Discharging") { return "Plugged"; } return "Discharging"; From 912d7f85880be72628ccad07df329c729043246a Mon Sep 17 00:00:00 2001 From: Tom Charnock Date: Sat, 3 Sep 2022 18:08:26 +0200 Subject: [PATCH 5/5] Making calculations uint64_t --- src/modules/battery.cpp | 56 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 35d41ed..d46c051 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -255,34 +255,34 @@ const std::tuple waybar::modules::Battery::g if (!voltage_now_exists) { if (power_now_exists && current_now_exists && current_now != 0) { voltage_now_exists = true; - voltage_now = 1000000 * power_now / current_now; + voltage_now = 1000000 * (uint64_t)power_now / (uint64_t)current_now; } else if (energy_full_design_exists && charge_full_design_exists && charge_full_design != 0) { voltage_now_exists = true; - voltage_now = 1000000 * energy_full_design / charge_full_design; + voltage_now = 1000000 * (uint64_t)energy_full_design / (uint64_t)charge_full_design; } else if (energy_now_exists) { if (charge_now_exists && charge_now != 0) { voltage_now_exists = true; - voltage_now = 1000000 * energy_now / charge_now; + voltage_now = 1000000 * (uint64_t)energy_now / (uint64_t)charge_now; } else if (capacity_exists && charge_full_exists) { charge_now_exists = true; - charge_now = charge_full * capacity / 100; + charge_now = (uint64_t)charge_full * (uint64_t)capacity / 100; if (charge_full != 0 && capacity != 0) { voltage_now_exists = true; - voltage_now = 1000000 * energy_now * 100 / charge_full / capacity; + voltage_now = 1000000 * (uint64_t)energy_now * 100 / (uint64_t)charge_full / (uint64_t)capacity; } } } else if (energy_full_exists) { if (charge_full_exists && charge_full != 0) { voltage_now_exists = true; - voltage_now = 1000000 * energy_full / charge_full; + voltage_now = 1000000 * (uint64_t)energy_full / (uint64_t)charge_full; } else if (charge_now_exists && capacity_exists) { if (capacity != 0) { charge_full_exists = true; - charge_full = 100 * charge_now / capacity; + charge_full = 100 * (uint64_t)charge_now / (uint64_t)capacity; } if (charge_now != 0) { voltage_now_exists = true; - voltage_now = 10000 * energy_full * capacity / charge_now; + voltage_now = 10000 * (uint64_t)energy_full * (uint64_t)capacity / (uint64_t)charge_now; } } } @@ -291,27 +291,27 @@ const std::tuple waybar::modules::Battery::g if (!capacity_exists) { if (charge_now_exists && charge_full_exists && charge_full != 0) { capacity_exists = true; - capacity = 100 * charge_now / charge_full; + capacity = 100 * (uint64_t)charge_now / (uint64_t)charge_full; } else if (energy_now_exists && energy_full_exists && energy_full != 0) { capacity_exists = true; - capacity = 100 * energy_now / energy_full; + capacity = 100 * (uint64_t)energy_now / (uint64_t)energy_full; } else if (charge_now_exists && energy_full_exists && voltage_now_exists) { if (!charge_full_exists && voltage_now != 0) { charge_full_exists = true; - charge_full = 1000000 * energy_full / voltage_now; + charge_full = 1000000 * (uint64_t)energy_full / (uint64_t)voltage_now; } if (energy_full != 0) { capacity_exists = true; - capacity = charge_now * voltage_now / 10000 / energy_full; + capacity = (uint64_t)charge_now * (uint64_t)voltage_now / 10000 / (uint64_t)energy_full; } } else if (charge_full_exists && energy_now_exists && voltage_now_exists) { if (!charge_now_exists && voltage_now != 0) { charge_now_exists = true; - charge_now = 1000000 * energy_now / voltage_now; + charge_now = 1000000 * (uint64_t)energy_now / (uint64_t)voltage_now; } if (voltage_now != 0 && charge_full != 0) { capacity_exists = true; - capacity = 100 * 1000000 * energy_now / voltage_now / charge_full; + capacity = 100 * 1000000 * (uint64_t)energy_now / (uint64_t)voltage_now / (uint64_t)charge_full; } } } @@ -319,44 +319,44 @@ const std::tuple waybar::modules::Battery::g if (!energy_now_exists && voltage_now_exists) { if (charge_now_exists) { energy_now_exists = true; - energy_now = charge_now * voltage_now / 1000000; + energy_now = (uint64_t)charge_now * (uint64_t)voltage_now / 1000000; } else if (capacity_exists && charge_full_exists) { charge_now_exists = true; - charge_now = capacity * charge_full / 100; + charge_now = (uint64_t)capacity * (uint64_t)charge_full / 100; energy_now_exists = true; - energy_now = voltage_now * capacity * charge_full / 1000000 / 100; + energy_now = (uint64_t)voltage_now * (uint64_t)capacity * (uint64_t)charge_full / 1000000 / 100; } else if (capacity_exists && energy_full) { if (voltage_now != 0) { charge_full_exists = true; - charge_full = 1000000 * energy_full / voltage_now; + charge_full = 1000000 * (uint64_t)energy_full / (uint64_t)voltage_now; charge_now_exists = true; - charge_now = capacity * 10000 * energy_full / voltage_now; + charge_now = (uint64_t)capacity * 10000 * (uint64_t)energy_full / (uint64_t)voltage_now; } energy_now_exists = true; - energy_now = capacity * energy_full / 100; + energy_now = (uint64_t)capacity * (uint64_t)energy_full / 100; } } if (!energy_full_exists && voltage_now_exists) { if (charge_full_exists) { energy_full_exists = true; - energy_full = charge_full * voltage_now / 1000000; + energy_full = (uint64_t)charge_full * (uint64_t)voltage_now / 1000000; } else if (charge_now_exists && capacity_exists && capacity != 0) { charge_full_exists = true; - charge_full = 100 * charge_now / capacity; + charge_full = 100 * (uint64_t)charge_now / (uint64_t)capacity; energy_full_exists = true; - energy_full = charge_now * voltage_now / capacity / 10000; + energy_full = (uint64_t)charge_now * (uint64_t)voltage_now / (uint64_t)capacity / 10000; } else if (capacity_exists && energy_now) { if (voltage_now != 0) { charge_now_exists = true; - charge_now = 1000000 * energy_now / voltage_now; + charge_now = 1000000 * (uint64_t)energy_now / (uint64_t)voltage_now; } if (capacity != 0) { energy_full_exists = true; - energy_full = 100 * energy_now / capacity; + energy_full = 100 * (uint64_t)energy_now / (uint64_t)capacity; if (voltage_now != 0) { charge_full_exists = true; - charge_full = 100 * 1000000 * energy_now / voltage_now / capacity; + charge_full = 100 * 1000000 * (uint64_t)energy_now / (uint64_t)voltage_now / (uint64_t)capacity; } } } @@ -364,12 +364,12 @@ const std::tuple waybar::modules::Battery::g if (!power_now_exists && voltage_now_exists && current_now_exists) { power_now_exists = true; - power_now = voltage_now * current_now / 1000000; + power_now = (uint64_t)voltage_now * (uint64_t)current_now / 1000000; } if (!energy_full_design_exists && voltage_now_exists && charge_full_design_exists) { energy_full_design_exists = true; - energy_full_design = voltage_now * charge_full_design / 1000000; + energy_full_design = (uint64_t)voltage_now * (uint64_t)charge_full_design / 1000000; } // Show the "smallest" status among all batteries