application class, still broken

This commit is contained in:
2026-01-02 21:01:43 +01:00
parent 870c8a2987
commit 1d9231d88b
3 changed files with 63 additions and 52 deletions

View File

@@ -4,25 +4,47 @@
#include <cpu.hpp>
#include <gpu.hpp>
#include <mutex>
#include <semaphore>
#include <stop_token>
#include <sys.hpp>
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

View File

@@ -1,6 +1,7 @@
#include <app.hpp>
// 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;
}

View File

@@ -8,7 +8,6 @@
#include <hass.hpp>
#include <log.hpp>
#include <mutex>
#include <semaphore>
#include <stop_token>
#include <string>
#include <sys.hpp>
@@ -16,30 +15,16 @@
#include <utils.hpp>
#include <vector>
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::microseconds>(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<std::mutex> 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<std::jthread> threads;
std::function<void(std::stop_token st, app::sensors & s)> 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<void(std::stop_token st, Sensors & s)> 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