clean up main

This commit is contained in:
Drath
2025-09-11 18:29:09 +02:00
parent 2783e41f2a
commit b3e339027f
5 changed files with 50 additions and 54 deletions

View File

@@ -1,12 +1,22 @@
#ifndef SYS_HPP
#define SYS_HPP
#include <ctime>
#include <sys/statvfs.h>
#include <array>
#include <ctime>
#include <string>
#include <utils.hpp>
struct mem_usage_t {
uint64_t free = 0;
uint64_t used = 0;
uint64_t total = 0;
};
std::string getAvgs();
std::array<std::string, 2> getTimeDate();
mem_usage_t getDisk(std::string mount);
mem_usage_t getRam();
#endif

View File

@@ -15,8 +15,11 @@
#include <sstream>
#include <string>
#define FILL_CHAR ' '
std::string pNum(double num, uint8_t decs = 0);
std::string pNum(float num, uint8_t decs = 0);
std::string pad(std::string s, uint8_t len, bool ljust = false);
std::string pBytes(double bytes, bool isKbytes = false);
std::string readFile(std::string path, size_t trim = 4096);
ssize_t readFile2(std::string path, char *buffer, size_t maxlen);

View File

@@ -1,4 +1,3 @@
#include <sys/statvfs.h>
#include <array>
#include <chrono>
@@ -22,17 +21,10 @@
#define LCD_PORT "/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00" // tis a symlink, can also be just /dev/ttyACM0
#define LCD_INTERVAL_MS 1000
#define LCD_CONTRAST 215
#define FILL_CHAR ' '
std::chrono::milliseconds interval(LCD_INTERVAL_MS);
std::stop_source sigint;
struct mem_usage_t {
uint64_t free = 0;
uint64_t used = 0;
uint64_t total = 0;
};
struct sensors_t {
std::array<std::string, 2> td;
std::string loadavg;
@@ -43,46 +35,6 @@ struct sensors_t {
float hass_state = 0;
};
/* DISPLAY FUNCS */
// pads given string to length
std::string pad(std::string s, uint8_t len, bool ljust = false) {
if (len > s.size())
s.insert(ljust ? s.end() : s.begin(), len - s.size(), FILL_CHAR);
return s;
}
// fills n spaces
std::string fill(uint8_t n) {
return std::string(n, FILL_CHAR);
}
/* GETTERS */
mem_usage_t getRam() {
mem_usage_t 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 mem;
}
mem_usage_t getDisk(std::string mount) {
mem_usage_t disk;
struct statvfs diskData;
statvfs(mount.c_str(), &diskData);
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 disk;
}
void sighandler(int s) {
std::println("[Signal] got SIGINT, exiting...");
sigint.request_stop();
@@ -140,22 +92,22 @@ void display_thread(sensors_t *s, std::stop_token st) {
lcd.writeBuf(0, 0, pad(s->td[1], 11, true) + " " + s->td[0] + lcd.bar(1, s->gpu.core_usage / 100.0) + pad(s->loadavg, 17));
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, 11, pad(pBytes(s->ram.used, true) + "B", 7, true));
lcd.writeBuf(1, 8, pad(pBytes(s->ram.used, true) + "B", 7, true));
lcd.writeBuf(1, 16, pad(pNum(s->hass_state) + "W", 4));
lcd.writeBuf(1, 22, pad(pBytes(s->gpu.vram_used) + "B", 7));
lcd.writeBuf(1, 29, pad(std::to_string(s->gpu.edge_temp) + gpu_temps + lcd.cc[0], 11));
lcd.writeBuf(2, 0, pad(pNum(s->cpu.ptotal, 1) + "W", 8, true));
lcd.writeBuf(2, 10, pad(pNum(s->hass_state) + "W", 4));
lcd.writeBuf(2, 15, pad("h:" + pBytes(s->home.free), 8));
lcd.writeBuf(2, 14, "home:" + pad(pBytes(s->home.free), 5));
// lcd.writeBuf(2, 25, pad(s->gpu.fan_speed + "R", 5));
lcd.writeBuf(2, 25, pad(std::to_string(s->gpu.fan_speed), 4) + "R");
// std::println("{}", s->gpu.fan_speed);
lcd.writeBuf(2, 31, pad(std::to_string(s->gpu.power_avg) + "/" + std::to_string(s->gpu.power_cap) + "W", 9));
lcd.writeBuf(3, 0, pad(pNum(s->cpu.vcore, 3) + "V", 7, true));
lcd.writeBuf(3, 7, std::to_string(s->cpu.avg_freq) + "MHz");
lcd.writeBuf(3, 21, pad(std::to_string(s->gpu.core_clock) + "/" + std::to_string(s->gpu.mem_clock) + "MHz ", 13));
lcd.writeBuf(3, 34, pad(pNum(s->gpu.vcore, 3) + "V", 5));
lcd.writeBuf(3, 34, pad(pNum(s->gpu.vcore, 3) + "Vabscd", 5));
lcd.flushBuffer();

View File

@@ -24,3 +24,27 @@ std::array<std::string, 2> getTimeDate() {
date << wdays[tm.tm_wday] << ", " << tm.tm_mday << " " << months[tm.tm_mon];
return {time.str(), date.str()};
}
mem_usage_t getRam() {
mem_usage_t 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 mem;
}
mem_usage_t getDisk(std::string mount) {
mem_usage_t disk;
struct statvfs diskData;
statvfs(mount.c_str(), &diskData);
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 disk;
}

View File

@@ -11,6 +11,13 @@ std::string pNum(float num, uint8_t decs) {
return pNum((double)num, decs);
}
// pads given string to length
std::string pad(std::string s, uint8_t len, bool ljust) {
if (len > s.size())
s.insert(ljust ? s.end() : s.begin(), len - s.size(), FILL_CHAR);
return s;
}
// converts large number in (k)bytes to human readable numer with suffix and sane decimal
std::string pBytes(double bytes, bool isKbytes) {