Skip to content

Commit 964cdea

Browse files
committed
Converted the series of function-like macros in Arduino.h into constexpr functions in the case of C++. Required in order to avoid conflicts to C++ stdlib.
1 parent c3d11a5 commit 964cdea

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Diff for: cores/arduino/Arduino.h

+64
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@ void yield(void);
9393
}; // extern "C"
9494
#endif
9595

96+
#if defined(__cplusplus)
97+
template <typename T> constexpr T min(const T a, const T b){
98+
return a<b?a:b;
99+
}
100+
101+
template <typename T> constexpr T max(const T a, const T b){
102+
return a>b?a:b;
103+
}
104+
105+
template <typename T> constexpr T abs(const T x){
106+
return x>0?x:-x;
107+
}
108+
109+
template <typename T> constexpr T constrain(const T amt, const T low, const T high){
110+
return amt<low?low:(amt>high?high:amt);
111+
}
112+
113+
template <typename T> constexpr T round(const T x){
114+
return x>=0?static_cast<long>(x+0.5):static_cast<long>(x-0.5);
115+
}
116+
117+
template <typename T> constexpr T radians(const T deg){
118+
return deg * DEG_TO_RAD;
119+
}
120+
121+
template <typename T> constexpr T degrees(const T rad){
122+
return rad * RAD_TO_DEG;
123+
}
124+
125+
template <typename T> constexpr T sq(const T x){
126+
return x * x;
127+
}
128+
#else
96129
#define min(a,b) ((a)<(b)?(a):(b))
97130
#define max(a,b) ((a)>(b)?(a):(b))
98131
#define abs(x) ((x)>0?(x):-(x))
@@ -101,10 +134,39 @@ void yield(void);
101134
#define radians(deg) ((deg)*DEG_TO_RAD)
102135
#define degrees(rad) ((rad)*RAD_TO_DEG)
103136
#define sq(x) ((x)*(x))
137+
#endif
104138

105139
#define interrupts() sei()
106140
#define noInterrupts() cli()
107141

142+
#if defined(__cplusplus)
143+
constexpr unsigned long clockCyclesPerMicrosecond(){
144+
return F_CPU / 1000000L;
145+
}
146+
147+
template <typename T> constexpr T clockCyclesToMicroseconds(const T a){
148+
return a / clockCyclesPerMicrosecond();
149+
}
150+
151+
template <typename T> constexpr T microsecondsToClockCycles(const T a){
152+
return a * clockCyclesPerMicrosecond();
153+
}
154+
155+
constexpr unsigned char lowByte(uint16_t w){
156+
return w & 0xff;
157+
}
158+
159+
constexpr unsigned char highByte(uint16_t w){
160+
return w >> 8;
161+
}
162+
163+
template <typename T>
164+
constexpr bool bitRead(T value, unsigned char bit){
165+
return (value >> bit) & 0x01;
166+
}
167+
168+
#else
169+
108170
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
109171
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
110172
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
@@ -113,6 +175,8 @@ void yield(void);
113175
#define highByte(w) ((uint8_t) ((w) >> 8))
114176

115177
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
178+
#endif
179+
116180
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
117181
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
118182
#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))

0 commit comments

Comments
 (0)