HoloLib
High-performance holonomic (X-Drive) control library for VEX V5
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4namespace hololib {
5class Timer {
6 public:
7 /**
8 * @brief Construct a new Timer
9 *
10 * @note the timer will start counting down as soon as it is created
11 * @note the timer constructor is non-blocking so code after it will be executed immediately
12 * @note if the timer is constructed in a global scope, its behavior is undefined. You can
13 * call set() before using the timer if you absolutely need to construct it in a global scope
14 *
15 * @param time how long to wait
16 *
17 * @b Example
18 * @code {.cpp}
19 * // create a timer that will wait for 1 second
20 * Timer timer(1_sec);
21 * @endcode
22 */
23 Timer(uint32_t time);
24 /**
25 * @brief Get the amount of time the timer was set to
26 *
27 * @return Time time
28 *
29 * @b Example
30 * @code {.cpp}
31 * // create a timer that will wait for 1 second
32 * Timer timer(1_sec);
33 * // get the time the timer was set to
34 * const Time time = timer.getTimeSet(); // time = 1_sec
35 * @endcode
36 */
37 uint32_t getTimeSet();
38 /**
39 * @brief Get the amount of time left on the timer
40 *
41 * @return Time time
42 *
43 * @b Example
44 * @code {.cpp}
45 * // create a timer that will wait for 1 second
46 * Timer timer(1_sec);
47 * // delay for 300ms
48 * pros::delay(300);
49 * // get the time left on the timer
50 * const Time time = timer.getTimeLeft(); // time = 700_msec
51 * @endcode
52 */
53 uint32_t getTimeLeft();
54 /**
55 * @brief Get the amount of time passed on the timer
56 *
57 * @return Time time
58 *
59 * @b Example
60 * @code {.cpp}
61 * // create a timer that will wait for 1 second
62 * Timer timer(1_sec);
63 * // delay for 300ms
64 * pros::delay(300);
65 * // get the time passed on the timer
66 * const Time time = timer.getTimePassed(); // time = 300_msec
67 * @endcode
68 */
69 uint32_t getTimePassed();
70 /**
71 * @brief Get whether the timer is done or not
72 *
73 * @return true the timer is done
74 * @return false the timer is not done
75 *
76 * @b Example
77 * @code {.cpp}
78 * // create a timer that will wait for 1 second
79 * Timer timer(1_sec);
80 * // delay for 500ms
81 * pros::delay(500);
82 * // check if the timer is done
83 * const bool done = timer.isDone(); // done = false
84 * // delay for another 500ms
85 * pros::delay(500);
86 * // check if the timer is done
87 * const bool done = timer.isDone(); // done = true
88 * @endcode
89 */
90 bool isDone();
91 /**
92 * @brief Get whether the timer is paused or not
93 *
94 * @return true the timer is paused
95 * @return false the timer is not paused
96 *
97 * @b Example
98 * @code {.cpp}
99 * // create a timer that will wait for 1 second
100 * Timer timer(1_sec);
101 * // pause the timer
102 * timer.pause();
103 * // check if the timer is paused
104 * bool paused = timer.isPaused(); // paused = true
105 * // resume the timer
106 * timer.resume();
107 * // check if the timer is paused
108 * paused = timer.isPaused(); // paused = false
109 * @endcode
110 */
111 bool isPaused();
112 /**
113 * @brief Set the amount of time the timer should count down. Resets the timer
114 *
115 * @param time time in milliseconds
116 *
117 * @b Example
118 * @code {.cpp}
119 * // create a timer that will wait for 1 second
120 * Timer timer(1_sec);
121 * // set the timer to wait for 2 seconds
122 * timer.set(2_sec);
123 * @endcode
124 */
125 void set(uint32_t time);
126 /**
127 * @brief reset the timer
128 *
129 * @b Example
130 * @code {.cpp}
131 * // create a timer that will wait for 1 second
132 * Timer timer(1_sec);
133 * // delay for 500ms
134 * pros::delay(500);
135 * // reset the timer
136 * timer.reset();
137 * // delay for another 500ms
138 * pros::delay(500);
139 * // check if the timer is done
140 * const bool done = timer.isDone(); // done = false
141 * @endcode
142 */
143 void reset();
144 /**
145 * @brief pause the timer
146 *
147 * @b Example
148 * @code {.cpp}
149 * // create a timer that will wait for 1 second
150 * Timer timer(1_sec);
151 * // pause the timer
152 * timer.pause();
153 * // delay for 2000ms
154 * pros::delay(2000);
155 * // check if the timer is done
156 * const bool done = timer.isDone(); // done = false
157 * @endcode
158 */
159 void pause();
160 /**
161 * @brief resume the timer
162 *
163 * @b Example
164 * @code {.cpp}
165 * // create a timer that will wait for 1 second
166 * Timer timer(1_sec);
167 * // pause the timer
168 * timer.pause();
169 * // delay for 500ms
170 * pros::delay(500);
171 * // resume the timer
172 * timer.resume();
173 * // delay for another 500ms
174 * pros::delay(500);
175 * // check if the timer is done
176 * const bool done = timer.isDone(); // done = false
177 * @endcode
178 */
179 void resume();
180 /**
181 * @brief wait until the timer is done
182 *
183 * @b Example
184 * @code {.cpp}
185 * // create a timer that will wait for 1 second
186 * Timer timer(1_sec);
187 * // wait until the timer is done
188 * timer.waitUntilDone();
189 * std::cout << "done!" << std::endl;
190 * @endcode
191 */
193 private:
194 void update();
195
196 uint32_t m_period;
197 uint32_t m_lastTime = 0;
198 uint32_t m_timeWaited = 0;
199 bool m_paused = false;
200};
201} // namespace hololib
uint32_t getTimeLeft()
Get the amount of time left on the timer.
uint32_t getTimeSet()
Get the amount of time the timer was set to.
void resume()
resume the timer
void waitUntilDone()
wait until the timer is done
void set(uint32_t time)
Set the amount of time the timer should count down.
bool isPaused()
Get whether the timer is paused or not.
bool isDone()
Get whether the timer is done or not.
uint32_t getTimePassed()
Get the amount of time passed on the timer.
Timer(uint32_t time)
Construct a new Timer.
void pause()
pause the timer
void reset()
reset the timer