diff --git a/include/app.hpp b/include/app.hpp index a6c4568..0912c3c 100644 --- a/include/app.hpp +++ b/include/app.hpp @@ -13,17 +13,23 @@ class Application { public: struct Config { - uint16_t lcd_interval_ms; - uint8_t lcd_contrast; - std::string lcd_port; + struct Lcd { + uint16_t refresh_interval_ms; + uint8_t contrast; + std::string port; + }; + struct HomeAssistant { + std::string url; + std::string entity; + std::string token; + }; + Lcd lcd; + HomeAssistant hass; std::string cpu_pciaddr; std::string gpu_pciaddr; std::string sys_sensor; std::string sys_ifname; std::string home_dir; - std::string hass_url; - std::string hass_entity; - std::string hass_token; }; explicit Application(Config config); diff --git a/main.cpp b/main.cpp index 9d04248..87f4564 100644 --- a/main.cpp +++ b/main.cpp @@ -3,17 +3,17 @@ int main() { Application::Config config; - config.lcd_port = "/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00"; // tis a symlink, can also be just /dev/ttyACM0) - config.lcd_interval_ms = 500; - config.lcd_contrast = 215; + config.lcd.port = "/dev/serial/by-id/usb-Raspberry_Pi_Pico_E6616407E37AA12F-if00"; // tis a symlink, can also be just /dev/ttyACM0) + config.lcd.refresh_interval_ms = 500; + config.lcd.contrast = 215; config.cpu_pciaddr = "0000:00:18.3"; config.gpu_pciaddr = "0000:0b:00.0"; config.sys_sensor = "nct6775.656"; config.sys_ifname = "enp4s0f0"; config.home_dir = "/home/drath"; - config.hass_url = "home.drath.win"; - config.hass_entity = "sensor.gniazdko_komputer_moc"; - config.hass_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI0MzUxMTZiY2VjNTg0MTI3OGI4YmU0NTY4ZGQ5MzIyYiIsImlhdCI6MTc1NzQzMDI0MCwiZXhwIjoyMDcyNzkwMjQwfQ.6wKTlxEA0FLWfv-qW35vewUSvTxdVrZWqFwHT5Khu6E"; + config.hass.url = "home.drath.win"; + config.hass.entity = "sensor.gniazdko_komputer_moc"; + config.hass.token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI0MzUxMTZiY2VjNTg0MTI3OGI4YmU0NTY4ZGQ5MzIyYiIsImlhdCI6MTc1NzQzMDI0MCwiZXhwIjoyMDcyNzkwMjQwfQ.6wKTlxEA0FLWfv-qW35vewUSvTxdVrZWqFwHT5Khu6E"; Application App(config); App.Run(); diff --git a/src/app.cpp b/src/app.cpp index 5ffecd1..a51f561 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -19,9 +19,9 @@ void Application::hassThread(const std::stop_token& st) { applog(LOG::START, "Thread started"); HomeAssistant::Config config; - config.address = m_config.hass_url; - config.entity = m_config.hass_entity; - config.token = m_config.hass_token; + config.address = m_config.hass.url; + config.entity = m_config.hass.entity; + config.token = m_config.hass.token; HomeAssistant pc_power(config); pc_power.connect(); @@ -36,7 +36,7 @@ void Application::hassThread(const std::stop_token& st) { } void Application::sensorsThread(const std::stop_token& st) { - applog(LOG::START, "Thread started, poll interval {} ms", m_config.lcd_interval_ms); + applog(LOG::START, "Thread started, poll interval {} ms", m_config.lcd.refresh_interval_ms); cpu::detectHwmon(&m_sensors.cpu, m_config.cpu_pciaddr); gpu::detectHwmon(&m_sensors.gpu, m_config.gpu_pciaddr); sys::detectHwmon(&m_sensors.sys, m_config.sys_sensor); @@ -59,7 +59,7 @@ void Application::sensorsThread(const std::stop_token& st) { // log(LOG::DBG, "took {} microseconds", std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count()); m_control.display.release(); // unlocks display loop - std::this_thread::sleep_until(start + std::chrono::milliseconds(m_config.lcd_interval_ms)); + std::this_thread::sleep_until(start + std::chrono::milliseconds(m_config.lcd.refresh_interval_ms)); } applog(LOG::INF, "Thread stopped"); } @@ -68,10 +68,10 @@ void Application::displayThread(const std::stop_token& st) { std::stop_callback cb(st, [&] { m_control.display.release(); }); applog(LOG::START, "Thread started"); // init internal variables of LCDDisplay class - LCDDisplay lcd(m_config.lcd_port); + LCDDisplay lcd(m_config.lcd.port); // basic lcd setup - lcd.setContrast(m_config.lcd_contrast); + lcd.setContrast(m_config.lcd.contrast); lcd.setBacklight(true); lcd.setCustomChar(0, {16, 6, 9, 8, 8, 9, 6, 0}); // celcius lcd.setCustomChar(1, {0, 0, 4, 10, 10, 17, 31, 0}); // delta @@ -86,7 +86,7 @@ void Application::displayThread(const std::stop_token& st) { // actual main display loop while (!st.stop_requested()) { - m_control.display.try_acquire_for(std::chrono::milliseconds(static_cast(m_config.lcd_interval_ms * 1.25))); // holds the loop until sensor data is ready, max 1.25x of the loop time + m_control.display.try_acquire_for(std::chrono::milliseconds(static_cast(m_config.lcd.refresh_interval_ms * 1.25))); // holds the loop until sensor data is ready, max 1.25x of the loop time if (st.stop_requested()) break; // needed, otherwise it wont quit correctly { std::lock_guard lock(m_control.sensors);