8000 Use generic space encoding send/receive. · RemoteControl/Arduino-IRremote@983a3da · GitHub
[go: up one dir, main page]

Skip to content

Commit 983a3da

Browse files
committed
Use generic space encoding send/receive.
Instead of decoding Sony / NEC / Dish / Sharp separately, decode them all as space encoded.
1 parent 1d41102 commit 983a3da

File tree

4 files changed

+83
-199
lines changed

4 files changed

+83
-199
lines changed

IRremote.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
1818
// TEST must be defined for the IRtest unittests to work. It will make some
1919
// methods virtual, which will be slightly slower, which is why it is optional.
20-
// #define DEBUG
20+
#define DEBUG
2121
// #define TEST
2222

23-
// Information on a generic space encoding code
23+
// Information on a generic space encoding code.
24+
// This is a code that varies the mark width (or the space
25+
// width) to distingish bits.
2426
class space_enc_data {
2527
public:
2628
int headerMark; // Mark time for header in us
@@ -46,12 +48,9 @@ class decode_results {
4648
};
4749

4850
// Values for decode_type
49-
#define NEC 1
50-
#define SONY 2
51+
#define NEC_REPEAT 1
5152
#define RC5 3
52< 8000 /td>53
#define RC6 4
53-
#define DISH 5
54-
#define SHARP 6
5554
#define SPACE_ENC 7 // Generic space encoding
5655
#define UNKNOWN -1
5756

@@ -71,12 +70,10 @@ class IRrecv
7170
int getRClevel(decode_results *results, int *offset, int *used, int t1);
7271
long decodeSpaceEnc(decode_results *results);
7372
long decodeNEC(decode_results *results);
74-
long decodeSony(decode_results *results);
7573
long decodeRC5(decode_results *results);
7674
long decodeRC6(decode_results *results);
7775
long decodeHash(decode_results *results);
7876
int compare(unsigned int oldval, unsigned int newval);
79-
8077
}
8178
;
8279

