IRremote
ir_LG.hpp
Go to the documentation of this file.
1 /*
2  * ir_LG.hpp
3  *
4  * Contains functions for receiving and sending LG IR Protocol for air conditioner
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) 2017-2024 Darryl Smith, 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_LG_HPP
33 #define _IR_LG_HPP
34 
35 #if defined(DEBUG) && !defined(LOCAL_DEBUG)
36 #define LOCAL_DEBUG
37 #else
38 //#define LOCAL_DEBUG // This enables debug output only for this file
39 #endif
40 
44 //==============================================================================
45 // L GGGG
46 // L G
47 // L G GG
48 // L G G
49 // LLLLL GGG
50 //==============================================================================
51 /*
52  * Protocol=LG Address=0xF1 Command=0x7776 Raw-Data=0xF17776B 28 bits MSB first
53  +8950,-4150
54  + 500,-1550 + 550,-1550 + 500,-1550 + 500,-1600
55  + 500,- 700 + 350,- 600 + 450,- 600 + 450,-1550
56  + 500,- 550 + 500,-1550 + 500,-1600 + 500,-1550
57  + 550,- 550 + 500,-1550 + 500,-1550 + 550,-1550
58  + 500,- 550 + 500,-1550 + 500,-1600 + 500,-1550
59  + 500,- 550 + 500,-1550 + 500,-1600 + 500,- 550
60  + 500,-1550 + 500,- 600 + 450,-1600 + 500,-1550
61  + 500
62  Sum: 62400
63  */
64 
65 // LG originally added by Darryl Smith (based on the JVC protocol)
66 // see: https://github.com/Arduino-IRremote/Arduino-IRremote/tree/master/examples/LGAirConditionerSendDemo
67 // see: https://www.mikrocontroller.net/articles/IRMP_-_english#LGAIR
68 // MSB first, 1 start bit + 8 bit address + 16 bit command + 4 bit checksum + 1 stop bit (28 data bits).
69 // Bit and repeat timing is like NEC
70 // LG2 has different header timing and a shorter bit time
71 /*
72  * LG remote IR-LED measurements: Type AKB 73315611 for air conditioner, Ver1.1 from 2011.03.01
73  * Protocol: LG2
74  * Internal crystal: 4 MHz
75  * Header: 8.9 ms mark 4.15 ms space
76  * Data: 500 / 540 and 500 / 1580;
77  * Clock is not synchronized with gate so you have 19 and sometimes 19 and a spike pulses for mark
78  * Duty: 9 us on 17 us off => around 33 % duty
79  * NO REPEAT: If value like temperature has changed during long press, the last value is send at button release.
80  * If you do a double press, the next value can be sent after around 118 ms. Tested with the fan button.
81 
82  * LG remote IR-LED measurements: Type AKB 75095308 for LG TV
83  * Protocol: NEC!!!
84  * Frequency 37.88 kHz
85  * Header: 9.0 ms mark 4.5 ms space
86  * Data: 560 / 560 and 560 / 1680;
87  * Clock is synchronized with gate, mark always starts with a full period
88  * Duty: 13 us on 13 us off => 50 % duty
89  * Repeat: 110 ms 9.0 ms mark, 2250 us space, 560 stop
90  * LSB first!
91  *
92  * The codes of the LG air conditioner are documented in https://github.com/Arduino-IRremote/Arduino-IRremote/blob/master/ac_LG.cpp
93  */
94 #define LG_ADDRESS_BITS 8
95 #define LG_COMMAND_BITS 16
96 #define LG_CHECKSUM_BITS 4
97 #define LG_BITS (LG_ADDRESS_BITS + LG_COMMAND_BITS + LG_CHECKSUM_BITS) // 28
98 
99 #define LG_UNIT 500 // 19 periods of 38 kHz
100 
101 #define LG_HEADER_MARK (18 * LG_UNIT) // 9000
102 #define LG_HEADER_SPACE 4200 // 4200 | 84
103 
104 #define LG2_HEADER_MARK (19 * LG_UNIT) // 9500
105 #define LG2_HEADER_SPACE (6 * LG_UNIT) // 3000
106 
107 #define LG_BIT_MARK LG_UNIT
108 #define LG_ONE_SPACE 1580 // 60 periods of 38 kHz
109 #define LG_ZERO_SPACE 550
110 
111 #define LG_REPEAT_HEADER_SPACE (4 * LG_UNIT) // 2250
112 #define LG_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.
113 //#define LG_AVERAGE_DURATION 58000 // LG_HEADER_MARK + LG_HEADER_SPACE + 32 * 2,5 * LG_UNIT) + LG_UNIT // 2.5 because we assume more zeros than ones
114 //#define LG_REPEAT_DURATION (LG_HEADER_MARK + LG_REPEAT_HEADER_SPACE + LG_BIT_MARK)
115 //#define LG_REPEAT_DISTANCE (LG_REPEAT_PERIOD - LG_AVERAGE_DURATION) // 52 ms
116 
119 
122 
123 /************************************
124  * Start of send and decode functions
125  ************************************/
126 /*
127  * Send special LG2 repeat - not used internally
128  */
130  enableIROut (LG_KHZ); // 38 kHz
131  mark(LG2_HEADER_MARK); // + 3000
132  space(LG_REPEAT_HEADER_SPACE); // - 2250
133  mark(LG_BIT_MARK); // + 500
134 }
135 
141  IrSender.enableIROut(LG_KHZ); // 38 kHz
142  IrSender.mark(LG2_HEADER_MARK); // + 3000
144  IrSender.mark(LG_BIT_MARK); // + 500
145 }
146 
147 uint32_t IRsend::computeLGRawDataAndChecksum(uint8_t aAddress, uint16_t aCommand) {
148  uint32_t tRawData = ((uint32_t) aAddress << (LG_COMMAND_BITS + LG_CHECKSUM_BITS)) | ((uint32_t) aCommand << LG_CHECKSUM_BITS);
149  /*
150  * My guess of the 4 bit checksum
151  * Addition of all 4 nibbles of the 16 bit command
152  */
153  uint8_t tChecksum = 0;
154  uint16_t tTempForChecksum = aCommand;
155  for (int i = 0; i < 4; ++i) {
156  tChecksum += tTempForChecksum & 0xF; // add low nibble
157  tTempForChecksum >>= 4; // shift by a nibble
158  }
159  return (tRawData | (tChecksum & 0xF));
160 }
161 
165 void IRsend::sendLG(uint8_t aAddress, uint16_t aCommand, int_fast8_t aNumberOfRepeats) {
167 }
168 
172 void IRsend::sendLG2(uint8_t aAddress, uint16_t aCommand, int_fast8_t aNumberOfRepeats) {
174 }
175 
177  decode_type_t tProtocol = LG;
178  uint16_t tHeaderSpace = LG_HEADER_SPACE;
179 
180  /*
181  * First check for right data length
182  * Next check start bit
183  * Next try the decode
184  */
185 
186 // Check we have the right amount of data (60). The +4 is for initial gap, start bit mark and space + stop bit mark.
187  if (decodedIRData.rawlen != ((2 * LG_BITS) + 4) && (decodedIRData.rawlen != 4)) {
188  IR_DEBUG_PRINT(F("LG: "));
189  IR_DEBUG_PRINT(F("Data length="));
191  IR_DEBUG_PRINTLN(F(" is not 60 or 4"));
192  return false;
193  }
194 
195 // Check header "mark" this must be done for repeat and data
198  tProtocol = LG2;
199  tHeaderSpace = LG2_HEADER_SPACE;
200  } else {
201 #if defined(LOCAL_DEBUG)
202  Serial.print(F("LG: "));
203  Serial.println(F("Header mark is wrong"));
204 #endif
205  return false; // neither LG nor LG2 header
206  }
207  }
208 
209 // Check for repeat - here we have another header space length
210  if (decodedIRData.rawlen == 4) {
217  return true;
218  }
219 #if defined(LOCAL_DEBUG)
220  Serial.print(F("LG: "));
221  Serial.print(F("Repeat header space is wrong"));
222 #endif
223  return false;
224  }
225 
226 // Check command header space
227  if (!matchSpace(decodedIRData.rawDataPtr->rawbuf[2], tHeaderSpace)) {
228 #if defined(LOCAL_DEBUG)
229  Serial.print(F("LG: "));
230  Serial.println(F("Header space length is wrong"));
231 #endif
232  return false;
233  }
234 
236 #if defined(LOCAL_DEBUG)
237  Serial.print(F("LG: "));
238  Serial.println(F("Decode failed"));
239 #endif
240  return false;
241  }
242 
243 // Success
247 
248  /*
249  * My guess of the checksum
250  */
251  uint8_t tChecksum = 0;
252  uint16_t tTempForChecksum = decodedIRData.command;
253  for (int i = 0; i < 4; ++i) {
254  tChecksum += tTempForChecksum & 0xF; // add low nibble
255  tTempForChecksum >>= 4; // shift by a nibble
256  }
257 // Checksum check
258  if ((tChecksum & 0xF) != (decodedIRData.decodedRawData & 0xF)) {
259 #if defined(LOCAL_DEBUG)
260  Serial.print(F("LG: "));
261  Serial.print(F("4 bit checksum is not correct. expected=0x"));
262  Serial.print(tChecksum, HEX);
263  Serial.print(F(" received=0x"));
264  Serial.print((decodedIRData.decodedRawData & 0xF), HEX);
265  Serial.print(F(" data=0x"));
266  Serial.println(decodedIRData.command, HEX);
267 #endif
269  }
270 
271  decodedIRData.protocol = tProtocol; // LG or LG2
273 
274  return true;
275 }
276 
277 /*********************************************************************************
278  * Old deprecated functions, kept for backward compatibility to old 2.0 tutorials
279  *********************************************************************************/
280 
286 void IRsend::sendLGRaw(uint32_t aRawData, int_fast8_t aNumberOfRepeats) {
288 }
289 
291  unsigned int offset = 1; // Skip first space
292 
293 // Check we have enough data (60) - +4 for initial gap, start bit mark and space + stop bit mark
294  if (aResults->rawlen != (2 * LG_BITS) + 4) {
295  return false;
296  }
297 
298 // Initial mark/space
299  if (!matchMark(aResults->rawbuf[offset], LG_HEADER_MARK)) {
300  return false;
301  }
302  offset++;
303 
304  if (!matchSpace(aResults->rawbuf[offset], LG_HEADER_SPACE)) {
305  return false;
306  }
307  offset++;
308 
310  return false;
311  }
312 // Stop bit
313  if (!matchMark(aResults->rawbuf[offset + (2 * LG_BITS)], LG_BIT_MARK)) {
314 #if defined(LOCAL_DEBUG)
315  Serial.println(F("Stop bit mark length is wrong"));
316 #endif
317  return false;
318  }
319 
320 // Success
321  aResults->value = decodedIRData.decodedRawData;
322  aResults->bits = LG_BITS;
323  aResults->decode_type = LG;
325  return true;
326 }
327 
328 //+=============================================================================
329 void IRsend::sendLG(unsigned long data, int nbits) {
330 // Set IR carrier frequency
332 #if !(defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__))
333 // Serial.println(F(
334 // "The function sendLG(data, nbits) is deprecated and may not work as expected! Use sendLGRaw(data, NumberOfRepeats) or better sendLG(Address, Command, NumberOfRepeats)."));
335 #endif
336 // Header
339 // mark(LG_BIT_MARK);
340 
341 // Data + stop bit
343 }
344 
346 #if defined(LOCAL_DEBUG)
347 #undef LOCAL_DEBUG
348 #endif
349 #endif // _IR_LG_HPP
IRData::address
uint16_t address
Decoded address, Distance protocol (tMarkTicksLong (if tMarkTicksLong == 0, then tMarkTicksShort) << ...
Definition: IRProtocol.h:109
LG_REPEAT_HEADER_SPACE
#define LG_REPEAT_HEADER_SPACE
Definition: ir_LG.hpp:111
IRsend::sendLGRaw
void sendLGRaw(uint32_t aRawData, int_fast8_t aNumberOfRepeats=NO_REPEATS)
Here you can put your raw data, even one with "wrong" checksum.
Definition: ir_LG.hpp:286
decode_results
Results returned from old decoders !!!deprecated!!!
Definition: IRremoteInt.h:159
decode_results::rawbuf
uint16_t * rawbuf
Definition: IRremoteInt.h:170
PROTOCOL_IS_MSB_FIRST
#define PROTOCOL_IS_MSB_FIRST
Definition: IRProtocol.h:145
IRrecv::lastDecodedProtocol
decode_type_t lastDecodedProtocol
Definition: IRremoteInt.h:324
IRData::numberOfBits
uint16_t numberOfBits
Number of bits received for data (address + command + parity) - to determine protocol length if diffe...
Definition: IRProtocol.h:118
LG_REPEAT_PERIOD
#define LG_REPEAT_PERIOD
Definition: ir_LG.hpp:112
IRsend::aNumberOfRepeats
void int_fast8_t aNumberOfRepeats
Definition: IRremoteInt.h:520
MICROS_IN_ONE_MILLI
#define MICROS_IN_ONE_MILLI
Definition: IRremote.hpp:254
IRsend::mark
void mark(uint16_t aMarkMicros)
Sends an IR mark for the specified number of microseconds.
Definition: IRSend.hpp:969
IRData::rawlen
uint_fast8_t rawlen
counter of entries in rawbuf
Definition: IRProtocol.h:123
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:91
decode_type_t
decode_type_t
An enum consisting of all supported formats.
Definition: IRProtocol.h:40
IRsend::sendPulseDistanceWidth
void sendPulseDistanceWidth(PulseDistanceWidthProtocolConstants *aProtocolConstants, IRRawDataType aData, uint_fast8_t aNumberOfBits, int_fast8_t aNumberOfRepeats)
Sends PulseDistance frames and repeats and enables receiver again.
Definition: IRSend.hpp:702
irparams_struct::rawbuf
uint16_t rawbuf[RAW_BUFFER_LENGTH]
raw data / tick counts per mark/space, first entry is the length of the gap between previous and curr...
Definition: IRremoteInt.h:113
IRData::rawDataPtr
irparams_struct * rawDataPtr
Pointer of the raw timing data to be decoded. Mainly the OverflowFlag and the data buffer filled by r...
Definition: IRProtocol.h:129
decode_results::bits
uint8_t bits
Definition: IRremoteInt.h:165
decode_results::decode_type
decode_type_t decode_type
Definition: IRremoteInt.h:162
IRData::decodedRawData
IRRawDataType decodedRawData
Up to 32/64 bit decoded raw data, to be used for send functions.
Definition: IRProtocol.h:112
IR_DEBUG_PRINT
#define IR_DEBUG_PRINT(...)
If DEBUG, print the arguments, otherwise do nothing.
Definition: IRremoteInt.h:137
matchSpace
bool matchSpace(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros)
Compensate for spaces shortened by demodulator hardware.
Definition: IRReceive.hpp:1116
sendLG2SpecialRepeat
void sendLG2SpecialRepeat()
Static function for sending special repeat frame.
Definition: ir_LG.hpp:140
IRrecv::decodePulseDistanceWidthData
bool decodePulseDistanceWidthData(PulseDistanceWidthProtocolConstants *aProtocolConstants, uint_fast8_t aNumberOfBits, uint_fast8_t aStartOffset=3)
Decode pulse distance protocols for PulseDistanceWidthProtocolConstants.
Definition: IRReceive.hpp:845
IRrecv::decodeLGMSB
bool decodeLGMSB(decode_results *aResults)
Definition: ir_LG.hpp:290
IRDATA_FLAGS_PARITY_FAILED
#define IRDATA_FLAGS_PARITY_FAILED
The current (autorepeat) frame violated parity check.
Definition: IRProtocol.h:93
LG_CHECKSUM_BITS
#define LG_CHECKSUM_BITS
Definition: ir_LG.hpp:96
decode_results::value
uint32_t value
Definition: IRremoteInt.h:164
PulseDistanceWidthProtocolConstants
Definition: IRProtocol.h:132
IRrecv::decodedIRData
IRData decodedIRData
Definition: IRremoteInt.h:321
IRsend::sendLG2Repeat
void sendLG2Repeat()
Definition: ir_LG.hpp:129
LG_COMMAND_BITS
#define LG_COMMAND_BITS
Definition: ir_LG.hpp:95
IRData::flags
uint8_t flags
IRDATA_FLAGS_IS_REPEAT, IRDATA_FLAGS_WAS_OVERFLOW etc. See IRDATA_FLAGS_* definitions above.
Definition: IRProtocol.h:119
LG_ONE_SPACE
#define LG_ONE_SPACE
Definition: ir_LG.hpp:108
IRsend::computeLGRawDataAndChecksum
uint32_t computeLGRawDataAndChecksum(uint8_t aAddress, uint16_t aCommand)
Definition: ir_LG.hpp:147
IRData::command
uint16_t command
Decoded command, Distance protocol (tMarkTicksShort << 8) | tSpaceTicksShort.
Definition: IRProtocol.h:110
LG_BITS
#define LG_BITS
Definition: ir_LG.hpp:97
matchMark
bool matchMark(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros)
Compensate for marks exceeded by demodulator hardware.
Definition: IRReceive.hpp:1083
LG_KHZ
#define LG_KHZ
Definition: IRProtocol.h:161
LG2_HEADER_MARK
#define LG2_HEADER_MARK
Definition: ir_LG.hpp:104
IrSender
IRsend IrSender
Definition: IRSend.hpp:65
IRDATA_FLAGS_IS_MSB_FIRST
#define IRDATA_FLAGS_IS_MSB_FIRST
Value is mainly determined by the (known) protocol.
Definition: IRProtocol.h:99
IRrecv::lastDecodedCommand
uint32_t lastDecodedCommand
Definition: IRremoteInt.h:326
LG2_HEADER_SPACE
#define LG2_HEADER_SPACE
Definition: ir_LG.hpp:105
LGProtocolConstants
struct PulseDistanceWidthProtocolConstants LGProtocolConstants
Definition: ir_LG.hpp:117
LG2
@ LG2
Definition: IRProtocol.h:48
IRsend::space
static void space(uint16_t aSpaceMicros)
Sends an IR space for the specified number of microseconds.
Definition: IRSend.hpp:1165
LG_HEADER_MARK
#define LG_HEADER_MARK
Definition: ir_LG.hpp:101
LG_ZERO_SPACE
#define LG_ZERO_SPACE
Definition: ir_LG.hpp:109
LG2ProtocolConstants
struct PulseDistanceWidthProtocolConstants LG2ProtocolConstants
Definition: ir_LG.hpp:120
LG_HEADER_SPACE
#define LG_HEADER_SPACE
Definition: ir_LG.hpp:102
IRrecv::decodeLG
bool decodeLG()
Definition: ir_LG.hpp:176
IRsend::sendPulseDistanceWidthData
void sendPulseDistanceWidthData(PulseDistanceWidthProtocolConstants *aProtocolConstants, IRRawDataType aData, uint_fast8_t aNumberOfBits)
Sends PulseDistance data The output always ends with a space Each additional call costs 16 bytes prog...
Definition: IRSend.hpp:832
IRrecv::lastDecodedAddress
uint32_t lastDecodedAddress
Definition: IRremoteInt.h:325
LG_BIT_MARK
#define LG_BIT_MARK
Definition: ir_LG.hpp:107
IR_DEBUG_PRINTLN
#define IR_DEBUG_PRINTLN(...)
If DEBUG, print the arguments as a line, otherwise do nothing.
Definition: IRremoteInt.h:141
decode_results::rawlen
uint_fast8_t rawlen
Definition: IRremoteInt.h:171
IRData::protocol
decode_type_t protocol
UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
Definition: IRProtocol.h:108
IRsend::sendLG
void sendLG(uint8_t aAddress, uint16_t aCommand, int_fast8_t aNumberOfRepeats)
LG uses the NEC repeat.
Definition: ir_LG.hpp:165
IRsend::aCommand
void aCommand
Definition: IRremoteInt.h:582
LG
@ LG
Definition: IRProtocol.h:47
IRsend::enableIROut
void enableIROut(uint_fast8_t aFrequencyKHz)
Enables IR output.
Definition: IRSend.hpp:1205
IRsend::sendLG2
void sendLG2(uint8_t aAddress, uint16_t aCommand, int_fast8_t aNumberOfRepeats)
LG2 uses a special repeat.
Definition: ir_LG.hpp:172
sendNECSpecialRepeat
void sendNECSpecialRepeat()
Static function variant of IRsend::sendNECRepeat For use in ProtocolConstants.
Definition: ir_NEC.hpp:146