feat(cmat): Completed Ex51
This commit is contained in:
+13
-1
@@ -1,8 +1,11 @@
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#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); }
|
||||
|
||||
@@ -22,3 +25,12 @@ TimerSet &TimerSet::Instance() {
|
||||
static TimerSet pInstance;
|
||||
return pInstance;
|
||||
}
|
||||
|
||||
void TimerSet::poll10Hz() {
|
||||
using namespace std::chrono_literals;
|
||||
while (true) {
|
||||
auto const pause = 100ms;
|
||||
pollAllTimers();
|
||||
std::this_thread::sleep_for(pause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
class Timer;
|
||||
|
||||
@@ -15,6 +16,7 @@ class TimerSet {
|
||||
TimerSet();
|
||||
std::vector<Timer *> timers;
|
||||
std::time_t previousTime;
|
||||
std::thread pollingThread;
|
||||
|
||||
// Meyer's singleton
|
||||
public:
|
||||
@@ -24,6 +26,8 @@ class TimerSet {
|
||||
// prevent copying and assignment of the singleton
|
||||
TimerSet(const TimerSet &);
|
||||
TimerSet &operator=(const TimerSet &);
|
||||
|
||||
void poll10Hz();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -16,11 +16,6 @@ int main() {
|
||||
char c = 0;
|
||||
|
||||
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
|
||||
if (c == ' ') {
|
||||
// it's a space, so press the WAIT button
|
||||
|
||||
Reference in New Issue
Block a user