Maintain compatibility with Arduino API, etc.
More...
#include <type_traits>
Go to the source code of this file.
|
| namespace | m5 |
| | Top level namespace of M5.
|
| |
|
|
|
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.
|
| |
Maintain compatibility with Arduino API, etc.
◆ elapsed_time_t
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))
◆ delay()
| void m5::utility::delay |
( |
const unsigned long | ms | ) |
|
Pauses the program for the amount of time (in milliseconds) specified as parameter.
- Parameters
-
- 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
-
- 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_at | Time 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_at | Earlier time |
| now | Current 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]
Has the duration passed since start_at?
- Parameters
-
| start_at | Time obtained by millis() |
| duration | Duration 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_at | Earlier time |
| duration | Duration to wait for (ms) |
| now | Current 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