My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macros.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. #include "config.h"
  24. #define NUM_AXIS 4
  25. #define ABCE 4
  26. #define XYZE 4
  27. #define ABC 3
  28. #define XYZ 3
  29. #define _AXIS(A) (A##_AXIS)
  30. // Clock speed factors
  31. #if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
  32. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
  33. #endif
  34. // Nanoseconds per cycle
  35. #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
  36. // Remove compiler warning on an unused variable
  37. #undef UNUSED
  38. #define UNUSED(x) ((void)(x))
  39. // Assembly wrappers for code and labels
  40. #define A(CODE) " " CODE "\n\t"
  41. #define L(CODE) CODE ":\n\t"
  42. // Macros for bit masks
  43. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  44. #define RADIANS(d) ((d)*float(M_PI)/180.0f)
  45. #define DEGREES(r) ((r)*180.0f/float(M_PI))
  46. #define HYPOT2(x,y) (sq(x)+sq(y))
  47. #define CIRCLE_AREA(R) (float(M_PI) * sq(float(R)))
  48. #define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
  49. #define SIGN(a) ((a>0)-(a<0))
  50. // Convenience templates / macros
  51. #undef ABS
  52. #undef MIN
  53. #undef MAX
  54. #ifdef __cplusplus
  55. // Standards-compliant C++11 solutions
  56. extern "C++" {
  57. template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
  58. template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
  59. if (v < n) v = n;
  60. }
  61. template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
  62. if (v > n) v = n;
  63. }
  64. template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
  65. if (v < n1) v = n1;
  66. else if (v > n2) v = n2;
  67. }
  68. template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  69. return lhs < rhs ? lhs : rhs;
  70. }
  71. template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  72. return lhs > rhs ? lhs : rhs;
  73. }
  74. template<class T, class ... Ts> static inline constexpr const T MIN(T V, Ts... Vs) { return MIN(V, MIN(Vs...)); }
  75. template<class T, class ... Ts> static inline constexpr const T MAX(T V, Ts... Vs) { return MAX(V, MAX(Vs...)); }
  76. }
  77. #else
  78. #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
  79. // Using GCC extensions, but Travis GCC version does not like it and gives
  80. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  81. #define NOLESS(v, n) \
  82. do { \
  83. __typeof__(n) _n = (n); \
  84. if (v < _n) v = _n; \
  85. } while(0)
  86. #define NOMORE(v, n) \
  87. do { \
  88. __typeof__(n) _n = (n); \
  89. if (v > _n) v = _n; \
  90. } while(0)
  91. #define LIMIT(v, n1, n2) \
  92. do { \
  93. __typeof__(n1) _n1 = (n1); \
  94. __typeof__(n2) _n2 = (n2); \
  95. if (v < _n1) v = _n1; \
  96. else if (v > _n2) v = _n2; \
  97. } while(0)
  98. // NUM_ARGS(...) evaluates to the number of arguments
  99. #define _NUM_ARGS(X,X6,X5,X4,X3,X2,X1,N,...) N
  100. #define NUM_ARGS(...) _NUM_ARGS(0, __VA_ARGS__ ,6,5,4,3,2,1,0)
  101. #define MIN_2(a,b) ({__typeof__(a) _a = (a); __typeof__(b) _b = (b); _a < _b ? _a : _b;})
  102. #define MIN_3(a,...) MIN_2(a,MIN_2(__VA_ARGS__))
  103. #define MIN_4(a,...) MIN_2(a,MIN_3(__VA_ARGS__))
  104. #define MIN_5(a,...) MIN_2(a,MIN_4(__VA_ARGS__))
  105. #define MIN_6(a,...) MIN_2(a,MIN_5(__VA_ARGS__))
  106. #define __MIN_N(N, ...) MIN_ ## N(__VA_ARGS__)
  107. #define _MIN_N(N, ...) __MIN_N(N, __VA_ARGS__)
  108. #define MIN(...) _MIN_N(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
  109. #define MAX_2(a,b) ({__typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b;})
  110. #define MAX_3(a,...) MAX_2(a,MAX_2(__VA_ARGS__))
  111. #define MAX_4(a,...) MAX_2(a,MAX_3(__VA_ARGS__))
  112. #define MAX_5(a,...) MAX_2(a,MAX_4(__VA_ARGS__))
  113. #define MAX_6(a,...) MAX_2(a,MAX_5(__VA_ARGS__))
  114. #define __MAX_N(N, ...) MAX_ ## N(__VA_ARGS__)
  115. #define _MAX_N(N, ...) __MAX_N(N, __VA_ARGS__)
  116. #define MAX(...) _MAX_N(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
  117. #endif
  118. #define NUMERIC(a) WITHIN(a, '0', '9')
  119. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  120. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  121. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  122. #define COUNT(a) (sizeof(a)/sizeof(*a))
  123. #define ZERO(a) memset(a,0,sizeof(a))
  124. #define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b)))
  125. #define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
  126. #define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
  127. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0f)
  128. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0f)
  129. #define NOOP do{} while(0)
  130. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  131. #define UNEAR_ZERO(x) ((x) < 0.000001f)
  132. #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
  133. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  134. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x)))
  135. #define FIXFLOAT(f) (f + (f < 0 ? -0.00005f : 0.00005f))
  136. //
  137. // Maths macros that can be overridden by HAL
  138. //
  139. #define ATAN2(y, x) atan2f(y, x)
  140. #define POW(x, y) powf(x, y)
  141. #define SQRT(x) sqrtf(x)
  142. #define RSQRT(x) (1 / sqrtf(x))
  143. #define CEIL(x) ceilf(x)
  144. #define FLOOR(x) floorf(x)
  145. #define LROUND(x) lroundf(x)
  146. #define FMOD(x, y) fmodf(x, y)
  147. #define HYPOT(x,y) SQRT(HYPOT2(x,y))