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

A class to manage alarms with time-based triggers. More...

#include <Alarm.hpp>

Collaboration diagram for Alarm:
Collaboration graph
[legend]

Public Member Functions

 Alarm (int h, int m)
 Constructor for the Alarm object. More...
 
chrono::time_point< chrono::system_clock > getTime (int h, int m)
 Creates the nearest future time point with hours h and minutes m. More...
 
string timeToString (chrono::time_point< chrono::system_clock > t)
 Converts a given time point to a string format (HH:MM). More...
 
chrono::time_point< chrono::system_clock > getStartTime ()
 Returns the alarm's start time. More...
 
chrono::time_point< chrono::system_clock > getEndTime ()
 Returns the alarm's end time. More...
 
void start ()
 
bool isRunning ()
 Checks if the alarm is currently running. More...
 
bool isDone ()
 Checks if the alarm has finished. More...
 
int remainingMilliseconds ()
 Calculates the remaining time in milliseconds until the alarm ends. More...
 
double percentElapsed ()
 Calculates the percentage of time that has elapsed from start to end. More...
 

Static Public Member Functions

static chrono::time_point< chrono::system_clock > currentTime ()
 Returns the current system time. More...
 

Private Attributes

int _remainingMilliseconds
 
bool _running
 
chrono::time_point< chrono::system_clock > _startTime
 
chrono::time_point< chrono::system_clock > _endTime
 

Detailed Description

A class to manage alarms with time-based triggers.

The Alarm class allows setting up an alarm for a future time, checking the progress of time, and determining when the alarm has finished.

Definition at line 13 of file Alarm.hpp.

Constructor & Destructor Documentation

◆ Alarm()

Alarm::Alarm ( int  h,
int  m 
)

Constructor for the Alarm object.

Initializes the alarm with the specified hours and minutes. If the current time is past the set time, it schedules the alarm for the next day.

Parameters
hHour value for the new alarm (0-23).
mMinute value for the new alarm (0-59).

Definition at line 18 of file Alarm.cpp.

References _endTime, _remainingMilliseconds, _running, _startTime, currentTime(), and getTime().

18  {
20  _endTime = getTime(h, m);
22  if(_startTime >= _endTime){
23  _endTime = getTime(h+24, m);
24  }
25  _running = true;
26 }
chrono::time_point< chrono::system_clock > _startTime
Definition: Alarm.hpp:41
chrono::time_point< chrono::system_clock > getTime(int h, int m)
Creates the nearest future time point with hours h and minutes m.
Definition: Alarm.cpp:38
bool _running
Definition: Alarm.hpp:39
chrono::time_point< chrono::system_clock > _endTime
Definition: Alarm.hpp:42
static chrono::time_point< chrono::system_clock > currentTime()
Returns the current system time.
Definition: Alarm.cpp:159
int _remainingMilliseconds
Definition: Alarm.hpp:37
Here is the call graph for this function:

Member Function Documentation

◆ currentTime()

chrono::time_point< chrono::system_clock > Alarm::currentTime ( )
static

Returns the current system time.

Returns
A time point representing the current time.

Definition at line 159 of file Alarm.cpp.

Referenced by Alarm(), getTime(), percentElapsed(), remainingMilliseconds(), and timeToString().

159  {
160  return chrono::system_clock::now();
161 }
Here is the caller graph for this function:

◆ getEndTime()

chrono::time_point< chrono::system_clock > Alarm::getEndTime ( )

Returns the alarm's end time.

Returns
Time point representing the end time of the alarm.

Definition at line 123 of file Alarm.cpp.

References _endTime.

Referenced by Display::tickAlarm().

123  {
124  return _endTime;
125 }
chrono::time_point< chrono::system_clock > _endTime
Definition: Alarm.hpp:42
Here is the caller graph for this function:

◆ getStartTime()

chrono::time_point< chrono::system_clock > Alarm::getStartTime ( )

Returns the alarm's start time.

Returns
Time point representing the start time of the alarm.

Definition at line 114 of file Alarm.cpp.

References _startTime.

114  {
115  return _startTime;
116 }
chrono::time_point< chrono::system_clock > _startTime
Definition: Alarm.hpp:41

◆ getTime()

chrono::time_point< chrono::system_clock > Alarm::getTime ( int  h,
int  m 
)

Creates the nearest future time point with hours h and minutes m.

If the specified time has already passed today, this method sets the alarm for the same time on the next day.

Parameters
hHour value for the new alarm (0-23).
mMinute value for the new alarm (0-59).
Returns
Time point representing the future alarm time.

Definition at line 38 of file Alarm.cpp.

References currentTime().

Referenced by Alarm().

38  {
39 
40  auto now = currentTime();
41 
42  time_t currentTime = chrono::system_clock::to_time_t(now);
43  tm* localTime = localtime(&currentTime);
44 
45  localTime->tm_hour = h;
46  localTime->tm_min = m;
47  localTime->tm_sec = 0;
48 
49  auto futureTime = chrono::system_clock::from_time_t(mktime(localTime));
50  return futureTime;
51 }
static chrono::time_point< chrono::system_clock > currentTime()
Returns the current system time.
Definition: Alarm.cpp:159
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDone()

