Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Timer.cpp
Go to the documentation of this file.
1 #include "../include/Timer.hpp"
2 #include "../include/Display.hpp"
3 
4 using namespace std;
5 
6 /* =========================================================
7 CONSTRUCTOR
8 ========================================================= */
9 
19 Timer::Timer(int hours, int minutes, int seconds) {
20  int milli = 3600000 * hours + 60000 * minutes + 1000 * seconds;
21  if (milli >= 360000000) { milli = 359999999; }
22  _countdownMilliseconds = milli;
23  _remainingMilliseconds = milli;
24  _startMilliseconds = milli;
25  _incrementMilliseconds = 0;
26  start(_countdownMilliseconds);
27 }
28 
29 /* =========================================================
30 START THE TIMER
31 ========================================================= */
32 
38 void Timer::start(int countdownMilliseconds) {
39  _running = true;
40  _endTime = chrono::steady_clock::now() + chrono::milliseconds(_countdownMilliseconds);
41 }
42 
43 /* =========================================================
44 PAUSE THE TIMER
45 ========================================================= */
46 
50 void Timer::pause() {
51  if (_running) {
52  _pauseTime = chrono::steady_clock::now();
53  _remainingMilliseconds = chrono::duration_cast<chrono::milliseconds>(_endTime - _pauseTime).count();
54  _running = false;
55  }
56 }
57 
58 /* =========================================================
59 RESUME THE TIMER
60 ========================================================= */
61 
65 void Timer::resume() {
66  if (!_running) {
67  _endTime = chrono::steady_clock::now() + chrono::milliseconds(_remainingMilliseconds);
68  _running = true;
69  }
70 }
71 /* =========================================================
72 RESET THE TIMER
73 ========================================================= */
74 
78 void Timer::reset() {
79  pause();
80  _remainingMilliseconds = _startMilliseconds;
81  _countdownMilliseconds = _startMilliseconds;
82 }
83 
84 /* =========================================================
85 ADD TIME TO THE TIMER
86 ========================================================= */
87 
95 void Timer::addTime(int seconds) {
96  bool wasRunning = _running;
97  pause();
98 
99  _remainingMilliseconds += 1000 * seconds;
100  _countdownMilliseconds += 1000 * seconds;
101 
102  if(wasRunning){
103  resume();
104  }
105 }
106 
107 /* =========================================================
108 GET REMAINING TIME IN MILLISECONDS
109 ========================================================= */
110 
119  if (_running) {
120  auto now = chrono::steady_clock::now();
121  auto remaining = chrono::duration_cast<chrono::milliseconds>(_endTime - now).count();
122  if (remaining > 0) {
123  return remaining;
124  } else {
125  _running = false;
126  _remainingMilliseconds = 0;
127  return 0;
128  }
129  } else {
130  return _remainingMilliseconds;
131  }
132 }
133 
134 /* =========================================================
135 CHECK IF TIMER IS RUNNING
136 ========================================================= */
137 
144  return _running;
145 }
146 
148  return !_running && !remainingMilliseconds();
149 }
150 
151 /* =========================================================
152 GET PERCENTAGE OF TIME ELAPSED
153 ========================================================= */
154 
161  int start = 0;
162  if (_remainingMilliseconds > _startMilliseconds) {
163  start = _remainingMilliseconds;
164  } else {
165  start = _startMilliseconds;
166  }
167  if (start == 0) {
168  return 100.0;
169  }
170 
171  int elapsedMilliseconds = start - remainingMilliseconds();
172  double percentage = ((double)(elapsedMilliseconds) / start) * 100;
173  return percentage;
174 }
bool isDone()
Definition: Timer.cpp:147
double percentElapsed()
Returns the percentage of time that has elapsed since the timer started.
Definition: Timer.cpp:160
void resume()
Resumes the timer from where it was paused by recalculating the end time.
Definition: Timer.cpp:65
int remainingMilliseconds()
Returns the remaining time in milliseconds.
Definition: Timer.cpp:118
STL namespace.
void pause()
Pauses the timer and calculates the remaining milliseconds.
Definition: Timer.cpp:50
Timer(int hours, int minutes, int seconds)
Constructor to specify the timer duration directly in hours, minutes, and seconds.
Definition: Timer.cpp:19
void addTime(int seconds)
Adds a specified amount of time (in seconds) to the timer.
Definition: Timer.cpp:95
void start(int countdownMilliseconds)
Starts the timer by setting the end time based on the current time plus the countdown duration...
Definition: Timer.cpp:38
bool isRunning()
Checks if the timer is currently running.
Definition: Timer.cpp:143
void reset()
Resets the timer to its original duration and pauses it.
Definition: Timer.cpp:78