From 7478e4f63deed9316f7bc65309d82862baf5af2c Mon Sep 17 00:00:00 2001 From: Drath Date: Sat, 3 May 2025 20:52:21 +0200 Subject: [PATCH] add signal handling for clean exit --- main.cpp | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/main.cpp b/main.cpp index 2cc9232..7447474 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -17,7 +18,7 @@ #include "include/utils.hpp" #define LCD_PORT "/dev/ttyACM0" -#define LCD_INTERVAL_MS 1000 +#define LCD_INTERVAL_MS 500 #define LCD_CONTRAST 215 #define FILL_CHAR ' ' @@ -27,6 +28,8 @@ struct mem_usage_t { uint64_t total; }; +bool daemon_mode = false; + /* DISPLAY FUNCS */ std::string pBytes(double i, bool isKbytes = false) { std::stringstream ss; @@ -141,42 +144,45 @@ auto getCpuTemp(std::string hw) { return cpu; } +void sighandler(int s) { + daemon_mode = false; + std::cout << "[Signal] deamon_mode = false, exiting on next loop" << std::endl; +} + int main(int argc, char *argv[]) { - bool daemon_mode = (argc > 1 && std::string(argv[1]) == "-d"); + signal(SIGINT, sighandler); + daemon_mode = (argc > 1 && std::string(argv[1]) == "-d"); /* init lcd and chars, set contrast and backlight */ LCDDisplay lcd(LCD_PORT, 0010002); + std::array lines; lcd.setCustomChar(0, {16, 6, 9, 8, 8, 9, 6, 0}); // celcius - /* only set in daemon mode, so we dont burn through eeprom writes if running with watch -n 1 when testing */ - if (daemon_mode) { - lcd.setContrast(LCD_CONTRAST); - lcd.setBacklight(true); - } - - /* initial vars */ - std::array lines; + /* find correct cpu module */ auto cpu_hwmon = findHwmonPath("k10temp"); if (cpu_hwmon.size() == 0) { std::cout << "[Hwmon] k10temp not found, trying zenpower" << std::endl; cpu_hwmon = findHwmonPath("zenpower"); } + /* figure out gpu path */ std::string gpu_hwmon; GpuMetricsHeader_t gpu_mtr_rev; - for(uint8_t i = 0; i < 10; i++) { - gpu_hwmon = "/sys/class/drm/card" + std::to_string(i) + "/device"; + for (uint8_t c = 0; c < 10; c++) { + gpu_hwmon = "/sys/class/drm/card" + std::to_string(c) + "/device"; gpu_mtr_rev = getGpuMetricsRev(gpu_hwmon + "/gpu_metrics"); - if(gpu_mtr_rev.structure_size > 0) + if (gpu_mtr_rev.structure_size > 0) break; } - - /* loop setup */ - std::cout << "[Chrono] Running in daemon mode, " << LCD_INTERVAL_MS << "ms interval" << std::endl; std::chrono::milliseconds interval(LCD_INTERVAL_MS); auto start = std::chrono::steady_clock::now(); + if (daemon_mode) { + std::cout << "[Chrono] Running in daemon mode, " << LCD_INTERVAL_MS << "ms interval" << std::endl; + lcd.setContrast(LCD_CONTRAST); + lcd.setBacklight(true); + } /* main loop */ while (true) { @@ -212,9 +218,11 @@ int main(int argc, char *argv[]) { std::this_thread::sleep_until(start + interval); start += interval; } else { - break; + std::cout << "[Chrono] took " << std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count() / 1000.0 << "ms" << std::endl; + return 0; } } - return 0; + /* should exit in loop, if it doesnt something went wrong */ + return 1; }