FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
time Namespace Reference

Defines simple API to handle time and delays. More...

Classes

class  auto_delay
 Set a new time::delay function for the duration of the current scope; when the scope is left, the previous time::delay function is restored. More...
 
class  auto_millis
 Set a new time::millis function for the duration of the current scope; when the scope is left, the previous time::millis function is restored. More...
 
class  RTTTime
 Structure used to hold a time value with microsecond precision. More...
 

Typedefs

using DELAY_PTR = void(*)(uint32_t ms)
 Function pointer type used for time::delay global variable.
 
using MILLIS_PTR = uint32_t(*)()
 Function pointer type used for time::millis global variable.
 

Functions

RTTTime operator- (const RTTTime &a, const RTTTime &b)
 Subtract 2 RTTTime instances. More...
 
bool operator> (const RTTTime &a, const RTTTime &b)
 Compare 2 RTTTime instances. More...
 
bool operator>= (const RTTTime &a, const RTTTime &b)
 Compare 2 RTTTime instances. More...
 
bool operator< (const RTTTime &a, const RTTTime &b)
 Compare 2 RTTTime instances. More...
 
bool operator<= (const RTTTime &a, const RTTTime &b)
 Compare 2 RTTTime instances. More...
 
bool operator== (const RTTTime &a, const RTTTime &b)
 Compare 2 RTTTime instances. More...
 
bool operator!= (const RTTTime &a, const RTTTime &b)
 Compare 2 RTTTime instances. More...
 
RTTTime operator+ (const RTTTime &a, const RTTTime &b)
 Add 2 RTTTime instances. More...
 
void yield ()
 Utility method used by many FastArduino API in order to "yield" some processor time; concretely it just calls power::Power::sleep() which will put the MCU into a default sleep mode. More...
 
RTTTime delta (const RTTTime &time1, const RTTTime &time2)
 Compute the time delta from time1 and time2. More...
 
uint32_t since (uint32_t start_ms)
 Compute the time elapsed, in milliseconds, since start_ms. More...
 
void delay_us (uint16_t us) INLINE
 Delay program execution for the given amount of microseconds. More...
 
void delay_ms (uint16_t ms) INLINE
 Delay program execution for the given amount of milliseconds. More...
 
void default_delay (uint32_t ms)
 Delay program execution for the given amount of milliseconds. More...
 
template<typename CLOCK >
void set_clock (const CLOCK &clock)
 Utility method to transform millis() and delay() methods of a clock instance of any CLOCK class into static method equivalents, and assign the resulting static methods to time::millis and time::delay global function pointers. More...
 

Variables

DELAY_PTR delay = time::default_delay
 Delay program execution for the given amount of milliseconds. More...
 
MILLIS_PTR millis = nullptr
 Count number of milliseconds elapsed since some time base reference (generally since MCU startup). More...
 

Detailed Description

Defines simple API to handle time and delays.

Function Documentation

◆ operator-()

RTTTime time::operator- ( const RTTTime a,
const RTTTime b 
)
inline

Subtract 2 RTTTime instances.

Parameters
athe first RTTTime instance in subtraction
bthe RTTTime instance to be subtracted from a
Returns
a - b if a > b, 0 if a <= b

Definition at line 245 of file time.h.

◆ operator>()

bool time::operator> ( const RTTTime a,
const RTTTime b 
)
inline

Compare 2 RTTTime instances.

Parameters
athe first RTTTime instance in comparison
bthe second RTTTime instance in comparison
Return values
trueif a > b
falseif a <= b

Definition at line 148 of file time.h.

◆ operator>=()

bool time::operator>= ( const RTTTime a,
const RTTTime b 
)
inline

Compare 2 RTTTime instances.

Parameters
athe first RTTTime instance in comparison
bthe second RTTTime instance in comparison
Return values
trueif a >= b
falseif a < b

Definition at line 162 of file time.h.

◆ operator<()

bool time::operator< ( const RTTTime a,
const RTTTime b 
)
inline

Compare 2 RTTTime instances.

Parameters
athe first RTTTime instance in comparison
bthe second RTTTime instance in comparison
Return values
trueif a < b
falseif a >= b

Definition at line 176 of file time.h.

◆ operator<=()

bool time::operator<= ( const RTTTime a,
const RTTTime b 
)
inline

Compare 2 RTTTime instances.

Parameters
athe first RTTTime instance in comparison
bthe second RTTTime instance in comparison
Return values
trueif a <= b
falseif a > b

Definition at line 190 of file time.h.

◆ operator==()

bool time::operator== ( const RTTTime a,
const RTTTime b 
)
inline

