23 #ifndef ETCPAL_CPP_TIMER_H_
24 #define ETCPAL_CPP_TIMER_H_
31 #include "etcpal/timer.h"
61 std::stringstream res;
63 std::chrono::milliseconds milliseconds{std::abs(duration_ms)};
65 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(milliseconds);
66 milliseconds -= std::chrono::duration_cast<std::chrono::milliseconds>(seconds);
67 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(seconds);
68 seconds -= std::chrono::duration_cast<std::chrono::seconds>(minutes);
69 auto hours = std::chrono::duration_cast<std::chrono::hours>(minutes);
70 minutes -= std::chrono::duration_cast<std::chrono::minutes>(hours);
74 if (hours > std::chrono::hours{0})
75 res << hours.count() <<
" hr ";
76 if (minutes > std::chrono::minutes{0})
77 res << minutes.count() <<
" min ";
78 if (seconds > std::chrono::seconds{0})
79 res << seconds.count() <<
" sec ";
81 res << milliseconds.count() <<
" ms";
145 constexpr uint32_t
value() const noexcept;
216 explicit Timer(uint32_t interval) noexcept;
217 template <
typename Rep,
typename Period>
218 explicit Timer(
const std::chrono::duration<Rep, Period>& interval) noexcept;
229 void Start(uint32_t interval) noexcept;
230 template <typename Rep, typename Period>
231 void Start(const std::chrono::duration<Rep, Period>& interval) noexcept;
232 void Reset() noexcept;
259 template <
typename Rep,
typename Period>
260 Timer::Timer(
const std::chrono::duration<Rep, Period>& interval) noexcept
318 template <
typename Rep,
typename Period>
319 void Timer::Start(
const std::chrono::duration<Rep, Period>& interval) noexcept
321 uint32_t interval_clamped =
static_cast<uint32_t
>(
322 std::min(std::chrono::milliseconds(interval).count(),
323 static_cast<std::chrono::milliseconds::rep
>(std::numeric_limits<uint32_t>::max())));
341 return static_cast<int32_t
>(a.value() - b.value());
344 constexpr
bool operator==(
const TimePoint& a,
const TimePoint& b) noexcept
346 return (a.value() == b.value());
349 constexpr
bool operator!=(
const TimePoint& a,
const TimePoint& b) noexcept
354 constexpr
bool operator<(
const TimePoint& a,
const TimePoint& b) noexcept
359 constexpr
bool operator>(
const TimePoint& a,
const TimePoint& b) noexcept
364 constexpr
bool operator<=(
const TimePoint& a,
const TimePoint& b) noexcept
369 constexpr
bool operator>=(
const TimePoint& a,
const TimePoint& b) noexcept
Represents a point in time.
Definition: timer.h:135
ETCPAL_CONSTEXPR_14 TimePoint & operator-=(uint32_t duration) noexcept
Subtract a millisecond duration from a TimePoint.
Definition: timer.h:175
constexpr uint32_t value() const noexcept
Get the raw millisecond value from a TimePoint.
Definition: timer.h:162
ETCPAL_CONSTEXPR_14 TimePoint & operator+=(uint32_t duration) noexcept
Add a millisecond duration to a TimePoint.
Definition: timer.h:168
TimePoint()=default
Construct a TimePoint with a value of 0 by default.
static TimePoint Now() noexcept
Get a TimePoint representing the current time.
Definition: timer.h:182
A wrapper class for the EtcPal timer type.
Definition: timer.h:208
Timer()=default
Creates an expired timer with an interval of 0.
TimePoint GetStartTime() const noexcept
Get the time when this timer was started or reset.
Definition: timer.h:278
bool IsExpired() const noexcept
Whether the timer's interval is expired.
Definition: timer.h:302
Timer & operator=(const EtcPalTimer &c_timer) noexcept
Assign an instance of the C EtcPalTimer type to an instance of this class.
Definition: timer.h:244
void Reset() noexcept
Reset the timer while keeping the same interval.
Definition: timer.h:328
constexpr const EtcPalTimer & get() const noexcept
Get a const reference to the underlying C type.
Definition: timer.h:266
uint32_t GetElapsed() const noexcept
Get the time since the timer was reset.
Definition: timer.h:290
uint32_t GetInterval() const noexcept
Get the current interval being timed by the timer.
Definition: timer.h:284
void Start(uint32_t interval) noexcept
Start the timer with a new interval.
Definition: timer.h:310
uint32_t GetRemaining() const noexcept
Get the amount of time remaining in the timer's interval (returns 0 if the timer is expired).
Definition: timer.h:296
Common definitions used by EtcPal C++ wrappers.
std::string DurationToString(int32_t duration_ms) noexcept
Get a string represention of a millisecond duration.
Definition: timer.h:59
#define ETCPAL_CONSTEXPR_14
Stand-in for "constexpr" on entities that can only be defined "constexpr" in C++14 or later.
Definition: common.h:53
#define ETCPAL_CONSTEXPR_14_OR_INLINE
Defined to "constexpr" in C++14 or later, "inline" earlier.
Definition: common.h:54
uint32_t etcpal_timer_elapsed(const EtcPalTimer *timer)
Get the time since a timer was reset.
Definition: timer.c:56
void etcpal_timer_start(EtcPalTimer *timer, uint32_t interval)
Start a timer.
Definition: timer.c:30
uint32_t etcpal_timer_remaining(const EtcPalTimer *timer)
Get the amount of time remaining in a timer.
Definition: timer.c:87
bool etcpal_timer_is_expired(const EtcPalTimer *timer)
Check to see if a timer is expired.
Definition: timer.c:73
uint32_t etcpal_getms(void)
Get a monotonically-increasing millisecond value.
void etcpal_timer_reset(EtcPalTimer *timer)
Reset a timer while keeping the same interval.
Definition: timer.c:43
A millisecond-resolution timer.
Definition: timer.h:102
uint32_t reset_time
The time at which this timer was reset.
Definition: timer.h:103
uint32_t interval
This timer's timeout interval.
Definition: timer.h:104