Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Public Member Functions | Private Attributes
Timer Class Reference

The Timer class provides a countdown timer with start, pause, resume, reset, and time adjustment functionalities. More...

#include <Timer.hpp>

Collaboration diagram for Timer:
Collaboration graph
[legend]

Public Member Functions

 Timer (int hours, int minutes, int seconds)
 Constructor to specify the timer duration directly in hours, minutes, and seconds. More...
 
void start (int countdownMilliseconds)
 Starts the timer by setting the end time based on the current time plus the countdown duration. More...
 
void pause ()
 Pauses the timer and calculates the remaining milliseconds. More...
 
void resume ()
 Resumes the timer from where it was paused by recalculating the end time. More...
 
void reset ()
 Resets the timer to its original duration and pauses it. More...
 
void addTime (int seconds)
 Adds a specified amount of time (in seconds) to the timer. More...
 
bool isRunning ()
 Checks if the timer is currently running. More...
 
bool isDone ()
 
int remainingMilliseconds ()
 Returns the remaining time in milliseconds. More...
 
double percentElapsed ()
 Returns the percentage of time that has elapsed since the timer started. More...
 

Private Attributes

int _startMilliseconds
 The initial duration of the timer in milliseconds. More...
 
int _countdownMilliseconds
 The duration of the countdown in milliseconds. More...
 
int _remainingMilliseconds
 The remaining time in milliseconds. More...
 
int _incrementMilliseconds
 The increment time in milliseconds for adding time. More...
 
bool _running
 Indicates whether the timer is currently running. More...
 
chrono::time_point< chrono::steady_clock > _endTime
 The time point when the timer will end. More...
 
chrono::time_point< chrono::steady_clock > _pauseTime
 The time point when the timer was paused. More...
 

Detailed Description

The Timer class provides a countdown timer with start, pause, resume, reset, and time adjustment functionalities.

Definition at line 12 of file Timer.hpp.

Constructor & Destructor Documentation

◆ Timer()

Timer::Timer ( int  hours,
int  minutes,
int  seconds 
)

Constructor to specify the timer duration directly in hours, minutes, and seconds.

Initializes and starts the timer with the specified duration.

Parameters
hoursThe number of hours to set.
minutesThe number of minutes to set.
secondsThe number of seconds to set.

Definition at line 19 of file Timer.cpp.

19  {
20  int milli = 3600000 * hours + 60000 * minutes + 1000 * seconds;
21  if (milli >= 360000000) { milli = 359999999; }
22  _countdownMilliseconds = milli;
23  _remainingMilliseconds = milli;
24  _startMilliseconds = milli;
27 }
int _countdownMilliseconds
The duration of the countdown in milliseconds.
Definition: Timer.hpp:84
int _startMilliseconds
The initial duration of the timer in milliseconds.
Definition: Timer.hpp:83
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85
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
int _incrementMilliseconds
The increment time in milliseconds for adding time.
Definition: Timer.hpp:86

Member Function Documentation

◆ addTime()

void Timer::addTime ( int  seconds)

Adds a specified amount of time (in seconds) to the timer.

If the timer is running, it resumes after adding time.

Parameters
secondsThe number of seconds to add to the timer.

Definition at line 95 of file Timer.cpp.

95  {
96  bool wasRunning = _running;
97  pause();
98 
99  _remainingMilliseconds += 1000 * seconds;
100  _countdownMilliseconds += 1000 * seconds;
101 
102  if(wasRunning){
103  resume();
104  }
105 }
int _countdownMilliseconds
The duration of the countdown in milliseconds.
Definition: Timer.hpp:84
void resume()
Resumes the timer from where it was paused by recalculating the end time.
Definition: Timer.cpp:65
void pause()
Pauses the timer and calculates the remaining milliseconds.
Definition: Timer.cpp:50
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85

◆ isDone()

bool Timer::isDone ( )

Definition at line 147 of file Timer.cpp.

Referenced by Display::tickTimer().

147  {
148  return !_running && !remainingMilliseconds();
149 }
int remainingMilliseconds()
Returns the remaining time in milliseconds.
Definition: Timer.cpp:118
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
Here is the caller graph for this function:

◆ isRunning()

bool Timer::isRunning ( )

Checks if the timer is currently running.

Returns
bool True if the timer is running, false otherwise.

Definition at line 143 of file Timer.cpp.

Referenced by Menu::checkTimerInput(), and Display::tickTimer().

143  {
144  return _running;
145 }
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
Here is the caller graph for this function:

◆ pause()

void Timer::pause ( )

Pauses the timer and calculates the remaining milliseconds.

Definition at line 50 of file Timer.cpp.

Referenced by Menu::checkTimerInput().

