From 39f6ae98ec2392c255d74dcaeb9e79f621611f20 Mon Sep 17 00:00:00 2001 From: FlightlessMango Date: Wed, 9 Feb 2022 05:20:59 +0100 Subject: [PATCH] Battery remaining time --- src/battery.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++-- src/battery.h | 4 +++- src/hud_elements.cpp | 9 ++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/battery.cpp b/src/battery.cpp index 1825b35a..524f7da9 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -33,6 +33,7 @@ void BatteryStats::update() { if (batt_count > 0) { current_watt = getPower(); current_percent = getPercent(); + remaining_time = getTimeRemaining(); } } @@ -40,7 +41,7 @@ float BatteryStats::getPercent() { float charge_n = 0; float charge_f = 0; - for(int i =0; i < batt_count; i++) { + for(int i = 0; i < batt_count; i++) { string syspath = battPath[i]; string charge_now = syspath + "/charge_now"; string charge_full = syspath + "/charge_full"; @@ -89,7 +90,7 @@ float BatteryStats::getPercent() float BatteryStats::getPower() { float current = 0; float voltage = 0; - for(int i =0; i < batt_count; i++) { + for(int i = 0; i < batt_count; i++) { string syspath = battPath[i]; string current_power = syspath + "/current_now"; string current_voltage = syspath + "/voltage_now"; @@ -130,4 +131,53 @@ float BatteryStats::getPower() { return current * voltage; } +float BatteryStats::getTimeRemaining(){ + float current = 0; + float charge = 0; + for(int i = 0; i < batt_count; i++) { + string syspath = battPath[i]; + string current_now = syspath + "/current_now"; + string charge_now = syspath + "/charge_now"; + string voltage_now = syspath + "/voltage_now"; + string power_now = syspath + "/power_now"; + + if (fs::exists(current_now)) { + std::ifstream input(current_now); + std::string line; + if(std::getline(input, line)){ + current_now_vec.push_back(stof(line)); + } + } else if (fs::exists(power_now)){ + float voltage = 0; + float power = 0; + std::ifstream input_power(power_now); + std::ifstream input_voltage(power_now); + std::string line; + if(std::getline(input_voltage, line)){ + voltage = stof(line); + } + if(std::getline(input_power, line)){ + power = stof(line); + } + current_now_vec.push_back(power / voltage); + } + if (fs::exists(charge_now)){ + std::string line; + std::ifstream input(charge_now); + if(std::getline(input, line)){ + charge += stof(line); + } + } + if (current_now_vec.size() > 25) + current_now_vec.erase(current_now_vec.begin()); + } + + for(auto& current_now : current_now_vec){ + current += current_now; + } + current /= current_now_vec.size(); + + return charge / current; +} + BatteryStats Battery_Stats; diff --git a/src/battery.h b/src/battery.h index d087dcff..098f976d 100644 --- a/src/battery.h +++ b/src/battery.h @@ -7,14 +7,16 @@ class BatteryStats{ void update(); float getPower(); float getPercent(); + float getTimeRemaining(); std::string battPath[2]; float current_watt = 0; float current_percent = 0; + float remaining_time = 0; std::string current_status; std::string state [2]; int batt_count=0; bool batt_check = false; - + std::vector current_now_vec = {}; }; extern BatteryStats Battery_Stats; diff --git a/src/hud_elements.cpp b/src/hud_elements.cpp index dd964085..86300941 100644 --- a/src/hud_elements.cpp +++ b/src/hud_elements.cpp @@ -729,6 +729,15 @@ void HudElements::battery(){ ImGui::PushFont(HUDElements.sw_stats->font1); ImGui::Text("W"); ImGui::PopFont(); + ImGui::TableNextRow(); ImGui::TableNextColumn(); ImGui::TableNextColumn(); ImGui::TableNextColumn(); + float hours; + float minutes; + float seconds; + minutes = std::modf(Battery_Stats.remaining_time, &hours); + minutes *= 60; + seconds = std::modf(minutes, &minutes); + seconds *= 60; + right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%02.0f:%02.0f:%02.0f", hours, minutes, seconds); } } }