bool Alarm::isDone ( )

Checks if the alarm has finished.

If the remaining milliseconds are less than or equal to 0, the alarm is considered done.

Returns
True if the alarm has completed, false otherwise.

Definition at line 134 of file Alarm.cpp.

References _remainingMilliseconds, _running, and remainingMilliseconds().

Referenced by Menu::alarmSequence(), and Display::tickAlarm().

134  {
135  int remaining = remainingMilliseconds();
136  if (remaining > 0) {
137  return false;
138  } else {
139  _running = false;
141  return true;
142  }
143 }
bool _running
Definition: Alarm.hpp:39
int remainingMilliseconds()
Calculates the remaining time in milliseconds until the alarm ends.
Definition: Alarm.cpp:102
int _remainingMilliseconds
Definition: Alarm.hpp:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isRunning()

bool Alarm::isRunning ( )

Checks if the alarm is currently running.

Returns
True if the alarm is running, false otherwise.

Definition at line 150 of file Alarm.cpp.

References _running.

150  {
151  return _running;
152 }
bool _running
Definition: Alarm.hpp:39

◆ percentElapsed()

double Alarm::percentElapsed ( )

Calculates the percentage of time that has elapsed from start to end.

This method calculates the percentage of time passed relative to the alarm's start and end times.

Returns
The percentage of time elapsed as a double.

Definition at line 78 of file Alarm.cpp.

References _endTime, _startTime, and currentTime().

Referenced by Display::tickAlarm().

78  {
79 
80  auto totalMilliseconds = chrono::duration_cast<chrono::milliseconds>(_endTime - _startTime).count();
81 
82  if (totalMilliseconds == 0) {
83  return 100.0;
84  }
85 
86  auto now = currentTime();
87  auto elapsedMilliseconds = chrono::duration_cast<chrono::milliseconds>(now - _startTime).count();
88 
89  if (elapsedMilliseconds >= totalMilliseconds) {
90  return 100.0;
91  }
92 
93  double percentage = (static_cast<double>(elapsedMilliseconds) / totalMilliseconds) * 100.0;
94  return percentage;
95 }
chrono::time_point< chrono::system_clock > _startTime
Definition: Alarm.hpp:41
chrono::time_point< chrono::system_clock > _endTime
Definition: Alarm.hpp:42
static chrono::time_point< chrono::system_clock > currentTime()
Returns the current system time.
Definition: Alarm.cpp:159
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remainingMilliseconds()

int Alarm::remainingMilliseconds ( )

Calculates the remaining time in milliseconds until the alarm ends.

Returns
Remaining milliseconds until the alarm ends.

Definition at line 102 of file Alarm.cpp.

References _endTime, and currentTime().

Referenced by isDone().

102  {
103 
104  auto now = currentTime();
105  auto ms = chrono::duration_cast<chrono::milliseconds>(_endTime - now).count();
106  return ms;
107 }
chrono::time_point< chrono::system_clock > _endTime
Definition: Alarm.hpp:42
static chrono::time_point< chrono::system_clock > currentTime()
Returns the current system time.
Definition: Alarm.cpp:159
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start()

void Alarm::start ( )

◆ timeToString()

string Alarm::timeToString ( chrono::time_point< chrono::system_clock >  t)

Converts a given time point to a string format (HH:MM).

Parameters
tTime point to convert to string.
Returns
A string representing the time in HH:MM format.

Definition at line 59 of file Alarm.cpp.

References currentTime().

Referenced by Display::tickAlarm().

59  {
60 
61  time_t currentTime = chrono::system_clock::to_time_t(t);
62  tm* localTime = localtime(&currentTime);
63 
64  stringstream ss;
65  ss << setw(2) << setfill('0') << localTime->tm_hour << ":"
66  << setw(2) << setfill('0') << localTime->tm_min;
67 
68  return ss.str();
69 }
static chrono::time_point< chrono::system_clock > currentTime()
Returns the current system time.
Definition: Alarm.cpp:159
Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ _endTime

chrono::time_point<chrono::system_clock> Alarm::_endTime
private

Definition at line 42 of file Alarm.hpp.

Referenced by Alarm(), getEndTime(), percentElapsed(), and remainingMilliseconds().

◆ _remainingMilliseconds

int Alarm::_remainingMilliseconds
private

Definition at line 37 of file Alarm.hpp.

Referenced by Alarm(), and isDone().

◆ _running

bool Alarm::_running
private

Definition at line 39 of file Alarm.hpp.

Referenced by Alarm(), isDone(), and isRunning().

◆ _startTime

chrono::time_point<chrono::system_clock> Alarm::_startTime
private

Definition at line 41 of file Alarm.hpp.

Referenced by Alarm(), getStartTime(), and percentElapsed().


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