Skip to content

Commit

Permalink
[Build] Add compatibility with C++20
Browse files Browse the repository at this point in the history
  • Loading branch information
tonhuisman committed Oct 6, 2023
1 parent a295f87 commit 9cfcebc
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 20 deletions.
4 changes: 4 additions & 0 deletions examples/IRMQTTServer/IRMQTTServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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;
Expand Down
76 changes: 66 additions & 10 deletions src/IRrecv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ namespace _IRrecv { // Namespace extension
#if defined(ESP32)
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
#endif // ESP32
#if __cplusplus >= 202002L
atomic<irparams_t> params;
#else
volatile irparams_t params;
#endif
irparams_t *params_save; // A copy of the interrupt state while decoding.
} // namespace _IRrecv

Expand Down Expand Up @@ -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<irparams_t> *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)
Expand Down Expand Up @@ -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<uint16_t> *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) {
Expand Down Expand Up @@ -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<uint16_t> *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,
Expand Down Expand Up @@ -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<uint16_t> *data_ptr,
#else
volatile uint16_t *data_ptr,
#endif
uint64_t *result_bits_ptr,
uint8_t *result_bytes_ptr,
const bool use_bits,
Expand Down Expand Up @@ -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<uint16_t> *data_ptr,
#else
volatile uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand Down Expand Up @@ -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<uint16_t> *data_ptr,
#else
volatile uint16_t *data_ptr,
#endif
uint8_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand Down Expand Up @@ -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<uint16_t> *data_ptr,
#else
volatile uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand Down Expand Up @@ -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<const uint16_t> *data_ptr,
#else
volatile const uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand Down Expand Up @@ -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<const uint16_t> *data_ptr,
#else
volatile const uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand Down Expand Up @@ -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<irparams_t>
#else
volatile irparams_t
#endif
*IRrecv::_getParamsPtr(void) {
return &params;
}
#endif // UNIT_TEST
Expand Down
79 changes: 69 additions & 10 deletions src/IRrecv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t> *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?
Expand Down Expand Up @@ -171,11 +175,23 @@ class IRrecv {
uint16_t _unknown_threshold;
#endif
#ifdef UNIT_TEST
volatile irparams_t *_getParamsPtr(void);
#if __cplusplus >= 202002L
atomic<irparams_t>
#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<irparams_t>
#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,
Expand All @@ -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<uint16_t> *data_ptr,
#else
volatile uint16_t *data_ptr,
#endif
uint64_t *result_bits_ptr,
uint8_t *result_ptr,
const bool use_bits,
Expand All @@ -204,22 +225,39 @@ 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<uint16_t> *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<uint16_t> *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,
const uint8_t tolerance = kUseDefTol,
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<uint16_t> *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,
Expand All @@ -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<uint16_t> *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,
Expand All @@ -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<uint16_t> *data_ptr,
#else
volatile uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand All @@ -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<const uint16_t> *data_ptr,
#else
volatile const uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand All @@ -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<const> uint16_t *data_ptr,
#else
volatile const uint16_t *data_ptr,
#endif
uint64_t *result_ptr,
const uint16_t remaining,
const uint16_t nbits,
Expand Down
3 changes: 3 additions & 0 deletions src/IRremoteESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#include <iostream>
#include <string>
#endif // UNIT_TEST
#if __cplusplus >= 202002L
#include <atomic>
#endif

// Library Version Information
// Major version number (X.x.x)
Expand Down
4 changes: 4 additions & 0 deletions src/IRutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,11 @@ namespace irutils {
/// issue has been found.
uint8_t lowLevelSanityCheck(void) {
const uint64_t kExpectedBitFieldResult = 0x8000012340000039ULL;
#if __cplusplus >= 202002L
atomic<uint32_t> EndianTest = 0x12345678;
#else
volatile uint32_t EndianTest = 0x12345678;
#endif
const uint8_t kBitFieldError = 0b01;
const uint8_t kEndiannessError = 0b10;
uint8_t result = 0;
Expand Down
4 changes: 4 additions & 0 deletions test/IRrecv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ TEST(TestIRrecv, DecodeHeapOverflow) {
IRrecv irrecv(1);
irrecv.enableIRIn();
ASSERT_EQ(kRawBuf, irrecv.getBufSize());
#if __cplusplus >= 202002L
atomic<irparams_t> *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];
Expand Down

0 comments on commit 9cfcebc

Please sign in to comment.