From 3611c730acaf0a25819f276fdda2de094b3f0ac8 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 10 May 2022 15:31:07 -0300 Subject: [PATCH 1/2] Adds Arduino standard functions --- cores/esp32/Arduino.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index c8be9222b39..87b6e441075 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -69,7 +69,12 @@ #define __STRINGIFY(a) #a #endif +// can't define max() / min() because of conflicts with C++ +#define _min(a,b) ((a)<(b)?(a):(b)) +#define _max(a,b) ((a)>(b)?(a):(b)) +//#define abs(x) ((x)>0?(x):-(x)) // abs() comes from stdlib #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) +#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) #define radians(deg) ((deg)*DEG_TO_RAD) #define degrees(rad) ((rad)*RAD_TO_DEG) #define sq(x) ((x)*(x)) @@ -89,6 +94,7 @@ #define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) +#define bitToggle(value, bit) ((value) ^= (1UL << (bit))) #define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit)) // 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); #include "Esp.h" #include "esp32/spiram.h" +// Use float-compatible stl abs() and round(), we don't use Arduino macros to avoid issues with the C++ libraries using std::abs; using std::isinf; using std::isnan; using std::max; using std::min; -using ::round; +using std::round; uint16_t makeWord(uint16_t w); uint16_t makeWord(uint8_t h, uint8_t l); @@ -203,9 +210,6 @@ void noTone(uint8_t _pin); long random(long); #endif /* __cplusplus */ -#define _min(a,b) ((a)<(b)?(a):(b)) -#define _max(a,b) ((a)>(b)?(a):(b)) - #include "pins_arduino.h" #endif /* _ESP32_CORE_ARDUINO_H_ */ From 995e5b13f69874f7ace006a4469e76b0e227ebac Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 10 May 2022 21:05:19 -0300 Subject: [PATCH 2/2] Fixes round() Adds Arduino basic math Macros with '_' prefix. --- cores/esp32/Arduino.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 87b6e441075..d2f641c5882 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -70,11 +70,11 @@ #endif // can't define max() / min() because of conflicts with C++ -#define _min(a,b) ((a)<(b)?(a):(b)) +#define _min(a,b) ((a)<(b)?(a):(b)) #define _max(a,b) ((a)>(b)?(a):(b)) -//#define abs(x) ((x)>0?(x):-(x)) // abs() comes from stdlib +#define _abs(x) ((x)>0?(x):-(x)) // abs() comes from STL #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) -#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) +#define _round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) // round() comes from STL #define radians(deg) ((deg)*DEG_TO_RAD) #define degrees(rad) ((rad)*RAD_TO_DEG) #define sq(x) ((x)*(x))