IRremote
ir_Denon.hpp
Go to the documentation of this file.
1 /*
2  * ir_Denon.cpp
3  *
4  * Contains functions for receiving and sending Denon/Sharp IR Protocol
5  *
6  * This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
7  *
8  ************************************************************************************
9  * MIT License
10  *
11  * Copyright (c) 2020-2023 Armin Joachimsmeyer
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this software and associated documentation files (the "Software"), to deal
15  * in the Software without restriction, including without limitation the rights
16  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  * copies of the Software, and to permit persons to whom the Software is furnished
18  * to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in all
21  * copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
24  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
25  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
28  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29  *
30  ************************************************************************************
31  */
32 #ifndef _IR_DENON_HPP
33 #define _IR_DENON_HPP
34 
35 #if defined(DEBUG)
36 #define LOCAL_DEBUG
37 #else
38 //#define LOCAL_DEBUG // This enables debug output only for this file
39 #endif
40 
44 //==============================================================================
45 // DDDD EEEEE N N OOO N N
46 // D D E NN N O O NN N
47 // D D EEE N N N O O N N N
48 // D D E N NN O O N NN
49 // DDDD EEEEE N N OOO N N
50 //==============================================================================
51 // SSSS H H AAA RRRR PPPP
52 // S H H A A R R P P
53 // SSS HHHHH AAAAA RRRR PPPP
54 // S H H A A R R P
55 // SSSS H H A A R R P
56 //==============================================================================
57 /*
58  Protocol=Denon Address=0x11 Command=0x76 Raw-Data=0xED1 15 bits LSB first
59  + 200,-1800 + 300,- 750 + 300,- 800 + 200,- 800
60  + 250,-1800 + 250,- 800 + 250,-1800 + 300,-1750
61  + 300,- 750 + 300,-1800 + 250,-1800 + 250,-1850
62  + 250,- 750 + 300,- 800 + 250,- 800 + 250
63  Sum: 23050
64 
65  Denon/Sharp variant
66  Protocol=Denon Address=0x11 Command=0x76 Raw-Data=0x4ED1 15 bits LSB first
67  + 200,-1800 + 300,- 750 + 250,- 800 + 250,- 750
68  + 300,-1800 + 250,- 800 + 250,-1800 + 300,-1750
69  + 300,- 750 + 300,-1800 + 250,-1800 + 250,-1800
70  + 300,- 750 + 300,- 750 + 300,-1800 + 250
71  Sum: 23050
72  */
73 /*
74  * https://www.mikrocontroller.net/articles/IRMP_-_english#DENON
75  * Denon published all their IR codes:
76  * http://assets.denon.com/documentmaster/us/denon%20master%20ir%20hex.xls
77  * Example:
78  * 0000 006D 0000 0020 000A 001E 000A 0046 000A 001E 000A 001E 000A 001E // 5 address bits
79  * 000A 001E 000A 001E 000A 0046 000A 0046 000A 0046 000A 001E 000A 0046 000A 0046 // 8 command bits
80  * 000A 001E 000A 001E 000A 0679 // 2 frame bits 0,0 + stop bit + space for AutoRepeat
81  * 000A 001E 000A 0046 000A 001E 000A 001E 000A 001E // 5 address bits
82  * 000A 0046 000A 0046 000A 001E 000A 001E 000A 001E 000A 0046 000A 001E 000A 001E // 8 inverted command bits
83  * 000A 0046 000A 0046 000A 0679 // 2 frame bits 1,1 + stop bit + space for Repeat
84  * From analyzing the codes for Tuner preset 1 to 8 in tab Main Zone ID#1 it is obvious, that the protocol is LSB first at least for command.
85  * All Denon codes with 32 as 3. value use the Kaseikyo Denon variant.
86  */
87 // LSB first, no start bit, 5 address + 8 command + 2 frame (0,0) + 1 stop bit - each frame 2 times
88 // Every frame is auto repeated with a space period of 45 ms and the command and frame inverted to (1,1) or (0,1) for SHARP.
89 //
90 #define DENON_ADDRESS_BITS 5
91 #define DENON_COMMAND_BITS 8
92 #define DENON_FRAME_BITS 2 // 00/10 for 1. frame Denon/Sharp, inverted for autorepeat frame
93 
94 #define DENON_BITS (DENON_ADDRESS_BITS + DENON_COMMAND_BITS + DENON_FRAME_BITS) // 15 - The number of bits in the command
95 #define DENON_UNIT 260
96 
97 #define DENON_BIT_MARK DENON_UNIT // The length of a Bit:Mark
98 #define DENON_ONE_SPACE (7 * DENON_UNIT) // 1820 // The length of a Bit:Space for 1's
99 #define DENON_ZERO_SPACE (3 * DENON_UNIT) // 780 // The length of a Bit:Space for 0's
100 
101 #define DENON_AUTO_REPEAT_DISTANCE 45000 // Every frame is auto repeated with a space period of 45 ms and the command and frame inverted.
102 #define DENON_REPEAT_PERIOD 110000 // Commands are repeated every 110 ms (measured from start to start) for as long as the key on the remote control is held down.
103 
104 // for old decoder
105 #define DENON_HEADER_MARK DENON_UNIT // The length of the Header:Mark
106 #define DENON_HEADER_SPACE (3 * DENON_UNIT) // 780 // The length of the Header:Space
107 
111 
112 /************************************
113  * Start of send and decode functions
114  ************************************/
115 
116 void IRsend::sendSharp(uint8_t aAddress, uint8_t aCommand, int_fast8_t aNumberOfRepeats) {
117  sendDenon(aAddress, aCommand, aNumberOfRepeats, 1);
118  // see https://github.com/Arduino-IRremote/Arduino-IRremote/issues/1272
119 }
120 
121 void IRsend::sendSharp2(uint8_t aAddress, uint8_t aCommand, int_fast8_t aNumberOfRepeats) {
122  sendDenon(aAddress, aCommand, aNumberOfRepeats, 2);
123 }
124 
125 /*
126  * Denon frames are always sent 3 times. A non inverted (normal), an inverted frame, ending with a normal frame.
127  * Repeats are done by just adding an inverted and a normal frame with no extra delay, so it is quite responsible :-)
128  * If you specify a repeat of e.g. 3, then 3 + 6 frames are sent.
129  * Measured at Denon RC 1081.
130  */
131 void IRsend::sendDenon(uint8_t aAddress, uint8_t aCommand, int_fast8_t aNumberOfRepeats, uint8_t aSendSharpFrameMarker) {
132  // Set IR carrier frequency
133  enableIROut (DENON_KHZ); // 38 kHz
134 
135  // Add frame marker for sharp
136  uint16_t tCommand = aCommand;
137  // see https://github.com/Arduino-IRremote/Arduino-IRremote/issues/1272
138  tCommand |= aSendSharpFrameMarker << 8; // the 2 upper bits are 00 for Denon and 01 or 10 for Sharp
139 
140  uint16_t tData = aAddress | ((uint16_t) tCommand << DENON_ADDRESS_BITS);
141  uint16_t tInvertedData = (tData ^ 0x7FE0); // Command and frame (upper 10 bits, bit 5 to 14) are inverted
142 
143  uint_fast8_t tNumberOfCommands = aNumberOfRepeats + 1;
144  while (tNumberOfCommands > 0) {
145 
146  // Data
147  sendPulseDistanceWidthData_P(&DenonProtocolConstants, tData, DENON_BITS);
148 
149  // Inverted autorepeat frame
151  sendPulseDistanceWidthData_P(&DenonProtocolConstants, tInvertedData, DENON_BITS);
152 
153  tNumberOfCommands--;
154  // send repeated command with a fixed space gap
156  }
157  /*
158  * always end with a normal frame
159  * skip last delay!
160  */
161  sendPulseDistanceWidthData_P(&DenonProtocolConstants, tData, DENON_BITS);
162 
163 }
164 
166  return decodeDenon();
167 }
168 
170 
171  // we have no start bit, so check for the exact amount of data bits
172  // Check we have the right amount of data (32). The + 2 is for initial gap + stop bit mark
173  if (decodedIRData.rawlen != (2 * DENON_BITS) + 2) {
174  IR_DEBUG_PRINT(F("Denon: "));
175  IR_DEBUG_PRINT(F("Data length="));
177  IR_DEBUG_PRINTLN(F(" is not 32"));
178  return false;
179  }
180 
181  // Try to decode as Denon protocol
182  decodePulseDistanceWidthData_P(&DenonProtocolConstants, DENON_BITS, 1);
183 
184  // Check for stop mark, this stop bit distinguishes it from decoding as Sony
186 #if defined(LOCAL_DEBUG)
187  Serial.print(F("Denon: "));
188  Serial.println(F("Stop bit mark length is wrong"));
189 #endif
190  return false;
191  }
192 
193  // Success
196  uint8_t tFrameBits = (decodedIRData.command >> 8) & 0x03;
197  decodedIRData.command &= 0xFF;
198 
199  // Check for (auto) repeat
202 
203  repeatCount++;
204  /*
205  * 1. repeat is inverted autorepeat -> IRDATA_FLAGS_IS_AUTO_REPEAT
206  * 2. repeat is normal autorepeat (end) or 1. normal repeat -> IRDATA_FLAGS_IS_AUTO_REPEAT
207  * 3. repeat is inverted autorepeat of the 1. repeat -> IRDATA_FLAGS_IS_REPEAT (not the correct frame, but this enables easy counting of repeats)
208  * 4. repeat is normal autorepeat (end) or 2. normal repeat -> IRDATA_FLAGS_IS_AUTO_REPEAT
209  * 5. repeat is inverted autorepeat of the 2. repeat -> IRDATA_FLAGS_IS_REPEAT (not the correct frame, but this enables easy counting of repeats)
210  * 6. repeat is normal autorepeat (end) or 3. normal repeat -> IRDATA_FLAGS_IS_AUTO_REPEAT
211  */
212 
213  if (repeatCount & 0x01) {
214  /*
215  * Here we are in an inverted auto repeated frame where the command and frame bits are inverted
216  */
217 #if defined(LOCAL_DEBUG)
218  Serial.print(F("Denon: Inverted frame"));
219 #endif
220  if (repeatCount > 1) { // skip first auto repeat
222  }
223  // Check parity of consecutive received commands. There is no parity in one data set.
224  if ((uint8_t) lastDecodedCommand != (uint8_t)(~decodedIRData.command)) {
226 #if defined(LOCAL_DEBUG)
227  Serial.print(F("Denon: Parity check of inverted with non inverted frame failed. Last command="));
228  Serial.print((uint8_t)lastDecodedCommand, HEX);
229  Serial.print(F(" current="));
230  Serial.println((uint8_t)~decodedIRData.command, HEX);
231 #endif
232  }
233  // always take non inverted command
235 
236  // inverted command here, re-invert the frame bits for later protocol decoding
237  tFrameBits = tFrameBits ^ 0x03;
238  }
239  } else {
240  // first command here
241  repeatCount = 0;
242  }
243 
244  if (tFrameBits) {
246  } else {
248  }
249 
251 
252  return true;
253 }
254 
255 /*********************************************************************************
256  * Old deprecated functions, kept for backward compatibility to old 2.0 tutorials
257  *********************************************************************************/
258 /*
259  * Only for backwards compatibility
260  */
261 void IRsend::sendDenonRaw(uint16_t aRawData, int_fast8_t aNumberOfRepeats) {
262  sendDenon(aRawData >> (DENON_COMMAND_BITS + DENON_FRAME_BITS), (aRawData >> DENON_FRAME_BITS) & 0xFF, aNumberOfRepeats);
263 }
264 
265 /*
266  * Old function with parameter data
267  */
268 void IRsend::sendDenon(unsigned long data, int nbits) {
269  // Set IR carrier frequency
271 #if !(defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__))
272 // Serial.println(
273 // "The function sendDenon(data, nbits) is deprecated and may not work as expected! Use sendDenonRaw(data, NumberOfRepeats) or better sendDenon(Address, Command, NumberOfRepeats).");
274 #endif
275 
276  // Header
279 
280  // Data
283 }
284 
285 /*
286  * Old function without parameter aNumberOfRepeats
287  */
288 void IRsend::sendSharp(uint16_t aAddress, uint16_t aCommand) {
289  sendDenon(aAddress, aCommand, 0, true);
290 }
291 
293 
294  // Check we have the right amount of data
295  if (decodedIRData.rawlen != 1 + 2 + (2 * DENON_BITS) + 1) {
296  return false;
297  }
298 
299  // Check initial Mark+Space match
300  if (!matchMark(aResults->rawbuf[1], DENON_HEADER_MARK)) {
301  return false;
302  }
303 
304  if (!matchSpace(aResults->rawbuf[2], DENON_HEADER_SPACE)) {
305  return false;
306  }
307 
308  // Try to decode as Denon protocol.
310 
311  // Success
312  aResults->value = decodedIRData.decodedRawData;
313  aResults->bits = DENON_BITS;
314  aResults->decode_type = DENON;
316  return true;
317 }
318 
320 #if defined(LOCAL_DEBUG)
321 #undef LOCAL_DEBUG
322 #endif
323 #endif // _IR_DENON_HPP
IRData::address
uint16_t address
Decoded address, Distance protocol (tMarkTicksLong (if tMarkTicksLong == 0, then tMarkTicksShort) << ...
Definition: IRremoteInt.h:152
MICROS_PER_TICK
#define MICROS_PER_TICK
microseconds per clock interrupt tick
Definition: IRremote.hpp:207
DENON_COMMAND_BITS
#define DENON_COMMAND_BITS
Definition: ir_Denon.hpp:91
decode_results
Results returned from old decoders !!!deprecated!!!
Definition: IRremoteInt.h:208
decode_results::rawbuf
uint16_t * rawbuf
Definition: IRremoteInt.h:219
IRrecv::decodePulseDistanceWidthData
void decodePulseDistanceWidthData(PulseDistanceWidthProtocolConstants *aProtocolConstants, uint_fast8_t aNumberOfBits, IRRawlenType aStartOffset=3)
Decode pulse distance protocols for PulseDistanceWidthProtocolConstants.
Definition: IRReceive.hpp:1081
PROTOCOL_IS_MSB_FIRST
#define PROTOCOL_IS_MSB_FIRST
Definition: IRProtocol.h:178
IRData::numberOfBits
uint16_t numberOfBits
Number of bits received for data (address + command + parity) - to determine protocol length if diffe...
Definition: IRremoteInt.h:161
IRsend::aNumberOfRepeats
void int_fast8_t aNumberOfRepeats
Definition: IRremoteInt.h:538
IRrecv::decodePulseDistanceWidthData_P
void decodePulseDistanceWidthData_P(PulseDistanceWidthProtocolConstants const *aProtocolConstantsPGM, uint_fast8_t aNumberOfBits, IRRawlenType aStartOffset=3)
Definition: IRReceive.hpp:1117
MICROS_IN_ONE_MILLI
#define MICROS_IN_ONE_MILLI
Definition: IRremote.hpp:212
DENON_FRAME_BITS
#define DENON_FRAME_BITS
Definition: ir_Denon.hpp:92
PROTOCOL_IS_PULSE_DISTANCE
#define PROTOCOL_IS_PULSE_DISTANCE
Definition: IRProtocol.h:173
IRsend::sendSharp2
void sendSharp2(uint8_t aAddress, uint8_t aCommand, int_fast8_t aNumberOfRepeats)
Definition: ir_Denon.hpp:121
IRsend::mark
void mark(uint16_t aMarkMicros)
Sends an IR mark for the specified number of microseconds.
Definition: IRSend.hpp:1153
IRDATA_FLAGS_IS_REPEAT
#define IRDATA_FLAGS_IS_REPEAT
The gap between the preceding frame is as smaller than the maximum gap expected for a repeat....
Definition: IRProtocol.h:147
IRrecv::decodeSharp
bool decodeSharp()
Definition: ir_Denon.hpp:165
IRsend::sendDenonRaw
void sendDenonRaw(uint16_t aRawData, int_fast8_t aNumberOfRepeats=NO_REPEATS) void sendFAST(uint8_t aCommand
Definition: ir_Denon.hpp:261
IRDATA_FLAGS_IS_AUTO_REPEAT
#define IRDATA_FLAGS_IS_AUTO_REPEAT
The current repeat frame is a repeat, that is always sent after a regular frame and cannot be avoided...
Definition: IRProtocol.h:148
decode_results::bits
uint8_t bits
Definition: IRremoteInt.h:214
decode_results::decode_type
decode_type_t decode_type
Definition: IRremoteInt.h:211
IRData::decodedRawData
IRRawDataType decodedRawData
Up to 32/64 bit decoded raw data, to be used for send<protocol>Raw functions.
Definition: IRremoteInt.h:155
DENON_HEADER_MARK
#define DENON_HEADER_MARK
Definition: ir_Denon.hpp:105
IR_DEBUG_PRINT
#define IR_DEBUG_PRINT(...)
If DEBUG, print the arguments, otherwise do nothing.
Definition: IRremoteInt.h:186
matchSpace
bool matchSpace(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros)
Compensate for spaces shortened by demodulator hardware.
Definition: IRReceive.hpp:1459
DENON_REPEAT_PERIOD
#define DENON_REPEAT_PERIOD
Definition: ir_Denon.hpp:102
DENON_ZERO_SPACE
#define DENON_ZERO_SPACE
Definition: ir_Denon.hpp:99
DENON_HEADER_SPACE
#define DENON_HEADER_SPACE
Definition: ir_Denon.hpp:106
IRsend::sendDenon
void sendDenon(uint8_t aAddress, uint8_t aCommand, int_fast8_t aNumberOfRepeats, uint8_t aSendSharpFrameMarker=0)
Definition: ir_Denon.hpp:131
IRDATA_FLAGS_PARITY_FAILED
#define IRDATA_FLAGS_PARITY_FAILED
The current (autorepeat) frame violated parity check.
Definition: IRProtocol.h:149
decode_results::value
uint32_t value
Definition: IRremoteInt.h:213
PulseDistanceWidthProtocolConstants
Definition: IRProtocol.h:161
IRsend::sendPulseDistanceWidthData_P
void sendPulseDistanceWidthData_P(PulseDistanceWidthProtocolConstants const *aProtocolConstantsPGM, IRRawDataType aData, uint_fast8_t aNumberOfBits)
Definition: IRSend.hpp:586
DENON_ONE_SPACE
#define DENON_ONE_SPACE
Definition: ir_Denon.hpp:98
IRrecv::decodedIRData
IRData decodedIRData
Definition: IRremoteInt.h:409
IRData::flags
uint8_t flags
IRDATA_FLAGS_IS_REPEAT, IRDATA_FLAGS_WAS_OVERFLOW etc. See IRDATA_FLAGS_* definitions above.
Definition: IRremoteInt.h:162
PROTOCOL_IS_LSB_FIRST
#define PROTOCOL_IS_LSB_FIRST
Definition: IRProtocol.h:179
IRsend::sendSharp
void sendSharp(uint8_t aAddress, uint8_t aCommand, int_fast8_t aNumberOfRepeats)
Definition: ir_Denon.hpp:116
IRData::command
uint16_t command
Decoded command, Distance protocol (tMarkTicksShort << 8) | tSpaceTicksShort.
Definition: IRremoteInt.h:153
matchMark
bool matchMark(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros)
Compensate for marks exceeded by demodulator hardware.
Definition: IRReceive.hpp:1411
irparams_struct::rawbuf
IRRawbufType rawbuf[RAW_BUFFER_LENGTH]
raw data / tick counts per mark/space. With 8 bit we can only store up to 12.7 ms....
Definition: IRremoteInt.h:140
IRrecv::decodeDenonOld
bool decodeDenonOld(decode_results *aResults)
Definition: ir_Denon.hpp:292
IRrecv::irparams
irparams_struct irparams
Definition: IRremoteInt.h:408
IRrecv::lastDecodedCommand
uint16_t lastDecodedCommand
Definition: IRremoteInt.h:414
DENON
@ DENON
Definition: IRProtocol.h:98
PROGMEM
struct PulseDistanceWidthProtocolConstants const DenonProtocolConstants PROGMEM
Definition: ir_Denon.hpp:108
DENON_AUTO_REPEAT_DISTANCE
#define DENON_AUTO_REPEAT_DISTANCE
Definition: ir_Denon.hpp:101
IRsend::space
static void space(uint16_t aSpaceMicros)
Sends an IR space for the specified number of microseconds.
Definition: IRSend.hpp:1364
DENON_BITS
#define DENON_BITS
Definition: ir_Denon.hpp:94
IRData::rawlen
IRRawlenType rawlen
Counter of entries in rawbuf of last received frame.
Definition: IRremoteInt.h:170
DENON_ADDRESS_BITS
#define DENON_ADDRESS_BITS
Definition: ir_Denon.hpp:90
IRrecv::decodeDenon
bool decodeDenon()
Definition: ir_Denon.hpp:169
IRsend::sendPulseDistanceWidthData
void sendPulseDistanceWidthData(PulseDistanceWidthProtocolConstants *aProtocolConstants, IRRawDataType aData, uint_fast8_t aNumberOfBits)
Sends PulseDistance from data contained in parameter using ProtocolConstants structure for timing etc...
Definition: IRSend.hpp:578
DENON_BIT_MARK
#define DENON_BIT_MARK
Definition: ir_Denon.hpp:97
IR_DEBUG_PRINTLN
#define IR_DEBUG_PRINTLN(...)
If DEBUG, print the arguments as a line, otherwise do nothing.
Definition: IRremoteInt.h:190
DENON_KHZ
#define DENON_KHZ
Definition: IRProtocol.h:190
IRData::protocol
decode_type_t protocol
UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
Definition: IRremoteInt.h:151
IRsend::aCommand
void aCommand
Definition: IRremoteInt.h:688
IRData::initialGapTicks
uint16_t initialGapTicks
Contains the initial gap (pre 4.4: the value in rawbuf[0]) of the last received frame.
Definition: IRremoteInt.h:171
IRsend::enableIROut
void enableIROut(uint_fast8_t aFrequencyKHz)
Enables IR output.
Definition: IRSend.hpp:1404
SHARP
@ SHARP
Definition: IRProtocol.h:117
IRrecv::repeatCount
uint8_t repeatCount
Definition: IRremoteInt.h:419