diff --git a/examples/IRMQTTServer/IRMQTTServer.ino b/examples/IRMQTTServer/IRMQTTServer.ino index 35a528107..78045c50a 100644 --- a/examples/IRMQTTServer/IRMQTTServer.ino +++ b/examples/IRMQTTServer/IRMQTTServer.ino @@ -443,7 +443,11 @@ char Hostname[kHostnameLength + 1] = "ir_server"; // Default hostname. uint16_t *codeArray; uint32_t lastReconnectAttempt = 0; // MQTT last attempt reconnection number bool boot = true; +#if __cplusplus >= 202002L +atomic lockIr = false; // Primitive locking for gating the IR LED. +#else volatile bool lockIr = false; // Primitive locking for gating the IR LED. +#endif uint32_t sendReqCounter = 0; bool lastSendSucceeded = false; // Store the success status of the last send. uint32_t lastSendTime = 0; diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 173526104..544da42c5 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -144,7 +144,11 @@ namespace _IRrecv { // Namespace extension #if defined(ESP32) portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; #endif // ESP32 +#if __cplusplus >= 202002L +atomic params; +#else volatile irparams_t params; +#endif irparams_t *params_save; // A copy of the interrupt state while decoding. } // namespace _IRrecv @@ -437,7 +441,13 @@ void IRrecv::resume(void) { /// i.e. In kStopState. /// @param[in] src Pointer to an irparams_t structure to copy from. /// @param[out] dst Pointer to an irparams_t structure to copy to. -void IRrecv::copyIrParams(volatile irparams_t *src, irparams_t *dst) { +void IRrecv::copyIrParams( + #if __cplusplus >= 202002L + atomic *src + #else + volatile irparams_t *src + #endif + , irparams_t *dst) { // Typecast src and dst addresses to (char *) char *csrc = (char *)src; // NOLINT(readability/casting) char *cdst = (char *)dst; // NOLINT(readability/casting) @@ -1449,7 +1459,12 @@ bool IRrecv::decodeHash(decode_results *results) { /// @return A match_result_t structure containing the success (or not), the /// data value, and how many buffer entries were used. match_result_t IRrecv::matchData( - volatile uint16_t *data_ptr, const uint16_t nbits, const uint16_t onemark, + #if __cplusplus >= 202002L + atomic *data_ptr + #else + volatile uint16_t *data_ptr + #endif + , const uint16_t nbits, const uint16_t onemark, const uint32_t onespace, const uint16_t zeromark, const uint32_t zerospace, const uint8_t tolerance, const int16_t excess, const bool MSBfirst, const bool expectlastspace) { @@ -1509,7 +1524,13 @@ match_result_t IRrecv::matchData( /// true is Most Significant Bit First Order, false is Least Significant First /// @param[in] expectlastspace Do we expect a space at the end of the message? /// @return If successful, how many buffer entries were used. Otherwise 0. -uint16_t IRrecv::matchBytes(volatile uint16_t *data_ptr, uint8_t *result_ptr, +uint16_t IRrecv::matchBytes( + #if __cplusplus >= 202002L + atomic *data_ptr + #else + volatile uint16_t *data_ptr + #endif + , uint8_t *result_ptr, const uint16_t remaining, const uint16_t nbytes, const uint16_t onemark, const uint32_t onespace, const uint16_t zeromark, const uint32_t zerospace, @@ -1561,7 +1582,12 @@ uint16_t IRrecv::matchBytes(volatile uint16_t *data_ptr, uint8_t *result_ptr, /// @param[in] MSBfirst Bit order to save the data in. (Def: true) /// true is Most Significant Bit First Order, false is Least Significant First /// @return If successful, how many buffer entries were used. Otherwise 0. -uint16_t IRrecv::_matchGeneric(volatile uint16_t *data_ptr, +uint16_t IRrecv::_matchGeneric( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint64_t *result_bits_ptr, uint8_t *result_bytes_ptr, const bool use_bits, @@ -1663,7 +1689,12 @@ uint16_t IRrecv::_matchGeneric(volatile uint16_t *data_ptr, /// @param[in] MSBfirst Bit order to save the data in. (Def: true) /// true is Most Significant Bit First Order, false is Least Significant First /// @return If successful, how many buffer entries were used. Otherwise 0. -uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr, +uint16_t IRrecv::matchGeneric( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -1710,7 +1741,12 @@ uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr, /// @param[in] MSBfirst Bit order to save the data in. (Def: true) /// true is Most Significant Bit First Order, false is Least Significant First /// @return If successful, how many buffer entries were used. Otherwise 0. -uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr, +uint16_t IRrecv::matchGeneric( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint8_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -1757,7 +1793,12 @@ uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr, /// @return If successful, how many buffer entries were used. Otherwise 0. /// @note Parameters one + zero add up to the total time for a bit. /// e.g. mark(one) + space(zero) is a `1`, mark(zero) + space(one) is a `0`. -uint16_t IRrecv::matchGenericConstBitTime(volatile uint16_t *data_ptr, +uint16_t IRrecv::matchGenericConstBitTime( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -1844,7 +1885,12 @@ uint16_t IRrecv::matchGenericConstBitTime(volatile uint16_t *data_ptr, /// @return If successful, how many buffer entries were used. Otherwise 0. /// @see https://en.wikipedia.org/wiki/Manchester_code /// @see http://ww1.microchip.com/downloads/en/AppNotes/Atmel-9164-Manchester-Coding-Basics_Application-Note.pdf -uint16_t IRrecv::matchManchester(volatile const uint16_t *data_ptr, +uint16_t IRrecv::matchManchester( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile const uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -1951,7 +1997,12 @@ uint16_t IRrecv::matchManchester(volatile const uint16_t *data_ptr, /// @see https://en.wikipedia.org/wiki/Manchester_code /// @see http://ww1.microchip.com/downloads/en/AppNotes/Atmel-9164-Manchester-Coding-Basics_Application-Note.pdf /// @todo Clean up and optimise this. It is just "get it working code" atm. -uint16_t IRrecv::matchManchesterData(volatile const uint16_t *data_ptr, +uint16_t IRrecv::matchManchesterData( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile const uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -2072,7 +2123,12 @@ uint16_t IRrecv::matchManchesterData(volatile const uint16_t *data_ptr, #if UNIT_TEST /// Unit test helper to get access to the params structure. -volatile irparams_t *IRrecv::_getParamsPtr(void) { +#if __cplusplus >= 202002L +atomic +#else +volatile irparams_t +#endif + *IRrecv::_getParamsPtr(void) { return ¶ms; } #endif // UNIT_TEST diff --git a/src/IRrecv.h b/src/IRrecv.h index 7adf5eb1b..87c8cefd6 100644 --- a/src/IRrecv.h +++ b/src/IRrecv.h @@ -111,7 +111,11 @@ class decode_results { uint8_t state[kStateSizeMax]; // Multi-byte results. }; uint16_t bits; // Number of bits in decoded value + #if __cplusplus >= 202002L + atomic *rawbuf; // Raw intervals in .5 us ticks + #else volatile uint16_t *rawbuf; // Raw intervals in .5 us ticks + #endif uint16_t rawlen; // Number of records in rawbuf. bool overflow; bool repeat; // Is the result a repeat code? @@ -171,11 +175,23 @@ class IRrecv { uint16_t _unknown_threshold; #endif #ifdef UNIT_TEST - volatile irparams_t *_getParamsPtr(void); + #if __cplusplus >= 202002L + atomic + #else + volatile irparams_t + #endif + *_getParamsPtr(void); #endif // UNIT_TEST // These are called by decode uint8_t _validTolerance(const uint8_t percentage); - void copyIrParams(volatile irparams_t *src, irparams_t *dst); + void copyIrParams( + #if __cplusplus >= 202002L + atomic + #else + volatile irparams_t + #endif + *src, + irparams_t *dst); uint16_t compare(const uint16_t oldval, const uint16_t newval); uint32_t ticksLow(const uint32_t usecs, const uint8_t tolerance = kUseDefTol, @@ -186,7 +202,12 @@ class IRrecv { bool matchAtLeast(const uint32_t measured, const uint32_t desired, const uint8_t tolerance = kUseDefTol, const uint16_t delta = 0); - uint16_t _matchGeneric(volatile uint16_t *data_ptr, + uint16_t _matchGeneric( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint64_t *result_bits_ptr, uint8_t *result_ptr, const bool use_bits, @@ -204,14 +225,26 @@ class IRrecv { const uint8_t tolerance = kUseDefTol, const int16_t excess = kMarkExcess, const bool MSBfirst = true); - match_result_t matchData(volatile uint16_t *data_ptr, const uint16_t nbits, + match_result_t matchData( + #if __cplusplus >= 202002L + atomic *data_ptr + #else + volatile uint16_t *data_ptr + #endif + , const uint16_t nbits, const uint16_t onemark, const uint32_t onespace, const uint16_t zeromark, const uint32_t zerospace, const uint8_t tolerance = kUseDefTol, const int16_t excess = kMarkExcess, const bool MSBfirst = true, const bool expectlastspace = true); - uint16_t matchBytes(volatile uint16_t *data_ptr, uint8_t *result_ptr, + uint16_t matchBytes( + #if __cplusplus >= 202002L + atomic *data_ptr + #else + volatile uint16_t *data_ptr + #endif + , uint8_t *result_ptr, const uint16_t remaining, const uint16_t nbytes, const uint16_t onemark, const uint32_t onespace, const uint16_t zeromark, const uint32_t zerospace, @@ -219,7 +252,12 @@ class IRrecv { const int16_t excess = kMarkExcess, const bool MSBfirst = true, const bool expectlastspace = true); - uint16_t matchGeneric(volatile uint16_t *data_ptr, + uint16_t matchGeneric( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, const uint16_t hdrmark, const uint32_t hdrspace, @@ -230,7 +268,13 @@ class IRrecv { const uint8_t tolerance = kUseDefTol, const int16_t excess = kMarkExcess, const bool MSBfirst = true); - uint16_t matchGeneric(volatile uint16_t *data_ptr, uint8_t *result_ptr, + uint16_t matchGeneric( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif + uint8_t *result_ptr, const uint16_t remaining, const uint16_t nbits, const uint16_t hdrmark, const uint32_t hdrspace, const uint16_t onemark, const uint32_t onespace, @@ -241,7 +285,12 @@ class IRrecv { const uint8_t tolerance = kUseDefTol, const int16_t excess = kMarkExcess, const bool MSBfirst = true); - uint16_t matchGenericConstBitTime(volatile uint16_t *data_ptr, + uint16_t matchGenericConstBitTime( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -255,7 +304,12 @@ class IRrecv { const uint8_t tolerance = kUseDefTol, const int16_t excess = kMarkExcess, const bool MSBfirst = true); - uint16_t matchManchesterData(volatile const uint16_t *data_ptr, + uint16_t matchManchesterData( + #if __cplusplus >= 202002L + atomic *data_ptr, + #else + volatile const uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, @@ -265,7 +319,12 @@ class IRrecv { const int16_t excess = kMarkExcess, const bool MSBfirst = true, const bool GEThomas = true); - uint16_t matchManchester(volatile const uint16_t *data_ptr, + uint16_t matchManchester( + #if __cplusplus >= 202002L + atomic uint16_t *data_ptr, + #else + volatile const uint16_t *data_ptr, + #endif uint64_t *result_ptr, const uint16_t remaining, const uint16_t nbits, diff --git a/src/IRremoteESP8266.h b/src/IRremoteESP8266.h index 949de1ecf..33e71af2c 100644 --- a/src/IRremoteESP8266.h +++ b/src/IRremoteESP8266.h @@ -51,6 +51,9 @@ #include #include #endif // UNIT_TEST +#if __cplusplus >= 202002L +#include +#endif // Library Version Information // Major version number (X.x.x) diff --git a/src/IRutils.cpp b/src/IRutils.cpp index e9af0b28f..05dd55d03 100644 --- a/src/IRutils.cpp +++ b/src/IRutils.cpp @@ -1397,7 +1397,11 @@ namespace irutils { /// issue has been found. uint8_t lowLevelSanityCheck(void) { const uint64_t kExpectedBitFieldResult = 0x8000012340000039ULL; + #if __cplusplus >= 202002L + atomic EndianTest = 0x12345678; + #else volatile uint32_t EndianTest = 0x12345678; + #endif const uint8_t kBitFieldError = 0b01; const uint8_t kEndiannessError = 0b10; uint8_t result = 0; diff --git a/test/IRrecv_test.cpp b/test/IRrecv_test.cpp index 2d32847d3..2470d3f31 100644 --- a/test/IRrecv_test.cpp +++ b/test/IRrecv_test.cpp @@ -48,7 +48,11 @@ TEST(TestIRrecv, DecodeHeapOverflow) { IRrecv irrecv(1); irrecv.enableIRIn(); ASSERT_EQ(kRawBuf, irrecv.getBufSize()); + #if __cplusplus >= 202002L + atomic *params_ptr = irrecv._getParamsPtr(); + #else volatile irparams_t *params_ptr = irrecv._getParamsPtr(); + #endif // replace the buffer with a slightly bigger one to see if we go past the end // accidentally. params_ptr->rawbuf = new uint16_t[kRawBuf + 10];