@@ -91,13 +88,12 @@ class IRsend
9188
{
9289
public:
9390
IRsend() {}
94-
void sendNEC(unsigned long data, int nbits);
95-
void sendSony(unsigned long data, int nbits);
91+
void sendSpaceEnc(unsigned long data, int nbits, space_enc_data *spaceEncData);
92+
void sendNEC(unsigned long data, int nbits); // deprecated
93+
void sendSony(unsigned long data, int nbits); // deprecated
9694
void sendRaw(unsigned int buf[], int len, int hz);
9795
void sendRC5(unsigned long data, int nbits);
9896
void sendRC6(unsigned long data, int nbits);
99-
void sendDISH(unsigned long data, int nbits);
100-
void sendSharp(unsigned long data, int nbits);
10197
// private:
10298
void enableIROut(int khz);
10399
VIRTUAL void mark(int usec);

IRremoteRecv.cpp

Lines changed: 12 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "IRremote.h"
1212
#include "IRremoteInt.h"
1313

14+
#define DEBUG
15+
1416
volatile irparams_t irparams;
1517

1618
// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging.
@@ -143,8 +145,6 @@ void IRrecv::resume() {
143145
irparams.rawlen = 0;
144146
}
145147

146-
147-
148148
// Decodes the received IR message
149149
// Returns 0 if no data ready, 1 if data ready.
150150
// Results of decoding are stored in results
@@ -158,36 +158,37 @@ int IRrecv::decode(decode_results *results) {
158158
Serial.println("Attempting SPACE_ENC decode");
159159
#endif
160160
if (decodeSpaceEnc(results)) {
161+
// Don't resume until decoding is done because we don't
162+
// want the data to change in the middle of decoding.
163+
resume();
161164
return DECODED;
162165
}
163166
#ifdef DEBUG
164167
Serial.println("Attempting NEC decode");
165168
#endif
166169
if (decodeNEC(results)) {
167-
return DECODED;
168-
}
169-
#ifdef DEBUG
170-
Serial.println("Attempting Sony decode");
171-
#endif
172-
if (decodeSony(results)) {
170+
resume();
173171
return DECODED;
174172
}
175173
#ifdef DEBUG
176174
Serial.println("Attempting RC5 decode");
177175
#endif
178176
if (decodeRC5(results)) {
177+
resume();
179178
return DECODED;
180179
}
181180
#ifdef DEBUG
182181
Serial.println("Attempting RC6 decode");
183182
#endif
184183
if (decodeRC6(results)) {
184+
resume();
185185
return DECODED;
186186
}
187187
// decodeHash returns a hash on any input.
188188
// Thus, it needs to be last in the list.
189189
// If you add any decodes, add them before this.
190190
if (decodeHash(results)) {
191+
resume();
191192
return DECODED;
192193
}
193194
// Throw away and start over
@@ -380,6 +381,7 @@ long IRrecv::decodeSpaceEnc(decode_results *results) {
380381
return SPACE_ENC;
381382
}
382383

384+
// Just handle the repeat code; decodeSpaceEnc can handle the rest
383385
long IRrecv::decodeNEC(decode_results *results) {
384386
long data = 0;
385387
int offset = 1; // Skip first space
@@ -394,78 +396,11 @@ long IRrecv::decodeNEC(decode_results *results) {
394396
MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) {
395397
results->bits = 0;
396398
results->value = REPEAT;
397-
results->decode_type = NEC;
399+
results->decode_type = NEC_REPEAT;
398400
return DECODED;
399-
}
400-
if (irparams.rawlen < 2 * NEC_BITS + 4) {
401-
return ERR;
402-
}
403-
// Initial space
404-
if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) {
405-
return ERR;
406-
}
407-
offset++;
408-
for (int i = 0; i < NEC_BITS; i++) {
409-
if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) {
410-
return ERR;
411-
}
412-
offset++;
413-
if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) {
414-
data = (data << 1) | 1;
415-
}
416-
else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) {
417-
data <<= 1;
418-
}
419-
else {
420-
return ERR;
421-
}
422-
offset++;
423-
}
424-
// Success
425-
results->bits = NEC_BITS;
426-
results->value = data;
427-
results->decode_type = NEC;
428-
return DECODED;
429-
}
430-
431-
long IRrecv::decodeSony(decode_results *results) {
432-
long data = 0;
433-
if (irparams.rawlen < 2 * SONY_BITS + 2) {
434-
return ERR;
435-
}
436-
int offset = 1; // Skip first space
437-
// Initial mark
438-
if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) {
439-
return ERR;
440-
}
441-
offset++;
442-
443-
while (offset + 1 < irparams.rawlen) {
444-
if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) {
445-
break;
446-
}
447-
offset++;
448-
if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) {
449-
data = (data << 1) | 1;
450-
}
451-
else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) {
452-
data <<= 1;
453-
}
454-
else {
455-
return ERR;
456-
}
457-
offset++;
458-
}
459-
460-
// Success
461-
results->bits = (offset - 1) / 2;
462-
if (results->bits < 12) {
463-
results->bits = 0;
401+
} else {
464402
return ERR;
465403
}
466-
results->value = data;
467-
results->decode_type = SONY;
468-
return DECODED;
469404
}
470405

471406
// Gets one undecoded level at a time from the raw buffer.

IRremoteSend.cpp

Lines changed: 24 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
#include "IRremote.h"
1212
#include "IRremoteInt.h"
1313

