1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

macros/utils: add LIMIT() macro

This patch adds a macro that can be used to limit a value to a given
range.
This commit is contained in:
Joshua DeWeese 2024-02-07 09:50:13 -05:00
parent 4df530663b
commit 78a1a18683

View File

@ -63,6 +63,22 @@ extern "C" {
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif #endif
/**
* @brief Limit a value to an inclusive range
*
* If @p val is > @p high, @p high is returned. If @p val is < @p low, @p low
* is returned. Otherwise, @p val is returned.
*
* @note This macro evaluate its arguments more than once.
*
* @param[in] val value to limit
* @param[in] low minimum limit
* @param[in] high maximum limit
*
* @return range limited value
*/
#define LIMIT(val, low, high) ((val < low) ? low : (val > high) ? high : val)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif