From a9afb46c195a5da8892b1233f6ba77b6a0a9eb04 Mon Sep 17 00:00:00 2001 From: Drath Date: Mon, 22 Sep 2025 22:17:51 +0200 Subject: [PATCH] overhaul of sys --- include/sys.hpp | 12 +++++++++--- main.cpp | 15 +++++++++------ src/sys.cpp | 39 +++++++-------------------------------- 3 files changed, 25 insertions(+), 41 deletions(-) diff --git a/include/sys.hpp b/include/sys.hpp index d35eb8c..77a532f 100644 --- a/include/sys.hpp +++ b/include/sys.hpp @@ -8,17 +8,23 @@ #include #include -std::string getAvgs(); -std::array 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); diff --git a/main.cpp b/main.cpp index c1da356..c59620d 100644 --- a/main.cpp +++ b/main.cpp @@ -31,8 +31,8 @@ std::binary_semaphore main{0}; } // namespace sem struct Sensors { - std::array 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 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)); diff --git a/src/sys.cpp b/src/sys.cpp index ed9dfac..5605656 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -1,41 +1,17 @@ #include -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 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(proc_meminfo)), std::istreambuf_iterator()); - 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; } \ No newline at end of file