From 1d9231d88bff34ee08c53ccbb2faed8c594ad0bc Mon Sep 17 00:00:00 2001 From: Drath Date: Fri, 2 Jan 2026 21:01:43 +0100 Subject: [PATCH] application class, still broken --- include/app.hpp | 54 +++++++++++++++++++++++++++++++-------------- main.cpp | 3 ++- src/app.cpp | 58 ++++++++++++++++++++----------------------------- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/include/app.hpp b/include/app.hpp index 0660d80..8b811dd 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -4,25 +4,47 @@ #include #include #include +#include +#include #include -namespace app { +class Application { + struct Sensors { + sys::TimeDate td; + double avg[3] = {0.0f}; + sys::Memory ram; + sys::Disk home; + sys::Network net; + sys::sysSensors sys; + cpu::cpuSensors cpu; + gpu::gpuSensors gpu; + double hass_state = 0.0f; + std::mutex mtx; // making sure the struct isnt accessed by multiple threads at once + }; -struct sensors { - sys::TimeDate td; - double avg[3] = {0.0f}; - sys::Memory ram; - sys::Disk home; - sys::Network net; - sys::sysSensors sys; - cpu::cpuSensors cpu; - gpu::gpuSensors gpu; - double hass_state = 0.0f; - std::mutex mtx; // making sure the struct isnt accessed by multiple threads at once + struct Semaphores { + inline static std::binary_semaphore display{0}; + inline static std::binary_semaphore main{0}; + }; + + std::string lcd_port; + uint16_t lcd_interval_ms = 500; + uint8_t lcd_contrast = 215; + + std::string cpu_pciaddr = "0000:00:18.3"; + std::string gpu_pciaddr = "0000:0b:00.0"; + std::string sys_sensor = "nct6775.656"; + std::string sys_ifname = "enp4s0f0"; + std::string home_dir = "/home/drath"; + + static void signalHandler(int); + static void hassThread(const std::stop_token& st, Sensors& s); + static void sensorsThread(const std::stop_token& st, Sensors& s); + static void displayThread(const std::stop_token& st, Sensors& s); + + public: + explicit Application(const std::string& display_port); + void Run(); }; -void run(); - -} // namespace app - #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index ced20a3..3eb68ca 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include // put app in a class w a constructor for all the basic params int main() { - app::run(); + Application app("/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00"); // tis a symlink, can also be just /dev/ttyACM0) + app.Run(); return 0; } diff --git a/src/app.cpp b/src/app.cpp index 5fa0bd4..611744c 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -16,30 +15,16 @@ #include #include -namespace app { +// std::binary_semaphore Application::Semaphores::main{0}; +// std::binary_semaphore Application::Semaphores::display{0}; -const std::string lcd_port = "/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00"; // tis a symlink, can also be just /dev/ttyACM0 -constexpr uint16_t lcd_interval_ms = 500; -constexpr uint8_t lcd_contrast = 215; - -const std::string cpu_pciaddr = "0000:00:18.3"; -const std::string gpu_pciaddr = "0000:0b:00.0"; -const std::string sys_sensor = "nct6775.656"; -const std::string sys_ifname = "enp4s0f0"; -constexpr std::string home_dir = "/home/drath"; - -namespace semaphores { -std::binary_semaphore display{0}; -std::binary_semaphore main{0}; -} // namespace semaphores - -void signalHandler(int) { +void Application::signalHandler(int) { applog(LOG::WRN, "got SIGINT/SIGTERM, exiting..."); - app::semaphores::main.release(); - app::semaphores::display.release(); + this->Semaphores::main.release(); + this->Semaphores::display.release(); } -void hassThread(const std::stop_token& st, app::sensors& s) { +void Application::hassThread(const std::stop_token& st, Sensors& s) { applog(LOG::START, "Thread started"); HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc"); @@ -54,7 +39,7 @@ void hassThread(const std::stop_token& st, app::sensors& s) { } } -void sensorsThread(const std::stop_token& st, app::sensors& s) { +void Application::sensorsThread(const std::stop_token& st, Sensors& s) { applog(LOG::START, "Thread started, poll interval {} ms", lcd_interval_ms); cpu::detectHwmon(&s.cpu, cpu_pciaddr); gpu::detectHwmon(&s.gpu, gpu_pciaddr); @@ -77,12 +62,12 @@ void sensorsThread(const std::stop_token& st, app::sensors& s) { } // log(LOG::DBG, "took {} microseconds", std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count()); - app::semaphores::display.release(); // unlocks display loop + this->Semaphores::display.release(); // unlocks display loop std::this_thread::sleep_until(start + std::chrono::milliseconds(lcd_interval_ms)); } } -void displayThread(const std::stop_token& st, app::sensors& s) { +void Application::displayThread(const std::stop_token& st, Sensors& s) { applog(LOG::START, "Thread started"); // init internal variables of LCDDisplay class LCDDisplay lcd(lcd_port); @@ -103,7 +88,7 @@ void displayThread(const std::stop_token& st, app::sensors& s) { // actual main display loop while (!st.stop_requested()) { - app::semaphores::display.acquire(); // holds the loop until sensor data is ready + this->Semaphores::display.acquire(); // holds the loop until sensor data is ready { std::lock_guard lock(s.mtx); @@ -152,23 +137,26 @@ void displayThread(const std::stop_token& st, app::sensors& s) { lcd.flushBuf(); } -void run() { +Application::Application(const std::string& display_port) { + this->lcd_port = display_port; +} + +void Application::Run() { applog(LOG::START, "Application started"); - std::signal(SIGINT, app::signalHandler); - std::signal(SIGTERM, app::signalHandler); + std::signal(SIGINT, signalHandler); + std::signal(SIGTERM, signalHandler); { - app::sensors Sensors; + Sensors sensors; std::vector threads; - std::function funcs[] = {app::displayThread, app::sensorsThread, app::hassThread}; - for (auto& f : funcs) { - threads.emplace_back(f, std::ref(Sensors)); // params: function, stop_token (implied), pointer to sensor struct + for (std::function funcs[] = {displayThread, sensorsThread, hassThread}; + auto& f : funcs) + { + threads.emplace_back(f, std::ref(sensors)); // params: function, stop_token (implied), pointer to sensor struct std::this_thread::sleep_for(std::chrono::milliseconds(10)); // start threads with delay so displayThread can quit the app if theres no lcd } - app::semaphores::main.acquire(); + this->Semaphores::main.acquire(); } } - -} // namespace app