overhaul of sys

This commit is contained in:
Drath
2025-09-22 22:17:51 +02:00
parent 1998fb6953
commit a9afb46c19
3 changed files with 25 additions and 41 deletions

View File

@@ -8,17 +8,23 @@
#include <string>
#include <utils.hpp>
std::string getAvgs();
std::array<std::string, 2> getTimeDate();
namespace sys {
const char months[][4] = {"sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"};
const char wdays[][4] = {"Nie", "Pon", "Wto", "Sro", "Czw", "Pia", "Sob"};
struct Memory {
uint64_t free = 0;
uint64_t used = 0;
uint64_t total = 0;
};
struct TimeDate {
char time[9];
char date[13];
};
void getTimeDate(TimeDate* td);
void getDisk(Memory* disk, std::string mount);
void getRam(Memory* mem);

View File

@@ -31,8 +31,8 @@ std::binary_semaphore main{0};
} // namespace sem
struct Sensors {
std::array<std::string, 2> td;
std::string loadavg;
sys::TimeDate td;
double avg[3];
sys::Memory ram;
sys::Memory home;
cpu::cpuSensors cpu;
@@ -73,9 +73,9 @@ void sensorsThread(std::stop_token st, Sensors* s) {
{
// this will unlock when program goes out of scope
std::lock_guard<std::mutex> lock(s->mtx);
// maybe make these take pointers too
s->td = getTimeDate();
s->loadavg = getAvgs();
sys::getTimeDate(&s->td);
getloadavg(s->avg, 3);
sys::getRam(&s->ram);
sys::getDisk(&s->home, "/home/drath");
cpu::update(&s->cpu);
@@ -104,7 +104,10 @@ void displayThread(std::stop_token st, Sensors* s) {
auto gpu_temps = (s->gpu.temp_hotspot) ? "/" + std::to_string(s->gpu.temp_hotspot) : "";
gpu_temps += (s->gpu.temp_mem) ? "/" + std::to_string(s->gpu.temp_mem) : "";
lcd.writeBuf(0, 0, pad(s->td[1], 11, true) + " " + s->td[0] + lcd.bar(1, s->gpu.usage_core / 100.0) + pad(s->loadavg, 17));
lcd.writeBuf(0, 0, pad(s->td.date, 11, true) + " " + s->td.time + lcd.bar(1, s->gpu.usage_core / 100.0) /* + pad(s->loadavg, 17)*/);
lcd.writeBuf(0, 26, pNum(s->avg[0], s->avg[0] > 10 ? 1 : 2));
lcd.writeBuf(0, 31, pNum(s->avg[1], s->avg[1] > 10 ? 1 : 2));
lcd.writeBuf(0, 36, pNum(s->avg[2], s->avg[2] > 10 ? 1 : 2));
lcd.writeBuf(1, 0, pad(std::to_string(s->cpu.tctl) + "/" + std::to_string(s->cpu.tccd1) + lcd.cc[0], 8, true));
lcd.writeBuf(1, 8, pad(pBytes(s->ram.used, true) + "B", 7, true));

View File

@@ -1,41 +1,17 @@
#include <sys.hpp>
std::string getAvgs() {
std::string ret;
double avgs[3];
getloadavg(avgs, 3);
for (uint8_t i = 0; i < 3; i++)
ret += pNum(avgs[i], avgs[i] > 10 ? 1 : 2) + " ";
ret.pop_back(); // remove trailing space
return ret;
}
std::array<std::string, 2> getTimeDate() {
std::array months = {"sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"};
std::array wdays = {"Nie", "Pon", "Wto", "Sro", "Czw", "Pia", "Sob"};
// std::array wdays = {"Ni", "Pn", "Wt", "Sr", "Cz", "Pi", "So"};
void sys::getTimeDate(TimeDate* td) {
auto now = std::time(nullptr);
auto tm = *std::localtime(&now);
std::stringstream time, date;
time << std::setfill('0');
time << std::setw(2) << tm.tm_hour << ":";
time << std::setw(2) << tm.tm_min << ":";
time << std::setw(2) << tm.tm_sec;
date << wdays[tm.tm_wday] << ", " << tm.tm_mday << " " << months[tm.tm_mon];
return {time.str(), date.str()};
sprintf(td->time, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
sprintf(td->date, "%s, %d %s", wdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon]);
}
void sys::getRam(Memory* mem) {
std::ifstream proc_meminfo("/proc/meminfo");
if (proc_meminfo.good()) {
std::string content((std::istreambuf_iterator<char>(proc_meminfo)), std::istreambuf_iterator<char>());
mem->free = get_val("MemAvailable:", content);
mem->total = get_val("MemTotal:", content);
mem->used = mem->total - mem->free;
}
proc_meminfo.close();
return;
auto content = readFile("/proc/meminfo", 128); // file is longer than that but its all we need
mem->free = get_val("MemAvailable:", content);
mem->total = get_val("MemTotal:", content);
mem->used = mem->total - mem->free;
}
void sys::getDisk(Memory* disk, std::string mount) {
@@ -44,5 +20,4 @@ void sys::getDisk(Memory* disk, std::string mount) {
disk->free = diskData.f_bfree * diskData.f_frsize;
disk->used = (diskData.f_blocks - diskData.f_bfree) * diskData.f_frsize;
disk->total = diskData.f_blocks * diskData.f_frsize;
return;
}