mirror of
https://github.com/baketnk/frame-yap.git
synced 2026-10-06 02:00:06 +02:00
Add read-only Linux system battery reader
Reads the device's own battery from /sys/class/power_supply, skipping peripheral (scope=Device) supplies and implausible values. Offline tested against a fake power_supply tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94b747bb04
commit
59f4f9cff8
4 files changed
+93
-1
No files matched your search
@@ -0,0 +1,35 @@
|
||||
#include "battery.hpp"
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
namespace frameyap {
|
||||
namespace {
|
||||
std::string first_line(const std::filesystem::path& path) {
|
||||
std::ifstream in(path);
|
||||
std::string line;
|
||||
if (in) std::getline(in, line);
|
||||
return line;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::optional<BatteryLevel> system_battery(const std::filesystem::path& root) {
|
||||
std::error_code error;
|
||||
std::vector<std::filesystem::path> supplies;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(root, error)) supplies.push_back(entry.path());
|
||||
std::sort(supplies.begin(), supplies.end()); // deterministic choice among several
|
||||
for (const auto& supply : supplies) {
|
||||
if (first_line(supply / "type") != "Battery" || first_line(supply / "scope") == "Device") continue;
|
||||
const auto capacity = first_line(supply / "capacity");
|
||||
if (capacity.empty() || capacity.size() > 3 ||
|
||||
!std::all_of(capacity.begin(), capacity.end(), [](char c) { return c >= '0' && c <= '9'; }))
|
||||
continue;
|
||||
const int percent = std::stoi(capacity);
|
||||
if (percent > 100) continue;
|
||||
return BatteryLevel{percent, first_line(supply / "status") == "Charging"};
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
} // namespace frameyap
|
||||
Reference in new issue
Block a user