Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

191 lines
4.5KB

  1. /*
  2. * IRremote: IRtest unittest
  3. * Version 0.1 July, 2009
  4. * Copyright 2009 Ken Shirriff
  5. * http://arcfn.com
  6. *
  7. * Note: to run these tests, edit IRremote/IRremote.h to add "#define TEST"
  8. * You must then recompile the library by removing IRremote.o and restarting
  9. * the arduino IDE.
  10. */
  11. #include <IRremote.h>
  12. #include <IRremoteInt.h>
  13. // Dumps out the decode_results structure.
  14. // Call this after IRrecv::decode()
  15. // void * to work around compiler issue
  16. //void dump(void *v) {
  17. // decode_results *results = (decode_results *)v
  18. void dump(decode_results *results) {
  19. int count = results->rawlen;
  20. if (results->decode_type == UNKNOWN) {
  21. Serial.println("Could not decode message");
  22. }
  23. else {
  24. if (results->decode_type == NEC) {
  25. Serial.print("Decoded NEC: ");
  26. }
  27. else if (results->decode_type == SONY) {
  28. Serial.print("Decoded SONY: ");
  29. }
  30. else if (results->decode_type == RC5) {
  31. Serial.print("Decoded RC5: ");
  32. }
  33. else if (results->decode_type == RC6) {
  34. Serial.print("Decoded RC6: ");
  35. }
  36. Serial.print(results->value, HEX);
  37. Serial.print(" (");
  38. Serial.print(results->bits, DEC);
  39. Serial.println(" bits)");
  40. }
  41. Serial.print("Raw (");
  42. Serial.print(count, DEC);
  43. Serial.print("): ");
  44. for (int i = 0; i < count; i++) {
  45. if ((i % 2) == 1) {
  46. Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  47. }
  48. else {
  49. Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  50. }
  51. Serial.print(" ");
  52. }
  53. Serial.println("");
  54. }
  55. IRrecv irrecv(0);
  56. decode_results results;
  57. class IRsendDummy :
  58. public IRsend
  59. {
  60. public:
  61. // For testing, just log the marks/spaces
  62. #define SENDLOG_LEN 128
  63. int sendlog[SENDLOG_LEN];
  64. int sendlogcnt;
  65. IRsendDummy() :
  66. IRsend() {
  67. }
  68. void reset() {
  69. sendlogcnt = 0;
  70. }
  71. void mark(int time) {
  72. sendlog[sendlogcnt] = time;
  73. if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
  74. }
  75. void space(int time) {
  76. sendlog[sendlogcnt] = -time;
  77. if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
  78. }
  79. // Copies the dummy buf into the interrupt buf
  80. void useDummyBuf() {
  81. int last = SPACE;
  82. irparams.rcvstate = STATE_STOP;
  83. irparams.rawlen = 1; // Skip the gap
  84. for (int i = 0 ; i < sendlogcnt; i++) {
  85. if (sendlog[i] < 0) {
  86. if (last == MARK) {
  87. // New space
  88. irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK;
  89. last = SPACE;
  90. }
  91. else {
  92. // More space
  93. irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK;
  94. }
  95. }
  96. else if (sendlog[i] > 0) {
  97. if (last == SPACE) {
  98. // New mark
  99. irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK;
  100. last = MARK;
  101. }
  102. else {
  103. // More mark
  104. irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK;
  105. }
  106. }
  107. }
  108. if (irparams.rawlen % 2) {
  109. irparams.rawlen--; // Remove trailing space
  110. }
  111. }
  112. };
  113. IRsendDummy irsenddummy;
  114. void verify(unsigned long val, int bits, int type) {
  115. irsenddummy.useDummyBuf();
  116. irrecv.decode(&results);
  117. Serial.print("Testing ");
  118. Serial.print(val, HEX);
  119. if (results.value == val && results.bits == bits && results.decode_type == type) {
  120. Serial.println(": OK");
  121. }
  122. else {
  123. Serial.println(": Error");
  124. dump(&results);
  125. }
  126. }
  127. void testNEC(unsigned long val, int bits) {
  128. irsenddummy.reset();
  129. irsenddummy.sendNEC(val, bits);
  130. verify(val, bits, NEC);
  131. }
  132. void testSony(unsigned long val, int bits) {
  133. irsenddummy.reset();
  134. irsenddummy.sendSony(val, bits);
  135. verify(val, bits, SONY);
  136. }
  137. void testRC5(unsigned long val, int bits) {
  138. irsenddummy.reset();
  139. irsenddummy.sendRC5(val, bits);
  140. verify(val, bits, RC5);
  141. }
  142. void testRC6(unsigned long val, int bits) {
  143. irsenddummy.reset();
  144. irsenddummy.sendRC6(val, bits);
  145. verify(val, bits, RC6);
  146. }
  147. void test() {
  148. Serial.println("NEC tests");
  149. testNEC(0x00000000, 32);
  150. testNEC(0xffffffff, 32);
  151. testNEC(0xaaaaaaaa, 32);
  152. testNEC(0x55555555, 32);
  153. testNEC(0x12345678, 32);
  154. Serial.println("Sony tests");
  155. testSony(0xfff, 12);
  156. testSony(0x000, 12);
  157. testSony(0xaaa, 12);
  158. testSony(0x555, 12);
  159. testSony(0x123, 12);
  160. Serial.println("RC5 tests");
  161. testRC5(0xfff, 12);
  162. testRC5(0x000, 12);
  163. testRC5(0xaaa, 12);
  164. testRC5(0x555, 12);
  165. testRC5(0x123, 12);
  166. Serial.println("RC6 tests");
  167. testRC6(0xfffff, 20);
  168. testRC6(0x00000, 20);
  169. testRC6(0xaaaaa, 20);
  170. testRC6(0x55555, 20);
  171. testRC6(0x12345, 20);
  172. }
  173. void setup()
  174. {
  175. Serial.begin(9600);
  176. test();
  177. }
  178. void loop() {
  179. }