Skip to content

Commit 7892d5c

Browse files
committed
Extracted protocol functions used by receive and send to IRProtocol.hpp
1 parent 5f50263 commit 7892d5c

File tree

24 files changed

+326
-254
lines changed

24 files changed

+326
-254
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ Modify them by enabling / disabling them, or change the values if applicable.
534534
| `USE_NO_SEND_PWM` | disabled | Uses no carrier PWM, just simulate an **active low** receiver signal. Overrides `SEND_PWM_BY_TIMER` definition. |
535535
| `IR_SEND_DUTY_CYCLE_PERCENT` | 30 | Duty cycle of IR send signal. |
536536
| `USE_OPEN_DRAIN_OUTPUT_FOR_SEND_PIN` | disabled | Uses or simulates open drain output mode at send pin. **Attention, active state of open drain is LOW**, so connect the send LED between positive supply and send pin! |
537-
| `DISABLE_RECEIVER_RESTART_AFTER_SENDING` | disabled | Saves up to 450 bytes program memory and 269 bytes RAM if receiving functionality is not required. |
537+
| `DISABLE_CODE_FOR_RECEIVER` | disabled | Saves up to 450 bytes program memory and 269 bytes RAM if receiving functionality is not required. |
538538
| `EXCLUDE_EXOTIC_PROTOCOLS` | disabled | Excludes BANG_OLUFSEN, BOSEWAVE, WHYNTER and LEGO_PF from `decode()` and from sending with `IrSender.write()`. Saves up to 650 bytes program memory. |
539539
| `FEEDBACK_LED_IS_ACTIVE_LOW` | disabled | Required on some boards (like my BluePill and my ESP8266 board), where the feedback LED is active low. |
540540
| `NO_LED_FEEDBACK_CODE` | disabled | Disables the LED feedback code for send and receive. Saves around 100 bytes program memory for receiving, around 500 bytes for sending and halving the receiver ISR (Interrupt Service Routine) processing time. |

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ See also the commit log at github: https://github.com/Arduino-IRremote/Arduino-I
2222
- Improved TinyIRReceiver and added FAST protocol for it.
2323
- Removed static function printIRSendUsage(), but kept class function printIRSendUsage().
2424
- Added TinyIRSender.hpp and MinimalSender example, renamed TinyReceiver.h to TinyIR.h.
25-
- Added DISABLE_RECEIVER_RESTART_AFTER_SENDING to save program memory and RAM if receiving functionality is not required.
25+
- Added DISABLE_CODE_FOR_RECEIVER to save program memory and RAM if receiving functionality is not required.
26+
- Extracted protocol functions used by receive and send to IRProtocol.hpp.
2627

