From c2f71d83e7835ae342e9dcc9a6c472f3085fb944 Mon Sep 17 00:00:00 2001 From: Drath Date: Sun, 14 Sep 2025 00:07:29 +0200 Subject: [PATCH] wrap globals in namespace, avoid globals tbh --- main.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 4cd6a7c..381cf3d 100644 --- a/main.cpp +++ b/main.cpp @@ -24,8 +24,11 @@ #define LCD_CONTRAST 215 /* these need to be global since we're talking to them from sighandler */ -std::binary_semaphore sDisp{0}; // halts display func until we have sensor data -std::binary_semaphore sMain{0}; // halts the main thread until SIGINT + +namespace sem { +std::binary_semaphore display{0}; +std::binary_semaphore main{0}; +} // namespace sem struct Sensors { std::array td; @@ -40,8 +43,8 @@ struct Sensors { void sighandler(int s) { std::println("[Signal] got SIGINT/SIGTERM, exiting..."); - sMain.release(); - sDisp.release(); + sem::main.release(); + sem::display.release(); } void hassThread(std::stop_token st, Sensors* s) { @@ -78,7 +81,7 @@ void sensorsThread(std::stop_token st, Sensors* s) { } // std::println("[Sensors] took {} microseconds", std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count()); - sDisp.release(); // unlocks display loop + sem::display.release(); // unlocks display loop std::this_thread::sleep_until(start + std::chrono::milliseconds(LCD_INTERVAL_MS)); } } @@ -92,7 +95,7 @@ void displayThread(std::stop_token st, Sensors* s) { lcd.setCustomChar(0, {16, 6, 9, 8, 8, 9, 6, 0}); // celcius while (!st.stop_requested()) { - sDisp.acquire(); // holds the loop until sensor data is ready + sem::display.acquire(); // holds the loop until sensor data is ready { std::lock_guard lock(s->mtx); @@ -150,7 +153,7 @@ int main(int argc, char* argv[]) { for (auto& f : funcs) threads.emplace_back(f, pSensors); - sMain.acquire(); + sem::main.acquire(); } delete pSensors;