Compare 2 RTTTime instances.

Parameters
athe first RTTTime instance in comparison
bthe second RTTTime instance in comparison
Return values
trueif a == b
falseif a != b

Definition at line 204 of file time.h.

◆ operator!=()

bool time::operator!= ( const RTTTime a,
const RTTTime b 
)
inline

Compare 2 RTTTime instances.

Parameters
athe first RTTTime instance in comparison
bthe second RTTTime instance in comparison
Return values
trueif a != b
falseif a == b

Definition at line 216 of file time.h.

◆ operator+()

RTTTime time::operator+ ( const RTTTime a,
const RTTTime b 
)
inline

Add 2 RTTTime instances.

Parameters
athe first RTTTime instance in addition
bthe second RTTTime instance in addition
Returns
a + b

Definition at line 227 of file time.h.

◆ yield()

void time::yield ( )

Utility method used by many FastArduino API in order to "yield" some processor time; concretely it just calls power::Power::sleep() which will put the MCU into a default sleep mode.

See also
power::Power::sleep()

Definition at line 22 of file time.cpp.

◆ delta()

time::RTTTime time::delta ( const RTTTime time1,
const RTTTime time2 
)

Compute the time delta from time1 and time2.

The method expects time1 to occur before time2; otherwise, behavior is undefined.

Parameters
time1the starting time
time2the ending time
Returns
the difference between time1 and time2

Definition at line 27 of file time.cpp.

◆ since()

uint32_t time::since ( uint32_t  start_ms)

Compute the time elapsed, in milliseconds, since start_ms.

To calculate the elapsed time, this method uses the function pointed to by time::millis().

Parameters
start_ms
Returns
number of milliseconds elapsed between start_ms and now (as returned by time::millis())
See also
time::millis

Definition at line 41 of file time.cpp.

◆ delay_us()

void time::delay_us ( uint16_t  us)
inline

Delay program execution for the given amount of microseconds.

Delay is achieved by a busy loop for the right amount of MCU cycles.

Parameters
usthe number of microseconds to hold program execution

Definition at line 334 of file time.h.

◆ delay_ms()

void time::delay_ms ( uint16_t  ms)
inline

Delay program execution for the given amount of milliseconds.

Delay is achieved by a busy loop for the right amount of MCU cycles.

Parameters
msthe number of milliseconds to hold program execution

Definition at line 346 of file time.h.

◆ default_delay()

void time::default_delay ( uint32_t  ms)

Delay program execution for the given amount of milliseconds.

This is the default implementation for delay function pointer global variable.

Parameters
msthe number of milliseconds to hold program execution
See also
time::delay

Definition at line 47 of file time.cpp.

◆ set_clock()

template<typename CLOCK >
void time::set_clock ( const CLOCK &  clock)

Utility method to transform millis() and delay() methods of a clock instance of any CLOCK class into static method equivalents, and assign the resulting static methods to time::millis and time::delay global function pointers.

This will work with any class as long as it defines the following methods:

void delay(uint32_t ms);
uint32_t millis();
Template Parameters
CLOCKthe class that defines the needed methods millis() and delay()
Parameters
clockthe instance which methods will be called

Definition at line 408 of file time.h.

Variable Documentation

◆ delay

time::DELAY_PTR time::delay = time::default_delay
extern

Delay program execution for the given amount of milliseconds.

delay is not actually a function but a function pointer; this allows FastArduino to set the best implementation based on currently used features. By default, delay just performs a busy loop for the right amount of MCU cycles. Note that if you want to be sure reduce code size to the minimum and you are OK with always using busy loops for delays, then you should use time::delay_ms() instead.

Parameters
msnumber of milliseconds to wait for
See also
time::delay_ms()

Definition at line 19 of file time.cpp.

◆ millis

time::MILLIS_PTR time::millis = nullptr
extern

Count number of milliseconds elapsed since some time base reference (generally since MCU startup).

millis is not actually a function but a function pointer; this allows FastArduino to set the best implementation based on currently used features. By default, millis is not pointing to any concrete implementation, hence you should not call it if it has not been initialized yet. You can initialize it with timer::set_clock() provided with the instance of a class implementing a minimal "clock" API. The following classes implement this minimal clock API:

Returns
number of milliseconds elapsed since some given time base base reference
See also
time::set_clock()

Definition at line 20 of file time.cpp.

time::delay
DELAY_PTR delay
Delay program execution for the given amount of milliseconds.
Definition: time.cpp:19
time::millis
MILLIS_PTR millis
Count number of milliseconds elapsed since some time base reference (generally since MCU startup).
Definition: time.cpp:20