IRremote
IRProtocol.hpp
Go to the documentation of this file.
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-2023 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)
37 #define LOCAL_DEBUG
38 #else
39 //#define LOCAL_DEBUG // This enables debug output only for this file
40 #endif
41 
46 const char string_Unknown[] PROGMEM = "UNKNOWN";
47 const char string_PulseWidth[] PROGMEM = "PulseWidth";
48 const char string_PulseDistance[] PROGMEM = "PulseDistance";
49 const char string_Apple[] PROGMEM = "Apple";
50 const char string_Denon[] PROGMEM = "Denon";
51 const char string_JVC[] PROGMEM = "JVC";
52 const char string_LG[] PROGMEM = "LG";
53 const char string_LG2[] PROGMEM = "LG2";
54 const char string_NEC[] PROGMEM = "NEC";
55 const char string_NEC2[] PROGMEM = "NEC2";
56 const char string_Onkyo[] PROGMEM = "Onkyo";
57 const char string_Panasonic[] PROGMEM = "Panasonic";
58 const char string_Kaseikyo[] PROGMEM = "Kaseikyo";
59 const char string_Kaseikyo_Denon[] PROGMEM = "Kaseikyo_Denon";
60 const char string_Kaseikyo_Sharp[] PROGMEM = "Kaseikyo_Sharp";
61 const char string_Kaseikyo_JVC[] PROGMEM = "Kaseikyo_JVC";
62 const char string_Kaseikyo_Mitsubishi[] PROGMEM = "Kaseikyo_Mitsubishi";
63 const char string_RC5[] PROGMEM = "RC5";
64 const char string_RC6[] PROGMEM = "RC6";
65 const char string_RC6A[] PROGMEM = "RC6A";
66 const char string_Samsung[] PROGMEM = "Samsung";
67 const char string_SamsungLG[] PROGMEM = "SamsungLG";
68 const char string_Samsung48[] PROGMEM = "Samsung48";
69 const char string_Sharp[] PROGMEM = "Sharp";
70 const char string_Sony[] PROGMEM = "Sony";
71 const char string_BangOlufsen[] PROGMEM = "Bang&Olufsen";
72 const char string_BoseWave[] PROGMEM = "BoseWave";
73 const char string_Lego[] PROGMEM = "Lego";
74 const char string_MagiQuest[] PROGMEM = "MagiQuest";
75 const char string_Whynter[] PROGMEM = "Whynter";
76 const char string_FAST[] PROGMEM = "FAST";
77 const char string_Other[] PROGMEM = "OTHER";
78 
79 /*
80  * !!Must be the same order as in decode_type_t in IRProtocol.h!!!
81  */
82 const char *const ProtocolNames[]
83 PROGMEM = { string_Unknown, string_PulseWidth, string_PulseDistance, string_Apple, string_Denon, string_JVC, string_LG, string_LG2,
84  string_NEC, string_NEC2, string_Onkyo, string_Panasonic, string_Kaseikyo, string_Kaseikyo_Denon, string_Kaseikyo_Sharp,
85  string_Kaseikyo_JVC, string_Kaseikyo_Mitsubishi, string_RC5, string_RC6, string_RC6A, string_Samsung, string_SamsungLG,
86  string_Samsung48, string_Sharp, string_Sony
87 #if !defined(EXCLUDE_EXOTIC_PROTOCOLS)
88  , string_BangOlufsen, string_BoseWave, string_Lego, string_MagiQuest, string_Whynter, string_FAST, string_Other
89 #endif
90  };
91 
92 #if defined(__AVR__)
93 const __FlashStringHelper* getProtocolString(decode_type_t aProtocol) {
94  const char *tProtocolStringPtr = (char*) pgm_read_word(&ProtocolNames[aProtocol]);
95  return ((__FlashStringHelper*) (tProtocolStringPtr));
96 }
97 #else
98 const char* getProtocolString(decode_type_t aProtocol) {
99  return ProtocolNames[aProtocol];
100 }
101 #endif
102 
103 #if (__INT_WIDTH__ >= 32)
104 # if __has_include(<type_traits>)
105 /*
106  * This code to handle the missing print(unsigned long long...) function of seeduino core was contributed by sklott
107  * https://stackoverflow.com/questions/74622227/avoid-calling-of-function-size-t-printprintunsigned-long-long-n-int-base-if
108  */
109 #include <type_traits>
110 
111 // If you have C++17 you can just use std::void_t, or use this for all versions
112 #if __cpp_lib_void_t >= 201411L
113 template<typename T>
114 using void_t = std::void_t<T>;
115 #else
116 template<typename ... Ts> struct make_void {
117  typedef void type;
118 };
119 template<typename ... Ts> using void_t = typename make_void<Ts...>::type;
120 #endif
121 
122 // Detecting if we have print(unsigned long long value, int base) / print(0ull, 0) overload
123 template<typename T, typename = void>
124 struct has_ull_print: std::false_type {
125 };
126 template<typename T>
127 struct has_ull_print<T, void_t<decltype(std::declval<T>().print(0ull, 0))>> : std::true_type {
128 };
129 
130 // Must be namespace, to avoid public and static declarations for class
131 namespace PrintULL {
132 template<typename PrintImplType, typename std::enable_if<!has_ull_print<PrintImplType>::value, bool>::type = true>
133 size_t print(PrintImplType *p, unsigned long long value, int base) {
134  size_t tLength = p->print(static_cast<uint32_t>(value >> 32), base);
135  tLength += p->print(static_cast<uint32_t>(value), base);
136  return tLength;
137 }
138 
139 template<typename PrintImplType, typename std::enable_if<has_ull_print<PrintImplType>::value, bool>::type = true>
140 size_t print(PrintImplType *p, unsigned long long value, int base) {
141  return p->print(value, base);
142 }
143 }
144 ;
145 # else
146 namespace PrintULL {
147  size_t print(Print *aSerial, unsigned long long n, int base) {
148  return aSerial->print(n, base);
149  }
150 };
151 # endif
152 #endif
153 
158 /**********************************************************************************************************************
159  * Function to bit reverse OLD MSB values of e.g. NEC.
160  **********************************************************************************************************************/
161 uint8_t bitreverseOneByte(uint8_t aValue) {
162 // uint8_t tReversedValue;
163 // return __builtin_avr_insert_bits(0x01234567, aValue, tReversedValue);
164 // 76543210
165  aValue = (aValue >> 4) | (aValue << 4); // Swap in groups of 4
166 // 32107654
167  aValue = ((aValue & 0xcc) >> 2) | ((aValue & 0x33) << 2); // Swap in groups of 2
168 // 10325476
169  aValue = ((aValue & 0xaa) >> 1) | ((aValue & 0x55) << 1); // Swap bit pairs
170 // 01234567
171  return aValue;
172 }
173 
174 uint32_t bitreverse32Bit(uint32_t aInput) {
175 // __builtin_avr_insert_bits();
176  LongUnion tValue;
177  tValue.UByte.HighByte = bitreverseOneByte(aInput);
178  tValue.UByte.MidHighByte = bitreverseOneByte(aInput >> 8);
179  tValue.UByte.MidLowByte = bitreverseOneByte(aInput >> 16);
180  tValue.UByte.LowByte = bitreverseOneByte(aInput >> 24);
181  return tValue.ULong;
182 }
183 
186 #if defined(LOCAL_DEBUG)
187 #undef LOCAL_DEBUG
188 #endif
189 #endif // _IR_PROTOCOL_HPP
LongUnion
Union to specify parts / manifestations of a 32 bit Long without casts and shifts.
Definition: LongUnion.h:59
LongUnion::UByte
struct LongUnion::@4 UByte
decode_type_t
decode_type_t
An enum consisting of all supported formats.
Definition: IRProtocol.h:93
LongUnion::LowByte
uint8_t LowByte
Definition: LongUnion.h:61
LongUnion::HighByte
uint8_t HighByte
Definition: LongUnion.h:64
LongUnion::MidLowByte
uint8_t MidLowByte
Definition: LongUnion.h:62
ProtocolNames
const char *const ProtocolNames[]
Definition: IRProtocol.hpp:83
bitreverse32Bit
uint32_t bitreverse32Bit(uint32_t aInput)
Definition: IRProtocol.hpp:174
LongUnion::ULong
uint32_t ULong
Definition: LongUnion.h:95
PROGMEM
const char string_Unknown[] PROGMEM
Definition: IRProtocol.hpp:46
LongUnion::MidHighByte
uint8_t MidHighByte
Definition: LongUnion.h:63
bitreverseOneByte
uint8_t bitreverseOneByte(uint8_t aValue)
Definition: IRProtocol.hpp:161
getProtocolString
const char * getProtocolString(decode_type_t aProtocol)
Definition: IRProtocol.hpp:98