Check battery info

This commit is contained in:
FlightlessMango 2021-03-16 09:50:07 +01:00
parent e32934ee40
commit 51b0939a80
4 changed files with 56 additions and 0 deletions

32
src/battery.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "battery.h"
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();
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;
}
}
current_watt = powerMap["current_now"].value * powerMap["voltage_now"].value;
}
BatteryStats Battery_Stats;

21
src/battery.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "overlay.h"
#include "overlay_params.h"
#include <logging.h>
#include <vector>
#include <unordered_map>
class BatteryStats{
public:
void findFiles();
void update();
bool files_fetched = false;
struct powerStruct{
FILE *file = nullptr;
float value;
};
std::unordered_map<std::string, powerStruct> powerMap;
float current_watt = 0;
};
extern BatteryStats Battery_Stats;

View File

@ -69,6 +69,7 @@ if is_unixy
'elfhacks.cpp',
'real_dlsym.cpp',
'pci_ids.cpp',
'battery.cpp',
)
opengl_files = files(

View File

@ -9,6 +9,7 @@
#include "timing.hpp"
#include "mesa/util/macros.h"
#include "string_utils.h"
#include "battery.h"
#ifdef HAVE_DBUS
float g_overflow = 50.f /* 3333ms * 0.5 / 16.6667 / 2 (to edge and back) */;
#endif
@ -21,6 +22,7 @@ std::vector<logData> graph_data;
void update_hw_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID)
{
Battery_Stats.update();
if (params.enabled[OVERLAY_PARAM_ENABLED_cpu_stats] || logger->is_active()) {
cpuStats.UpdateCPUData();
#ifdef __gnu_linux__