|
@@ -19,10 +19,9 @@
|
19
|
19
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
20
|
*
|
21
|
21
|
*/
|
22
|
|
-
|
23
|
22
|
#pragma once
|
24
|
23
|
|
25
|
|
-#include "minmax.h"
|
|
24
|
+#include "config.h"
|
26
|
25
|
|
27
|
26
|
#define NUM_AXIS 4
|
28
|
27
|
#define ABCE 4
|
|
@@ -32,22 +31,6 @@
|
32
|
31
|
|
33
|
32
|
#define _AXIS(A) (A##_AXIS)
|
34
|
33
|
|
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
|
|
-
|
51
|
34
|
// Clock speed factors
|
52
|
35
|
#if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
|
53
|
36
|
#define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
|
|
@@ -57,32 +40,16 @@
|
57
|
40
|
#define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
|
58
|
41
|
|
59
|
42
|
// Remove compiler warning on an unused variable
|
|
43
|
+#undef UNUSED
|
60
|
44
|
#define UNUSED(x) ((void)(x))
|
61
|
45
|
|
62
|
|
-// Macros to make a string from a macro
|
63
|
|
-#define STRINGIFY_(M) #M
|
64
|
|
-#define STRINGIFY(M) STRINGIFY_(M)
|
65
|
|
-
|
|
46
|
+// Assembly wrappers for code and labels
|
66
|
47
|
#define A(CODE) " " CODE "\n\t"
|
67
|
48
|
#define L(CODE) CODE ":\n\t"
|
68
|
49
|
|
69
|
50
|
// 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))
|
75
|
51
|
#define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
|
76
|
52
|
|
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
|
|
-
|
86
|
53
|
#define RADIANS(d) ((d)*float(M_PI)/180.0f)
|
87
|
54
|
#define DEGREES(r) ((r)*180.0f/float(M_PI))
|
88
|
55
|
#define HYPOT2(x,y) (sq(x)+sq(y))
|
|
@@ -91,25 +58,45 @@
|
91
|
58
|
#define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
|
92
|
59
|
|
93
|
60
|
#define SIGN(a) ((a>0)-(a<0))
|
94
|
|
-#define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
|
95
|
61
|
|
96
|
|
-// Macros to constrain values
|
|
62
|
+// Convenience templates / macros
|
|
63
|
+#undef ABS
|
|
64
|
+#undef MIN
|
|
65
|
+#undef MAX
|
97
|
66
|
#ifdef __cplusplus
|
98
|
67
|
|
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;
|
|
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
|
+
|
109
|
94
|
}
|
110
|
95
|
|
111
|
96
|
#else
|
112
|
97
|
|
|
98
|
+ #define ABS(a) ({__typeof__(a) _a = (a); _a >= 0 ? _a : -_a;})
|
|
99
|
+
|
113
|
100
|
// Using GCC extensions, but Travis GCC version does not like it and gives
|
114
|
101
|
// "error: statement-expressions are not allowed outside functions nor in template-argument lists"
|
115
|
102
|
#define NOLESS(v, n) \
|
|
@@ -132,21 +119,30 @@
|
132
|
119
|
else if (v > _n2) v = _n2; \
|
133
|
120
|
} while(0)
|
134
|
121
|
|
|
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
|
+
|
135
|
144
|
#endif
|
136
|
145
|
|
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))
|
150
|
146
|
#define NUMERIC(a) WITHIN(a, '0', '9')
|
151
|
147
|
#define DECIMAL(a) (NUMERIC(a) || a == '.')
|
152
|
148
|
#define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
|
|
@@ -155,45 +151,6 @@
|
155
|
151
|
#define ZERO(a) memset(a,0,sizeof(a))
|
156
|
152
|
#define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b)))
|
157
|
153
|
|
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
|
|
-
|
197
|
154
|
#define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
|
198
|
155
|
#define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
|
199
|
156
|
|
|
@@ -204,13 +161,6 @@
|
204
|
161
|
|
205
|
162
|
#define CEILING(x,y) (((x) + (y) - 1) / (y))
|
206
|
163
|
|
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
|
|
-
|
214
|
164
|
#define UNEAR_ZERO(x) ((x) < 0.000001f)
|
215
|
165
|
#define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
|
216
|
166
|
#define NEAR(x,y) NEAR_ZERO((x)-(y))
|