L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
mydefs.h
Go to the documentation of this file.
1/*
2 * mydefs.h
3 *
4 * Common utility macros used across the application.
5 *
6 * Created on: 23. 1. 2026
7 * Author: Milan
8 */
9
10#ifndef SRC_UTILS_MYDEFS_H_
11#define SRC_UTILS_MYDEFS_H_
12
13/**
14 * @brief Compute the absolute value of x without using math.h
15 * @param x Any numeric expression
16 * @return Non-negative value of x
17 */
18#define ABS(x) (((x) >= 0) ? (x) : -(x))
19
20/**
21 * @brief printf format string for printing a float/double as integer with 2 decimal places.
22 * Use together with PRIf_02D to avoid floating-point printf on embedded targets.
23 *
24 * Example:
25 * @code
26 * float val = 3.14f;
27 * printf("Value: " PRIf_02 "\n", PRIf_02D(val)); // prints "Value: 3.14"
28 * @endcode
29 */
30#define PRIf_02 "%d.%02d"
31
32/**
33 * @brief Expands to integer and fractional arguments for use with PRIf_02.
34 * Splits a float/double into the integer part and the two-digit fractional part.
35 * @param fData The float or double value to format
36 * @return Two comma-separated integer arguments: integer part and abs(fractional * 100)
37 */
38#define PRIf_02D(fData) (int)fData, ABS(((int)(fData * 100.0f) % 100))
39
40/**
41 * @brief Clamp val to the closed interval [minVal, maxVal].
42 * All three arguments are evaluated exactly once via temporary variables,
43 * avoiding double-evaluation side effects. Uses a GCC statement expression.
44 * @param val The value to clamp
45 * @param minVal Lower bound (inclusive)
46 * @param maxVal Upper bound (inclusive)
47 * @return val if it is within [minVal, maxVal], minVal if val < minVal,
48 * or maxVal if val > maxVal
49 */
50#define CLAMP(val, minVal, maxVal) __extension__({ \
51 __typeof__(val) _v = (val); \
52 __typeof__(minVal) _min = (minVal); \
53 __typeof__(maxVal) _max = (maxVal); \
54 _v < _min ? _min : (_v > _max ? _max : _v); \
55})
56#endif /* SRC_UTILS_MYDEFS_H_ */