Skip to content

Commit 38f4eb6

Browse files
committed
Support for old 2.x code by backwards compatible decode(decode_results *aResults) function. Removed USE_OLD_DECODE macro and added NO_LEGACY_COMPATIBILITY macro
1 parent 2694793 commit 38f4eb6

File tree

15 files changed

+289
-193
lines changed

15 files changed

+289
-193
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,23 @@ If you use a core that does not use the `-flto` flag for compile, you can activa
4242
- Since the decoded values are now in `IrReceiver.decodedIRData` and not in `results` any more, remove the line `decode_results results` or similar.
4343
- Like for the Serial object, call [`IrReceiver.begin(IR_RECEIVE_PIN, ENABE_ED_FEEDBACK);`](https://github.com/Arduino-IRremote/Arduino-IRremote/blob/master/examples/ReceiveDemo/ReceiveDemo.ino#L38) or `IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);` instead of the `IrReceiver.enableIRIn();` or `irrecv.enableIRIn();` in setup().
4444
- Old `decode(decode_results *aResults)` function is replaced by simple `decode()`. So if you have a statement `if(irrecv.decode(&results))` replace it with `if (IrReceiver.decode())`.
45-
- The decoded result is now in in `IrReceiver.decodedIRData` and not in `results` any more, therefore replace any occurrences of `results.value` and / or `results.decode_type` (and similar) to `IrReceiver.decodedIRData.decodedRawData` and / or `IrReceiver.decodedIRData.decodedRawData`.
45+
- The decoded result is now in in `IrReceiver.decodedIRData` and not in `results` any more, therefore replace any occurrences of `results.value` and `results.decode_type` (and similar) to `IrReceiver.decodedIRData.decodedRawData` and `IrReceiver.decodedIRData.protocol`.
4646
- Overflow, Repeat and other flags are now in [`IrReceiver.receivedIRData.flags`](https://github.com/Arduino-IRremote/Arduino-IRremote/blob/master/src/IRremote.h#L126).
4747
- Seldom used: `results.rawbuf` and `results.rawlen` must be replaced by `IrReceiver.decodedIRData.rawDataPtr->rawbuf` and `IrReceiver.decodedIRData.rawDataPtr->rawlen`.
48-
- For **NEC, Panasonic, Sony, Samsung and JVC** the `IrReceiver.decodedIRData.decodedRawData` is now LSB-first, as the definition of these protocols suggests! To use your **old MSB-first 32 bit IR data codes**, you must activate the line `#define USE_OLD_DECODE` in IRremoteInt.h.<br/>
49-
To convert one into the other, you must reverse the byte positions and then reverse all bit positions of each byte or write it as one binary string and reverse/mirror it.
48+
49+
# Running your 2.x program with the 3.x library version
50+
- The `results.value` is not set by the new decoders and for **NEC, Panasonic, Sony, Samsung and JVC** the `IrReceiver.decodedIRData.decodedRawData` is now LSB-first, as the definition of these protocols suggests!<br/>
51+
To use your **old MSB-first 32 bit IR data codes**, you must have `decode_results results` and call `decode(&results)` as in most old sources.
52+
- The old functions `sendNEC()` and `sendJVC()` are deprecated and renamed to `sendNECMSB()` and `sendJVCMSB()` to make it clearer that they send data with MSB first, which is not the standard for NEC and JVC. Use them to send your **old MSB-first 32 bit IR data codes**.
53+
In the new version you will send NEC commands not by 32 bit codes but by a (constant) 8 bit address and an 8 bit command.
54+
55+
# Convert old MSB first 32 bit IR data codes to new LSB first 32 bit IR data codes
56+
To convert one into the other, you must reverse the byte positions and then reverse all bit positions of each byte or write it as one binary string and reverse/mirror it.<br/>
5057
Example:<br/>
5158
0xCB340102 byte reverse -> 02 01 34 CB bit reverse-> 40 80 2C D3.<br/>
5259
0xCB340102 is binary 11001011001101000000000100000010.<br/>
5360
0x40802CD3 is binary 01000000100000000010110011010011.<br/>
5461
If you read the first binary sequence backwards (right to left), you get the second sequence.
55-
- The old functions `sendNEC()` and `sendJVC()` are deprecated and renamed to `sendNECMSB()` and `sendJVCMSB()` to make it clearer that they send data with MSB first, which is not the standard for NEC and JVC. Use them to send your **old MSB-first 32 bit IR data codes**.
56-
In the new version you will send NEC commands not by 32 bit codes but by a (constant) 8 bit address and an 8 bit command.
5762

5863
# FAQ
5964
- IR does not work right when I use Neopixels (aka WS2811/WS2812/WS2812B) or other libraries blocking interrupts for a longer time (> 50 us).<br/>
@@ -126,7 +131,7 @@ Modify it by commenting them out or in, or change the values if applicable. Or d
126131
|-|-|-|-|
127132
| `SEND_PWM_BY_TIMER` | Before `#include <IRremote.h>` | disabled | Disable carrier PWM generation in software and use (restricted) hardware PWM ecxept for ESP32 where both modes are using the flexible `hw_timer_t`. |
128133
| `USE_NO_SEND_PWM` | Before `#include <IRremote.h>` | disabled | Use no carrier PWM, just simulate an active low receiver signal. Overrides `SEND_PWM_BY_TIMER` definition. |
129-
| `USE_OLD_DECODE` | IRremoteInt.h | disabled | Enables the old decoder in order to be version 2.x compatible, where all protocols -especially NEC, Panasonic, Sony, Samsung and JVC- were MSB first. |
134+
| `NO_LEGACY_COMPATIBILITY` | IRremoteInt.h | disabled | Disables the old decoder for version 2.x compatibility, where all protocols -especially NEC, Panasonic, Sony, Samsung and JVC- were MSB first. Saves around 60 bytes program space and 14 bytes RAM. |
130135
| `EXCLUDE_EXOTIC_PROTOCOLS` | Before `#include <IRremote.h>` | disabled | If activated, BOSEWAVE, MAGIQUEST,WHYNTER and LEGO_PF are excluded in `decode()` and in sending with `IrSender.write()`. Saves up to 900 bytes program space. |
131136
| `MARK_EXCESS_MICROS` | Before `#include <IRremote.h>` | 20 | MARK_EXCESS_MICROS is subtracted from all marks and added to all spaces before decoding, to compensate for the signal forming of different IR receiver modules. |
132137
| `FEEDBACK_LED_IS_ACTIVE_LOW` | Before `#include <IRremote.h>` | disabled | Required on some boards (like my BluePill and my ESP8266 board), where the feedback LED is active low. |

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The latest version may not be released!
44
## 3.1.1
55
- Fix for ESP32 send Error, removed `USE_SOFT_SEND_PWM` macro.
66
- Added Onkyo protocol.
7+
- Support for old 2.x code by backwards compatible `decode(decode_results *aResults)` function.
8+
- Removed USE_OLD_DECODE macro and added NO_LEGACY_COMPATIBILITY macro.
79

810
## 3.1.0
911
- Generation of PWM by software is active by default.

examples/IRremoteInfo/IRremoteInfo.ino

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,6 @@ void dumpProtocols() {
275275
Serial.println(F("Disabled"));
276276
#endif
277277

278-
Serial.print(F("SHARP: "));
279-
#if defined(DECODE_SHARP)
280-
Serial.println(F("Enabled"));
281-
#else
282-
Serial.println(F("Disabled"));
283-
#endif
284-
285278
Serial.print(F("DENON: "));
286279
#if defined(DECODE_DENON)
287280
Serial.println(F("Enabled"));

examples/SimpleReceiver/SimpleReceiver.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
* Specify which protocol(s) should be used for decoding.
1616
* If no protocol is defined, all protocols are active.
1717
*/
18-
//#define DECODE_DENON
19-
//#define DECODE_SHARP // the same as DECODE_DENON
18+
//#define DECODE_DENON // Includes Sharp
2019
//#define DECODE_JVC
2120
//#define DECODE_KASEIKYO
2221
//#define DECODE_PANASONIC // the same as DECODE_KASEIKYO
2322
//#define DECODE_LG
24-
#define DECODE_NEC
23+
#define DECODE_NEC // Includes Apple and Onkyo
2524
//#define DECODE_SAMSUNG
2625
//#define DECODE_SONY
2726
//#define DECODE_RC5

src/IRReceive.cpp.h

Lines changed: 134 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,6 @@ bool IRrecv::decode() {
246246
return false;
247247
}
248248

249-
#if defined(USE_OLD_DECODE)
250-
// Copy 3 values from irparams for legacy compatibility
251-
results.rawbuf = irparams.rawbuf;
252-
results.rawlen = irparams.rawlen;
253-
results.overflow = irparams.OverflowFlag;
254-
#endif
255-
256249
initDecodedIRData(); // sets IRDATA_FLAGS_WAS_OVERFLOW
257250

258251
if (decodedIRData.flags & IRDATA_FLAGS_WAS_OVERFLOW) {
@@ -270,20 +263,13 @@ bool IRrecv::decode() {
270263
}
271264
#endif
272265

273-
#if defined(DECODE_PANASONIC)
266+
#if defined(DECODE_PANASONIC) || defined(DECODE_KASEIKYO)
274267
TRACE_PRINTLN("Attempting Panasonic/Kaseikyo decode");
275268
if (decodeKaseikyo()) {
276269
return true;
277270
}
278271
#endif
279272

280-
#if defined(DECODE_KASEIKYO) && defined(USE_OLD_DECODE) // if not USE_OLD_DECODE enabled, decodeKaseikyo() is already called by decodePanasonic()
281-
TRACE_PRINTLN("Attempting Panasonic/Kaseikyo decode");
282-
if (decodeKaseikyo()) {
283-
return true;
284-
}
285-
#endif
286-
287273
#if defined(DECODE_DENON)
288274
TRACE_PRINTLN("Attempting Denon/Sharp decode");
289275
if (decodeDenon()) {
@@ -298,13 +284,6 @@ bool IRrecv::decode() {
298284
}
299285
#endif
300286

301-
#if defined(DECODE_SHARP) && ! defined(DECODE_DENON)
302-
TRACE_PRINTLN("Attempting Denon/Sharp decode");
303-
if (decodeSharp()) {
304-
return true;
305-
}
306-
#endif
307-
308287
#if defined(DECODE_RC5)
309288
TRACE_PRINTLN("Attempting RC5 decode");
310289
if (decodeRC5()) {
@@ -335,15 +314,9 @@ bool IRrecv::decode() {
335314

336315
#if defined(DECODE_SAMSUNG)
337316
TRACE_PRINTLN("Attempting Samsung decode");
338-
#if !defined(USE_OLD_DECODE)
339317
if (decodeSamsung()) {
340318
return true;
341319
}
342-
#else
343-
if (decodeSAMSUNG()) {
344-
return true;
345-
}
346-
#endif
347320
#endif
348321
/*
349322
* Start of the exotic protocols
@@ -696,7 +669,6 @@ uint8_t IRrecv::compare(unsigned int oldval, unsigned int newval) {
696669
*
697670
* see: http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
698671
*/
699-
# if !defined(USE_OLD_DECODE)
700672
bool IRrecv::decodeHash() {
701673
long hash = FNV_BASIS_32;
702674

@@ -717,29 +689,30 @@ bool IRrecv::decodeHash() {
717689

718690
return true;
719691
}
720-
# else
721692

722-
bool IRrecv::decodeHash() {
693+
# if !defined(NO_LEGACY_COMPATIBILITY)
694+
bool IRrecv::decodeHashOld(decode_results *aResults) {
723695
long hash = FNV_BASIS_32;
724696

725697
// Require at least 6 samples to prevent triggering on noise
726-
if (results.rawlen < 6) {
698+
if (aResults->rawlen < 6) {
727699
return false;
728700
}
729701

730-
for (unsigned int i = 1; (i + 2) < results.rawlen; i++) {
731-
uint8_t value = compare(results.rawbuf[i], results.rawbuf[i + 2]);
702+
for (unsigned int i = 1; (i + 2) < aResults->rawlen; i++) {
703+
uint8_t value = compare(aResults->rawbuf[i], aResults->rawbuf[i + 2]);
732704
// Add value into the hash
733705
hash = (hash * FNV_PRIME_32) ^ value;
734706
}
735707

736-
results.value = hash;
737-
results.bits = 32;
708+
aResults->value = hash;
709+
aResults->bits = 32;
710+
aResults->decode_type = UNKNOWN;
738711
decodedIRData.protocol = UNKNOWN;
739712

740713
return true;
741714
}
742-
# endif // !defined(USE_OLD_DECODE)
715+
# endif
743716
#endif // DECODE_HASH
744717

745718
/**********************************************************************************************************************
@@ -916,15 +889,11 @@ void printIRResultShort(Print *aSerial, IRData *aIRDataPtr, uint16_t aLeadingSpa
916889
aSerial->print(aIRDataPtr->numberOfBits, DEC);
917890
aSerial->print(F(" bits"));
918891

919-
#if !defined(USE_OLD_DECODE)
920892
if (aIRDataPtr->flags & IRDATA_FLAGS_IS_MSB_FIRST) {
921893
aSerial->println(F(" MSB first"));
922894
} else {
923895
aSerial->println(F(" LSB first"));
924896
}
925-
#else
926-
aSerial->println();
927-
#endif
928897

929898
} else {
930899
aSerial->println();
@@ -1090,6 +1059,8 @@ void IRrecv::compensateAndPrintIRResultAsCArray(Print *aSerial, bool aOutputMicr
10901059
}
10911060

10921061
/**
1062+
* Store the decodedIRData to be used for sendRaw().
1063+
*
10931064
* Compensate received values by MARK_EXCESS_MICROS, like it is done for decoding and store it in an array provided.
10941065
*
10951066
* Maximum for uint8_t is 255*50 microseconds = 12750 microseconds = 12.75 ms, which hardly ever occurs inside an IR sequence.
@@ -1115,12 +1086,7 @@ void IRrecv::compensateAndStoreIRResultInArray(uint8_t *aArrayPtr) {
11151086
}
11161087

11171088
/**
1118-
* Store the decode_results structure to be used for sendRaw().
1119-
*
1120-
* Compensate received values by MARK_EXCESS_MICROS, like it is done for decoding!
1121-
* Maximum for uint8_t is 255*50 microseconds = 12750 microseconds = 12.75 ms, which hardly ever occurs inside an IR sequence.
1122-
* Recording of IRremote anyway stops at a gap of RECORD_GAP_MICROS (5 ms).
1123-
*
1089+
* Print results as C variables to be used for sendXXX()
11241090
* @param aSerial The Print object on which to write, for Arduino you can use &Serial.
11251091
*/
11261092
void IRrecv::printIRResultAsCVariables(Print *aSerial) {
@@ -1351,14 +1317,131 @@ ISR () // for functions definitions which are called by separate (board specific
13511317
#endif
13521318
}
13531319

1320+
#if !defined(NO_LEGACY_COMPATIBILITY)
13541321
/**********************************************************************************************************************
13551322
* The DEPRECATED decode function with parameter aResults for backwards compatibility
13561323
**********************************************************************************************************************/
13571324
bool IRrecv::decode(decode_results *aResults) {
1358-
Serial.println(
1359-
"The function decode(&results)) is deprecated and may not work as expected! Just use decode() - without any parameter.");
1360-
(void) aResults;
1361-
return decode();
1325+
static bool sDeprecationMessageSent = false;
1326+
1327+
if (irparams.StateForISR != IR_REC_STATE_STOP) {
1328+
return false;
1329+
}
1330+
1331+
if (!sDeprecationMessageSent) {
1332+
Serial.println(
1333+
"The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.<fieldname> .");
1334+
sDeprecationMessageSent = true;
1335+
}
1336+
1337+
// copy for usage by legacy programs
1338+
aResults->rawbuf = irparams.rawbuf;
1339+
aResults->rawlen = irparams.rawlen;
1340+
if (irparams.OverflowFlag) {
1341+
// Copy overflow flag to decodedIRData.flags
1342+
irparams.OverflowFlag = false;
1343+
irparams.rawlen = 0; // otherwise we have OverflowFlag again at next ISR call
1344+
DBG_PRINTLN("Overflow happened");
1345+
}
1346+
aResults->overflow = irparams.OverflowFlag;
1347+
aResults->value = 0;
1348+
1349+
decodedIRData.rawDataPtr = &irparams; // for decodePulseDistanceData() etc.
1350+
decodedIRData.flags = IRDATA_FLAGS_IS_MSB_FIRST; // for print
1351+
1352+
#if defined(DECODE_NEC)
1353+
DBG_PRINTLN("Attempting old NEC decode");
1354+
if (decodeNECMSB(aResults)) {
1355+
return true ;
1356+
}
1357+
#endif
1358+
1359+
#if defined(DECODE_SONY)
1360+
DBG_PRINTLN("Attempting old Sony decode");
1361+
if (decodeSonyMSB(aResults)) {
1362+
return true ;
1363+
}
1364+
#endif
1365+
1366+
//#if defined(DECODE_MITSUBISHI)
1367+
// DBG_PRINTLN("Attempting Mitsubishi decode");
1368+
// if (decodeMitsubishi(results)) return true ;
1369+
//#endif
1370+
1371+
#if defined(DECODE_RC5)
1372+
DBG_PRINTLN("Attempting RC5 decode");
1373+
if (decodeRC5()) {
1374+
aResults->bits = decodedIRData.numberOfBits;
1375+
aResults->value = decodedIRData.decodedRawData;
1376+
aResults->decode_type = RC5;
1377+
1378+
return true ;
1379+
}
1380+
#endif
1381+
1382+
#if defined(DECODE_RC6)
1383+
DBG_PRINTLN("Attempting RC6 decode");
1384+
if (decodeRC6()) {
1385+
aResults->bits = decodedIRData.numberOfBits;
1386+
aResults->value = decodedIRData.decodedRawData;
1387+
aResults->decode_type = RC6;
1388+
return true ;
1389+
}
1390+
#endif
1391+
1392+
#if defined( DECODE_PANASONIC)
1393+
DBG_PRINTLN("Attempting old Panasonic decode");
1394+
if (decodePanasonicMSB(aResults)) {
1395+
return true ;
1396+
}
1397+
#endif
1398+
1399+
#if defined(DECODE_LG)
1400+
DBG_PRINTLN("Attempting old LG decode");
1401+
if (decodeLGMSB(aResults)) { return true ;}
1402+
#endif
1403+
1404+
#if defined(DECODE_JVC)
1405+
DBG_PRINTLN("Attempting old JVC decode");
1406+
if (decodeJVCMSB(aResults)) {
1407+
return true ;
1408+
}
1409+
#endif
1410+
1411+
#if defined(DECODE_SAMSUNG)
1412+
DBG_PRINTLN("Attempting old SAMSUNG decode");
1413+
if (decodeSAMSUNG(aResults)) {
1414+
return true ;
1415+
}
1416+
#endif
1417+
1418+
//#if defined(DECODE_WHYNTER)
1419+
// DBG_PRINTLN("Attempting Whynter decode");
1420+
// if (decodeWhynter(results)) return true ;
1421+
//#endif
1422+
1423+
#if defined(DECODE_DENON)
1424+
DBG_PRINTLN("Attempting old Denon decode");
1425+
if (decodeDenonOld(aResults)) {
1426+
return true ;
1427+
}
1428+
#endif
1429+
1430+
//#if defined(DECODE_LEGO_PF)
1431+
// DBG_PRINTLN("Attempting Lego Power Functions");
1432+
// if (decodeLegoPowerFunctions(results)) return true ;
1433+
//#endif
1434+
1435+
// decodeHash returns a hash on any input.
1436+
// Thus, it needs to be last in the list.
1437+
// If you add any decodes, add them before this.
1438+
if (decodeHashOld(aResults)) {
1439+
return true;
1440+
}
1441+
// Throw away and start over
1442+
resume();
1443+
return false;
13621444
}
1445+
#endif
13631446

13641447
/** @}*/

src/IRremote.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,16 @@
6363
* Each protocol you include costs memory and, during decode, costs time
6464
* Disable (deactivate the line by adding a trailing comment "//") all the protocols you do not need/want!
6565
*/
66-
#if (!(defined(DECODE_DENON) || defined(DECODE_SHARP) || defined(DECODE_JVC) || defined(DECODE_KASEIKYO) \
66+
#if (!(defined(DECODE_DENON) || defined(DECODE_JVC) || defined(DECODE_KASEIKYO) \
6767
|| defined(DECODE_PANASONIC) || defined(DECODE_LG) || defined(DECODE_NEC) || defined(DECODE_SAMSUNG) \
6868
|| defined(DECODE_SONY) || defined(DECODE_RC5) || defined(DECODE_RC6) || defined(DECODE_HASH) \
6969
|| defined(DECODE_BOSEWAVE) || defined(DECODE_LEGO_PF) || defined(DECODE_MAGIQUEST) || defined(DECODE_WHYNTER)))
70-
#define DECODE_DENON
71-
#define DECODE_SHARP // the same as DECODE_DENON
70+
#define DECODE_DENON // Includes Sharp
7271
#define DECODE_JVC
7372
#define DECODE_KASEIKYO
7473
#define DECODE_PANASONIC // the same as DECODE_KASEIKYO
7574
#define DECODE_LG
76-
#define DECODE_NEC
75+
#define DECODE_NEC // Includes Apple and Onkyo
7776
#define DECODE_SAMSUNG
7877
#define DECODE_SONY
7978
#define DECODE_RC5

0 commit comments

Comments
 (0)