feat(cmat): Completed Ex51
This commit is contained in:
+36
-24
@@ -1,24 +1,36 @@
|
|||||||
#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();}) {
|
||||||
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
pollingThread.detach();
|
||||||
|
}
|
||||||
void TimerSet::pollAllTimers() {
|
|
||||||
std::time_t timeNow;
|
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
||||||
if (time(&timeNow) > previousTime) {
|
|
||||||
previousTime = timeNow;
|
void TimerSet::pollAllTimers() {
|
||||||
|
std::time_t timeNow;
|
||||||
for (auto it = timers.begin(); it != timers.end(); it++) {
|
if (time(&timeNow) > previousTime) {
|
||||||
(*it)->tick();
|
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;
|
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
@@ -1,29 +1,33 @@
|
|||||||
#ifndef TIMERSET_H
|
#ifndef TIMERSET_H
|
||||||
#define TIMERSET_H
|
#define TIMERSET_H
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <thread>
|
||||||
class Timer;
|
|
||||||
|
class Timer;
|
||||||
class TimerSet {
|
|
||||||
public:
|
class TimerSet {
|
||||||
void pollAllTimers();
|
public:
|
||||||
void addATimer(Timer *);
|
void pollAllTimers();
|
||||||
|
void addATimer(Timer *);
|
||||||
private:
|
|
||||||
TimerSet();
|
private:
|
||||||
std::vector<Timer *> timers;
|
TimerSet();
|
||||||
std::time_t previousTime;
|
std::vector<Timer *> timers;
|
||||||
|
std::time_t previousTime;
|
||||||
// Meyer's singleton
|
std::thread pollingThread;
|
||||||
public:
|
|
||||||
static TimerSet &Instance();
|
// Meyer's singleton
|
||||||
|
public:
|
||||||
private:
|
static TimerSet &Instance();
|
||||||
// prevent copying and assignment of the singleton
|
|
||||||
TimerSet(const TimerSet &);
|
private:
|
||||||
TimerSet &operator=(const TimerSet &);
|
// prevent copying and assignment of the singleton
|
||||||
};
|
TimerSet(const TimerSet &);
|
||||||
|
TimerSet &operator=(const TimerSet &);
|
||||||
#endif
|
|
||||||
|
void poll10Hz();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
+27
-32
@@ -1,32 +1,27 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Keyboard.h"
|
#include "Keyboard.h"
|
||||||
#include "Pelican.h"
|
#include "Pelican.h"
|
||||||
#include "Pole.h"
|
#include "Pole.h"
|
||||||
#include "TimerSet.h"
|
#include "TimerSet.h"
|
||||||
|
|
||||||
#include <cctype> // provides toupper()
|
#include <cctype> // provides toupper()
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Keyboard kbd;
|
Keyboard kbd;
|
||||||
Pelican thePelican;
|
Pelican thePelican;
|
||||||
Pole pole1(20, 0, thePelican);
|
Pole pole1(20, 0, thePelican);
|
||||||
Pole pole2(40, 0, thePelican);
|
Pole pole2(40, 0, thePelican);
|
||||||
|
|
||||||
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
|
||||||
|
c = kbd.getChar(); // get the character
|
||||||
while (!kbd.isCharAvail()) {
|
if (c == ' ') {
|
||||||
TimerSet::Instance().pollAllTimers(); // no key press to process, poll the timers
|
// it's a space, so press the WAIT button
|
||||||
}
|
thePelican.press();
|
||||||
|
}
|
||||||
c = kbd.getChar(); // get the character
|
}
|
||||||
if (c == ' ') {
|
|
||||||
// it's a space, so press the WAIT button
|
return 0;
|
||||||
thePelican.press();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user