50  {
51  if (_running) {
52  _pauseTime = chrono::steady_clock::now();
53  _remainingMilliseconds = chrono::duration_cast<chrono::milliseconds>(_endTime - _pauseTime).count();
54  _running = false;
55  }
56 }
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85
chrono::time_point< chrono::steady_clock > _pauseTime
The time point when the timer was paused.
Definition: Timer.hpp:91
chrono::time_point< chrono::steady_clock > _endTime
The time point when the timer will end.
Definition: Timer.hpp:90
Here is the caller graph for this function:

◆ percentElapsed()

double Timer::percentElapsed ( )

Returns the percentage of time that has elapsed since the timer started.

Returns
double The percentage of time elapsed.

Definition at line 160 of file Timer.cpp.

Referenced by Display::tickTimer().

160  {
161  int start = 0;
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 }
int remainingMilliseconds()
Returns the remaining time in milliseconds.
Definition: Timer.cpp:118
int _startMilliseconds
The initial duration of the timer in milliseconds.
Definition: Timer.hpp:83
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85
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
Here is the caller graph for this function:

◆ remainingMilliseconds()

int Timer::remainingMilliseconds ( )

Returns the remaining time in milliseconds.

If the timer has expired, it returns 0.

Returns
int The remaining time in milliseconds.

Definition at line 118 of file Timer.cpp.

Referenced by Menu::checkTimerInput(), Display::tickTimer(), and Menu::timerSequence().

118  {
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;
127  return 0;
128  }
129  } else {
130  return _remainingMilliseconds;
131  }
132 }
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85
chrono::time_point< chrono::steady_clock > _endTime
The time point when the timer will end.
Definition: Timer.hpp:90
Here is the caller graph for this function:

◆ reset()

void Timer::reset ( )

Resets the timer to its original duration and pauses it.

Definition at line 78 of file Timer.cpp.

Referenced by Menu::checkTimerInput().

78  {
79  pause();
82 }
int _countdownMilliseconds
The duration of the countdown in milliseconds.
Definition: Timer.hpp:84
int _startMilliseconds
The initial duration of the timer in milliseconds.
Definition: Timer.hpp:83
void pause()
Pauses the timer and calculates the remaining milliseconds.
Definition: Timer.cpp:50
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85
Here is the caller graph for this function:

◆ resume()

void Timer::resume ( )

Resumes the timer from where it was paused by recalculating the end time.

Definition at line 65 of file Timer.cpp.

Referenced by Menu::checkTimerInput().

65  {
66  if (!_running) {
67  _endTime = chrono::steady_clock::now() + chrono::milliseconds(_remainingMilliseconds);
68  _running = true;
69  }
70 }
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
int _remainingMilliseconds
The remaining time in milliseconds.
Definition: Timer.hpp:85
chrono::time_point< chrono::steady_clock > _endTime
The time point when the timer will end.
Definition: Timer.hpp:90
Here is the caller graph for this function:

◆ start()

void Timer::start ( int  countdownMilliseconds)

Starts the timer by setting the end time based on the current time plus the countdown duration.

Parameters
countdownMillisecondsThe duration for the countdown in milliseconds.

Definition at line 38 of file Timer.cpp.

38  {
39  _running = true;
40  _endTime = chrono::steady_clock::now() + chrono::milliseconds(_countdownMilliseconds);
41 }
int _countdownMilliseconds
The duration of the countdown in milliseconds.
Definition: Timer.hpp:84
bool _running
Indicates whether the timer is currently running.
Definition: Timer.hpp:88
chrono::time_point< chrono::steady_clock > _endTime
The time point when the timer will end.
Definition: Timer.hpp:90

Field Documentation

◆ _countdownMilliseconds

int Timer::_countdownMilliseconds
private

The duration of the countdown in milliseconds.

Definition at line 84 of file Timer.hpp.

◆ _endTime

chrono::time_point<chrono::steady_clock> Timer::_endTime
private

The time point when the timer will end.

Definition at line 90 of file Timer.hpp.

◆ _incrementMilliseconds

int Timer::_incrementMilliseconds
private

The increment time in milliseconds for adding time.

Definition at line 86 of file Timer.hpp.

◆ _pauseTime

chrono::time_point<chrono::steady_clock> Timer::_pauseTime
private

The time point when the timer was paused.

Definition at line 91 of file Timer.hpp.

◆ _remainingMilliseconds

int Timer::_remainingMilliseconds
private

The remaining time in milliseconds.

Definition at line 85 of file Timer.hpp.

◆ _running

bool Timer::_running
private

Indicates whether the timer is currently running.

Definition at line 88 of file Timer.hpp.

◆ _startMilliseconds

int Timer::_startMilliseconds
private

The initial duration of the timer in milliseconds.

Definition at line 83 of file Timer.hpp.


The documentation for this class was generated from the following files: