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
+36 -24
View File
@@ -1,24 +1,36 @@
#include "TimerSet.h"
#include "Timer.h"
#include <iostream>
TimerSet::TimerSet() : previousTime(time(NULL)) {}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
void TimerSet::pollAllTimers() {
std::time_t timeNow;
if (time(&timeNow) > previousTime) {
previousTime = timeNow;
for (auto it = timers.begin(); it != timers.end(); it++) {
(*it)->tick();
}
}
}
TimerSet &TimerSet::Instance() {
// Meyer's Singleton implementation
static TimerSet pInstance;
return pInstance;
}
#include "TimerSet.h"
#include "Timer.h"
#include <iostream>
#include <thread>
TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
pollingThread.detach();
}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
void TimerSet::pollAllTimers() {
std::time_t timeNow;
if (time(&timeNow) > previousTime) {
previousTime = timeNow;
for (auto it = timers.begin(); it != timers.end(); it++) {
(*it)->tick();
}
}
}
TimerSet &TimerSet::Instance() {
// Meyer's Singleton implementation
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);
}
}
+33 -29
View File
@@ -1,29 +1,33 @@
#ifndef TIMERSET_H
#define TIMERSET_H
#include <ctime>
#include <vector>
class Timer;
class TimerSet {
public:
void pollAllTimers();
void addATimer(Timer *);
private:
TimerSet();
std::vector<Timer *> timers;
std::time_t previousTime;
// Meyer's singleton
public:
static TimerSet &Instance();
private:
// prevent copying and assignment of the singleton
TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &);
};
#endif
#ifndef TIMERSET_H
#define TIMERSET_H
#include <ctime>
#include <vector>
#include <thread>
class Timer;
class TimerSet {
public:
void pollAllTimers();
void addATimer(Timer *);
private:
TimerSet();
std::vector<Timer *> timers;
std::time_t previousTime;
std::thread pollingThread;
// Meyer's singleton
public:
static TimerSet &Instance();
private:
// prevent copying and assignment of the singleton
TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &);
void poll10Hz();
};
#endif
+27 -32
View File
@@ -1,32 +1,27 @@
#include "Keyboard.h"
#include "Pelican.h"
#include "Pole.h"
#include "TimerSet.h"
#include <cctype> // provides toupper()
int main() {
Keyboard kbd;
Pelican thePelican;
Pole pole1(20, 0, thePelican);
Pole pole2(40, 0, thePelican);
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
thePelican.press();
}
}
return 0;
}
#include "Keyboard.h"
#include "Pelican.h"
#include "Pole.h"
#include "TimerSet.h"
#include <cctype> // provides toupper()
int main() {
Keyboard kbd;
Pelican thePelican;
Pole pole1(20, 0, thePelican);
Pole pole2(40, 0, thePelican);
char c = 0;
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
c = kbd.getChar(); // get the character
if (c == ' ') {
// it's a space, so press the WAIT button
thePelican.press();
}
}
return 0;
}