Check battery info

pull/536/head
FlightlessMango 3 years ago committed by Alessandro Toia
parent d556e8e357
commit a90c897feb

@ -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;

@ -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;

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

@ -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__

Loading…
Cancel
Save