|
@@ -20,45 +20,56 @@
|
|
|
centralize into event sender and subscriber
|
|
centralize into event sender and subscriber
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
- find a way to eject timer
|
|
|
|
|
- find a way to group timers into an object
|
|
|
|
|
-*/
|
|
|
|
|
-
|
|
|
|
|
-struct mysecondsdisplay {
|
|
|
|
|
- int intervalcount = 10;
|
|
|
|
|
- void myupdatedisplay() {
|
|
|
|
|
- intervalcount -= 1;
|
|
|
|
|
- mvprintw(intervalcount, intervalcount, "hello %d", intervalcount);
|
|
|
|
|
- if (intervalcount < 1) {
|
|
|
|
|
- intervalcount = 10;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-};
|
|
|
|
|
|
|
|
|
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
|
// calls callbackfunc on specified interval based on accumulated deltatime
|
|
|
class timer {
|
|
class timer {
|
|
|
|
|
+private:
|
|
|
|
|
+ double accumulator = 0;
|
|
|
|
|
+ int triggeramount = 0;
|
|
|
|
|
+ double time_interval = 1; // only display every 1 seconds
|
|
|
|
|
+
|
|
|
public:
|
|
public:
|
|
|
std::string name;
|
|
std::string name;
|
|
|
- std::function<void()> callbackfunc;
|
|
|
|
|
|
|
+ int triggercounter = 0;
|
|
|
|
|
+ bool is_infinite = false;
|
|
|
|
|
+ std::function<void(const timer&)> callbackfunc;
|
|
|
|
|
+ bool is_expired = false;
|
|
|
|
|
|
|
|
- timer(std::string name, size_t interval_input, std::function<void()> callback)
|
|
|
|
|
- : name(name), callbackfunc(callback), time_interval(interval_input) {}
|
|
|
|
|
|
|
+ //infinite timer
|
|
|
|
|
+ timer(std::string name_input, size_t interval_input,
|
|
|
|
|
+ std::function<void(const timer &)> callback_input)
|
|
|
|
|
+ : time_interval(interval_input), name(name_input), is_infinite(true),
|
|
|
|
|
+ callbackfunc(callback_input) {}
|
|
|
|
|
+
|
|
|
|
|
+ //limited timer
|
|
|
|
|
+ timer(std::string name, size_t interval_input, int triggeramount,
|
|
|
|
|
+ std::function<void(const timer &)> callback)
|
|
|
|
|
+ : triggeramount(triggeramount), time_interval(interval_input), name(name),
|
|
|
|
|
+ callbackfunc(callback) {}
|
|
|
|
|
|
|
|
// callbackfunc once enough time has accumulated
|
|
// callbackfunc once enough time has accumulated
|
|
|
void updatetime(double DeltaTime) {
|
|
void updatetime(double DeltaTime) {
|
|
|
accumulator += DeltaTime;
|
|
accumulator += DeltaTime;
|
|
|
- if (accumulator >= time_interval) {
|
|
|
|
|
- accumulator -= time_interval;
|
|
|
|
|
- callbackfunc();
|
|
|
|
|
|
|
+ if (!is_expired) {
|
|
|
|
|
+ if (accumulator >= time_interval) {
|
|
|
|
|
+ callbackfunc(*this);
|
|
|
|
|
+ accumulator -= time_interval;
|
|
|
|
|
+ triggercounter++;
|
|
|
|
|
+ if (!is_infinite && triggercounter >= triggeramount) {
|
|
|
|
|
+ is_expired = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-private:
|
|
|
|
|
- double accumulator = 0;
|
|
|
|
|
- double time_interval = 1; // only display every 1 seconds
|
|
|
|
|
|
|
+
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+void myupdatedisplay(const timer &timer_input) {
|
|
|
|
|
+ mvprintw(timer_input.triggercounter, timer_input.triggercounter, "hello %d",
|
|
|
|
|
+ timer_input.triggercounter);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// manage collection of timers
|
|
// manage collection of timers
|
|
|
class timermanager {
|
|
class timermanager {
|
|
|
std::vector<std::unique_ptr<timer>> timers;
|
|
std::vector<std::unique_ptr<timer>> timers;
|
|
@@ -71,7 +82,12 @@ public:
|
|
|
for (auto &mytimer : timers) {
|
|
for (auto &mytimer : timers) {
|
|
|
mytimer->updatetime(dtime);
|
|
mytimer->updatetime(dtime);
|
|
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
|
|
- };
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ timers.erase(std::remove_if(timers.begin(), timers.end(),
|
|
|
|
|
+ [](auto &timer) { return timer->is_expired; }),
|
|
|
|
|
+ timers.end());
|
|
|
|
|
+ // implement delete timers
|
|
|
|
|
+ ;
|
|
|
refresh();
|
|
refresh();
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -82,20 +98,17 @@ int main() {
|
|
|
refresh();
|
|
refresh();
|
|
|
// printf("verification\n");
|
|
// printf("verification\n");
|
|
|
|
|
|
|
|
- auto starttime = std::chrono::system_clock::now(); // initial starttime
|
|
|
|
|
- mysecondsdisplay secdisp;
|
|
|
|
|
- auto clocktimer = std::make_unique<timer>("secondsprinter", 1, [&]() { secdisp.myupdatedisplay(); });
|
|
|
|
|
|
|
+ auto starttime = std::chrono::steady_clock::now(); // initial starttime
|
|
|
|
|
+ auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, myupdatedisplay);
|
|
|
timermanager mytimermanager;
|
|
timermanager mytimermanager;
|
|
|
mytimermanager.addtimer(std::move(clocktimer));
|
|
mytimermanager.addtimer(std::move(clocktimer));
|
|
|
|
|
|
|
|
mvprintw(5, 5, "timer added");
|
|
mvprintw(5, 5, "timer added");
|
|
|
// calculate delta between current and previous time
|
|
// calculate delta between current and previous time
|
|
|
while (true) {
|
|
while (true) {
|
|
|
- auto currenttime = std::chrono::system_clock::now();
|
|
|
|
|
|
|
+ auto currenttime = std::chrono::steady_clock::now();
|
|
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
std::chrono::duration<double> myDeltaTime = currenttime - starttime;
|
|
|
double dtime = myDeltaTime.count();
|
|
double dtime = myDeltaTime.count();
|
|
|
- // mvprintw(11, 11, "dtime: %f", dtime);
|
|
|
|
|
- // clocktimer.updatetime(dtime);
|
|
|
|
|
mytimermanager.updatetimers(dtime);
|
|
mytimermanager.updatetimers(dtime);
|
|
|
|
|
|
|
|
starttime = currenttime;
|
|
starttime = currenttime;
|