26#include <util/delay_basic.h>
69 RTTTime(
const RTTTime& that) : millis_{that.millis_}, micros_{that.micros_} {}
80 millis_ = that.millis_;
81 micros_ = that.micros_;
93 uint32_t extra_millis = microseconds / ONE_MILLI_32;
94 uint16_t extra_micros = microseconds % ONE_MILLI_32;
95 millis_ += extra_millis;
96 micros_ += extra_micros;
97 if (micros_ >= ONE_MILLI_16)
100 micros_ -= ONE_MILLI_16;
113 return *
this = (*
this -
RTTTime{microseconds});
121 return millis_ * ONE_MILLI_32 + micros_;
231 if (micros >= ONE_MILLI_16)
234 micros -= ONE_MILLI_16;
247 if (a <= b)
return RTTTime{0, 0};
269 RTTTime
delta(
const RTTTime& time1,
const RTTTime& time2);
280 uint32_t
since(uint32_t start_ms);
336 _delay_loop_2(us * INST_PER_US / 4UL);
348 while (ms--)
delay_us(ONE_MILLI_16);
362 template<
typename CLOCK>
class ClockDelegate
364 using TYPE = ClockDelegate<CLOCK>;
367 ClockDelegate() =
delete;
369 static void set_clock(
const CLOCK& clock)
376 static void delay(uint32_t ms)
383 return clock_->millis();
387 static const CLOCK* clock_;
390 template<
typename CLOCK>
const CLOCK* ClockDelegate<CLOCK>::clock_ =
nullptr;
408 template<
typename CLOCK>
void set_clock(
const CLOCK& clock)
410 time::ClockDelegate<CLOCK>::set_clock(clock);
Structure used to hold a time value with microsecond precision.
RTTTime(uint32_t millis, uint16_t micros)
Construct a new RTTTime value.
RTTTime(uint32_t micros=0UL)
Construct a new RTTTime value.
RTTTime(const RTTTime &that)
Construct a copy of that.
uint32_t total_micros() const
Return current elapsed time in microseconds only.
uint16_t micros() const
Number of elapsed microseconds (0..999).
RTTTime & operator-=(uint32_t microseconds)
Remove microseconds from this RTTTime instance.
uint32_t millis() const
Number of elapsed milliseconds.
RTTTime & operator+=(uint32_t microseconds)
Add microseconds to this RTTTime instance.
RTTTime & operator=(const RTTTime &that)
Assign this RTTTime instance from that.
Set a new time::delay function for the duration of the current scope; when the scope is left,...
auto_delay(DELAY_PTR new_delay) INLINE
Set new time::delay to new_delay after storing current function for later restore.
Set a new time::millis function for the duration of the current scope; when the scope is left,...
auto_millis(MILLIS_PTR new_millis) INLINE
Set new time::millis to new_millis after storing current function for later restore.
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Defines simple API to handle time and delays.
void(*)(uint32_t ms) DELAY_PTR
Function pointer type used for time::delay global variable.
MILLIS_PTR millis
Count number of milliseconds elapsed since some time base reference (generally since MCU startup).
bool operator<=(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
void delay_us(uint16_t us) INLINE
Delay program execution for the given amount of microseconds.
void default_delay(uint32_t ms)
Delay program execution for the given amount of milliseconds.
bool operator>(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
void yield()
Utility method used by many FastArduino API in order to "yield" some processor time; concretely it ju...
RTTTime delta(const RTTTime &time1, const RTTTime &time2)
Compute the time delta from time1 and time2.
uint32_t(*)() MILLIS_PTR
Function pointer type used for time::millis global variable.
bool operator==(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
RTTTime operator-(const RTTTime &a, const RTTTime &b)
Subtract 2 RTTTime instances.
RTTTime operator+(const RTTTime &a, const RTTTime &b)
Add 2 RTTTime instances.
bool operator<(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
void set_clock(const CLOCK &clock)
Utility method to transform millis() and delay() methods of a clock instance of any CLOCK class into ...
uint32_t since(uint32_t start_ms)
Compute the time elapsed, in milliseconds, since start_ms.
bool operator!=(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
bool operator>=(const RTTTime &a, const RTTTime &b)
Compare 2 RTTTime instances.
void delay_ms(uint16_t ms) INLINE
Delay program execution for the given amount of milliseconds.
DELAY_PTR delay
Delay program execution for the given amount of milliseconds.
General utilities API that have broad application in programs.