feat(cmat): Completed Ex51

This commit is contained in:
2026-10-07 15:13:55 +02:00
parent a65e84e796
commit 058179b643
3 changed files with 96 additions and 85 deletions
+13 -1
View File
@@ -1,8 +1,11 @@
#include "TimerSet.h" #include "TimerSet.h"
#include "Timer.h" #include "Timer.h"
#include <iostream> #include <iostream>
#include <thread>
TimerSet::TimerSet() : previousTime(time(NULL)) {} TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
pollingThread.detach();
}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); } void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
@@ -22,3 +25,12 @@ TimerSet &TimerSet::Instance() {
static TimerSet pInstance; static TimerSet pInstance;
return pInstance; return pInstance;
} }
void TimerSet::poll10Hz() {
using namespace std::chrono_literals;
while (true) {
auto const pause = 100ms;
pollAllTimers();
std::this_thread::sleep_for(pause);
}
}
+4
View File
@@ -3,6 +3,7 @@
#include <ctime> #include <ctime>
#include <vector> #include <vector>
#include <thread>
class Timer; class Timer;
@@ -15,6 +16,7 @@ class TimerSet {
TimerSet(); TimerSet();
std::vector<Timer *> timers; std::vector<Timer *> timers;
std::time_t previousTime; std::time_t previousTime;
std::thread pollingThread;
// Meyer's singleton // Meyer's singleton
public: public:
@@ -24,6 +26,8 @@ class TimerSet {
// prevent copying and assignment of the singleton // prevent copying and assignment of the singleton
TimerSet(const TimerSet &); TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &); TimerSet &operator=(const TimerSet &);
void poll10Hz();
}; };
#endif #endif
-5
View File
@@ -16,11 +16,6 @@ int main() {
char c = 0; char c = 0;
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
while (!kbd.isCharAvail()) {
TimerSet::Instance().pollAllTimers(); // no key press to process, poll the timers
}
c = kbd.getChar(); // get the character c = kbd.getChar(); // get the character
if (c == ' ') { if (c == ' ') {
// it's a space, so press the WAIT button // it's a space, so press the WAIT button