Add param to check battery power and percent

pull/536/head
Alessandro Toia 3 years ago
parent a90c897feb
commit 1ab25220c6

File diff suppressed because it is too large Load Diff

@ -1,32 +1,192 @@
#include <stdio.h>
#include "battery.h"
#include <filesystem.h>
namespace fs = ghc::filesystem;
int BatteryStats::numBattery() {
int batteryCount = 0;
if (!fs::exists("/sys/class/power_supply/")) {
return batteryCount = 0;
}
fs::path path("/sys/class/power_supply/");
for (auto& p : fs::directory_iterator(path)) {
string fileName = p.path().filename();
if (fileName.find("BAT") != std::string::npos) {
batteryCount += 1;
battPath.push_back(p.path());
}
}
return batteryCount;
}
void BatteryStats::update() {
if (numBattery() > 0) {
if (numBattery() == 1) {
current_watt = getPower(0);
current_percent = getPercent(0) * 100;
}
else if (numBattery() == 2) {
float bat1_power = getPower(0);
float bat2_power = getPower(1);
getPercent(0);
getPercent(1);
//bat_percent[][] = [bat1 or 2] [power now or power full]
float energy=bat_percent[0][0] + bat_percent[1][0];
float energy_full=bat_percent[0][1] + bat_percent[1][1];
current_watt = (bat1_power + bat2_power);
current_percent = (energy / energy_full) * 100;
}
}
}
float BatteryStats::getPercent(int num)
{
string syspath = battPath[num];
string charge_now = syspath + "/charge_now";
string charge_full = syspath + "/charge_full";
string energy_now = syspath + "/energy_now";
string energy_full = syspath + "/energy_full";
string capacity = syspath + "/capacity";
if (fs::exists(charge_now)) {
float charge_n = 0;
float charge_f = 0;
std::ifstream input(charge_now);
std::string line;
if(std::getline(input, line)) {
charge_n = (stof(line) / 1000000);
bat_percent[num][0]=charge_n;
}
std::ifstream input2(charge_full);
if(std::getline(input2, line)) {
charge_f = (stof(line) / 1000000);
bat_percent[num][1]=charge_f;
}
return (charge_n / charge_f);
}
else if (fs::exists(energy_now)) {
float energy_n = 0;
float energy_f = 0;
std::ifstream input(energy_now);
std::string line;
if(std::getline(input, line)) {
energy_n = (stof(line) / 1000000);
bat_percent[num][0]=energy_n;
}
std::ifstream input2(energy_full);
if(std::getline(input2, line)) {
energy_f = (stof(line) / 1000000);
bat_percent[num][1]=energy_f;
}
return (energy_n / energy_f);
}
else {
float percent=0;
// using /sys/class/power_supply/BAT*/capacity
// No way to get an accurate reading just average the percents if mutiple batteries
std::ifstream input(capacity);
std::string line;
if(std::getline(input, line)) {
percent = stof(line) / 100;
bat_percent[num][0]=percent;
bat_percent[num][1]=1.0;
}
return percent;
}
}
float BatteryStats::getPower(int batt_num) {
string syspath = battPath[batt_num];
string current_power = syspath + "/current_now";
string current_voltage = syspath + "/voltage_now";
string power_now = syspath + "/power_now";
if (isCharging()) {
return 0;
}
else if (fs::exists(current_power)) {
float current = 0;
float voltage = 0;
std::ifstream input(current_power);
std::string line;
if(std::getline(input,line)) {
current = (stof(line) / 1000000);
}
std::ifstream input2(current_voltage);
if(std::getline(input2, line)) {
voltage = (stof(line) / 1000000);
}
return current * voltage;
}
else {
float power = 0;
std::ifstream input(power_now);
std::string line;
if(std::getline(input,line)) {
power = (stof(line) / 1000000);
}
return power;
}
void BatteryStats::findFiles(){
FILE *file;
file = fopen("/sys/class/power_supply/BAT1/current_now", "r");
powerMap["current_now"] = {file, 0};
file = fopen("/sys/class/power_supply/BAT1/voltage_now", "r");
powerMap["voltage_now"] = {file, 0};
// file = fopen("/sys/class/power_supply/BAT1/charge_now", "r");
// powerMap["charge_now"] = {file, 0};
// file = fopen("/sys/class/power_supply/BAT1/charge_full", "r");
// powerMap["charge_full"] = {file, 0};
files_fetched = true;
}
void BatteryStats::update(){
if (!files_fetched)
findFiles();
bool BatteryStats::isCharging() {
for(int i =0; i < 2; i++) {
string syspath = battPath[i];
string status = syspath + "/status";
std::ifstream input(status);
std::string line;
for(auto &pair : powerMap){
if(pair.second.file) {
rewind(pair.second.file);
fflush(pair.second.file);
fscanf(pair.second.file, "%f", &pair.second.value);
pair.second.value /= 1000000;
if(std::getline(input,line)) {
current_status= line;
state[i]=current_status;
}
}
current_watt = powerMap["current_now"].value * powerMap["voltage_now"].value;
for(int i =0; i < 2; i++) {
if (state[i] == "Charging") {
return true;
}
}
return false;
}
bool BatteryStats::fullCharge(){
//check if both batteries are fully charged
int charged =0;
for(int i =0; i < 2; i++) {
if (state[i] == "Full") {
charged +=1;
}
}
if (charged == 2) {
return true;
}
else {
return false;
}
}
BatteryStats Battery_Stats;
BatteryStats Battery_Stats;

@ -4,18 +4,24 @@
#include <logging.h>
#include <vector>
#include <unordered_map>
#include <filesystem>
class BatteryStats{
public:
int numBattery();
void findFiles();
void update();
bool files_fetched = false;
struct powerStruct{
FILE *file = nullptr;
float value;
};
std::unordered_map<std::string, powerStruct> powerMap;
float getPower(int num);
float getPercent(int num);
bool isCharging();
bool fullCharge();
std::vector<std::string> battPath;
float current_watt = 0;
float current_percent = 0;
float bat_percent [2][2];
string current_status="";
string state [2];
};
extern BatteryStats Battery_Stats;

@ -65,6 +65,7 @@ void HudElements::convert_colors(struct overlay_params& params)
HUDElements.colors.text = convert(params.text_color);
HUDElements.colors.media_player = convert(params.media_player_color);
HUDElements.colors.wine = convert(params.wine_color);
HUDElements.colors.battery = convert(params.battery_color);
HUDElements.colors.gpu_load_low = convert(params.gpu_load_color[0]);
HUDElements.colors.gpu_load_med = convert(params.gpu_load_color[1]);
HUDElements.colors.gpu_load_high = convert(params.gpu_load_color[2]);
@ -576,6 +577,41 @@ void HudElements::vkbasalt(){
}
}
void HudElements::battery_percent(){
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_battery_percent]){
ImGui::TableNextRow(); ImGui::TableNextColumn();
ImGui::TextColored(HUDElements.colors.battery, "BATT");
ImGui::TableNextColumn();
right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%.0f", Battery_Stats.current_percent);
ImGui::SameLine(0,1.0f);
ImGui::Text("%%");
}
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_battery_percent] && HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_battery_power]){
ImGui::TableNextColumn();
if (Battery_Stats.isCharging() && !Battery_Stats.fullCharge()) {
ImGui::PushFont(HUDElements.sw_stats->font1);
ImGui::Text("Plugged in");
ImGui::PopFont();
}
else if(int(Battery_Stats.current_percent) == 100 || Battery_Stats.fullCharge()) {
ImGui::PushFont(HUDElements.sw_stats->font1);
ImGui::Text("Full Charge");
ImGui::PopFont();
}
else {
right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%.1f", Battery_Stats.current_watt);
ImGui::SameLine(0,1.0f);
ImGui::PushFont(HUDElements.sw_stats->font1);
ImGui::Text("W");
ImGui::PopFont();
}
}
}
void HudElements::graphs(){
ImGui::TableNextRow(); ImGui::TableNextColumn();
ImGui::Dummy(ImVec2(0.0f, real_font_size.y));
@ -727,6 +763,7 @@ void HudElements::sort_elements(std::pair<std::string, std::string> option){
if (param == "vkbasalt") { ordered_functions.push_back({vkbasalt, value}); }
if (param == "exec") { ordered_functions.push_back({_exec, value});
exec_list.push_back({int(ordered_functions.size() - 1), value}); }
if (param == "battery_percent") {ordered_functions.push_back({battery_percent, value}); }
if (param == "graphs"){
if (!HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_graphs])
HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_graphs] = true;

@ -5,6 +5,7 @@
#include <map>
#include <sstream>
#include <logging.h>
#include <battery.h>
class HudElements{
public:
@ -56,6 +57,8 @@ class HudElements{
static void gamemode();
static void graphs();
static void _exec();
static void battery_power();
static void battery_percent();
void convert_colors(struct overlay_params& params);
void convert_colors(bool do_conv, struct overlay_params& params);
@ -73,6 +76,7 @@ class HudElements{
text,
media_player,
wine,
battery,
gpu_load_low,
gpu_load_med,
gpu_load_high,
@ -83,7 +87,7 @@ class HudElements{
fps_value_med,
fps_value_high;
} colors {};
};
extern HudElements HUDElements;

@ -431,6 +431,7 @@ parse_gl_size_query(const char *str)
#define parse_custom_text(s) parse_str(s)
#define parse_fps_value(s) parse_load_value(s)
#define parse_fps_color(s) parse_load_color(s)
#define parse_battery_color(s) parse_color(s)
static bool
@ -612,7 +613,12 @@ parse_overlay_config(struct overlay_params *params,
params->cellpadding_y = -0.085;
params->fps_color = { 0xb22222, 0xfdfd09, 0x39f900 };
params->fps_value = { 30, 60 };
<<<<<<< HEAD
params->round_corners = 0;
=======
params->battery_color =0xff9078;
>>>>>>> b46f261 (Add param to check battery power and percent)
#ifdef HAVE_X11
@ -697,7 +703,7 @@ parse_overlay_config(struct overlay_params *params,
params->font_scale_media_player = 0.55f;
// Convert from 0xRRGGBB to ImGui's format
std::array<unsigned *, 20> colors = {
std::array<unsigned *, 21> colors = {
&params->cpu_color,
&params->gpu_color,
&params->vram_color,
@ -709,6 +715,7 @@ parse_overlay_config(struct overlay_params *params,
&params->text_color,
&params->media_player_color,
&params->wine_color,
&params->battery_color,
&params->gpu_load_color[0],
&params->gpu_load_color[1],
&params->gpu_load_color[2],

@ -68,6 +68,8 @@ typedef unsigned long KeySym;
OVERLAY_PARAM_BOOL(exec) \
OVERLAY_PARAM_BOOL(vkbasalt) \
OVERLAY_PARAM_BOOL(gamemode) \
OVERLAY_PARAM_BOOL(battery_power) \
OVERLAY_PARAM_BOOL(battery_percent) \
OVERLAY_PARAM_CUSTOM(fps_sampling_period) \
OVERLAY_PARAM_CUSTOM(output_folder) \
OVERLAY_PARAM_CUSTOM(output_file) \
@ -112,6 +114,7 @@ typedef unsigned long KeySym;
OVERLAY_PARAM_CUSTOM(io_color) \
OVERLAY_PARAM_CUSTOM(text_color) \
OVERLAY_PARAM_CUSTOM(wine_color) \
OVERLAY_PARAM_CUSTOM(battery_color) \
OVERLAY_PARAM_CUSTOM(alpha) \
OVERLAY_PARAM_CUSTOM(log_duration) \
OVERLAY_PARAM_CUSTOM(pci_dev) \
@ -203,7 +206,7 @@ struct overlay_params {
enum gl_size_query gl_size_query {GL_SIZE_DRAWABLE};
bool gl_dont_flip {false};
uint64_t log_duration;
unsigned cpu_color, gpu_color, vram_color, ram_color, engine_color, io_color, frametime_color, background_color, text_color, wine_color;
unsigned cpu_color, gpu_color, vram_color, ram_color, engine_color, io_color, frametime_color, background_color, text_color, wine_color, battery_color;
std::vector<unsigned> gpu_load_color;
std::vector<unsigned> cpu_load_color;
std::vector<unsigned> gpu_load_value;

Loading…
Cancel
Save