macros.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifndef MACROS_H
  23. #define MACROS_H
  24. #define XYZ 3
  25. #define XYZE 4
  26. #define ABC 3
  27. #define ABCD 4
  28. #define ABCE 4
  29. #define ABCDE 5
  30. /**
  31. * For use in macros that take a single axis letter
  32. * The axis order in all axis related arrays is X, Y, Z, E
  33. * For Hangprinter it is A, B, C, D, E
  34. */
  35. #define _AXIS(A) (A##_AXIS)
  36. #define _XMIN_ 100
  37. #define _YMIN_ 200
  38. #define _ZMIN_ 300
  39. #define _XMAX_ 101
  40. #define _YMAX_ 201
  41. #define _ZMAX_ 301
  42. #define FORCE_INLINE __attribute__((always_inline)) inline
  43. #define _UNUSED __attribute__((unused))
  44. #define _O0 __attribute__((optimize("O0")))
  45. #define _Os __attribute__((optimize("Os")))
  46. #define _O1 __attribute__((optimize("O1")))
  47. #define _O2 __attribute__((optimize("O2")))
  48. #define _O3 __attribute__((optimize("O3")))
  49. // Clock speed factors
  50. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20
  51. #define INT0_PRESCALER 8
  52. // Nanoseconds per cycle
  53. #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
  54. // Remove compiler warning on an unused variable
  55. #define UNUSED(x) (void) (x)
  56. // Macros to make a string from a macro
  57. #define STRINGIFY_(M) #M
  58. #define STRINGIFY(M) STRINGIFY_(M)
  59. #define A(CODE) " " CODE "\n\t"
  60. #define L(CODE) CODE ":\n\t"
  61. // Macros for bit masks
  62. #undef _BV
  63. #define _BV(b) (1 << (b))
  64. #define TEST(n,b) !!((n)&_BV(b))
  65. #define SBI(n,b) (n |= _BV(b))
  66. #define CBI(n,b) (n &= ~_BV(b))
  67. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  68. #define _BV32(b) (1UL << (b))
  69. #define TEST32(n,b) !!((n)&_BV32(b))
  70. #define SBI32(n,b) (n |= _BV32(b))
  71. #define CBI32(n,b) (n &= ~_BV32(b))
  72. // Macro to check that a number if a power if 2
  73. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  74. // Macros for maths shortcuts
  75. #undef M_PI
  76. #define M_PI 3.14159265358979323846f
  77. #define RADIANS(d) ((d)*M_PI/180.0f)
  78. #define DEGREES(r) ((r)*180.0f/M_PI)
  79. #define HYPOT2(x,y) (sq(x)+sq(y))
  80. #define CIRCLE_AREA(R) (M_PI * sq(float(R)))
  81. #define CIRCLE_CIRC(R) (2 * M_PI * (float(R)))
  82. #define SIGN(a) ((a>0)-(a<0))
  83. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  84. // Macros to contrain values
  85. #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
  86. #define NOMORE(v,n) do{ if (v > n) v = n; }while(0)
  87. #define LIMIT(v,n1,n2) do{ if (v < n1) v = n1; else if (v > n2) v = n2; }while(0)
  88. // Macros to support option testing
  89. #define _CAT(a, ...) a ## __VA_ARGS__
  90. #define SWITCH_ENABLED_false 0
  91. #define SWITCH_ENABLED_true 1
  92. #define SWITCH_ENABLED_0 0
  93. #define SWITCH_ENABLED_1 1
  94. #define SWITCH_ENABLED_0x0 0
  95. #define SWITCH_ENABLED_0x1 1
  96. #define SWITCH_ENABLED_ 1
  97. #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
  98. #define DISABLED(b) !ENABLED(b)
  99. #define WITHIN(V,L,H) ((V) >= (L) && (V) <= (H))
  100. #define NUMERIC(a) WITHIN(a, '0', '9')
  101. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  102. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  103. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  104. #define COUNT(a) (sizeof(a)/sizeof(*a))
  105. #define ZERO(a) memset(a,0,sizeof(a))
  106. #define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b)))
  107. // Macros for initializing arrays
  108. #define ARRAY_6(v1, v2, v3, v4, v5, v6, ...) { v1, v2, v3, v4, v5, v6 }
  109. #define ARRAY_5(v1, v2, v3, v4, v5, ...) { v1, v2, v3, v4, v5 }
  110. #define ARRAY_4(v1, v2, v3, v4, ...) { v1, v2, v3, v4 }
  111. #define ARRAY_3(v1, v2, v3, ...) { v1, v2, v3 }
  112. #define ARRAY_2(v1, v2, ...) { v1, v2 }
  113. #define ARRAY_1(v1, ...) { v1 }
  114. #define _ARRAY_N(N, ...) ARRAY_ ##N(__VA_ARGS__)
  115. #define ARRAY_N(N, ...) _ARRAY_N(N, __VA_ARGS__)
  116. // Macros for adding
  117. #define INC_0 1
  118. #define INC_1 2
  119. #define INC_2 3
  120. #define INC_3 4
  121. #define INC_4 5
  122. #define INC_5 6
  123. #define INC_6 7
  124. #define INC_7 8
  125. #define INC_8 9
  126. #define INCREMENT_(n) INC_ ##n
  127. #define INCREMENT(n) INCREMENT_(n)
  128. // Macros for subtracting
  129. #define DEC_1 0
  130. #define DEC_2 1
  131. #define DEC_3 2
  132. #define DEC_4 3
  133. #define DEC_5 4
  134. #define DEC_6 5
  135. #define DEC_7 6
  136. #define DEC_8 7
  137. #define DEC_9 8
  138. #define DECREMENT_(n) DEC_ ##n
  139. #define DECREMENT(n) DECREMENT_(n)
  140. #define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0)
  141. #define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
  142. #define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
  143. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0f)
  144. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0f)
  145. #define NOOP do{} while(0)
  146. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  147. // Avoid double evaluation of arguments on MIN/MAX/ABS
  148. #undef MIN
  149. #undef MAX
  150. #undef ABS
  151. #ifdef __cplusplus
  152. // C++11 solution that is standards compliant. Return type is deduced automatically
  153. template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  154. return lhs < rhs ? lhs : rhs;
  155. }
  156. template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs){
  157. return lhs > rhs ? lhs : rhs;
  158. }
  159. template <class T> static inline constexpr const T ABS(const T v) {
  160. return v >= 0 ? v : -v;
  161. }
  162. #else
  163. // Using GCC extensions, but Travis GCC version does not like it and gives
  164. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  165. #define MIN(a, b) \
  166. ({__typeof__(a) _a = (a); \
  167. __typeof__(b) _b = (b); \
  168. _a < _b ? _a : _b;})
  169. #define MAX(a, b) \
  170. ({__typeof__(a) _a = (a); \
  171. __typeof__(b) _b = (b); \
  172. _a > _b ? _a : _b;})
  173. #define ABS(a) \
  174. ({__typeof__(a) _a = (a); \
  175. _a >= 0 ? _a : -_a;})
  176. #endif
  177. #define MIN3(a, b, c) MIN(MIN(a, b), c)
  178. #define MIN4(a, b, c, d) MIN(MIN3(a, b, c), d)
  179. #define MIN5(a, b, c, d, e) MIN(MIN4(a, b, c, d), e)
  180. #define MAX3(a, b, c) MAX(MAX(a, b), c)
  181. #define MAX4(a, b, c, d) MAX(MAX3(a, b, c), d)
  182. #define MAX5(a, b, c, d, e) MAX(MAX4(a, b, c, d), e)
  183. #define UNEAR_ZERO(x) ((x) < 0.000001f)
  184. #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
  185. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  186. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0.0f : 1.0f / (x))
  187. #define FIXFLOAT(f) (f + (f < 0.0f ? -0.00005f : 0.00005f))
  188. //
  189. // Maths macros that can be overridden by HAL
  190. //
  191. #define ATAN2(y, x) atan2f(y, x)
  192. #define POW(x, y) powf(x, y)
  193. #define SQRT(x) sqrtf(x)
  194. #define RSQRT(x) (1 / sqrtf(x))
  195. #define CEIL(x) ceilf(x)
  196. #define FLOOR(x) floorf(x)
  197. #define LROUND(x) lroundf(x)
  198. #define FMOD(x, y) fmodf(x, y)
  199. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  200. #endif // MACROS_H