|
@@ -19,9 +19,10 @@
|
19
|
19
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
20
|
*
|
21
|
21
|
*/
|
|
22
|
+
|
22
|
23
|
#pragma once
|
23
|
24
|
|
24
|
|
-#include "config.h"
|
|
25
|
+#include "minmax.h"
|
25
|
26
|
|
26
|
27
|
#define NUM_AXIS 4
|
27
|
28
|
#define ABCE 4
|
|
@@ -31,6 +32,22 @@
|
31
|
32
|
|
32
|
33
|
#define _AXIS(A) (A##_AXIS)
|
33
|
34
|
|
|
35
|
+#define _XMIN_ 100
|
|
36
|
+#define _YMIN_ 200
|
|
37
|
+#define _ZMIN_ 300
|
|
38
|
+#define _XMAX_ 101
|
|
39
|
+#define _YMAX_ 201
|
|
40
|
+#define _ZMAX_ 301
|
|
41
|
+
|
|
42
|
+#define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
|
|
43
|
+#define FORCE_INLINE __attribute__((always_inline)) inline
|
|
44
|
+#define _UNUSED __attribute__((unused))
|
|
45
|
+#define _O0 __attribute__((optimize("O0")))
|
|
46
|
+#define _Os __attribute__((optimize("Os")))
|
|
47
|
+#define _O1 __attribute__((optimize("O1")))
|
|
48
|
+#define _O2 __attribute__((optimize("O2")))
|
|
49
|
+#define _O3 __attribute__((optimize("O3")))
|
|
50
|
+
|
34
|
51
|
// Clock speed factors
|
35
|
52
|
#if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
|
36
|
53
|
#define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
|
|
@@ -40,16 +57,32 @@
|
40
|
57
|
#define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
|
41
|
58
|
|
42
|
59
|
// Remove compiler warning on an unused variable
|
43
|
|
-#undef UNUSED
|
44
|
60
|
#define UNUSED(x) ((void)(x))
|
45
|
61
|
|
46
|
|
-// Assembly wrappers for code and labels
|
|
62
|
+// Macros to make a string from a macro
|
|
63
|
+#define STRINGIFY_(M) #M
|
|
64
|
+#define STRINGIFY(M) STRINGIFY_(M)
|
|
65
|
+
|
47
|
66
|
#define A(CODE) " " CODE "\n\t"
|
48
|
67
|
#define L(CODE) CODE ":\n\t"
|
49
|
68
|
|
50
|
69
|
// Macros for bit masks
|
|
70
|
+#undef _BV
|
|
71
|
+#define _BV(n) (1<<(n))
|
|
72
|
+#define TEST(n,b) !!((n)&_BV(b))
|
|
73
|
+#define SBI(n,b) (n |= _BV(b))
|
|
74
|
+#define CBI(n,b) (n &= ~_BV(b))
|
51
|
75
|
#define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
|
52
|
76
|
|
|
77
|
+#define _BV32(b) (1UL << (b))
|
|
78
|
+#define TEST32(n,b) !!((n)&_BV32(b))
|
|
79
|
+#define SBI32(n,b) (n |= _BV32(b))
|
|
80
|
+#define CBI32(n,b) (n &= ~_BV32(b))
|
|
81
|
+
|
|
82
|
+// Macros for maths shortcuts
|
|
83
|
+#undef M_PI
|
|
84
|
+#define M_PI 3.14159265358979323846f
|
|
85
|
+
|
53
|
86
|
#define RADIANS(d) ((d)*float(M_PI)/180.0f)
|
54
|
87
|
#define DEGREES(r) ((r)*180.0f/float(M_PI))
|
55
|
88
|
#define HYPOT2(x,y) (sq(x)+sq(y))
|
|
@@ -58,45 +91,25 @@
|
58
|
91
|
#define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
|
59
|
92
|
|
60
|
93
|
#define SIGN(a) ((a>0)-(a<0))
|
|
94
|
+#define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
|
61
|
95
|
|
62
|
|
-// Convenience templates / macros
|
63
|
|
-#undef ABS
|
64
|
|
-#undef MIN
|
65
|
|
-#undef MAX
|
|
96
|
+// Macros to constrain values
|
66
|
97
|
#ifdef __cplusplus
|
67
|
98
|
|
68
|
|
- // Standards-compliant C++11 solutions
|
69
|
|
-
|
70
|
|
- extern "C++" {
|
71
|
|
-
|
72
|
|
- template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
|
73
|
|
-
|
74
|
|
- template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
|
75
|
|
- if (v < n) v = n;
|
76
|
|
- }
|
77
|
|
- template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
|
78
|
|
- if (v > n) v = n;
|
79
|
|
- }
|
80
|
|
- template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
|
81
|
|
- if (v < n1) v = n1;
|
82
|
|
- else if (v > n2) v = n2;
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
|
86
|
|
- return lhs < rhs ? lhs : rhs;
|
87
|
|
- }
|
88
|
|
- template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs) {
|
89
|
|
- return lhs > rhs ? lhs : rhs;
|
90
|
|
- }
|
91
|
|
- template<class T, class ... Ts> static inline constexpr const T MIN(T V, Ts... Vs) { return MIN(V, MIN(Vs...)); }
|
92
|
|
- template<class T, class ... Ts> static inline constexpr const T MAX(T V, Ts... Vs) { return MAX(V, MAX(Vs...)); }
|
93
|
|
-
|
|
99
|
+ // C++11 solution that is standards compliant.
|
|
100
|
+ template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
|
|
101
|
+ if (v < n) v = n;
|
|
102
|
+ }
|
|
103
|
+ template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
|
|
104
|
+ if (v > n) v = n;
|
|
105
|
+ }
|
|
106
|
+ template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
|
|
107
|
+ if (v < n1) v = n1;
|
|
108
|
+ else if (v > n2) v = n2;
|
94
|
109
|
}
|
95
|
110
|
|
96
|
111
|
#else
|
97
|
112
|
|
98
|
|
- #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
|
99
|
|
-
|
100
|
113
|
// Using GCC extensions, but Travis GCC version does not like it and gives
|
101
|
114
|
// "error: statement-expressions are not allowed outside functions nor in template-argument lists"
|
102
|
115
|
#define NOLESS(v, n) \
|
|
@@ -119,30 +132,21 @@
|
119
|
132
|
else if (v > _n2) v = _n2; \
|
120
|
133
|
} while(0)
|
121
|
134
|
|
122
|
|
- // NUM_ARGS(...) evaluates to the number of arguments
|
123
|
|
- #define _NUM_ARGS(X,X6,X5,X4,X3,X2,X1,N,...) N
|
124
|
|
- #define NUM_ARGS(...) _NUM_ARGS(0, __VA_ARGS__ ,6,5,4,3,2,1,0)
|
125
|
|
-
|
126
|
|
- #define MIN_2(a,b) ({__typeof__(a) _a = (a); __typeof__(b) _b = (b); _a < _b ? _a : _b;})
|
127
|
|
- #define MIN_3(a,...) MIN_2(a,MIN_2(__VA_ARGS__))
|
128
|
|
- #define MIN_4(a,...) MIN_2(a,MIN_3(__VA_ARGS__))
|
129
|
|
- #define MIN_5(a,...) MIN_2(a,MIN_4(__VA_ARGS__))
|
130
|
|
- #define MIN_6(a,...) MIN_2(a,MIN_5(__VA_ARGS__))
|
131
|
|
- #define __MIN_N(N, ...) MIN_ ## N(__VA_ARGS__)
|
132
|
|
- #define _MIN_N(N, ...) __MIN_N(N, __VA_ARGS__)
|
133
|
|
- #define MIN(...) _MIN_N(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
|
134
|
|
-
|
135
|
|
- #define MAX_2(a,b) ({__typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b;})
|
136
|
|
- #define MAX_3(a,...) MAX_2(a,MAX_2(__VA_ARGS__))
|
137
|
|
- #define MAX_4(a,...) MAX_2(a,MAX_3(__VA_ARGS__))
|
138
|
|
- #define MAX_5(a,...) MAX_2(a,MAX_4(__VA_ARGS__))
|
139
|
|
- #define MAX_6(a,...) MAX_2(a,MAX_5(__VA_ARGS__))
|
140
|
|
- #define __MAX_N(N, ...) MAX_ ## N(__VA_ARGS__)
|
141
|
|
- #define _MAX_N(N, ...) __MAX_N(N, __VA_ARGS__)
|
142
|
|
- #define MAX(...) _MAX_N(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
|
143
|
|
-
|
144
|
135
|
#endif
|
145
|
136
|
|
|
137
|
+// Macros to support option testing
|
|
138
|
+#define _CAT(a, ...) a ## __VA_ARGS__
|
|
139
|
+#define SWITCH_ENABLED_false 0
|
|
140
|
+#define SWITCH_ENABLED_true 1
|
|
141
|
+#define SWITCH_ENABLED_0 0
|
|
142
|
+#define SWITCH_ENABLED_1 1
|
|
143
|
+#define SWITCH_ENABLED_0x0 0
|
|
144
|
+#define SWITCH_ENABLED_0x1 1
|
|
145
|
+#define SWITCH_ENABLED_ 1
|
|
146
|
+#define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
|
|
147
|
+#define DISABLED(b) !ENABLED(b)
|
|
148
|
+
|
|
149
|
+#define WITHIN(V,L,H) ((V) >= (L) && (V) <= (H))
|
146
|
150
|
#define NUMERIC(a) WITHIN(a, '0', '9')
|
147
|
151
|
#define DECIMAL(a) (NUMERIC(a) || a == '.')
|
148
|
152
|
#define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
|
|
@@ -151,6 +155,45 @@
|
151
|
155
|
#define ZERO(a) memset(a,0,sizeof(a))
|
152
|
156
|
#define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b)))
|
153
|
157
|
|
|
158
|
+// Macros for initializing arrays
|
|
159
|
+#define ARRAY_6(v1, v2, v3, v4, v5, v6, ...) { v1, v2, v3, v4, v5, v6 }
|
|
160
|
+#define ARRAY_5(v1, v2, v3, v4, v5, ...) { v1, v2, v3, v4, v5 }
|
|
161
|
+#define ARRAY_4(v1, v2, v3, v4, ...) { v1, v2, v3, v4 }
|
|
162
|
+#define ARRAY_3(v1, v2, v3, ...) { v1, v2, v3 }
|
|
163
|
+#define ARRAY_2(v1, v2, ...) { v1, v2 }
|
|
164
|
+#define ARRAY_1(v1, ...) { v1 }
|
|
165
|
+
|
|
166
|
+#define _ARRAY_N(N, ...) ARRAY_ ##N(__VA_ARGS__)
|
|
167
|
+#define ARRAY_N(N, ...) _ARRAY_N(N, __VA_ARGS__)
|
|
168
|
+
|
|
169
|
+// Macros for adding
|
|
170
|
+#define INC_0 1
|
|
171
|
+#define INC_1 2
|
|
172
|
+#define INC_2 3
|
|
173
|
+#define INC_3 4
|
|
174
|
+#define INC_4 5
|
|
175
|
+#define INC_5 6
|
|
176
|
+#define INC_6 7
|
|
177
|
+#define INC_7 8
|
|
178
|
+#define INC_8 9
|
|
179
|
+#define INCREMENT_(n) INC_ ##n
|
|
180
|
+#define INCREMENT(n) INCREMENT_(n)
|
|
181
|
+
|
|
182
|
+// Macros for subtracting
|
|
183
|
+#define DEC_1 0
|
|
184
|
+#define DEC_2 1
|
|
185
|
+#define DEC_3 2
|
|
186
|
+#define DEC_4 3
|
|
187
|
+#define DEC_5 4
|
|
188
|
+#define DEC_6 5
|
|
189
|
+#define DEC_7 6
|
|
190
|
+#define DEC_8 7
|
|
191
|
+#define DEC_9 8
|
|
192
|
+#define DECREMENT_(n) DEC_ ##n
|
|
193
|
+#define DECREMENT(n) DECREMENT_(n)
|
|
194
|
+
|
|
195
|
+#define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0)
|
|
196
|
+
|
154
|
197
|
#define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
|
155
|
198
|
#define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
|
156
|
199
|
|
|
@@ -161,6 +204,13 @@
|
161
|
204
|
|
162
|
205
|
#define CEILING(x,y) (((x) + (y) - 1) / (y))
|
163
|
206
|
|
|
207
|
+#undef ABS
|
|
208
|
+#ifdef __cplusplus
|
|
209
|
+ template <class T> static inline constexpr const T ABS(const T v) { return v >= 0 ? v : -v; }
|
|
210
|
+#else
|
|
211
|
+ #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
|
|
212
|
+#endif
|
|
213
|
+
|
164
|
214
|
#define UNEAR_ZERO(x) ((x) < 0.000001f)
|
165
|
215
|
#define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
|
166
|
216
|
#define NEAR(x,y) NEAR_ZERO((x)-(y))
|