2728
## 3.9.0
2829
- Improved documentation with the help of [ElectronicsArchiver}(https://github.com/ElectronicsArchiver).

examples/SendBoseWaveDemo/SendBoseWaveDemo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
#include <Arduino.h>
3535

36-
#define DISABLE_RECEIVER_RESTART_AFTER_SENDING // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
36+
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
3737

3838
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
3939
#include <IRremote.hpp>

examples/SendDemo/SendDemo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include <Arduino.h>
3434

35-
#define DISABLE_RECEIVER_RESTART_AFTER_SENDING // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
35+
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
3636

3737
//#define EXCLUDE_EXOTIC_PROTOCOLS // Saves around 240 bytes program memory if IrSender.write is used
3838
//#define SEND_PWM_BY_TIMER // Disable carrier PWM generation in software and use (restricted) hardware PWM.

examples/SendLGAirConditionerDemo/SendLGAirConditionerDemo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//#define USE_LG2_PROTOCOL // Try it if you do not have success with the default LG protocol
4343
#define NUMBER_OF_COMMANDS_BETWEEN_PRINT_OF_MENU 5
4444

45-
#define DISABLE_RECEIVER_RESTART_AFTER_SENDING // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
45+
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
4646

4747
#define INFO // Deactivate this to save program memory and suppress info output from the LG-AC driver.
4848
//#define DEBUG // Activate this for more output from the LG-AC driver.

examples/SendProntoDemo/SendProntoDemo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
#include <Arduino.h>
3434

35-
#define DISABLE_RECEIVER_RESTART_AFTER_SENDING // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
35+
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
3636

3737
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
3838
#include <IRremote.hpp>

examples/SendRawDemo/SendRawDemo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
#include <Arduino.h>
3939

40-
#define DISABLE_RECEIVER_RESTART_AFTER_SENDING // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
40+
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
4141

4242
//#define SEND_PWM_BY_TIMER // Disable carrier PWM generation in software and use (restricted) hardware PWM.
4343
//#define USE_NO_SEND_PWM // Use no carrier PWM, just simulate an active low receiver signal. Overrides SEND_PWM_BY_TIMER definition

examples/SimpleSender/SimpleSender.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
#include <Arduino.h>
1515

16-
#define DISABLE_RECEIVER_RESTART_AFTER_SENDING // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
16+
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
1717
//#define SEND_PWM_BY_TIMER // Disable carrier PWM generation in software and use (restricted) hardware PWM.
1818
//#define USE_NO_SEND_PWM // Use no carrier PWM, just simulate an active low receiver signal. Overrides SEND_PWM_BY_TIMER definition
1919

src/IRProtocol.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,42 @@ struct PulseDistanceWidthProtocolConstants {
115115
// void (IRsend::*SpecialSendRepeatFunction)();
116116
};
117117

118+
/*
119+
* Definitions for member IRData.flags
120+
*/
121+
#define IRDATA_FLAGS_EMPTY 0x00
122+
#define IRDATA_FLAGS_IS_REPEAT 0x01
123+
#define IRDATA_FLAGS_IS_AUTO_REPEAT 0x02
124+
#define IRDATA_FLAGS_PARITY_FAILED 0x04 ///< the current (autorepeat) frame violated parity check
125+
#define IRDATA_FLAGS_TOGGLE_BIT 0x08 ///< is set if RC5 or RC6 toggle bit is set
126+
#define IRDATA_FLAGS_EXTRA_INFO 0x10 ///< there is extra info not contained in address and data (e.g. Kaseikyo unknown vendor ID)
127+
#define IRDATA_FLAGS_WAS_OVERFLOW 0x40 ///< irparams.rawlen is 0 in this case to avoid endless OverflowFlag
128+
#define IRDATA_FLAGS_IS_LSB_FIRST 0x00
129+
#define IRDATA_FLAGS_IS_MSB_FIRST 0x80 ///< Just for info. Value is mainly determined by the protocol
130+
131+
// deprecated
132+
#define IRDATA_TOGGLE_BIT_MASK 0x08 ///< is set if RC5 or RC6 toggle bit is set
133+
134+
#define RAW_DATA_ARRAY_SIZE ((((RAW_BUFFER_LENGTH - 2) - 1) / 64) + 1) // The -2 is for initial gap + stop bit mark, 64 mark + spaces for 32 bit.
135+
136+
/**
137+
* Data structure for the user application, available as decodedIRData.
138+
* Filled by decoders and read by print functions or user application.
139+
*/
140+
struct IRData {
141+
decode_type_t protocol; ///< UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
142+
uint16_t address; ///< Decoded address, Distance protocol (tMarkTicksLong (if tMarkTicksLong == 0, then tMarkTicksShort) << 8) | tSpaceTicksLong
143+
uint16_t command; ///< Decoded command, Distance protocol (tMarkTicksShort << 8) | tSpaceTicksShort
144+
uint16_t extra; ///< Contains upper 16 bit of Magiquest WandID, Kaseikyo unknown vendor ID and Distance protocol (HeaderMarkTicks << 8) | HeaderSpaceTicks.
145+
uint16_t numberOfBits; ///< Number of bits received for data (address + command + parity) - to determine protocol length if different length are possible.
146+
uint8_t flags; ///< See IRDATA_FLAGS_* definitions above
147+
uint32_t decodedRawData; ///< Up to 32 bit decoded raw data, to be used for send functions.
148+
# if defined(DECODE_DISTANCE_WIDTH)
149+
uint32_t decodedRawDataArray[RAW_DATA_ARRAY_SIZE]; ///< 32 bit decoded raw data, to be used for send function.
150+
# endif
151+
irparams_struct *rawDataPtr; ///< Pointer of the raw timing data to be decoded. Mainly the OverflowFlag and the data buffer filled by receiving ISR.
152+
};
153+
118154
#define PROTOCOL_IS_LSB_FIRST false
119155
#define PROTOCOL_IS_MSB_FIRST true
120156

@@ -134,4 +170,17 @@ struct PulseDistanceWidthProtocolConstants {
134170
#define KASEIKYO_KHZ 37
135171
#define RC5_RC6_KHZ 36
136172

173+
#if defined(__AVR__)
174+
const __FlashStringHelper* getProtocolString(decode_type_t aProtocol);
175+
#else
176+
const char* getProtocolString(decode_type_t aProtocol);
177+
#endif
178+
void printIRResultShort(Print *aSerial, IRData *aIRDataPtr, bool aPrintGap); // A static function to be able to print send or copied received data.
179+
180+
/*
181+
* Convenience functions to convert MSB to LSB values
182+
*/
183+
uint8_t bitreverseOneByte(uint8_t aValue);
184+
uint32_t bitreverse32Bit(uint32_t aInput);
185+
137186
#endif // _IR_PROTOCOL_H

src/IRProtocol.hpp

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* IRReceive.hpp
3+
* This file is exclusively included by IRremote.h to enable easy configuration of library switches
4+
*
5+
* Contains all protocol functions used by receiver and sender.
6+
*
7+
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
8+
*
9+
************************************************************************************
10+
* MIT License
11+
*
12+
* Copyright (c) 2009-2022 Ken Shirriff, Rafi Khan, Armin Joachimsmeyer
13+
*
14+
* Permission is hereby granted, free of charge, to any person obtaining a copy
15+
* of this software and associated documentation files (the "Software"), to deal
16+
* in the Software without restriction, including without limitation the rights
17+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
* copies of the Software, and to permit persons to whom the Software is furnished
19+
* to do so, subject to the following conditions:
20+
*
21+
* The above copyright notice and this permission notice shall be included in all
22+
* copies or substantial portions of the Software.
23+
*
24+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
25+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
26+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28+
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
29+
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30+
*
31+
************************************************************************************
32+
*/
33+
#ifndef _IR_PROTOCOL_HPP
34+
#define _IR_PROTOCOL_HPP
35+
36+
#if defined(DEBUG) && !defined(LOCAL_DEBUG)
37+
#define LOCAL_DEBUG
38+
#else
39+
//#define LOCAL_DEBUG // This enables debug output only for this file
40+
#endif
41+
42+
/*
43+
* Check for additional characteristics of timing like length of mark for a constant mark protocol,
44+
* where space length determines the bit value. Requires up to 194 additional bytes of program memory.
45+
*/
46+
//#define DECODE_STRICT_CHECKS
47+
/** \addtogroup Receiving Receiving IR data for multiple protocols
48+
* @{
49+
*/
50+
51+
/*
52+
* !!Must be the same order as in decode_type_t in IRProtocol.h!!!
53+
*/
54+
const char *const ProtocolNames[]
55+
PROGMEM = { string_Unknown, string_PulseWidth, string_PulseDistance, string_Apple, string_Denon, string_JVC, string_LG, string_LG2,
56+
string_NEC, string_NEC2, string_Onkyo, string_Panasonic, string_Kaseikyo, string_Kaseikyo_Denon, string_Kaseikyo_Sharp,
57+
string_Kaseikyo_JVC, string_Kaseikyo_Mitsubishi, string_RC5, string_RC6, string_Samsung, string_SamsungLG, string_Sharp,
58+
string_Sony
59+
#if !defined(EXCLUDE_EXOTIC_PROTOCOLS)
60+
, string_BangOlufsen, string_BoseWave, string_Lego, string_MagiQuest, string_Whynter
61+
#endif
62+
};
63+
64+
#if defined(__AVR__)
65+
const __FlashStringHelper* getProtocolString(decode_type_t aProtocol) {
66+
const char *tProtocolStringPtr = (char*) pgm_read_word(&ProtocolNames[aProtocol]);
67+
return ((__FlashStringHelper*) (tProtocolStringPtr));
68+
}
69+
#else
70+
const char* getProtocolString(decode_type_t aProtocol) {
71+
return ProtocolNames[aProtocol];
72+
}
73+
#endif
74+
75+
/**
76+
* Function to print decoded result and flags in one line.
77+
* A static function to be able to print data to send or copied received data.
78+
* Ends with println().
79+
*
80+
* @param aSerial The Print object on which to write, for Arduino you can use &Serial.
81+
* @param aIRDataPtr Pointer to the data to be printed.
82+
* @param aPrintRepeatGap If true also print the gap before repeats.
83+
* @param aCheckForRecordGapsMicros If true, call CheckForRecordGapsMicros() which may do a long printout,
84+
* which in turn may block the proper detection of repeats.
85+
* @return true, if CheckForRecordGapsMicros() has printed a message, i.e. gap <
86+
*
87+
*/
88+
void printIRResultShort(Print *aSerial, IRData *aIRDataPtr, bool aPrintRepeatGap) {
89+
aSerial->print(F("Protocol="));
90+
aSerial->print(getProtocolString(aIRDataPtr->protocol));
91+
if (aIRDataPtr->protocol == UNKNOWN) {
92+
#if defined(DECODE_HASH)
93+
aSerial->print(F(" Hash=0x"));
94+
aSerial->print(aIRDataPtr->decodedRawData, HEX);
95+
#endif
96+
#if !defined(DISABLE_CODE_FOR_RECEIVER)
97+
aSerial->print(' ');
98+
aSerial->print((aIRDataPtr->rawDataPtr->rawlen + 1) / 2, DEC);
99+
aSerial->println(F(" bits (incl. gap and start) received"));
100+
#endif
101+
} else {
102+
#if defined(DECODE_DISTANCE_WIDTH)
103+
if (aIRDataPtr->protocol != PULSE_DISTANCE && aIRDataPtr->protocol != PULSE_WIDTH) {
104+
#endif
105+
/*
106+
* New decoders have address and command
107+
*/
108+
aSerial->print(F(" Address=0x"));
109+
aSerial->print(aIRDataPtr->address, HEX);
110+
111+
aSerial->print(F(" Command=0x"));
112+
aSerial->print(aIRDataPtr->command, HEX);
113+
114+
if (aIRDataPtr->flags & IRDATA_FLAGS_EXTRA_INFO) {
115+
aSerial->print(F(" Extra=0x"));
116+
aSerial->print(aIRDataPtr->extra, HEX);
117+
}
118+
119+
if (aIRDataPtr->flags & IRDATA_FLAGS_PARITY_FAILED) {
120+
aSerial->print(F(" Parity fail"));
121+
}
122+
123+
if (aIRDataPtr->flags & IRDATA_FLAGS_TOGGLE_BIT) {
124+
if (aIRDataPtr->protocol == NEC) {
125+
aSerial->print(F(" Special repeat"));
126+
} else {
127+
aSerial->print(F(" Toggle=1"));
128+
}
129+
}
130+
#if defined(DECODE_DISTANCE_WIDTH)
131+
}
132+
#endif
133+
if (aIRDataPtr->flags & (IRDATA_FLAGS_IS_AUTO_REPEAT | IRDATA_FLAGS_IS_REPEAT)) {
134+
aSerial->print(' ');
135+
if (aIRDataPtr->flags & IRDATA_FLAGS_IS_AUTO_REPEAT) {
136+
aSerial->print(F("Auto-"));
137+
}
138+
aSerial->print(F("Repeat"));
139+
#if !defined(DISABLE_CODE_FOR_RECEIVER)
140+
if (aPrintRepeatGap) {
141+
aSerial->print(F(" gap="));
142+
aSerial->print((uint32_t) aIRDataPtr->rawDataPtr->rawbuf[0] * MICROS_PER_TICK);
143+
aSerial->print(F("us"));
144+
}
145+
#else
146+
(void)aPrintRepeatGap;
147+
#endif
148+
}
149+
150+
/*
151+
* Print raw data
152+
*/
153+
if (!(aIRDataPtr->flags & IRDATA_FLAGS_IS_REPEAT) || aIRDataPtr->decodedRawData != 0) {
154+
aSerial->print(F(" Raw-Data=0x"));
155+
aSerial->print(aIRDataPtr->decodedRawData, HEX);
156+
157+
/*
158+
* Print number of bits processed
159+
*/
160+
aSerial->print(' ');
161+
aSerial->print(aIRDataPtr->numberOfBits, DEC);
162+
aSerial->print(F(" bits"));
163+
164+
if (aIRDataPtr->flags & IRDATA_FLAGS_IS_MSB_FIRST) {
165+
aSerial->println(F(" MSB first"));
166+
} else {
167+
aSerial->println(F(" LSB first"));
168+
}
169+
170+
} else {
171+
aSerial->println();
172+
}
173+
}
174+
}
175+
176+
/**********************************************************************************************************************
177+
* Function to bit reverse OLD MSB values of e.g. NEC.
178+
**********************************************************************************************************************/
179+
uint8_t bitreverseOneByte(uint8_t aValue) {
180+
// uint8_t tReversedValue;
181+
// return __builtin_avr_insert_bits(0x01234567, aValue, tReversedValue);
182+
// 76543210
183+
aValue = (aValue >> 4) | (aValue << 4); // Swap in groups of 4
184+
// 32107654
185+
aValue = ((aValue & 0xcc) >> 2) | ((aValue & 0x33) << 2); // Swap in groups of 2
186+
// 10325476
187+
aValue = ((aValue & 0xaa) >> 1) | ((aValue & 0x55) << 1); // Swap bit pairs
188+
// 01234567
189+
return aValue;
190+
}
191+
192+
uint32_t bitreverse32Bit(uint32_t aInput) {
193+
// __builtin_avr_insert_bits();
194+
LongUnion tValue;
195+
tValue.UByte.HighByte = bitreverseOneByte(aInput);
196+
tValue.UByte.MidHighByte = bitreverseOneByte(aInput >> 8);
197+
tValue.UByte.MidLowByte = bitreverseOneByte(aInput >> 16);
198+
tValue.UByte.LowByte = bitreverseOneByte(aInput >> 24);
199+
return tValue.ULong;
200+
}
201+
202+
/** @}*/
203+
204+
#if defined(LOCAL_DEBUG)
205+
#undef LOCAL_DEBUG
206+
#endif
207+
#endif // _IR_PROTOCOL_HPP

0 commit comments

Comments
 (0)