Prusa MINI Firmware overview
macros.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 #define ABCE 4
25 #define XYZE 4
26 #define ABC 3
27 #define XYZ 3
28 #define XY 2
29 
30 #define _AXIS(A) (A##_AXIS)
31 
32 #define _XMIN_ 100
33 #define _YMIN_ 200
34 #define _ZMIN_ 300
35 #define _XMAX_ 101
36 #define _YMAX_ 201
37 #define _ZMAX_ 301
38 
39 #define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
40 #define FORCE_INLINE __attribute__((always_inline)) inline
41 #define _UNUSED __attribute__((unused))
42 #define _O0 __attribute__((optimize("O0")))
43 #define _Os __attribute__((optimize("Os")))
44 #define _O1 __attribute__((optimize("O1")))
45 #define _O2 __attribute__((optimize("O2")))
46 #define _O3 __attribute__((optimize("O3")))
47 
48 #ifndef UNUSED
49  #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
50  #define UNUSED(X) (void)X
51  #else
52  #define UNUSED(x) ((void)(x))
53  #endif
54 #endif
55 
56 // Clock speed factors
57 #if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
58  #define CYCLES_PER_MICROSECOND (F_CPU / 1000000UL) // 16 or 20 on AVR
59 #endif
60 
61 // Nanoseconds per cycle
62 #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
63 
64 // Macros to make sprintf_P read from PROGMEM (AVR extension)
65 #ifdef __AVR__
66  #define S_FMT "%S"
67 #else
68  #define S_FMT "%s"
69 #endif
70 
71 // Macros to make a string from a macro
72 #define STRINGIFY_(M) #M
73 #define STRINGIFY(M) STRINGIFY_(M)
74 
75 #define A(CODE) " " CODE "\n\t"
76 #define L(CODE) CODE ":\n\t"
77 
78 // Macros for bit masks
79 #undef _BV
80 #define _BV(n) (1<<(n))
81 #define TEST(n,b) (!!((n)&_BV(b)))
82 #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
83 
84 #ifndef SBI
85  #define SBI(A,B) (A |= (1 << (B)))
86 #endif
87 
88 #ifndef CBI
89  #define CBI(A,B) (A &= ~(1 << (B)))
90 #endif
91 
92 #define _BV32(b) (1UL << (b))
93 #define TEST32(n,b) !!((n)&_BV32(b))
94 #define SBI32(n,b) (n |= _BV32(b))
95 #define CBI32(n,b) (n &= ~_BV32(b))
96 
97 #define cu(x) ((x)*(x)*(x))
98 #define RADIANS(d) ((d)*float(M_PI)/180.0f)
99 #define DEGREES(r) ((r)*180.0f/float(M_PI))
100 #define HYPOT2(x,y) (sq(x)+sq(y))
101 
102 #define CIRCLE_AREA(R) (float(M_PI) * sq(float(R)))
103 #define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
104 
105 #define SIGN(a) ((a>0)-(a<0))
106 #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
107 
108 // Macros to constrain values
109 #ifdef __cplusplus
110 
111  // C++11 solution that is standards compliant.
112  template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
113  if (n > v) v = n;
114  }
115  template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
116  if (n < v) v = n;
117  }
118  template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
119  if (n1 > v) v = n1;
120  else if (n2 < v) v = n2;
121  }
122 
123 #else
124 
125  // Using GCC extensions, but Travis GCC version does not like it and gives
126  // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
127  #define NOLESS(v, n) \
128  do{ \
129  __typeof__(n) _n = (n); \
130  if (_n > v) v = _n; \
131  }while(0)
132 
133  #define NOMORE(v, n) \
134  do{ \
135  __typeof__(n) _n = (n); \
136  if (_n < v) v = _n; \
137  }while(0)
138 
139  #define LIMIT(v, n1, n2) \
140  do{ \
141  __typeof__(n1) _n1 = (n1); \
142  __typeof__(n2) _n2 = (n2); \
143  if (_n1 > v) v = _n1; \
144  else if (_n2 < v) v = _n2; \
145  }while(0)
146 
147 #endif
148 
149 // Macros to chain up to 12 conditions
150 #define _DO_1(W,C,A) (_##W##_1(A))
151 #define _DO_2(W,C,A,B) (_##W##_1(A) C _##W##_1(B))
152 #define _DO_3(W,C,A,V...) (_##W##_1(A) C _DO_2(W,C,V))
153 #define _DO_4(W,C,A,V...) (_##W##_1(A) C _DO_3(W,C,V))
154 #define _DO_5(W,C,A,V...) (_##W##_1(A) C _DO_4(W,C,V))
155 #define _DO_6(W,C,A,V...) (_##W##_1(A) C _DO_5(W,C,V))
156 #define _DO_7(W,C,A,V...) (_##W##_1(A) C _DO_6(W,C,V))
157 #define _DO_8(W,C,A,V...) (_##W##_1(A) C _DO_7(W,C,V))
158 #define _DO_9(W,C,A,V...) (_##W##_1(A) C _DO_8(W,C,V))
159 #define _DO_10(W,C,A,V...) (_##W##_1(A) C _DO_9(W,C,V))
160 #define _DO_11(W,C,A,V...) (_##W##_1(A) C _DO_10(W,C,V))
161 #define _DO_12(W,C,A,V...) (_##W##_1(A) C _DO_11(W,C,V))
162 #define __DO_N(W,C,N,V...) _DO_##N(W,C,V)
163 #define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
164 #define DO(W,C,V...) _DO_N(W,C,NUM_ARGS(V),V)
165 
166 // Macros to support option testing
167 #define _CAT(a,V...) a##V
168 #define SWITCH_ENABLED_false 0
169 #define SWITCH_ENABLED_true 1
170 #define SWITCH_ENABLED_0 0
171 #define SWITCH_ENABLED_1 1
172 #define SWITCH_ENABLED_0x0 0
173 #define SWITCH_ENABLED_0x1 1
174 #define SWITCH_ENABLED_ 1
175 #define _ENA_1(O) _CAT(SWITCH_ENABLED_, O)
176 #define _DIS_1(O) !_ENA_1(O)
177 #define ENABLED(V...) DO(ENA,&&,V)
178 #define DISABLED(V...) DO(DIS,&&,V)
179 
180 #define ANY(V...) !DISABLED(V)
181 #define NONE(V...) DISABLED(V)
182 #define ALL(V...) ENABLED(V)
183 #define BOTH(V1,V2) ALL(V1,V2)
184 #define EITHER(V1,V2) ANY(V1,V2)
185 
186 // Macros to support pins/buttons exist testing
187 #define _PINEX_1(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
188 #define PIN_EXISTS(V...) DO(PINEX,&&,V)
189 #define ANY_PIN(V...) DO(PINEX,||,V)
190 
191 #define _BTNEX_1(BN) (defined(BTN_##BN) && BTN_##BN >= 0)
192 #define BUTTON_EXISTS(V...) DO(BTNEX,&&,V)
193 #define ANY_BUTTON(V...) DO(BTNEX,||,V)
194 
195 #define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H))
196 #define NUMERIC(a) WITHIN(a, '0', '9')
197 #define DECIMAL(a) (NUMERIC(a) || a == '.')
198 #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
199 #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
200 #define COUNT(a) (sizeof(a)/sizeof(*a))
201 #define ZERO(a) memset(a,0,sizeof(a))
202 #define COPY(a,b) do{ \
203  static_assert(sizeof(a[0]) == sizeof(b[0]), "COPY: '" STRINGIFY(a) "' and '" STRINGIFY(b) "' types (sizes) don't match!"); \
204  memcpy(&a[0],&b[0],_MIN(sizeof(a),sizeof(b))); \
205  }while(0)
206 
207 // Macros for initializing arrays
208 #define LIST_16(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P
209 #define LIST_15(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O
210 #define LIST_14(A,B,C,D,E,F,G,H,I,J,K,L,M,N,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N
211 #define LIST_13(A,B,C,D,E,F,G,H,I,J,K,L,M,...) A,B,C,D,E,F,G,H,I,J,K,L,M
212 #define LIST_12(A,B,C,D,E,F,G,H,I,J,K,L,...) A,B,C,D,E,F,G,H,I,J,K,L
213 #define LIST_11(A,B,C,D,E,F,G,H,I,J,K,...) A,B,C,D,E,F,G,H,I,J,K
214 #define LIST_10(A,B,C,D,E,F,G,H,I,J,...) A,B,C,D,E,F,G,H,I,J
215 #define LIST_9( A,B,C,D,E,F,G,H,I,...) A,B,C,D,E,F,G,H,I
216 #define LIST_8( A,B,C,D,E,F,G,H,...) A,B,C,D,E,F,G,H
217 #define LIST_7( A,B,C,D,E,F,G,...) A,B,C,D,E,F,G
218 #define LIST_6( A,B,C,D,E,F,...) A,B,C,D,E,F
219 #define LIST_5( A,B,C,D,E,...) A,B,C,D,E
220 #define LIST_4( A,B,C,D,...) A,B,C,D
221 #define LIST_3( A,B,C,...) A,B,C
222 #define LIST_2( A,B,...) A,B
223 #define LIST_1( A,...) A
224 
225 #define _LIST_N(N,V...) LIST_##N(V)
226 #define LIST_N(N,V...) _LIST_N(N,V)
227 #define ARRAY_N(N,V...) { _LIST_N(N,V) }
228 
229 #define _JOIN_1(O) (O)
230 #define JOIN_N(N,C,V...) (DO(JOIN,C,LIST_N(N,V)))
231 
232 // Macros for adding
233 #define INC_0 1
234 #define INC_1 2
235 #define INC_2 3
236 #define INC_3 4
237 #define INC_4 5
238 #define INC_5 6
239 #define INC_6 7
240 #define INC_7 8
241 #define INC_8 9
242 #define INCREMENT_(n) INC_##n
243 #define INCREMENT(n) INCREMENT_(n)
244 
245 // Macros for subtracting
246 #define DEC_1 0
247 #define DEC_2 1
248 #define DEC_3 2
249 #define DEC_4 3
250 #define DEC_5 4
251 #define DEC_6 5
252 #define DEC_7 6
253 #define DEC_8 7
254 #define DEC_9 8
255 #define DECREMENT_(n) DEC_##n
256 #define DECREMENT(n) DECREMENT_(n)
257 
258 #define NOOP (void(0))
259 
260 #define CEILING(x,y) (((x) + (y) - 1) / (y))
261 
262 #undef ABS
263 #ifdef __cplusplus
264  template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
265 #else
266  #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
267 #endif
268 
269 #define UNEAR_ZERO(x) ((x) < 0.000001f)
270 #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
271 #define NEAR(x,y) NEAR_ZERO((x)-(y))
272 
273 #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x)))
274 #define FIXFLOAT(f) (f + (f < 0 ? -0.00005f : 0.00005f))
275 
276 //
277 // Maths macros that can be overridden by HAL
278 //
279 #define ATAN2(y, x) atan2f(y, x)
280 #define POW(x, y) powf(x, y)
281 #define SQRT(x) sqrtf(x)
282 #define RSQRT(x) (1 / sqrtf(x))
283 #define CEIL(x) ceilf(x)
284 #define FLOOR(x) floorf(x)
285 #define LROUND(x) lroundf(x)
286 #define FMOD(x, y) fmodf(x, y)
287 #define HYPOT(x,y) SQRT(HYPOT2(x,y))
288 
289 #ifdef TARGET_LPC1768
290  #define I2C_ADDRESS(A) ((A) << 1)
291 #else
292  #define I2C_ADDRESS(A) A
293 #endif
294 
295 // Use NUM_ARGS(__VA_ARGS__) to get the number of variadic arguments
296 #define _NUM_ARGS(_,Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A,OUT,...) OUT
297 #define NUM_ARGS(V...) _NUM_ARGS(0,V,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
298 
299 #ifdef __cplusplus
300 
301  #ifndef _MINMAX_H_
302  #define _MINMAX_H_
303 
304  extern "C++" {
305 
306  // C++11 solution that is standards compliant. Return type is deduced automatically
307  template <class L, class R> static inline constexpr auto _MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
308  return lhs < rhs ? lhs : rhs;
309  }
310  template <class L, class R> static inline constexpr auto _MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
311  return lhs > rhs ? lhs : rhs;
312  }
313  template<class T, class ... Ts> static inline constexpr const T _MIN(T V, Ts... Vs) { return _MIN(V, _MIN(Vs...)); }
314  template<class T, class ... Ts> static inline constexpr const T _MAX(T V, Ts... Vs) { return _MAX(V, _MAX(Vs...)); }
315 
316  }
317 
318  #endif
319 
320 #else
321 
322  #define MIN_2(a,b) ((a)<(b)?(a):(b))
323  #define MIN_3(a,V...) MIN_2(a,MIN_2(V))
324  #define MIN_4(a,V...) MIN_2(a,MIN_3(V))
325  #define MIN_5(a,V...) MIN_2(a,MIN_4(V))
326  #define MIN_6(a,V...) MIN_2(a,MIN_5(V))
327  #define MIN_7(a,V...) MIN_2(a,MIN_6(V))
328  #define MIN_8(a,V...) MIN_2(a,MIN_7(V))
329  #define MIN_9(a,V...) MIN_2(a,MIN_8(V))
330  #define MIN_10(a,V...) MIN_2(a,MIN_9(V))
331  #define __MIN_N(N,V...) MIN_##N(V)
332  #define _MIN_N(N,V...) __MIN_N(N,V)
333  #define _MIN(V...) _MIN_N(NUM_ARGS(V), V)
334 
335  #define MAX_2(a,b) ((a)>(b)?(a):(b))
336  #define MAX_3(a,V...) MAX_2(a,MAX_2(V))
337  #define MAX_4(a,V...) MAX_2(a,MAX_3(V))
338  #define MAX_5(a,V...) MAX_2(a,MAX_4(V))
339  #define MAX_6(a,V...) MAX_2(a,MAX_5(V))
340  #define MAX_7(a,V...) MAX_2(a,MAX_6(V))
341  #define MAX_8(a,V...) MAX_2(a,MAX_7(V))
342  #define MAX_9(a,V...) MAX_2(a,MAX_8(V))
343  #define MAX_10(a,V...) MAX_2(a,MAX_9(V))
344  #define __MAX_N(N,V...) MAX_##N(V)
345  #define _MAX_N(N,V...) __MAX_N(N,V)
346  #define _MAX(V...) _MAX_N(NUM_ARGS(V), V)
347 
348 #endif
NOLESS
#define NOLESS(v, n)
Definition: macros.h:127
_MAX
#define _MAX(V...)
Definition: macros.h:346
_MIN
#define _MIN(V...)
Definition: macros.h:333
LIMIT
#define LIMIT(v, n1, n2)
Definition: macros.h:139
NOMORE
#define NOMORE(v, n)
Definition: macros.h:133
ABS
#define ABS(a)
Definition: macros.h:266
L
#define L(CODE)
Definition: macros.h:76