Skip to content

Commit 1fed09b

Browse files
authored
Adds Arduino standard macro-functions (espressif#6726)
* Adds Arduino standard functions * Fixes round() Adds Arduino basic math Macros with '_' prefix.
1 parent 49bdd5f commit 1fed09b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: cores/esp32/Arduino.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@
6969
#define __STRINGIFY(a) #a
7070
#endif
7171

72+
// can't define max() / min() because of conflicts with C++
73+
#define _min(a,b) ((a)<(b)?(a):(b))
74+
#define _max(a,b) ((a)>(b)?(a):(b))
75+
#define _abs(x) ((x)>0?(x):-(x)) // abs() comes from STL
7276
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
77+
#define _round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) // round() comes from STL
7378
#define radians(deg) ((deg)*DEG_TO_RAD)
7479
#define degrees(rad) ((rad)*RAD_TO_DEG)
7580
#define sq(x) ((x)*(x))
@@ -89,6 +94,7 @@
8994
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
9095
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
9196
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
97+
#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
9298
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
9399

94100
// avr-libc defines _NOP() since 1.6.2
@@ -168,12 +174,13 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
168174
#include "Esp.h"
169175
#include "esp32/spiram.h"
170176

177+
// Use float-compatible stl abs() and round(), we don't use Arduino macros to avoid issues with the C++ libraries
171178
using std::abs;
172179
using std::isinf;
173180
using std::isnan;
174181
using std::max;
175182
using std::min;
176-
using ::round;
183+
using std::round;
177184

178185
uint16_t makeWord(uint16_t w);
179186
uint16_t makeWord(uint8_t h, uint8_t l);
@@ -203,9 +210,6 @@ void noTone(uint8_t _pin);
203210
long random(long);
204211
#endif /* __cplusplus */
205212

206-
#define _min(a,b) ((a)<(b)?(a):(b))
207-
#define _max(a,b) ((a)>(b)?(a):(b))
208-
209213
#include "pins_arduino.h"
210214

211215
#endif /* _ESP32_CORE_ARDUINO_H_ */

0 commit comments

Comments
 (0)