Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

291 rinda
8.0KB

  1. /*
  2. * Test send/receive functions of IRremote, using a pair of Arduinos.
  3. *
  4. * Arduino #1 should have an IR LED connected to the send pin (3).
  5. * Arduino #2 should have an IR detector/demodulator connected to the
  6. * receive pin (11) and a visible LED connected to pin 3.
  7. *
  8. * The cycle:
  9. * Arduino #1 will wait 2 seconds, then run through the tests.
  10. * It repeats this forever.
  11. * Arduino #2 will wait for at least one second of no signal
  12. * (to synchronize with #1). It will then wait for the same test
  13. * signals. It will log all the status to the serial port. It will
  14. * also indicate status through the LED, which will flash each time a test
  15. * is completed. If there is an error, it will light up for 5 seconds.
  16. *
  17. * The test passes if the LED flashes 19 times, pauses, and then repeats.
  18. * The test fails if the LED lights for 5 seconds.
  19. *
  20. * The test software automatically decides which board is the sender and which is
  21. * the receiver by looking for an input on the send pin, which will indicate
  22. * the sender. You should hook the serial port to the receiver for debugging.
  23. *
  24. * Copyright 2010 Ken Shirriff
  25. * http://arcfn.com
  26. */
  27. #include <IRremote.h>
  28. int RECV_PIN = 11;
  29. int LED_PIN = 3;
  30. IRrecv irrecv(RECV_PIN);
  31. IRsend irsend;
  32. decode_results results;
  33. #define RECEIVER 1
  34. #define SENDER 2
  35. #define ERROR 3
  36. int mode;
  37. void setup()
  38. {
  39. Serial.begin(9600);
  40. // Check RECV_PIN to decide if we're RECEIVER or SENDER
  41. if (digitalRead(RECV_PIN) == HIGH) {
  42. mode = RECEIVER;
  43. irrecv.enableIRIn();
  44. pinMode(LED_PIN, OUTPUT);
  45. digitalWrite(LED_PIN, LOW);
  46. Serial.println("Receiver mode");
  47. }
  48. else {
  49. mode = SENDER;
  50. Serial.println("Sender mode");
  51. }
  52. }
  53. // Wait for the gap between tests, to synchronize with
  54. // the sender.
  55. // Specifically, wait for a signal followed by a gap of at last gap ms.
  56. void waitForGap(unsigned long gap) {
  57. Serial.println("Waiting for gap");
  58. while (1) {
  59. while (digitalRead(RECV_PIN) == LOW) {
  60. }
  61. unsigned long time = millis();
  62. while (digitalRead(RECV_PIN) == HIGH) {
  63. if (millis() - time > gap) {
  64. return;
  65. }
  66. }
  67. }
  68. }
  69. // Dumps out the decode_results structure.
  70. // Call this after IRrecv::decode()
  71. void dump(decode_results *results) {
  72. int count = results->rawlen;
  73. if (results->decode_type == UNKNOWN) {
  74. Serial.println("Could not decode message");
  75. }
  76. else {
  77. if (results->decode_type == NEC) {
  78. Serial.print("Decoded NEC: ");
  79. }
  80. else if (results->decode_type == SONY) {
  81. Serial.print("Decoded SONY: ");
  82. }
  83. else if (results->decode_type == RC5) {
  84. Serial.print("Decoded RC5: ");
  85. }
  86. else if (results->decode_type == RC6) {
  87. Serial.print("Decoded RC6: ");
  88. }
  89. Serial.print(results->value, HEX);
  90. Serial.print(" (");
  91. Serial.print(results->bits, DEC);
  92. Serial.println(" bits)");
  93. }
  94. Serial.print("Raw (");
  95. Serial.print(count, DEC);
  96. Serial.print("): ");
  97. for (int i = 0; i < count; i++) {
  98. if ((i % 2) == 1) {
  99. Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  100. }
  101. else {
  102. Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  103. }
  104. Serial.print(" ");
  105. }
  106. Serial.println("");
  107. }
  108. // Test send or receive.
  109. // If mode is SENDER, send a code of the specified type, value, and bits
  110. // If mode is RECEIVER, receive a code and verify that it is of the
  111. // specified type, value, and bits. For success, the LED is flashed;
  112. // for failure, the mode is set to ERROR.
  113. // The motivation behind this method is that the sender and the receiver
  114. // can do the same test calls, and the mode variable indicates whether
  115. // to send or receive.
  116. void test(const char *label, int type, unsigned long value, int bits) {
  117. if (mode == SENDER) {
  118. Serial.println(label);
  119. if (type == NEC) {
  120. irsend.sendNEC(value, bits);
  121. }
  122. else if (type == SONY) {
  123. irsend.sendSony(value, bits);
  124. }
  125. else if (type == RC5) {
  126. irsend.sendRC5(value, bits);
  127. }
  128. else if (type == RC6) {
  129. irsend.sendRC6(value, bits);
  130. }
  131. else {
  132. Serial.print(label);
  133. Serial.println("Bad type!");
  134. }
  135. delay(200);
  136. }
  137. else if (mode == RECEIVER) {
  138. irrecv.resume(); // Receive the next value
  139. unsigned long max_time = millis() + 30000;
  140. Serial.print(label);
  141. // Wait for decode or timeout
  142. while (!irrecv.decode(&results)) {
  143. if (millis() > max_time) {
  144. Serial.println("Timeout receiving data");
  145. mode = ERROR;
  146. return;
  147. }
  148. }
  149. if (type == results.decode_type && value == results.value && bits == results.bits) {
  150. Serial.println (": OK");
  151. digitalWrite(LED_PIN, HIGH);
  152. delay(20);
  153. digitalWrite(LED_PIN, LOW);
  154. }
  155. else {
  156. Serial.println(": BAD");
  157. dump(&results);
  158. mode = ERROR;
  159. }
  160. }
  161. }
  162. // Test raw send or receive. This is similar to the test method,
  163. // except it send/receives raw data.
  164. void testRaw(const char *label, unsigned int *rawbuf, int rawlen) {
  165. if (mode == SENDER) {
  166. Serial.println(label);
  167. irsend.sendRaw(rawbuf, rawlen, 38 /* kHz */);
  168. delay(200);
  169. }
  170. else if (mode == RECEIVER ) {
  171. irrecv.resume(); // Receive the next value
  172. unsigned long max_time = millis() + 30000;
  173. Serial.print(label);
  174. // Wait for decode or timeout
  175. while (!irrecv.decode(&results)) {
  176. if (millis() > max_time) {
  177. Serial.println("Timeout receiving data");
  178. mode = ERROR;
  179. return;
  180. }
  181. }
  182. // Received length has extra first element for gap
  183. if (rawlen != results.rawlen - 1) {
  184. Serial.print("Bad raw length ");
  185. Serial.println(results.rawlen, DEC);
  186. mode = ERROR;
  187. return;
  188. }
  189. for (int i = 0; i < rawlen; i++) {
  190. long got = results.rawbuf[i+1] * USECPERTICK;
  191. // Adjust for extra duration of marks
  192. if (i % 2 == 0) {
  193. got -= MARK_EXCESS;
  194. }
  195. else {
  196. got += MARK_EXCESS;
  197. }
  198. // See if close enough, within 25%
  199. if (rawbuf[i] * 1.25 < got || got * 1.25 < rawbuf[i]) {
  200. Serial.println(": BAD");
  201. dump(&results);
  202. mode = ERROR;
  203. return;
  204. }
  205. }
  206. Serial.println (": OK");
  207. digitalWrite(LED_PIN, HIGH);
  208. delay(20);
  209. digitalWrite(LED_PIN, LOW);
  210. }
  211. }
  212. // This is the raw data corresponding to NEC 0x12345678
  213. unsigned int sendbuf[] = { /* NEC format */
  214. 9000, 4500,
  215. 560, 560, 560, 560, 560, 560, 560, 1690, /* 1 */
  216. 560, 560, 560, 560, 560, 1690, 560, 560, /* 2 */
  217. 560, 560, 560, 560, 560, 1690, 560, 1690, /* 3 */
  218. 560, 560, 560, 1690, 560, 560, 560, 560, /* 4 */
  219. 560, 560, 560, 1690, 560, 560, 560, 1690, /* 5 */
  220. 560, 560, 560, 1690, 560, 1690, 560, 560, /* 6 */
  221. 560, 560, 560, 1690, 560, 1690, 560, 1690, /* 7 */
  222. 560, 1690, 560, 560, 560, 560, 560, 560, /* 8 */
  223. 560};
  224. void loop() {
  225. if (mode == SENDER) {
  226. delay(2000); // Delay for more than gap to give receiver a better chance to sync.
  227. }
  228. else if (mode == RECEIVER) {
  229. waitForGap(1000);
  230. }
  231. else if (mode == ERROR) {
  232. // Light up for 5 seconds for error
  233. digitalWrite(LED_PIN, HIGH);
  234. delay(5000);
  235. digitalWrite(LED_PIN, LOW);
  236. mode = RECEIVER; // Try again
  237. return;
  238. }
  239. // The test suite.
  240. test("SONY1", SONY, 0x123, 12);
  241. test("SONY2", SONY, 0x000, 12);
  242. test("SONY3", SONY, 0xfff, 12);
  243. test("SONY4", SONY, 0x12345, 20);
  244. test("SONY5", SONY, 0x00000, 20);
  245. test("SONY6", SONY, 0xfffff, 20);
  246. test("NEC1", NEC, 0x12345678, 32);
  247. test("NEC2", NEC, 0x00000000, 32);
  248. test("NEC3", NEC, 0xffffffff, 32);
  249. test("NEC4", NEC, REPEAT, 32);
  250. test("RC51", RC5, 0x12345678, 32);
  251. test("RC52", RC5, 0x0, 32);
  252. test("RC53", RC5, 0xffffffff, 32);
  253. test("RC61", RC6, 0x12345678, 32);
  254. test("RC62", RC6, 0x0, 32);
  255. test("RC63", RC6, 0xffffffff, 32);
  256. // Tests of raw sending and receiving.
  257. // First test sending raw and receiving raw.
  258. // Then test sending raw and receiving decoded NEC
  259. // Then test sending NEC and receiving raw
  260. testRaw("RAW1", sendbuf, 67);
  261. if (mode == SENDER) {
  262. testRaw("RAW2", sendbuf, 67);
  263. test("RAW3", NEC, 0x12345678, 32);
  264. }
  265. else {
  266. test("RAW2", NEC, 0x12345678, 32);
  267. testRaw("RAW3", sendbuf, 67);
  268. }
  269. }