add signal handling for clean exit

This commit is contained in:
Drath
2025-05-03 20:52:21 +02:00
parent 22513e9047
commit 7478e4f63d

View File

@@ -2,6 +2,7 @@
#include <array>
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cstdlib>
#include <ctime>
@@ -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<std::string, 4> 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<std::string, 4> 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::microseconds>(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;
}