NiboRoboLib 3.6 - NIBObee 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 #ifdef ARDUINO
80 #include <Arduino.h>
81 #endif
82 
83 
84 
85 #ifndef __cplusplus
86 
87 // use GCC statement expressions...
88 // will only work with gcc compiler
89 #ifndef __GNUC__
90 #error GCC statement expressions will only work with gcc compiler!
91 #endif
92 
93 #ifndef ARDUINO
94 
95 #ifndef max
96 #define max(a,b) \
97  ({ __typeof__ (a) _a = (a); \
98  __typeof__ (b) _b = (b); \
99  _a > _b ? _a : _b; })
100 #define max3(a,b,c) max(max(a,b),c)
101 #define max4(a,b,c,d) max(max(a,b),max(c,d))
102 #endif
103 
104 
105 #ifndef min
106 #define min(a,b) \
107  ({ __typeof__ (a) _a = (a); \
108  __typeof__ (b) _b = (b); \
109  _a < _b ? _a : _b; })
110 #define min3(a,b,c) min(min(a,b),c)
111 #define min4(a,b,c,d) min(min(a,b),min(c,d))
112 #endif
113 
114 
115 #define constrain(x,a,b) \
116  ({ __typeof__ (x) _x = (x); \
117  __typeof__ (a) _a = (a); \
118  __typeof__ (b) _b = (b); \
119  _x < _a ? _a : (_x > _b ? _b : _x); })
120 
121 #else // ARDUINO
122 
123 #define max3(a,b,c) max(max(a,b),c)
124 #define max4(a,b,c,d) max(max(a,b),max(c,d))
125 #define min3(a,b,c) min(min(a,b),c)
126 #define min4(a,b,c,d) min(min(a,b),min(c,d))
127 
128 #endif // ARDUINO
129 
130 #define absdiff(a,b) \
131  ({ __typeof__ (a) _a = (a); \
132  __typeof__ (b) _b = (b); \
133  (_a < _b) ? (_b-_a) : (_a-_b); })
134 
135 
136 #define absall(a) \
137  ({ __typeof__ (a) _a = (a); \
138  (_a < 0) ? (-_a) : (_a); })
139 
140 #else // __cplusplus
141 
142 #ifndef ARDUINO
143 
144 template <typename T>
145 inline static T max(T a, T b) {
146  return a > b ? a : b;
147 };
148 
149 template <typename T>
150 inline static T max(T a, T b, T c) {
151  return max(max(a,b),c);
152 };
153 
154 template <typename T>
155 inline static T max(T a, T b, T c, T d) {
156  return max(max(a,b),max(c,d));
157 };
158 
159 #define max3 max
160 #define max4 max
161 
162 template <typename T>
163 inline static T min(T a, T b) {
164  return a < b ? a : b;
165 };
166 
167 template <typename T>
168 inline static T min(T a, T b, T c) {
169  return min(min(a,b),c);
170 };
171 
172 template <typename T>
173 inline static T min(T a, T b, T c, T d) {
174  return min(min(a,b),min(c,d));
175 };
176 
177 #define min3 min
178 #define min4 min
179 
180 template <typename T>
181 inline static T constrain(T x, T a, T b) {
182  return x < a ? a : (x > b ? b : x);
183 };
184 
185 #else // ARDUINO
186 
187 #define max3(a,b,c) max(max(a,b),c)
188 #define max4(a,b,c,d) max(max(a,b),max(c,d))
189 #define min3(a,b,c) min(min(a,b),c)
190 #define min4(a,b,c,d) min(min(a,b),min(c,d))
191 
192 #endif // ARDUINO
193 
194 template <typename T>
195 inline static T absall(T x) {
196  return (x < 0) ? -x : x;
197 };
198 
199 template <typename T>
200 inline static T absdiff(T a, T b) {
201  return (a < b) ? (b-a) : (a-b);
202 };
203 
204 
205 #endif // __cplusplus
206 
207 #endif // DOXYGEN
208 
209 #endif // _NIBO_UTILS_H_
210 
211 
212 
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.