Skip to content

Commit 9cfcebc

Browse files
committed
[Build] Add compatibility with C++20
1 parent a295f87 commit 9cfcebc

File tree

6 files changed

+150
-20
lines changed

6 files changed

+150
-20
lines changed

examples/IRMQTTServer/IRMQTTServer.ino

+4
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,11 @@ char Hostname[kHostnameLength + 1] = "ir_server"; // Default hostname.
443443
uint16_t *codeArray;
444444
uint32_t lastReconnectAttempt = 0; // MQTT last attempt reconnection number
445445
bool boot = true;
446+
#if __cplusplus >= 202002L
447+
atomic<bool> lockIr = false; // Primitive locking for gating the IR LED.
448+
#else
446449
volatile bool lockIr = false; // Primitive locking for gating the IR LED.
450+
#endif
447451
uint32_t sendReqCounter = 0;
448452
bool lastSendSucceeded = false; // Store the success status of the last send.
449453
uint32_t lastSendTime = 0;

src/IRrecv.cpp

+66-10
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ namespace _IRrecv { // Namespace extension
144144
#if defined(ESP32)
145145
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
146146
#endif // ESP32
147+
#if __cplusplus >= 202002L
148+
atomic<irparams_t> params;
149+
#else
147150
volatile irparams_t params;
151+
#endif
148152
irparams_t *params_save; // A copy of the interrupt state while decoding.
149153
} // namespace _IRrecv
150154