14+
class space_enc_data necEnc = {9000 /* headerMark */ , 4500 /* headerSpace */,
15+
560 /* mark0 */, 560 /* space0 */, 560 /* mark1 */, 1600 /* space1 */,
16+
560 /* trailer */, 38 /* frequency */};
17+
18+
class space_enc_data sonyEnc = {2400 /* headerMark */ , 600 /* headerSpace */,
19+
600 /* mark0 */, 600 /* space0 */, 1200 /* mark1 */, 600 /* space1 */,
20+
0 /* trailer */, 40 /* frequency */};
21+
22+
class space_enc_data sharpEnc = {2400 /* headerMark */ , 600 /* headerSpace */,
23+
600 /* mark0 */, 600 /* space0 */, 1200 /* mark1 */, 600 /* space1 */,
24+
0 /* trailer */, 40 /* frequency */};
25+
26+
#define RC5_T1 889
27+
#define RC5_RPT_LENGTH 46000
28+
29+
#define RC6_HDR_MARK 2666
30+
#define RC6_HDR_SPACE 889
31+
#define RC6_T1 444
32+
#define RC6_RPT_LENGTH 46000
33+
34+
// Send a generic space encoded code
35+
// The timings and frequency are in spaceEncData
1436
void IRsend::sendSpaceEnc(unsigned long data, int nbits, space_enc_data *spaceEncData)
1537
{
1638
enableIROut(spaceEncData->frequency);
@@ -36,40 +58,11 @@ void IRsend::sendSpaceEnc(unsigned long data, int nbits, space_enc_data *spaceEn
3658

3759
void IRsend::sendNEC(unsigned long data, int nbits)
3860
{
39-
enableIROut(38);
40-
mark(NEC_HDR_MARK);
41-
space(NEC_HDR_SPACE);
42-
for (int i = 0; i < nbits; i++) {
43-
if (data & TOPBIT) {
44-
mark(NEC_BIT_MARK);
45-
space(NEC_ONE_SPACE);
46-
}
47-
else {
48-
mark(NEC_BIT_MARK);
49-
space(NEC_ZERO_SPACE);
50-
}
51-
data <<= 1;
52-
}
53-
mark(NEC_BIT_MARK);
54-
space(0);
61+
sendSpaceEnc(data, nbits, &necEnc);
5562
}
5663

5764
void IRsend::sendSony(unsigned long data, int nbits) {
58-
enableIROut(40);
59-
mark(SONY_HDR_MARK);
60-
space(SONY_HDR_SPACE);
61-
data = data << (32 - nbits);
62-
for (int i = 0; i < nbits; i++) {
63-
if (data & TOPBIT) {
64-
mark(SONY_ONE_MARK);
65-
space(SONY_HDR_SPACE);
66-
}
67-
else {
68-
mark(SONY_ZERO_MARK);
69-
space(SONY_HDR_SPACE);
70-
}
71-
data <<= 1;
72-
}
65+
sendSpaceEnc(data, nbits, &sonyEnc);
7366
}
7467

7568
void IRsend::sendRaw(unsigned int buf[], int len, int hz)
@@ -158,75 +151,3 @@ void IRsend::space(int time) {
158151
void IRsend::enableIROut(int khz) {
159152
IRremoteEnableIRoutput(khz);
160153
}
161-
162-
/* Sharp and DISH support by Todd Treece
163-
164-
The Dish send function needs to be repeated 4 times and the Sharp function
165-
has the necessary repeats built in. I know that it's not consistent,
166-
but I don't have the time to update my code.
167-
168-
Here are the LIRC files that I found that seem to match the remote codes
169-
from the oscilloscope:
170-
171-
Sharp LCD TV:
172-
http://lirc.sourceforge.net/remotes/sharp/GA538WJSA
173-
174-
DISH NETWORK (echostar 301):
175-
http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx
176-
177-
For the DISH codes, only send the last for characters of the hex.
178-
i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the
179-
linked LIRC file.
180-
*/
181-
182-
void IRsend::sendSharp(unsigned long data, int nbits) {
183-
unsigned long invertdata = data ^ SHARP_TOGGLE_MASK;
184-
enableIROut(38);
185-
for (int i = 0; i < nbits; i++) {
186-
if (data & 0x4000) {
187-
mark(SHARP_BIT_MARK);
188-
space(SHARP_ONE_SPACE);
189-
}
190-
else {
191-
mark(SHARP_BIT_MARK);
192-
space(SHARP_ZERO_SPACE);
193-
}
194-
data <<= 1;
195-
}
196-
197-
mark(SHARP_BIT_MARK);
198-
space(SHARP_ZERO_SPACE);
199-
delay(46);
200-
for (int i = 0; i < nbits; i++) {
201-
if (invertdata & 0x4000) {
202-
mark(SHARP_BIT_MARK);
203-
space(SHARP_ONE_SPACE);
204-
}
205-
else {
206-
mark(SHARP_BIT_MARK);
207 BEE2 -
space(SHARP_ZERO_SPACE);
208-
}
209-
invertdata <<= 1;
210-
}
211-
mark(SHARP_BIT_MARK);
212-
space(SHARP_ZERO_SPACE);
213-
delay(46);
214-
}
215-
216-
void IRsend::sendDISH(unsigned long data, int nbits)
217-
{
218-
enableIROut(56);
219-
mark(DISH_HDR_MARK);
220-
space(DISH_HDR_SPACE);
221-
for (int i = 0; i < nbits; i++) {
222-
if (data & DISH_TOP_BIT) {
223-
mark(DISH_BIT_MARK);
224-
space(DISH_ONE_SPACE);
225-
}
226-
else {
227-
mark(DISH_BIT_MARK);
228-
space(DISH_ZERO_SPACE);
229-
}
230-
data <<= 1;
231-
}
232-
}

0 commit comments

Comments
 (0)
0