wrap globals in namespace, avoid globals tbh
This commit is contained in:
17
main.cpp
17
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<std::string, 2> 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::microseconds>(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<std::mutex> 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;
|
||||
|
||||
Reference in New Issue
Block a user