forked from crankyoldgit/IRremoteESP8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir_NEC_test.cpp
338 lines (303 loc) · 12 KB
/
ir_NEC_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2017 David Conran
#include "IRsend.h"
#include "IRsend_test.h"
#include "gtest/gtest.h"
// Tests for sendNEC().
// Test sending typical data only.
TEST(TestSendNEC, SendDataOnly) {
IRsendTest irsend(4);
irsend.begin();
irsend.sendNEC(0);
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s58240",
irsend.outputStr());
irsend.sendNEC(0xAA00FF55);
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s1680m560s560m560s1680m560s560m560s1680m560s560"
"m560s1680m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s560m560s1680m560s1680m560s1680m560s1680m560s1680"
"m560s1680m560s1680m560s1680m560s560m560s1680m560s560m560s1680"
"m560s560m560s1680m560s560m560s1680m560s40320",
irsend.outputStr());
}
// Test sending different bit lengths.
TEST(TestSendNEC, SendSmallData) {
IRsendTest irsend(4);
irsend.begin();
irsend.sendNEC(0xA, 4); // Send only 4 data bits.
EXPECT_EQ("f38000d33m8960s4480m560s1680m560s560m560s1680m560s560m560s87360",
irsend.outputStr());
irsend.sendNEC(0, 8); // Send only 8 data bits.
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s85120",
irsend.outputStr());
irsend.sendNEC(0x1234567890ABCDEF, 64); // Send 64 data bits.
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s560m560s560m560s560m560s1680m560s560m560s560"
"m560s1680m560s560m560s560m560s560m560s1680m560s1680m560s560"
"m560s1680m560s560m560s560m560s560m560s1680m560s560m560s1680"
"m560s560m560s1680m560s1680m560s560m560s560m560s1680m560s1680"
"m560s1680m560s1680m560s560m560s560m560s560m560s1680m560s560"
"m560s560m560s1680m560s560m560s560m560s560m560s560m560s1680m560s560"
"m560s1680m560s560m560s1680m560s560m560s1680m560s1680m560s1680"
"m560s1680m560s560m560s560m560s1680m560s1680m560s560m560s1680"
"m560s1680m560s1680m560s1680m560s560m560s1680m560s1680m560s1680"
"m560s1680m560s22400",
irsend.outputStr());
}
// Test sending with repeats.
TEST(TestSendNEC, SendWithRepeats) {
IRsendTest irsend(4);
irsend.begin();
irsend.sendNEC(0, 8, 0); // Send a command with 0 repeats.
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
"m560s560m560s85120",
irsend.outputStr());
irsend.sendNEC(0xAA, 8, 1); // Send a command with 1 repeat.
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s1680m560s560m560s1680m560s560m560s1680m560s560"
"m560s1680m560s560m560s80640"
"m8960s2240m560s96320",
irsend.outputStr());
irsend.sendNEC(0xAA, 8, 3); // Send a command with 3 repeats.
EXPECT_EQ(
"f38000d33"
"m8960s4480m560s1680m560s560m560s1680m560s560m560s1680m560s560"
"m560s1680m560s560m560s80640"
"m8960s2240m560s96320"
"m8960s2240m560s96320"
"m8960s2240m560s96320",
irsend.outputStr());
}
// Tests for encodeNEC().
TEST(TestEncodeNEC, NormalNECEncoding) {
IRsendTest irsend(4);
EXPECT_EQ(0x807F40BF, irsend.encodeNEC(1, 2));
EXPECT_EQ(0x9A656897, irsend.encodeNEC(0x59, 0x16));
}
TEST(TestEncodeNEC, ExtendedNECEncoding) {
IRsendTest irsend(4);
EXPECT_EQ(0x9A806897, irsend.encodeNEC(0x159, 0x16));
}
TEST(TestEncodeNEC, CommandTrimmedTo8Bits) {
IRsendTest irsend(4);
EXPECT_EQ(irsend.encodeNEC(0x1, 0x2), irsend.encodeNEC(0x1, 0xF02));
EXPECT_EQ(irsend.encodeNEC(0xFFF0, 0x2), irsend.encodeNEC(0xFFF0, 0xF02));
}
// Tests for decodeNEC().
// Decode normal NEC messages.
TEST(TestDecodeNEC, NormalNECDecodeWithStrict) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
// Synthesised Normal NEC message.
irsend.reset();
irsend.sendNEC(irsend.encodeNEC(0x1, 0x2));
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x807F40BF, irsend.capture.value);
EXPECT_EQ(0x1, irsend.capture.address);
EXPECT_EQ(0x2, irsend.capture.command);
// Real-life Extended NEC code from an actual capture/decode.
irsend.reset();
irsend.sendNEC(0xC1A28877);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0xC1A28877, irsend.capture.value);
EXPECT_EQ(0x4583, irsend.capture.address);
EXPECT_EQ(0x11, irsend.capture.command);
// Test strict decoding rejects a NEC-like message.
irsend.reset();
irsend.sendNEC(0x0);
irsend.makeDecodeResult();
EXPECT_FALSE(irrecv.decodeNEC(&irsend.capture));
// Synthesised Normal NEC message with a repeat.
irsend.reset();
irsend.sendNEC(irsend.encodeNEC(0x1, 0x2), 32, 1);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x807F40BF, irsend.capture.value);
EXPECT_EQ(0x1, irsend.capture.address);
EXPECT_EQ(0x2, irsend.capture.command);
}
// NEC-like messages without strict mode.
TEST(TestDecodeNEC, NormalNECDecodeWithoutStrict) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
irsend.reset();
irsend.sendNEC(0x0);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 32, false));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0, irsend.capture.value);
EXPECT_EQ(0, irsend.capture.address);
EXPECT_EQ(0, irsend.capture.command);
irsend.reset();
irsend.sendNEC(0x12345678);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 32, false));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x12345678, irsend.capture.value);
EXPECT_EQ(0x2C48, irsend.capture.address);
EXPECT_EQ(0, irsend.capture.command);
}
// Short NEC-like messages (without strict naturally)
TEST(TestDecodeNEC, ShortNECDecodeWithoutStrict) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
irsend.reset();
irsend.sendNEC(0x0, 16);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 16, false));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(16, irsend.capture.bits);
EXPECT_EQ(0, irsend.capture.value);
EXPECT_EQ(0, irsend.capture.address);
EXPECT_EQ(0, irsend.capture.command);
// Expecting less than what was sent is not valid.
irsend.reset();
irsend.sendNEC(0x0, 32);
irsend.makeDecodeResult();
EXPECT_FALSE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 16, false));
// Send 16 bits of data, but fail because we are expecting 17.
irsend.reset();
irsend.sendNEC(0x0, 16);
irsend.makeDecodeResult();
EXPECT_FALSE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 17, false));
}
// Longer NEC-like messages (without strict naturally)
TEST(TestDecodeNEC, LongerNECDecodeWithoutStrict) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
irsend.reset();
irsend.sendNEC(0x1234567890ABCDEF, 64);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 64, false));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(64, irsend.capture.bits);
EXPECT_EQ(0x1234567890ABCDEF, irsend.capture.value);
EXPECT_EQ(0xD509, irsend.capture.address);
EXPECT_EQ(0, irsend.capture.command);
// Send 63 bits of data, but fail because we are expecting 64.
irsend.reset();
irsend.sendNEC(0x0, 63);
irsend.makeDecodeResult();
EXPECT_FALSE(irrecv.decodeNEC(&irsend.capture, kStartOffset, 64, false));
}
// Incorrect decoding reported in Issue #243
// Incorrect handling of decodes when there is no gap recorded at
// the end of a command when using the interrupt code. sendRaw() best emulates
// this for unit testing purposes. sendGC() and sendXXX() will add the trailing
// gap. Users won't see this in normal use.
TEST(TestDecodeNEC, NoTrailingGap_Issue243) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
irsend.reset();
uint16_t rawData[67] = {
9000, 4500, 650, 550, 650, 1650, 600, 550, 650, 550, 600, 1650,
650, 550, 600, 1650, 650, 1650, 650, 1650, 600, 550, 650, 1650,
650, 1650, 650, 550, 600, 1650, 650, 1650, 650, 550, 650, 550,
650, 1650, 650, 550, 650, 550, 650, 550, 600, 550, 650, 550,
650, 550, 650, 1650, 600, 550, 650, 1650, 650, 1650, 650, 1650,
650, 1650, 650, 1650, 650, 1650, 600};
irsend.sendRaw(rawData, 67, 38);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x4BB640BF, irsend.capture.value);
EXPECT_EQ(0x6DD2, irsend.capture.address);
EXPECT_EQ(0x2, irsend.capture.command);
irsend.reset();
irsend.sendRaw(rawData, 67, 38);
irsend.makeDecodeResult();
ASSERT_TRUE(irrecv.decode(&irsend.capture));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x4BB640BF, irsend.capture.value);
// Add a zero length space to the message to test how it handles that as
// an end of command gap.
irsend.addGap(0);
irsend.makeDecodeResult();
ASSERT_TRUE(irrecv.decode(&irsend.capture));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x4BB640BF, irsend.capture.value);
}
// Inconsistent decoding for unknown in Issue #264
// Reported issues decoding an Apple Remote. Apple doesn't appear to respect
// or use the command structure/checks in the NEC protocol.
TEST(TestDecodeNEC, NonStrictNECDecode_Issue264) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
irsend.reset();
// Slightly modified example than reported due to poor timings that are too
// far out of spec.
uint16_t rawData[67] = {
9150, 4650, 550, 600, 550, 1800, 600, 1750, 600, 1800, 550, 600,
550, 1800, 550, 1750, 600, 1750, 600, 1750, 600, 1750, 600, 1700,
600, 600, 600, 600, 550, 600, 600, 600, 600, 1750, 600, 1750,
600, 600, 550, 1800, 600, 600, 600, 600, 600, 600, 500, 600,
600, 600, 600, 600, 600, 1750, 600, 600, 600, 550, 600, 600,
600, 600, 600, 600, 600, 550, 600};
irsend.sendRaw(rawData, 67, 38);
irsend.makeDecodeResult();
EXPECT_FALSE(irrecv.decodeNEC(&irsend.capture)); // Not strictly NEC
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture, kStartOffset, kNECBits, false));
EXPECT_EQ(0x77E1A040, irsend.capture.value);
// Do it all again, but with a normal decode.
irsend.reset();
irsend.sendRaw(rawData, 67, 38);
irsend.makeDecodeResult();
ASSERT_TRUE(irrecv.decode(&irsend.capture));
EXPECT_EQ(NEC_LIKE, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x77E1A040, irsend.capture.value);
}
TEST(TestDecodeNEC, AutoReceiveCalibration) {
IRsendTest irsend(4);
IRrecv irrecv(4);
irsend.begin();
irsend.reset();
// Data from Issue #264, for a remote that is slightly off spec.
// Should be matched as a NEC-like message however without self-calibrating
// the timings of the short spaces is out.
uint16_t rawData[67] = {
9150, 4600, 650, 600, 650, 1650, 650, 1700, 650, 1750, 650, 600,
650, 1700, 650, 1750, 650, 1750, 650, 1700, 650, 1700, 650, 1700,
650, 600, 650, 600, 650, 600, 600, 600, 650, 1750, 650, 1750,
650, 600, 650, 1700, 600, 600, 700, 550, 650, 550, 650, 600,
650, 600, 650, 600, 650, 1750, 600, 600, 650, 600, 650, 550,
650, 600, 650, 600, 650, 600, 600};
irsend.sendRaw(rawData, 67, 38);
irsend.makeDecodeResult();
EXPECT_TRUE(irrecv.decodeNEC(&irsend.capture, kStartOffset, kNECBits, false));
EXPECT_EQ(NEC, irsend.capture.decode_type);
EXPECT_EQ(kNECBits, irsend.capture.bits);
EXPECT_EQ(0x77E1A040, irsend.capture.value);
}