M5Utility 0.3.0 git rev:41a629c
Loading...
Searching...
No Matches
compatibility_feature.hpp File Reference

Maintain compatibility with Arduino API, etc. More...

#include <type_traits>

Go to the source code of this file.

Namespaces

namespace  m5
 Top level namespace of M5.
 

Functions

Arduino API
IRAM_ATTR unsigned long m5::utility::millis ()
 Returns the number of milliseconds passed since the Arduino board began running the current program.
 
IRAM_ATTR unsigned long m5::utility::micros ()
 Returns the number of microseconds since the Arduino board began running the current program.
 
void m5::utility::delay (const unsigned long ms)
 Pauses the program for the amount of time (in milliseconds) specified as parameter.
 
void m5::utility::delayMicroseconds (const unsigned int us)
 Pauses the program for the amount of time (in microseconds) specified by the parameter.
 

Elapsed time

using m5::utility::elapsed_time_t = unsigned long
 Elapsed time unit (ms)
 
template<typename T >
T m5::utility::elapsedSince (const T start_at, const T now)
 Gets the elapsed time between start_at and now.
 
elapsed_time_t m5::utility::elapsedSince (const elapsed_time_t start_at)
 Gets the elapsed time since start_at.
 
template<typename T >
bool m5::utility::hasElapsed (const T start_at, const typename std::common_type< T >::type duration, const T now)
 Has the duration passed between start_at and now?
 
bool m5::utility::hasElapsed (const elapsed_time_t start_at, const elapsed_time_t duration)
 Has the duration passed since start_at?
 

Detailed Description

Maintain compatibility with Arduino API, etc.

Typedef Documentation

◆ elapsed_time_t

using m5::utility::elapsed_time_t = unsigned long

Elapsed time unit (ms)

Note
Identical to the return type of millis(), so the elapsed time is computed at the width at which millis() itself wraps. Code driven by a clock of a different width should keep that width instead (see elapsedSince(const T, const T))

Function Documentation

◆ delay()

void m5::utility::delay ( const unsigned long ms)

Pauses the program for the amount of time (in milliseconds) specified as parameter.

Parameters
msdelay time (ms)
Warning
Accuracy varies depending on the environment.

◆ delayMicroseconds()

void m5::utility::delayMicroseconds ( const unsigned int us)

Pauses the program for the amount of time (in microseconds) specified by the parameter.

Parameters
usdelay time (us)
Warning
Accuracy varies depending on the environment.

◆ elapsedSince() [1/2]

elapsed_time_t m5::utility::elapsedSince ( const elapsed_time_t start_at)
inline

Gets the elapsed time since start_at.

Parameters
start_atTime obtained by millis()
Returns
Elapsed time (ms)

◆ elapsedSince() [2/2]

template<typename T >
T m5::utility::elapsedSince ( const T start_at,
const T now )
inline

Gets the elapsed time between start_at and now.

Parameters
start_atEarlier time
nowCurrent time
Returns
Elapsed time (ms)
Note
Unsigned subtraction yields the correct elapsed time even across the wrap of the clock
Overload for code that takes its current time from somewhere other than millis(), such as an injected clock that unit tests can drive
Warning
The subtraction happens in the caller's own type T, because modular arithmetic is only correct when its width matches the width at which the clock wraps. Widening a 32-bit clock to a 64-bit type before subtracting breaks the wrap, so T is deduced rather than fixed to elapsed_time_t

◆ hasElapsed() [1/2]

bool m5::utility::hasElapsed ( const elapsed_time_t start_at,
const elapsed_time_t duration )
inline

Has the duration passed since start_at?

Parameters
start_atTime obtained by millis()
durationDuration to wait for (ms)
Returns
True if the duration has passed

◆ hasElapsed() [2/2]

template<typename T >
bool m5::utility::hasElapsed ( const T start_at,
const typename std::common_type< T >::type duration,
const T now )
inline

Has the duration passed between start_at and now?

Parameters
start_atEarlier time
durationDuration to wait for (ms)
nowCurrent time
Returns
True if the duration has passed
Note
Compares the elapsed time rather than a precomputed deadline. A deadline such as (start_at + duration) overflows near the wrap of the clock and cuts the wait short, so compare the elapsed time instead
Overload for code that takes its current time from somewhere other than millis(), such as an injected clock that unit tests can drive. The current time is the trailing argument so that the shorter form is a prefix of this one and cannot be called by mistake
Warning
The elapsed time is computed in the caller's own type T; see elapsedSince(const T, const T). duration is not deduced, so an integer literal may be passed without making the deduction ambiguous