Initial commit

This commit is contained in:
datalore 2024-02-16 20:58:14 +01:00
commit a7af00864d
5 changed files with 106 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
build/batstat:src/*.cc
g++ -o build/batstat src/*.cc -Iinclude
clean:
rm build/batstat

23
include/batteryinfo.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include <filesystem>
#include <string>
namespace dlore
{
struct
{
std::string Manufacturer;
std::string Model;
std::string Technology;
unsigned int CellSeriesCount;
unsigned int Capacity;
float Vnow;
float Vmin;
unsigned int ChargeNow;
unsigned int ChargeFull;
unsigned int ChargeFullDesign;
} typedef BatteryInfo;
BatteryInfo GetBatteryInfo(std::filesystem::path classPath);
}

41
src/batteryinfo.cc Normal file
View File

@ -0,0 +1,41 @@
#include <batteryinfo.h>
#include <fstream>
#include <cmath>
namespace dlore
{
std::string readData(std::filesystem::path path)
{
std::ifstream file(path, std::ios::in);
if(!file) return "";
std::string ret;
std::getline(file, ret);
file.close();
return ret;
}
BatteryInfo GetBatteryInfo(std::filesystem::path classPath)
{
BatteryInfo info;
info.Manufacturer = readData(classPath / "manufacturer");
info.Model = readData(classPath / "model_name");
info.Technology = readData(classPath / "technology");
info.Capacity = std::stoi(readData(classPath / "capacity"));
info.ChargeNow = std::stoi(readData(classPath / "charge_now")) / 1000;
info.ChargeFull = std::stoi(readData(classPath / "charge_full")) / 1000;
info.ChargeFullDesign = std::stoi(readData(classPath / "charge_full_design")) / 1000;
int Vnow = std::stoi(readData(classPath / "voltage_now")) / 1000;
int Vmin = std::stoi(readData(classPath / "voltage_min_design")) / 1000;
info.Vnow = Vnow / 1000.0f;
info.Vmin = Vmin / 1000.0f;
info.CellSeriesCount = std::ceil(info.Vmin / 3.7f);
// LiPo and Li-Ion cells operate at 3.7V (with the exception of LiFePO4 cells, which operate
// at 3.3V but are not used in laptops due to low voltage and charge)
return info;
}
}

37
src/main.cc Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <iomanip>
#include <batteryinfo.h>
void printBattery(std::filesystem::path classPath)
{
dlore::BatteryInfo bat = dlore::GetBatteryInfo(classPath);
std::cout << std::fixed << std::setprecision(2);
std::cout << "Battery " << bat.Manufacturer << bat.Model; // manufacturer has a trailing whitespace, so no additional space necessary
std::cout << std::endl << "\tTechnology: " << bat.Technology;
std::cout << std::endl << "\tCell series count: " << bat.CellSeriesCount << 's' << std::endl;
std::cout << std::endl << "\tCapacity: " << bat.Capacity << '%';
std::cout << std::endl << "\tVmin: " << bat.Vmin << 'V';
std::cout << " Per cell: " << bat.Vmin / bat.CellSeriesCount << 'V';
std::cout << std::endl << "\tVnow: " << bat.Vnow << 'V';
std::cout << " Per cell: " << bat.Vnow / bat.CellSeriesCount << 'V';
std::cout << std::endl << "\tCharge now: " << bat.ChargeNow << "mAh";
std::cout << std::endl << "\tCharge full: " << bat.ChargeFull << "mAh";
std::cout << std::endl << "\tCharge full (design): " << bat.ChargeFullDesign << "mAh" << std::endl;
}
int main(int argc, const char** argv)
{
for(const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator("/sys/class/power_supply"))
{
std::filesystem::path path = entry.path();
if(path.filename().string().find("BAT") == std::string::npos) continue; // filename does not contain BAT, continue
printBattery(path);
}
return 0;
}