From 419a7d4ca3889d97afc006f7301017a224e16396 Mon Sep 17 00:00:00 2001 From: jkre Date: Thu, 14 Dec 2023 00:32:06 +0100 Subject: [PATCH] add power draw calculation for battery --- src/linux/btop_collect.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 212a165..c00c563 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -755,6 +755,7 @@ namespace Cpu { int percent = -1; long seconds = -1; + float watts = -1; //? Try to get battery percentage if (percent < 0) { @@ -820,6 +821,25 @@ namespace Cpu { } } + //? Get power draw + if (b.use_power) { + if (not b.power_now.empty()) { + try { + watts = (float)stoll(readfile(b.energy_now, "-1")) / 1000000.0; + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } + else if (not b.voltage_now.empty() and not b.current_now.empty()) { + try { + watts = (float)stoll(readfile(b.current_now, "-1")) / 1000000.0 * stoll(readfile(b.voltage_now, "1")) / 1000000.0; + } + catch (const std::invalid_argument&) { } + catch (const std::out_of_range&) { } + } + + } + return {percent, seconds, status}; }