NiboRoboLib 3.2 - NIBO burger Library
utils.h
gehe zur Dokumentation dieser Datei
1 #ifndef _NIBO_UTILS_H_
2 #define _NIBO_UTILS_H_
3 
21 #ifdef DOXYGEN
22 
30 any_type max(any_type a, any_type b);
31 
40 any_type min(any_type a, any_type b);
41 
54 any_type constrain(any_type x, any_type lo, any_type hi);
55 
64 any_type absdiff(any_type a, any_type b);
65 
74 any_type absall(any_type x);
75 
76 
77 #else
78 
79 
80 #ifndef __cplusplus
81 
82 // use GCC statement expressions...
83 // will only work with gcc compiler
84 #ifndef __GNUC__
85 #error GCC statement expressions will only work with gcc compiler!
86 #endif
87 
88 #ifndef ARDUINO
89 
90 #ifndef max
91 #define max(a,b) \
92  ({ __typeof__ (a) _a = (a); \
93  __typeof__ (b) _b = (b); \
94  _a > _b ? _a : _b; })
95 #define max3(a,b,c) max(max(a,b),c)
96 #define max4(a,b,c,d) max(max(a,b),max(c,d))
97 #endif
98 
99 
100 #ifndef min
101 #define min(a,b) \
102  ({ __typeof__ (a) _a = (a); \
103  __typeof__ (b) _b = (b); \
104  _a < _b ? _a : _b; })
105 #define min3(a,b,c) min(min(a,b),c)
106 #define min4(a,b,c,d) min(min(a,b),min(c,d))
107 #endif
108 
109 
110 #define constrain(x,a,b) \
111  ({ __typeof__ (x) _x = (x); \
112  __typeof__ (a) _a = (a); \
113  __typeof__ (b) _b = (b); \
114  _x < _a ? _a : (_x > _b ? _b : _x); })
115 
116 #endif // ARDUINO
117 
118 #define absdiff(a,b) \
119  ({ __typeof__ (a) _a = (a); \
120  __typeof__ (b) _b = (b); \
121  (_a < _b) ? (_b-_a) : (_a-_b); })
122 
123 
124 #define absall(a) \
125  ({ __typeof__ (a) _a = (a); \
126  (_a < 0) ? (-_a) : (_a); })
127 
128 #else // __cplusplus
129 
130 #ifndef ARDUINO
131 
132 template <typename T>
133 inline static T max(T a, T b) {
134  return a > b ? a : b;
135 };
136 
137 template <typename T>
138 inline static T max(T a, T b, T c) {
139  return max(max(a,b),c);
140 };
141 
142 template <typename T>
143 inline static T max(T a, T b, T c, T d) {
144  return max(max(a,b),max(c,d));
145 };
146 
147 #define max3 max
148 #define max4 max
149 
150 template <typename T>
151 inline static T min(T a, T b) {
152  return a < b ? a : b;
153 };
154 
155 template <typename T>
156 inline static T min(T a, T b, T c) {
157  return min(min(a,b),c);
158 };
159 
160 template <typename T>
161 inline static T min(T a, T b, T c, T d) {
162  return min(min(a,b),min(c,d));
163 };
164 
165 template <typename T>
166 inline static T min(T a, T b, T c, T d) {
167  return min(min(a,b),min(c,d));
168 };
169 
170 #define min3 min
171 #define min4 min
172 
173 template <typename T>
174 inline static T constrain(T x, T a, T b) {
175  return x < a ? a : (x > b ? b : x);
176 };
177 
178 #endif // ARDUINO
179 
180 template <typename T>
181 inline static T absall(T x) {
182  return (x < 0) ? -x : x;
183 };
184 
185 template <typename T>
186 inline static T absdiff(T a, T b) {
187  return (a < b) ? (b-a) : (a-b);
188 };
189 
190 
191 #endif // __cplusplus
192 
193 #endif // DOXYGEN
194 
195 #endif // _NIBO_UTILS_H_
196 
197 
198 
any_type min(any_type a, any_type b)
Minimum von zwei Werten.
any_type absdiff(any_type a, any_type b)
Absolute (positive) Differenz von zwei Werten.
any_type constrain(any_type x, any_type lo, any_type hi)
Beschränkt den Wert x auf das Interval [lo,hi].
any_type absall(any_type x)
Absolutwert (Wert ohne Vorzeichen).
any_type max(any_type a, any_type b)
Maximum von zwei Werten.