Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <boarddefs.h>
  2. #include <IRremote.h>
  3. #include <IRremoteInt.h>
  4. #include <ir_Lego_PF_BitStreamEncoder.h>
  5.  
  6.  
  7.  
  8. int RECV_PIN = 2;
  9. int STATUS_PIN = 13;
  10.  
  11. IRrecv irrecv(RECV_PIN);
  12. IRsend irsend;
  13.  
  14. decode_results results;
  15.  
  16. void setup()
  17. {
  18. Serial.begin(9600);
  19. irrecv.enableIRIn(); // Start the receiver
  20. pinMode(STATUS_PIN, OUTPUT);
  21. }
  22.  
  23. // Storage for the recorded code
  24. int codeType = -1; // The type of code
  25. unsigned long codeValue; // The code value if not raw
  26. unsigned int rawCodes[RAWBUF]; // The durations if raw
  27. int codeLen; // The length of the code
  28. int toggle = 0; // The RC5/6 toggle state
  29.  
  30. // Stores the code for later playback
  31. // Most of this code is just logging
  32. void storeCode(decode_results *results) {
  33. codeType = results->decode_type;
  34. int count = results->rawlen;
  35. if (codeType == UNKNOWN) {
  36. Serial.println("Received unknown code, saving as raw");
  37. codeLen = results->rawlen - 1;
  38. // To store raw codes:
  39. // Drop first value (gap)
  40. // Convert from ticks to microseconds
  41. // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
  42. for (int i = 1; i <= codeLen; i++) {
  43. if (i % 2) {
  44. // Mark
  45. rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
  46. Serial.print(" m");
  47. }
  48. else {
  49. // Space
  50. rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
  51. Serial.print(" s");
  52. }
  53. Serial.print(rawCodes[i - 1], DEC);
  54. }
  55. Serial.println("");
  56. }
  57. else {
  58. if (codeType == NEC) {
  59. Serial.print("Received NEC: ");
  60. }
  61. else if (codeType == SONY) {
  62. Serial.print("Received SONY: ");
  63. }
  64. else if (codeType == RC5) {
  65. Serial.print("Received RC5: ");
  66. }
  67. else if (codeType == RC6) {
  68. Serial.print("Received RC6: ");
  69. }
  70. else {
  71. Serial.print("Unexpected codeType ");
  72. Serial.print(codeType, DEC);
  73. Serial.println("");
  74. }
  75. Serial.println(results->value, HEX);
  76. codeValue = results->value;
  77. codeLen = results->bits;
  78. }
  79. }
  80.  
  81. void sendCode(int repeat) {
  82. if(codeValue == 0xFFFFFFFF) return; //ignore FFFFFFF IR code
  83. if (codeType == NEC) {
  84. irsend.sendNEC(codeValue, codeLen);
  85. Serial.print("Sent NEC ");
  86. Serial.println(codeValue, HEX);
  87. }
  88. else if (codeType == SONY) {
  89. irsend.sendSony(codeValue, codeLen);
  90. Serial.print("Sent Sony ");
  91. Serial.println(codeValue, HEX);
  92. }
  93. else if (codeType == RC5 || codeType == RC6) {
  94. if (!repeat) {
  95. // Flip the toggle bit for a new button press
  96. toggle = 1 - toggle;
  97. }
  98. // Put the toggle bit into the code to send
  99. codeValue = codeValue & ~(1 << (codeLen - 1));
  100. codeValue = codeValue | (toggle << (codeLen - 1));
  101. if (codeType == RC5) {
  102. Serial.print("Sent RC5 ");
  103. Serial.println(codeValue, HEX);
  104. irsend.sendRC5(codeValue, codeLen);
  105. }
  106. else {
  107. irsend.sendRC6(codeValue, codeLen);
  108. Serial.print("Sent RC6 ");
  109. Serial.println(codeValue, HEX);
  110. }
  111. }
  112. else if (codeType == UNKNOWN /* i.e. raw */) {
  113. // Assume 38 KHz
  114. irsend.sendRaw(rawCodes, codeLen, 38);
  115. Serial.println("Sent raw");
  116. }
  117. }
  118.  
  119.  
  120. void loop() {
  121.  
  122. if (irrecv.decode(&results)) {
  123. /* Receive IR code */
  124. Serial.println("Pressed!");
  125. digitalWrite(STATUS_PIN, HIGH);
  126. storeCode(&results);
  127. irrecv.resume(); // resume receiver
  128. digitalWrite(STATUS_PIN, LOW);
  129. delay(50);
  130.  
  131. /* Send IR code */
  132. Serial.println("Sending!");
  133. digitalWrite(STATUS_PIN, HIGH);
  134. sendCode(true);
  135. digitalWrite(STATUS_PIN, LOW);
  136. delay(50); // Wait a bit between retransmissions
  137. irrecv.enableIRIn(); // Re-start the receiver
  138. Serial.println("--------------------------------");
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement