ir_LG_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // Copyright 2017 David Conran
  2. #include "ir_LG.h"
  3. #include "IRsend.h"
  4. #include "IRsend_test.h"
  5. #include "gtest/gtest.h"
  6. // Tests for calcLGChecksum()
  7. TEST(TestCalcLGChecksum, General) {
  8. EXPECT_EQ(0x0, calcLGChecksum(0x0));
  9. EXPECT_EQ(0x1, calcLGChecksum(0x1));
  10. EXPECT_EQ(0xF, calcLGChecksum(0xF));
  11. EXPECT_EQ(0x4, calcLGChecksum(0x1111));
  12. EXPECT_EQ(0x8, calcLGChecksum(0x2222));
  13. EXPECT_EQ(0x0, calcLGChecksum(0x4444));
  14. EXPECT_EQ(0xA, calcLGChecksum(0x1234));
  15. EXPECT_EQ(0xA, calcLGChecksum(0x4321));
  16. EXPECT_EQ(0xE, calcLGChecksum(0xABCD));
  17. EXPECT_EQ(0x1, calcLGChecksum(0x4AE5));
  18. EXPECT_EQ(0xC, calcLGChecksum(0xFFFF));
  19. }
  20. // Tests for sendLG().
  21. // Test sending typical data only.
  22. TEST(TestSendLG, SendDataOnly) {
  23. IRsendTest irsend(4);
  24. irsend.begin();
  25. irsend.reset();
  26. irsend.sendLG(0x4B4AE51);
  27. EXPECT_EQ(
  28. "m8500s4250"
  29. "m550s550m550s1600m550s550m550s550"
  30. "m550s1600m550s550m550s1600m550s1600m550s550m550s1600m550s550m550s550"
  31. "m550s1600m550s550m550s1600m550s550m550s1600m550s1600m550s1600m550s550"
  32. "m550s550m550s1600m550s550m550s1600m550s550m550s550m550s550m550s1600"
  33. "m550s50300",
  34. irsend.outputStr());
  35. irsend.reset();
  36. irsend.sendLG(0xB4B4AE51, kLg32Bits);
  37. EXPECT_EQ(
  38. "m4480s4480"
  39. "m560s1680m560s560m560s1680m560s1680m560s560m560s1680m560s560m560s560"
  40. "m560s1680m560s560m560s1680m560s1680m560s560m560s1680m560s560m560s560"
  41. "m560s1680m560s560m560s1680m560s560m560s1680m560s1680m560s1680m560s560"
  42. "m560s560m560s1680m560s560m560s1680m560s560m560s560m560s560m560s1680"
  43. "m560s44800"
  44. "m8950s2250m550s96300",
  45. irsend.outputStr());
  46. }
  47. // Test sending with different repeats.
  48. TEST(TestSendLG, SendWithRepeats) {
  49. IRsendTest irsend(4);
  50. irsend.begin();
  51. irsend.reset();
  52. irsend.sendLG(0x4B4AE51, kLgBits, 1);
  53. EXPECT_EQ(
  54. "m8500s4250"
  55. "m550s550m550s1600m550s550m550s550"
  56. "m550s1600m550s550m550s1600m550s1600m550s550m550s1600m550s550m550s550"
  57. "m550s1600m550s550m550s1600m550s550m550s1600m550s1600m550s1600m550s550"
  58. "m550s550m550s1600m550s550m550s1600m550s550m550s550m550s550m550s1600"
  59. "m550s50300"
  60. "m8500s2250m550s96750",
  61. irsend.outputStr());
  62. irsend.reset();
  63. irsend.sendLG(0xB4B4AE51, kLg32Bits, 1);
  64. EXPECT_EQ(
  65. "m4480s4480"
  66. "m560s1680m560s560m560s1680m560s1680m560s560m560s1680m560s560m560s560"
  67. "m560s1680m560s560m560s1680m560s1680m560s560m560s1680m560s560m560s560"
  68. "m560s1680m560s560m560s1680m560s560m560s1680m560s1680m560s1680m560s560"
  69. "m560s560m560s1680m560s560m560s1680m560s560m560s560m560s560m560s1680"
  70. "m560s44800"
  71. "m8950s2250m550s96300"
  72. "m8950s2250m550s96300",
  73. irsend.outputStr());
  74. }
  75. // Test sending an atypical data size.
  76. TEST(TestSendLG, SendUnusualSize) {
  77. IRsendTest irsend(4);
  78. irsend.begin();
  79. irsend.reset();
  80. irsend.sendLG(0x0, 31);
  81. EXPECT_EQ(
  82. "m8500s4250"
  83. "m550s550m550s550m550s550m550s550m550s550m550s550m550s550m550s550"
  84. "m550s550m550s550m550s550m550s550m550s550m550s550m550s550m550s550"
  85. "m550s550m550s550m550s550m550s550m550s550m550s550m550s550m550s550"
  86. "m550s550m550s550m550s550m550s550m550s550m550s550m550s550"
  87. "m550s60650",
  88. irsend.outputStr());
  89. irsend.reset();
  90. irsend.sendLG(0x0, 64);
  91. EXPECT_EQ(
  92. "m4480s4480"
  93. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  94. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  95. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  96. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  97. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  98. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  99. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  100. "m560s560m560s560m560s560m560s560m560s560m560s560m560s560m560s560"
  101. "m560s26880"
  102. "m8950s2250m550s96300",
  103. irsend.outputStr());
  104. }
  105. // Tests for encodeLG().
  106. TEST(TestEncodeLG, NormalEncoding) {
  107. IRsendTest irsend(4);
  108. EXPECT_EQ(0x0, irsend.encodeLG(0, 0));
  109. EXPECT_EQ(0x100011, irsend.encodeLG(1, 1));
  110. EXPECT_EQ(0x100022, irsend.encodeLG(1, 2));
  111. EXPECT_EQ(0x43001DE, irsend.encodeLG(0x43, 0x1D));
  112. EXPECT_EQ(0xB4B4AE51, irsend.encodeLG(0xB4B, 0x4AE5));
  113. EXPECT_EQ(0xAA0055A, irsend.encodeLG(0xAA, 0x55));
  114. EXPECT_EQ(0xFFFFFFFC, irsend.encodeLG(0xFFFF, 0xFFFF));
  115. }
  116. // Tests for decodeLG().
  117. // Decode normal LG messages.
  118. TEST(TestDecodeLG, NormalDecodeWithStrict) {
  119. IRsendTest irsend(4);
  120. IRrecv irrecv(4);
  121. irsend.begin();
  122. // Normal LG 28-bit message.
  123. irsend.reset();
  124. irsend.sendLG(0x4B4AE51, kLgBits);
  125. irsend.makeDecodeResult();
  126. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  127. EXPECT_EQ(LG, irsend.capture.decode_type);
  128. EXPECT_EQ(kLgBits, irsend.capture.bits);
  129. EXPECT_EQ(0x4B4AE51, irsend.capture.value);
  130. EXPECT_EQ(0x4B, irsend.capture.address);
  131. EXPECT_EQ(0x4AE5, irsend.capture.command);
  132. EXPECT_FALSE(irsend.capture.repeat);
  133. // Normal LG 32-bit message.
  134. irsend.reset();
  135. irsend.sendLG(0xB4B4AE51, kLg32Bits);
  136. irsend.makeDecodeResult();
  137. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLg32Bits, false));
  138. EXPECT_EQ(LG, irsend.capture.decode_type);
  139. EXPECT_EQ(kLg32Bits, irsend.capture.bits);
  140. EXPECT_EQ(0xB4B4AE51, irsend.capture.value);
  141. EXPECT_EQ(0xB4B, irsend.capture.address);
  142. EXPECT_EQ(0x4AE5, irsend.capture.command);
  143. EXPECT_FALSE(irsend.capture.repeat);
  144. // Synthesised Normal LG 28-bit message.
  145. irsend.reset();
  146. irsend.sendLG(irsend.encodeLG(0x07, 0x99));
  147. irsend.makeDecodeResult();
  148. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  149. EXPECT_EQ(LG, irsend.capture.decode_type);
  150. EXPECT_EQ(kLgBits, irsend.capture.bits);
  151. EXPECT_EQ(0x700992, irsend.capture.value);
  152. EXPECT_EQ(0x07, irsend.capture.address);
  153. EXPECT_EQ(0x99, irsend.capture.command);
  154. EXPECT_FALSE(irsend.capture.repeat);
  155. // Synthesised Normal LG 32-bit message.
  156. irsend.reset();
  157. irsend.sendLG(irsend.encodeLG(0x800, 0x8000), kLg32Bits);
  158. irsend.makeDecodeResult();
  159. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  160. EXPECT_EQ(LG, irsend.capture.decode_type);
  161. EXPECT_EQ(kLg32Bits, irsend.capture.bits);
  162. EXPECT_EQ(0x80080008, irsend.capture.value);
  163. EXPECT_EQ(0x800, irsend.capture.address);
  164. EXPECT_EQ(0x8000, irsend.capture.command);
  165. EXPECT_FALSE(irsend.capture.repeat);
  166. }
  167. // Decode normal repeated LG messages.
  168. TEST(TestDecodeLG, NormalDecodeWithRepeatAndStrict) {
  169. IRsendTest irsend(4);
  170. IRrecv irrecv(4);
  171. irsend.begin();
  172. // Normal LG 28-bit message with 2 repeats.
  173. irsend.reset();
  174. irsend.sendLG(irsend.encodeLG(0x07, 0x99), kLgBits, 2);
  175. irsend.makeDecodeResult();
  176. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  177. EXPECT_EQ(LG, irsend.capture.decode_type);
  178. EXPECT_EQ(kLgBits, irsend.capture.bits);
  179. EXPECT_EQ(0x700992, irsend.capture.value);
  180. EXPECT_EQ(0x07, irsend.capture.address);
  181. EXPECT_EQ(0x99, irsend.capture.command);
  182. EXPECT_FALSE(irsend.capture.repeat);
  183. // Normal LG 32-bit message with 2 repeats.
  184. irsend.reset();
  185. irsend.sendLG(irsend.encodeLG(0x07, 0x99), kLg32Bits, 2);
  186. irsend.makeDecodeResult();
  187. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  188. EXPECT_EQ(LG, irsend.capture.decode_type);
  189. EXPECT_EQ(kLg32Bits, irsend.capture.bits);
  190. EXPECT_EQ(0x700992, irsend.capture.value);
  191. EXPECT_EQ(0x07, irsend.capture.address);
  192. EXPECT_EQ(0x99, irsend.capture.command);
  193. EXPECT_FALSE(irsend.capture.repeat);
  194. }
  195. // Decode unsupported LG message values.
  196. TEST(TestDecodeLG, DecodeWithNonStrictValues) {
  197. IRsendTest irsend(4);
  198. IRrecv irrecv(4);
  199. irsend.begin();
  200. // Illegal values should be rejected when strict is on.
  201. // Illegal LG 28-bit message value.
  202. irsend.reset();
  203. irsend.sendLG(0x1);
  204. irsend.makeDecodeResult();
  205. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  206. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  207. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, false));
  208. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLgBits, false));
  209. // Illegal LG 32-bit message value.
  210. irsend.reset();
  211. irsend.sendLG(0x1111111, kLg32Bits);
  212. irsend.makeDecodeResult();
  213. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  214. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  215. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLg32Bits, false));
  216. EXPECT_EQ(LG, irsend.capture.decode_type);
  217. EXPECT_EQ(kLg32Bits, irsend.capture.bits);
  218. EXPECT_EQ(0x1111111, irsend.capture.value);
  219. EXPECT_EQ(0x11, irsend.capture.address);
  220. EXPECT_EQ(0x1111, irsend.capture.command);
  221. EXPECT_FALSE(irsend.capture.repeat);
  222. irsend.reset();
  223. irsend.sendLG(0x1111111, kLg32Bits);
  224. irsend.makeDecodeResult();
  225. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, false));
  226. }
  227. // Decode unsupported LG message sizes.
  228. TEST(TestDecodeLG, DecodeWithNonStrictSizes) {
  229. IRsendTest irsend(4);
  230. IRrecv irrecv(4);
  231. irsend.begin();
  232. // Illegal sizes should be rejected when strict is on.
  233. // Illegal LG 16-bit message size.
  234. irsend.reset();
  235. irsend.sendLG(irsend.encodeLG(0x07, 0x99), 16);
  236. irsend.makeDecodeResult();
  237. // Should fail when unexpected against different bit sizes.
  238. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  239. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  240. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, false));
  241. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, false));
  242. // Should pass if strict off.
  243. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, 16, false));
  244. EXPECT_EQ(LG, irsend.capture.decode_type);
  245. EXPECT_EQ(16, irsend.capture.bits);
  246. EXPECT_EQ(0x992, irsend.capture.value);
  247. EXPECT_EQ(0x0, irsend.capture.address);
  248. EXPECT_EQ(0x99, irsend.capture.command);
  249. // Illegal LG 36-bit message size.
  250. irsend.reset();
  251. irsend.sendLG(0x123456789, 36); // Illegal value LG 36-bit message.
  252. irsend.makeDecodeResult();
  253. // Should fail when unexpected against different bit sizes.
  254. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, true));
  255. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, false));
  256. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  257. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLg32Bits, false));
  258. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, 36, false));
  259. EXPECT_EQ(LG, irsend.capture.decode_type);
  260. EXPECT_EQ(36, irsend.capture.bits);
  261. EXPECT_EQ(0x123456789, irsend.capture.value);
  262. EXPECT_EQ(0x1234, irsend.capture.address);
  263. EXPECT_EQ(0x5678, irsend.capture.command);
  264. EXPECT_FALSE(irsend.capture.repeat);
  265. }
  266. // Decode (non-standard) 64-bit messages.
  267. TEST(TestDecodeLG, Decode64BitMessages) {
  268. IRsendTest irsend(4);
  269. IRrecv irrecv(4);
  270. irsend.begin();
  271. irsend.reset();
  272. // Illegal value & size LG 64-bit message.
  273. irsend.sendLG(0xFFFFFFFFFFFFFFFF, 64);
  274. irsend.makeDecodeResult();
  275. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, 64, true));
  276. // Should work with a 'normal' match (not strict)
  277. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, 64, false));
  278. EXPECT_EQ(LG, irsend.capture.decode_type);
  279. EXPECT_EQ(64, irsend.capture.bits);
  280. EXPECT_EQ(0xFFFFFFFFFFFFFFFF, irsend.capture.value);
  281. EXPECT_EQ(0xFFFFFFFF, irsend.capture.address);
  282. EXPECT_EQ(0xFFFF, irsend.capture.command);
  283. }
  284. // Decode a 'real' example via GlobalCache
  285. TEST(TestDecodeLG, DecodeGlobalCacheExample) {
  286. IRsendTest irsend(4);
  287. IRrecv irrecv(4);
  288. irsend.begin();
  289. // TODO(anyone): Find a Global Cache example of the LG 28-bit message.
  290. irsend.reset();
  291. // LG (32-bit) code from Global Cache.
  292. uint16_t gc_test[75] = {
  293. 38000, 1, 69, 341, 170, 21, 64, 21, 21, 21, 64, 21, 64, 21, 21,
  294. 21, 64, 21, 21, 21, 21, 21, 64, 21, 21, 21, 64, 21, 64, 21,
  295. 21, 21, 64, 21, 21, 21, 21, 21, 64, 21, 21, 21, 64, 21, 21,
  296. 21, 64, 21, 64, 21, 64, 21, 21, 21, 21, 21, 64, 21, 21, 21,
  297. 64, 21, 21, 21, 21, 21, 21, 21, 64, 21, 1517, 341, 85, 21, 3655};
  298. irsend.sendGC(gc_test, 75);
  299. irsend.makeDecodeResult();
  300. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture, kLg32Bits, true));
  301. EXPECT_EQ(LG, irsend.capture.decode_type);
  302. EXPECT_EQ(kLg32Bits, irsend.capture.bits);
  303. EXPECT_EQ(0xB4B4AE51, irsend.capture.value);
  304. EXPECT_EQ(0xB4B, irsend.capture.address);
  305. EXPECT_EQ(0x4AE5, irsend.capture.command);
  306. EXPECT_FALSE(irsend.capture.repeat);
  307. }
  308. // Fail to decode a non-LG example via GlobalCache
  309. TEST(TestDecodeLG, FailToDecodeNonLGExample) {
  310. IRsendTest irsend(4);
  311. IRrecv irrecv(4);
  312. irsend.begin();
  313. irsend.reset();
  314. // Modified a few entries to unexpected values, based on previous test case.
  315. uint16_t gc_test[39] = {38000, 1, 1, 322, 162, 20, 61, 20, 61, 20,
  316. 20, 20, 20, 20, 20, 20, 127, 20, 61, 9,
  317. 20, 20, 61, 20, 20, 20, 61, 20, 61, 20,
  318. 61, 20, 20, 20, 20, 20, 20, 20, 884};
  319. irsend.sendGC(gc_test, 39);
  320. irsend.makeDecodeResult();
  321. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture));
  322. ASSERT_FALSE(irrecv.decodeLG(&irsend.capture, kLgBits, false));
  323. }
  324. // Tests for sendLG2().
  325. // Test sending typical data only.
  326. TEST(TestSendLG2, SendDataOnly) {
  327. IRsendTest irsend(0);
  328. irsend.begin();
  329. irsend.reset();
  330. irsend.sendLG2(0x880094D);
  331. EXPECT_EQ(
  332. "m3200s9850"
  333. "m550s1600m550s550m550s550m550s550m550s1600m550s550m550s550m550s550"
  334. "m550s550m550s550m550s550m550s550m550s550m550s550m550s550m550s550"
  335. "m550s1600m550s550m550s550m550s1600m550s550m550s1600m550s550m550s550"
  336. "m550s1600m550s1600m550s550m550s1600"
  337. "m550s55250",
  338. irsend.outputStr());
  339. }
  340. TEST(TestDecodeLG2, SyntheticExample) {
  341. IRsendTest irsend(0);
  342. IRrecv irrecv(0);
  343. irsend.begin();
  344. irsend.reset();
  345. irsend.sendLG2(0x880094D);
  346. irsend.makeDecodeResult();
  347. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture));
  348. ASSERT_EQ(LG2, irsend.capture.decode_type);
  349. EXPECT_EQ(kLgBits, irsend.capture.bits);
  350. EXPECT_EQ(0x880094D, irsend.capture.value);
  351. }
  352. // Verify decoding of LG variant 2 messages.
  353. TEST(TestDecodeLG2, RealLG2Example) {
  354. IRsendTest irsend(0);
  355. IRrecv irrecv(0);
  356. irsend.begin();
  357. irsend.reset();
  358. // From issue #548
  359. uint16_t rawData[59] = {
  360. 3154, 9834, 520, 1634, 424, 606, 424, 568, 462, 570, 462, 1564,
  361. 508, 568, 458, 544, 500, 546, 508, 530, 508, 532, 506, 566,
  362. 464, 568, 460, 578, 464, 568, 464, 532, 506, 552, 474, 1592,
  363. 506, 568, 460, 570, 462, 1564, 506, 606, 424, 1640, 424, 616,
  364. 422, 570, 462, 1616, 460, 1584, 500, 544, 506, 1598, 490}; // UNKNOWN
  365. // F6D13AE8
  366. irsend.sendRaw(rawData, 59, 38000);
  367. irsend.makeDecodeResult();
  368. ASSERT_TRUE(irrecv.decodeLG(&irsend.capture));
  369. ASSERT_EQ(LG2, irsend.capture.decode_type);
  370. EXPECT_EQ(kLgBits, irsend.capture.bits);
  371. EXPECT_EQ(0x880094D, irsend.capture.value);
  372. }