feat(battery): add option to smooth power for more stable time_remaining estimates

This commit is contained in:
Benji-clm
2026-01-09 13:02:28 +00:00
parent 06484547d1
commit 7f63a905a1
2 changed files with 35 additions and 1 deletions
+6
View File
@@ -9,6 +9,7 @@
#include <sys/poll.h>
#include <algorithm>
#include <chrono>
#include <fstream>
#include <string>
#include <vector>
@@ -51,6 +52,11 @@ class Battery : public ALabel {
bool warnFirstTime_{true};
bool weightedAverage_{true};
const Bar& bar_;
bool smoothPowerEnable_{false};
double time_constant_s_{260.0};
double smooth_power_{0.0}; // µW
std::chrono::steady_clock::time_point last_t_{std::chrono::steady_clock::now()};
std::string old_status_raw_{""};
util::SleeperThread thread_;
util::SleeperThread thread_battery_update_;
+29 -1
View File
@@ -2,6 +2,9 @@
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <string>
#include "util/command.hpp"
#if defined(__FreeBSD__)
@@ -33,6 +36,11 @@ waybar::modules::Battery::Battery(const std::string& id, const Bar& bar, const J
}
udev_monitor_enable_receiving(mon_.get());
if (config_["smooth-power"].isBool()) {
smoothPowerEnable_ = config_["smooth-power"].asBool();
if (smoothPowerEnable_ && config_["smooth-power-time-constant"].isNumeric())
time_constant_s_ = std::max(1.0, config_["smooth-power-time-constant"].asDouble());
}
if (config_["weighted-average"].isBool()) weightedAverage_ = config_["weighted-average"].asBool();
#endif
spdlog::debug("battery: worker interval is {}", interval_.count());
@@ -562,11 +570,31 @@ waybar::modules::Battery::getInfos() {
if (online && current_status != "Discharging") status = "Plugged";
}
if (total_energy_exists && total_power_exists && total_power != 0) {
if (!smoothPowerEnable_) {
smooth_power_ = total_power;
} else {
if (status != old_status_raw_) {
smooth_power_ = total_power;
last_t_ = std::chrono::steady_clock::now();
} else {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
double dt_s =
std::chrono::duration_cast<std::chrono::duration<double> >(now - last_t_).count();
smooth_power_ = smooth_power_ + ((1 - std::exp(-dt_s / time_constant_s_)) *
(total_power - smooth_power_));
last_t_ = now;
}
old_status_raw_ = status;
}
}
float time_remaining{0.0f};
if (status == "Discharging" && time_to_empty_now_exists) {
if (time_to_empty_now != 0) time_remaining = (float)time_to_empty_now / 3600.0f;
} else if (status == "Discharging" && total_power_exists && total_energy_exists) {
if (total_power != 0) time_remaining = (float)total_energy / total_power;
if (smooth_power_ != 0) time_remaining = (float)total_energy / smooth_power_;
} else if (status == "Charging" && time_to_full_now_exists) {
if (time_to_full_now_exists && (time_to_full_now != 0))
time_remaining = -(float)time_to_full_now / 3600.0f;