BrickUp API Service for Docker version.

IRremote.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //******************************************************************************
  2. // IRremote
  3. // Version 2.0.1 June, 2015
  4. // Copyright 2009 Ken Shirriff
  5. // For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
  6. //
  7. // Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
  8. // Modified by Mitra Ardron <mitra@mitra.biz>
  9. // Added Sanyo and Mitsubishi controllers
  10. // Modified Sony to spot the repeat codes that some Sony's send
  11. //
  12. // Interrupt code based on NECIRrcv by Joe Knapp
  13. // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
  14. // Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
  15. //
  16. // JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
  17. // LG added by Darryl Smith (based on the JVC protocol)
  18. // Whynter A/C ARC-110WD added by Francesco Meschia
  19. //******************************************************************************
  20. #include <avr/interrupt.h>
  21. // Defining IR_GLOBAL here allows us to declare the instantiation of global variables
  22. #define IR_GLOBAL
  23. # include "IRremote.h"
  24. # include "IRremoteInt.h"
  25. #undef IR_GLOBAL
  26. //+=============================================================================
  27. // The match functions were (apparently) originally MACROs to improve code speed
  28. // (although this would have bloated the code) hence the names being CAPS
  29. // A later release implemented debug output and so they needed to be converted
  30. // to functions.
  31. // I tried to implement a dual-compile mode (DEBUG/non-DEBUG) but for some
  32. // reason, no matter what I did I could not get them to function as macros again.
  33. // I have found a *lot* of bugs in the Arduino compiler over the last few weeks,
  34. // and I am currently assuming that one of these bugs is my problem.
  35. // I may revisit this code at a later date and look at the assembler produced
  36. // in a hope of finding out what is going on, but for now they will remain as
  37. // functions even in non-DEBUG mode
  38. //
  39. int MATCH (int measured, int desired)
  40. {
  41. DBG_PRINT(F("Testing: "));
  42. DBG_PRINT(TICKS_LOW(desired), DEC);
  43. DBG_PRINT(F(" <= "));
  44. DBG_PRINT(measured, DEC);
  45. DBG_PRINT(F(" <= "));
  46. DBG_PRINT(TICKS_HIGH(desired), DEC);
  47. bool passed = ((measured >= TICKS_LOW(desired)) && (measured <= TICKS_HIGH(desired)));
  48. if (passed)
  49. DBG_PRINTLN(F("?; passed"));
  50. else
  51. DBG_PRINTLN(F("?; FAILED"));
  52. return passed;
  53. }
  54. //+========================================================
  55. // Due to sensor lag, when received, Marks tend to be 100us too long
  56. //
  57. int MATCH_MARK (int measured_ticks, int desired_us)
  58. {
  59. DBG_PRINT(F("Testing mark (actual vs desired): "));
  60. DBG_PRINT(measured_ticks * USECPERTICK, DEC);
  61. DBG_PRINT(F("us vs "));
  62. DBG_PRINT(desired_us, DEC);
  63. DBG_PRINT("us");
  64. DBG_PRINT(": ");
  65. DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS) * USECPERTICK, DEC);
  66. DBG_PRINT(F(" <= "));
  67. DBG_PRINT(measured_ticks * USECPERTICK, DEC);
  68. DBG_PRINT(F(" <= "));
  69. DBG_PRINT(TICKS_HIGH(desired_us + MARK_EXCESS) * USECPERTICK, DEC);
  70. bool passed = ((measured_ticks >= TICKS_LOW (desired_us + MARK_EXCESS))
  71. && (measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS)));
  72. if (passed)
  73. DBG_PRINTLN(F("?; passed"));
  74. else
  75. DBG_PRINTLN(F("?; FAILED"));
  76. return passed;
  77. }
  78. //+========================================================
  79. // Due to sensor lag, when received, Spaces tend to be 100us too short
  80. //
  81. int MATCH_SPACE (int measured_ticks, int desired_us)
  82. {
  83. DBG_PRINT(F("Testing space (actual vs desired): "));
  84. DBG_PRINT(measured_ticks * USECPERTICK, DEC);
  85. DBG_PRINT(F("us vs "));
  86. DBG_PRINT(desired_us, DEC);
  87. DBG_PRINT("us");
  88. DBG_PRINT(": ");
  89. DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS) * USECPERTICK, DEC);
  90. DBG_PRINT(F(" <= "));
  91. DBG_PRINT(measured_ticks * USECPERTICK, DEC);
  92. DBG_PRINT(F(" <= "));
  93. DBG_PRINT(TICKS_HIGH(desired_us - MARK_EXCESS) * USECPERTICK, DEC);
  94. bool passed = ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS))
  95. && (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS)));
  96. if (passed)
  97. DBG_PRINTLN(F("?; passed"));
  98. else
  99. DBG_PRINTLN(F("?; FAILED"));
  100. return passed;
  101. }
  102. //+=============================================================================
  103. // Interrupt Service Routine - Fires every 50uS
  104. // TIMER2 interrupt code to collect raw data.
  105. // Widths of alternating SPACE, MARK are recorded in rawbuf.
  106. // Recorded in ticks of 50uS [microseconds, 0.000050 seconds]
  107. // 'rawlen' counts the number of entries recorded so far.
  108. // First entry is the SPACE between transmissions.
  109. // As soon as a the first [SPACE] entry gets long:
  110. // Ready is set; State switches to IDLE; Timing of SPACE continues.
  111. // As soon as first MARK arrives:
  112. // Gap width is recorded; Ready is cleared; New logging starts
  113. //
  114. ISR (TIMER_INTR_NAME)
  115. {
  116. TIMER_RESET;
  117. // Read if IR Receiver -> SPACE [xmt LED off] or a MARK [xmt LED on]
  118. // digitalRead() is very slow. Optimisation is possible, but makes the code unportable
  119. uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
  120. irparams.timer++; // One more 50uS tick
  121. if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_OVERFLOW ; // Buffer overflow
  122. switch(irparams.rcvstate) {
  123. //......................................................................
  124. case STATE_IDLE: // In the middle of a gap
  125. if (irdata == MARK) {
  126. if (irparams.timer < GAP_TICKS) { // Not big enough to be a gap.
  127. irparams.timer = 0;
  128. } else {
  129. // Gap just ended; Record duration; Start recording transmission
  130. irparams.overflow = false;
  131. irparams.rawlen = 0;
  132. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  133. irparams.timer = 0;
  134. irparams.rcvstate = STATE_MARK;
  135. }
  136. }
  137. break;
  138. //......................................................................
  139. case STATE_MARK: // Timing Mark
  140. if (irdata == SPACE) { // Mark ended; Record time
  141. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  142. irparams.timer = 0;
  143. irparams.rcvstate = STATE_SPACE;
  144. }
  145. break;
  146. //......................................................................
  147. case STATE_SPACE: // Timing Space
  148. if (irdata == MARK) { // Space just ended; Record time
  149. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  150. irparams.timer = 0;
  151. irparams.rcvstate = STATE_MARK;
  152. } else if (irparams.timer > GAP_TICKS) { // Space
  153. // A long Space, indicates gap between codes
  154. // Flag the current code as ready for processing
  155. // Switch to STOP
  156. // Don't reset timer; keep counting Space width
  157. irparams.rcvstate = STATE_STOP;
  158. }
  159. break;
  160. //......................................................................
  161. case STATE_STOP: // Waiting; Measuring Gap
  162. if (irdata == MARK) irparams.timer = 0 ; // Reset gap timer
  163. break;
  164. //......................................................................
  165. case STATE_OVERFLOW: // Flag up a read overflow; Stop the State Machine
  166. irparams.overflow = true;
  167. irparams.rcvstate = STATE_STOP;
  168. break;
  169. }
  170. // If requested, flash LED while receiving IR data
  171. if (irparams.blinkflag) {
  172. if (irdata == MARK)
  173. if (irparams.blinkpin) digitalWrite(irparams.blinkpin, HIGH); // Turn user defined pin LED on
  174. else BLINKLED_ON() ; // if no user defined LED pin, turn default LED pin for the hardware on
  175. else if (irparams.blinkpin) digitalWrite(irparams.blinkpin, LOW); // Turn user defined pin LED on
  176. else BLINKLED_OFF() ; // if no user defined LED pin, turn default LED pin for the hardware on
  177. }
  178. }