rt300@0
|
1 //
|
rt300@0
|
2 // timeController.cpp
|
rt300@0
|
3 // tweakathlon
|
rt300@0
|
4 //
|
rt300@0
|
5 // Created by Robert Tubb on 10/02/2014.
|
rt300@0
|
6 //
|
rt300@0
|
7 //
|
rt300@0
|
8
|
rt300@0
|
9 #include "timeController.h"
|
rt300@0
|
10
|
rt300@0
|
11 TimeController timeController;
|
rt300@0
|
12
|
rt300@0
|
13
|
rt300@0
|
14 //----------------
|
rt300@0
|
15 void TimeController::callAndEraseAllExpired(TimerMicrosec timeNow){
|
rt300@0
|
16
|
rt300@0
|
17 for(auto iter = timesAndCalls.begin(); iter != timesAndCalls.end(); ) {
|
rt300@0
|
18 if ( (*iter).first < timeNow) {
|
rt300@0
|
19 (*iter).second();
|
rt300@0
|
20 timesAndCalls.erase(iter++);
|
rt300@0
|
21 } else {
|
rt300@0
|
22 ++iter;
|
rt300@0
|
23 }
|
rt300@0
|
24 }
|
rt300@0
|
25 };
|
rt300@0
|
26
|
rt300@0
|
27 //----------------
|
rt300@0
|
28 void TimeController::cancelEvent(TimerID which){
|
rt300@0
|
29 //
|
rt300@0
|
30 for(auto it = timesAndCalls.begin(); it != timesAndCalls.end();){
|
rt300@0
|
31 if ((*it).first == which) {
|
rt300@0
|
32 timesAndCalls.erase(it++);
|
rt300@0
|
33 } else {
|
rt300@0
|
34 ++it;
|
rt300@0
|
35 }
|
rt300@0
|
36 }
|
rt300@0
|
37
|
rt300@0
|
38 };
|
rt300@0
|
39 //----------------
|
rt300@0
|
40 TimerID TimeController::scheduleEvent(TimerCallbackFunction cbfunc, int howLongMillisec){
|
rt300@0
|
41
|
rt300@0
|
42 TimerID fireTime = TimerID(getMicrosecTimeNow()+ 1000.0*howLongMillisec);
|
rt300@0
|
43 timesAndCalls.insert( TimeAndCallPair(fireTime, cbfunc));
|
rt300@0
|
44
|
rt300@0
|
45
|
rt300@0
|
46 return fireTime;
|
rt300@0
|
47 };
|
rt300@0
|
48
|
rt300@0
|
49 //----------------
|
rt300@0
|
50 void TimeController::startStopwatch(){
|
rt300@0
|
51 if (stopWatchRunning){
|
rt300@0
|
52 cout << "ERROR stop watch already running" << endl;
|
rt300@0
|
53 }
|
rt300@0
|
54
|
rt300@0
|
55 stopWatchStartTime = TimerMillisec(getMicrosecTimeNow()/1000);
|
rt300@0
|
56 stopWatchRunning = true;
|
rt300@0
|
57 }
|
rt300@0
|
58 //----------------
|
rt300@0
|
59 TimerMillisec TimeController::stopStopwatch(){
|
rt300@0
|
60 if (!stopWatchRunning) return 0;
|
rt300@0
|
61 stopWatchRunning = false;
|
rt300@0
|
62 TimerMillisec elapsedTime = getMicrosecTimeNow()/1000 - stopWatchStartTime;
|
rt300@0
|
63 stopWatchStartTime = 0;
|
rt300@0
|
64 return elapsedTime;
|
rt300@0
|
65 }
|
rt300@0
|
66 //----------------
|
rt300@0
|
67 TimerMillisec TimeController::getStopwatchElapsedTime(){
|
rt300@0
|
68 if (!stopWatchRunning) return 0;
|
rt300@0
|
69 TimerMillisec elapsedTime = getMicrosecTimeNow()/1000 - stopWatchStartTime;
|
rt300@0
|
70 return elapsedTime;
|
rt300@0
|
71 }
|
rt300@0
|
72 //---------------- |