@@ -437,7 +441,13 @@ void IRrecv::resume(void) {
437441
/// i.e. In kStopState.
438442
/// @param[in] src Pointer to an irparams_t structure to copy from.
439443
/// @param[out] dst Pointer to an irparams_t structure to copy to.
440-
void IRrecv::copyIrParams(volatile irparams_t *src, irparams_t *dst) {
444+
void IRrecv::copyIrParams(
445+
#if __cplusplus >= 202002L
446+
atomic<irparams_t> *src
447+
#else
448+
volatile irparams_t *src
449+
#endif
450+
, irparams_t *dst) {
441451
// Typecast src and dst addresses to (char *)
442452
char *csrc = (char *)src; // NOLINT(readability/casting)
443453
char *cdst = (char *)dst; // NOLINT(readability/casting)
@@ -1449,7 +1459,12 @@ bool IRrecv::decodeHash(decode_results *results) {
14491459
/// @return A match_result_t structure containing the success (or not), the
14501460
/// data value, and how many buffer entries were used.
14511461
match_result_t IRrecv::matchData(
1452-
volatile uint16_t *data_ptr, const uint16_t nbits, const uint16_t onemark,
1462+
#if __cplusplus >= 202002L
1463+
atomic<uint16_t> *data_ptr
1464+
#else
1465+
volatile uint16_t *data_ptr
1466+
#endif
1467+
, const uint16_t nbits, const uint16_t onemark,
14531468
const uint32_t onespace, const uint16_t zeromark, const uint32_t zerospace,
14541469
const uint8_t tolerance, const int16_t excess, const bool MSBfirst,
14551470
const bool expectlastspace) {
@@ -1509,7 +1524,13 @@ match_result_t IRrecv::matchData(
15091524
/// true is Most Significant Bit First Order, false is Least Significant First
15101525
/// @param[in] expectlastspace Do we expect a space at the end of the message?
15111526
/// @return If successful, how many buffer entries were used. Otherwise 0.
1512-
uint16_t IRrecv::matchBytes(volatile uint16_t *data_ptr, uint8_t *result_ptr,
1527+
uint16_t IRrecv::matchBytes(
1528+
#if __cplusplus >= 202002L
1529+
atomic<uint16_t> *data_ptr
1530+
#else
1531+
volatile uint16_t *data_ptr
1532+
#endif
1533+
, uint8_t *result_ptr,
15131534
const uint16_t remaining, const uint16_t nbytes,
15141535
const uint16_t onemark, const uint32_t onespace,
15151536
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,
15611582
/// @param[in] MSBfirst Bit order to save the data in. (Def: true)
15621583
/// true is Most Significant Bit First Order, false is Least Significant First
15631584
/// @return If successful, how many buffer entries were used. Otherwise 0.
1564-
uint16_t IRrecv::_matchGeneric(volatile uint16_t *data_ptr,
1585+
uint16_t IRrecv::_matchGeneric(
1586+
#if __cplusplus >= 202002L
1587+
atomic<uint16_t> *data_ptr,
1588+
#else
1589+
volatile uint16_t *data_ptr,
1590+
#endif
15651591
uint64_t *result_bits_ptr,
15661592
uint8_t *result_bytes_ptr,
15671593
const bool use_bits,
@@ -1663,7 +1689,12 @@ uint16_t IRrecv::_matchGeneric(volatile uint16_t *data_ptr,
16631689
/// @param[in] MSBfirst Bit order to save the data in. (Def: true)
16641690
/// true is Most Significant Bit First Order, false is Least Significant First
16651691
/// @return If successful, how many buffer entries were used. Otherwise 0.
1666-
uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr,
1692+
uint16_t IRrecv::matchGeneric(
1693+
#if __cplusplus >= 202002L
1694+
atomic<uint16_t> *data_ptr,
1695+
#else
1696+
volatile uint16_t *data_ptr,
1697+
#endif
16671698
uint64_t *result_ptr,
16681699
const uint16_t remaining,
16691700
const uint16_t nbits,
@@ -1710,7 +1741,12 @@ uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr,
17101741
/// @param[in] MSBfirst Bit order to save the data in. (Def: true)
17111742
/// true is Most Significant Bit First Order, false is Least Significant First
17121743
/// @return If successful, how many buffer entries were used. Otherwise 0.
1713-
uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr,
1744+
uint16_t IRrecv::matchGeneric(
1745+
#if __cplusplus >= 202002L
1746+
atomic<uint16_t> *data_ptr,
1747+
#else
1748+
volatile uint16_t *data_ptr,
1749+
#endif
17141750
uint8_t *result_ptr,
17151751
const uint16_t remaining,
17161752
const uint16_t nbits,
@@ -1757,7 +1793,12 @@ uint16_t IRrecv::matchGeneric(volatile uint16_t *data_ptr,
17571793
/// @return If successful, how many buffer entries were used. Otherwise 0.
17581794
/// @note Parameters one + zero add up to the total time for a bit.
17591795
/// e.g. mark(one) + space(zero) is a `1`, mark(zero) + space(one) is a `0`.
1760-
uint16_t IRrecv::matchGenericConstBitTime(volatile uint16_t *data_ptr,
1796+
uint16_t IRrecv::matchGenericConstBitTime(
1797+
#if __cplusplus >= 202002L
1798+
atomic<uint16_t> *data_ptr,
1799+
#else
1800+
volatile uint16_t *data_ptr,
1801+
#endif
17611802
uint64_t *result_ptr,
17621803
const uint16_t remaining,
17631804
const uint16_t nbits,
@@ -1844,7 +1885,12 @@ uint16_t IRrecv::matchGenericConstBitTime(volatile uint16_t *data_ptr,
18441885
/// @return If successful, how many buffer entries were used. Otherwise 0.
18451886
/// @see https://en.wikipedia.org/wiki/Manchester_code
18461887
/// @see http://ww1.microchip.com/downloads/en/AppNotes/Atmel-9164-Manchester-Coding-Basics_Application-Note.pdf
1847-
uint16_t IRrecv::matchManchester(volatile const uint16_t *data_ptr,
1888+
uint16_t IRrecv::matchManchester(
1889+
#if __cplusplus >= 202002L
1890+
atomic<const uint16_t> *data_ptr,
1891+
#else
1892+
volatile const uint16_t *data_ptr,
1893+
#endif
18481894
uint64_t *result_ptr,
18491895
const uint16_t remaining,
18501896
const uint16_t nbits,
@@ -1951,7 +1997,12 @@ uint16_t IRrecv::matchManchester(volatile const uint16_t *data_ptr,
19511997
/// @see https://en.wikipedia.org/wiki/Manchester_code
19521998
/// @see http://ww1.microchip.com/downloads/en/AppNotes/Atmel-9164-Manchester-Coding-Basics_Application-Note.pdf
19531999
/// @todo Clean up and optimise this. It is just "get it working code" atm.
1954-
uint16_t IRrecv::matchManchesterData(volatile const uint16_t *data_ptr,
2000+
uint16_t IRrecv::matchManchesterData(
2001+
#if __cplusplus >= 202002L
2002+
atomic<const uint16_t> *data_ptr,
2003+
#else
2004+
volatile const uint16_t *data_ptr,
2005+
#endif
19552006
uint64_t *result_ptr,
19562007
const uint16_t remaining,
19572008
const uint16_t nbits,
@@ -2072,7 +2123,12 @@ uint16_t IRrecv::matchManchesterData(volatile const uint16_t *data_ptr,
20722123

20732124
#if UNIT_TEST
20742125
/// Unit test helper to get access to the params structure.
2075-
volatile irparams_t *IRrecv::_getParamsPtr(void) {
2126+
#if __cplusplus >= 202002L
2127+
atomic<irparams_t>
2128+
#else
2129+
volatile irparams_t
2130+
#endif
2131+
*IRrecv::_getParamsPtr(void) {
20762132
return &params;
20772133
}
20782134
#endif // UNIT_TEST

src/IRrecv.h

+69-10
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ class decode_results {
111111
uint8_t state[kStateSizeMax]; // Multi-byte results.
112112
};
113113
uint16_t bits; // Number of bits in decoded value
114+
#if __cplusplus >= 202002L
115+
atomic<uint16_t> *rawbuf; // Raw intervals in .5 us ticks
116+
#else
114117
volatile uint16_t *rawbuf; // Raw intervals in .5 us ticks
118+
#endif
115119
uint16_t rawlen; // Number of records in rawbuf.
116120
bool overflow;
117121
bool repeat; // Is the result a repeat code?
@@ -171,11 +175,23 @@ class IRrecv {
171175
uint16_t _unknown_threshold;
172176
#endif
173177
#ifdef UNIT_TEST
174-
volatile irparams_t *_getParamsPtr(void);
178+
#if __cplusplus >= 202002L
179+
atomic<irparams_t>
180+
#else
181+
volatile irparams_t
182+
#endif
183+
*_getParamsPtr(void);
175184
#endif // UNIT_TEST
176185
// These are called by decode
177186
uint8_t _validTolerance(const uint8_t percentage);
178-
void copyIrParams(volatile irparams_t *src, irparams_t *dst);
187+
void copyIrParams(
188+
#if __cplusplus >= 202002L
189+
atomic<irparams_t>
190+
#else
191+
volatile irparams_t
192+
#endif
193+
*src,
194+
irparams_t *dst);
179195
uint16_t compare(const uint16_t oldval, const uint16_t newval);
180196
uint32_t ticksLow(const uint32_t usecs,
181197
const uint8_t tolerance = kUseDefTol,
@@ -186,7 +202,12 @@ class IRrecv {
186202
bool matchAtLeast(const uint32_t measured, const uint32_t desired,
187203
const uint8_t tolerance = kUseDefTol,
188204
const uint16_t delta = 0);
189-
uint16_t _matchGeneric(volatile uint16_t *data_ptr,
205+
uint16_t _matchGeneric(
206+
#if __cplusplus >= 202002L
207+
atomic<uint16_t> *data_ptr,
208+
#else
209+
volatile uint16_t *data_ptr,
210+
#endif
190211
uint64_t *result_bits_ptr,
191212
uint8_t *result_ptr,
192213
const bool use_bits,
@@ -204,22 +225,39 @@ class IRrecv {
204225
const uint8_t tolerance = kUseDefTol,
205226
const int16_t excess = kMarkExcess,
206227
const bool MSBfirst = true);
207-
match_result_t matchData(volatile uint16_t *data_ptr, const uint16_t nbits,
228+
match_result_t matchData(
229+
#if __cplusplus >= 202002L
230+
atomic<uint16_t> *data_ptr
231+
#else
232+
volatile uint16_t *data_ptr
233+
#endif
234+
, const uint16_t nbits,
208235
const uint16_t onemark, const uint32_t onespace,
209236
const uint16_t zeromark, const uint32_t zerospace,
210237
const uint8_t tolerance = kUseDefTol,
211238
const int16_t excess = kMarkExcess,
212239
const bool MSBfirst = true,
213240
const bool expectlastspace = true);
214-
uint16_t matchBytes(volatile uint16_t *data_ptr, uint8_t *result_ptr,
241+
uint16_t matchBytes(
242+
#if __cplusplus >= 202002L
243+
atomic<uint16_t> *data_ptr
244+
#else
245+
volatile uint16_t *data_ptr
246+
#endif
247+
, uint8_t *result_ptr,
215248
const uint16_t remaining, const uint16_t nbytes,
216249
const uint16_t onemark, const uint32_t onespace,
217250
const uint16_t zeromark, const uint32_t zerospace,
218251
const uint8_t tolerance = kUseDefTol,
219252
const int16_t excess = kMarkExcess,
220253
const bool MSBfirst = true,
221254
const bool expectlastspace = true);
222-
uint16_t matchGeneric(volatile uint16_t *data_ptr,
255+
uint16_t matchGeneric(
256+
#if __cplusplus >= 202002L
257+
atomic<uint16_t> *data_ptr,
258+
#else
259+
volatile uint16_t *data_ptr,
260+
#endif
223261
uint64_t *result_ptr,
224262
const uint16_t remaining, const uint16_t nbits,
225263
const uint16_t hdrmark, const uint32_t hdrspace,
@@ -230,7 +268,13 @@ class IRrecv {
230268
const uint8_t tolerance = kUseDefTol,
231269
const int16_t excess = kMarkExcess,
232270
const bool MSBfirst = true);
233-
uint16_t matchGeneric(volatile uint16_t *data_ptr, uint8_t *result_ptr,
271+
uint16_t matchGeneric(
272+
#if __cplusplus >= 202002L
273+
atomic<uint16_t> *data_ptr,
274+
#else
275+
volatile uint16_t *data_ptr,
276+
#endif
277+
uint8_t *result_ptr,
234278
const uint16_t remaining, const uint16_t nbits,
235279
const uint16_t hdrmark, const uint32_t hdrspace,
236280
const uint16_t onemark, const uint32_t onespace,
@@ -241,7 +285,12 @@ class IRrecv {
241285
const uint8_t tolerance = kUseDefTol,
242286
const int16_t excess = kMarkExcess,
243287
const bool MSBfirst = true);
244-
uint16_t matchGenericConstBitTime(volatile uint16_t *data_ptr,
288+
uint16_t matchGenericConstBitTime(
289+
#if __cplusplus >= 202002L
290+
atomic<uint16_t> *data_ptr,
291+
#else
292+
volatile uint16_t *data_ptr,
293+
#endif
245294
uint64_t *result_ptr,
246295
const uint16_t remaining,
247296
const uint16_t nbits,
@@ -255,7 +304,12 @@ class IRrecv {
255304
const uint8_t tolerance = kUseDefTol,
256305
const int16_t excess = kMarkExcess,
257306
const bool MSBfirst = true);
258-
uint16_t matchManchesterData(volatile const uint16_t *data_ptr,
307+
uint16_t matchManchesterData(
308+
#if __cplusplus >= 202002L
309+
atomic<const uint16_t> *data_ptr,
310+
#else
311+
volatile const uint16_t *data_ptr,
312+
#endif
259313
uint64_t *result_ptr,
260314
const uint16_t remaining,
261315
const uint16_t nbits,
@@ -265,7 +319,12 @@ class IRrecv {
265319
const int16_t excess = kMarkExcess,
266320
const bool MSBfirst = true,
267321
const bool GEThomas = true);
268-
uint16_t matchManchester(volatile const uint16_t *data_ptr,
322+
uint16_t matchManchester(
323+
#if __cplusplus >= 202002L
324+
atomic<const> uint16_t *data_ptr,
325+
#else
326+
volatile const uint16_t *data_ptr,
327+
#endif
269328
uint64_t *result_ptr,
270329
const uint16_t remaining,
271330
const uint16_t nbits,

src/IRremoteESP8266.h

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
#include <iostream>
5252
#include <string>
5353
#endif // UNIT_TEST
54+
#if __cplusplus >= 202002L
55+
#include <atomic>
56+
#endif
5457

5558
// Library Version Information
5659
// Major version number (X.x.x)

src/IRutils.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,11 @@ namespace irutils {
13971397
/// issue has been found.
13981398
uint8_t lowLevelSanityCheck(void) {
13991399
const uint64_t kExpectedBitFieldResult = 0x8000012340000039ULL;
1400+
#if __cplusplus >= 202002L
1401+
atomic<uint32_t> EndianTest = 0x12345678;
1402+
#else
14001403
volatile uint32_t EndianTest = 0x12345678;
1404+
#endif
14011405
const uint8_t kBitFieldError = 0b01;
14021406
const uint8_t kEndiannessError = 0b10;
14031407
uint8_t result = 0;

test/IRrecv_test.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ TEST(TestIRrecv, DecodeHeapOverflow) {
4848
IRrecv irrecv(1);
4949
irrecv.enableIRIn();
5050
ASSERT_EQ(kRawBuf, irrecv.getBufSize());
51+
#if __cplusplus >= 202002L
52+
atomic<irparams_t> *params_ptr = irrecv._getParamsPtr();
53+
#else
5154
volatile irparams_t *params_ptr = irrecv._getParamsPtr();
55+
#endif
5256
// replace the buffer with a slightly bigger one to see if we go past the end
5357
// accidentally.
5458
params_ptr->rawbuf = new uint16_t[kRawBuf + 10];

0 commit comments

Comments
 (0)