BrickUp API Service for Docker version.

irRecv.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "IRremote.h"
  2. #include "IRremoteInt.h"
  3. //+=============================================================================
  4. // Decodes the received IR message
  5. // Returns 0 if no data ready, 1 if data ready.
  6. // Results of decoding are stored in results
  7. //
  8. int IRrecv::decode (decode_results *results)
  9. {
  10. results->rawbuf = irparams.rawbuf;
  11. results->rawlen = irparams.rawlen;
  12. results->overflow = irparams.overflow;
  13. if (irparams.rcvstate != STATE_STOP) return false ;
  14. #if DECODE_NEC
  15. DBG_PRINTLN("Attempting NEC decode");
  16. if (decodeNEC(results)) return true ;
  17. #endif
  18. #if DECODE_SONY
  19. DBG_PRINTLN("Attempting Sony decode");
  20. if (decodeSony(results)) return true ;
  21. #endif
  22. #if DECODE_SANYO
  23. DBG_PRINTLN("Attempting Sanyo decode");
  24. if (decodeSanyo(results)) return true ;
  25. #endif
  26. #if DECODE_MITSUBISHI
  27. DBG_PRINTLN("Attempting Mitsubishi decode");
  28. if (decodeMitsubishi(results)) return true ;
  29. #endif
  30. #if DECODE_RC5
  31. DBG_PRINTLN("Attempting RC5 decode");
  32. if (decodeRC5(results)) return true ;
  33. #endif
  34. #if DECODE_RC6
  35. DBG_PRINTLN("Attempting RC6 decode");
  36. if (decodeRC6(results)) return true ;
  37. #endif
  38. #if DECODE_PANASONIC
  39. DBG_PRINTLN("Attempting Panasonic decode");
  40. if (decodePanasonic(results)) return true ;
  41. #endif
  42. #if DECODE_LG
  43. DBG_PRINTLN("Attempting LG decode");
  44. if (decodeLG(results)) return true ;
  45. #endif
  46. #if DECODE_JVC
  47. DBG_PRINTLN("Attempting JVC decode");
  48. if (decodeJVC(results)) return true ;
  49. #endif
  50. #if DECODE_SAMSUNG
  51. DBG_PRINTLN("Attempting SAMSUNG decode");
  52. if (decodeSAMSUNG(results)) return true ;
  53. #endif
  54. #if DECODE_WHYNTER
  55. DBG_PRINTLN("Attempting Whynter decode");
  56. if (decodeWhynter(results)) return true ;
  57. #endif
  58. #if DECODE_AIWA_RC_T501
  59. DBG_PRINTLN("Attempting Aiwa RC-T501 decode");
  60. if (decodeAiwaRCT501(results)) return true ;
  61. #endif
  62. #if DECODE_DENON
  63. DBG_PRINTLN("Attempting Denon decode");
  64. if (decodeDenon(results)) return true ;
  65. #endif
  66. // decodeHash returns a hash on any input.
  67. // Thus, it needs to be last in the list.
  68. // If you add any decodes, add them before this.
  69. if (decodeHash(results)) return true ;
  70. // Throw away and start over
  71. resume();
  72. return false;
  73. }
  74. //+=============================================================================
  75. IRrecv::IRrecv (int recvpin)
  76. {
  77. irparams.recvpin = recvpin;
  78. irparams.blinkflag = 0;
  79. }
  80. IRrecv::IRrecv (int recvpin, int blinkpin)
  81. {
  82. irparams.recvpin = recvpin;
  83. irparams.blinkpin = blinkpin;
  84. pinMode(blinkpin, OUTPUT);
  85. irparams.blinkflag = 0;
  86. }
  87. //+=============================================================================
  88. // initialization
  89. //
  90. void IRrecv::enableIRIn ( )
  91. {
  92. cli();
  93. // Setup pulse clock timer interrupt
  94. // Prescale /8 (16M/8 = 0.5 microseconds per tick)
  95. // Therefore, the timer interval can range from 0.5 to 128 microseconds
  96. // Depending on the reset value (255 to 0)
  97. TIMER_CONFIG_NORMAL();
  98. // Timer2 Overflow Interrupt Enable
  99. TIMER_ENABLE_INTR;
  100. TIMER_RESET;
  101. sei(); // enable interrupts
  102. // Initialize state machine variables
  103. irparams.rcvstate = STATE_IDLE;
  104. irparams.rawlen = 0;
  105. // Set pin modes
  106. pinMode(irparams.recvpin, INPUT);
  107. }
  108. //+=============================================================================
  109. // Enable/disable blinking of pin 13 on IR processing
  110. //
  111. void IRrecv::blink13 (int blinkflag)
  112. {
  113. irparams.blinkflag = blinkflag;
  114. if (blinkflag) pinMode(BLINKLED, OUTPUT) ;
  115. }
  116. //+=============================================================================
  117. // Return if receiving new IR signals
  118. //
  119. bool IRrecv::isIdle ( )
  120. {
  121. return (irparams.rcvstate == STATE_IDLE || irparams.rcvstate == STATE_STOP) ? true : false;
  122. }
  123. //+=============================================================================
  124. // Restart the ISR state machine
  125. //
  126. void IRrecv::resume ( )
  127. {
  128. irparams.rcvstate = STATE_IDLE;
  129. irparams.rawlen = 0;
  130. }
  131. //+=============================================================================
  132. // hashdecode - decode an arbitrary IR code.
  133. // Instead of decoding using a standard encoding scheme
  134. // (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
  135. //
  136. // The algorithm: look at the sequence of MARK signals, and see if each one
  137. // is shorter (0), the same length (1), or longer (2) than the previous.
  138. // Do the same with the SPACE signals. Hash the resulting sequence of 0's,
  139. // 1's, and 2's to a 32-bit value. This will give a unique value for each
  140. // different code (probably), for most code systems.
  141. //
  142. // http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
  143. //
  144. // Compare two tick values, returning 0 if newval is shorter,
  145. // 1 if newval is equal, and 2 if newval is longer
  146. // Use a tolerance of 20%
  147. //
  148. int IRrecv::compare (unsigned int oldval, unsigned int newval)
  149. {
  150. if (newval < oldval * .8) return 0 ;
  151. else if (oldval < newval * .8) return 2 ;
  152. else return 1 ;
  153. }
  154. //+=============================================================================
  155. // Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
  156. // Converts the raw code values into a 32-bit hash code.
  157. // Hopefully this code is unique for each button.
  158. // This isn't a "real" decoding, just an arbitrary value.
  159. //
  160. #define FNV_PRIME_32 16777619
  161. #define FNV_BASIS_32 2166136261
  162. long IRrecv::decodeHash (decode_results *results)
  163. {
  164. long hash = FNV_BASIS_32;
  165. // Require at least 6 samples to prevent triggering on noise
  166. if (results->rawlen < 6) return false ;
  167. for (int i = 1; (i + 2) < results->rawlen; i++) {
  168. int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
  169. // Add value into the hash
  170. hash = (hash * FNV_PRIME_32) ^ value;
  171. }
  172. results->value = hash;
  173. results->bits = 32;
  174. results->decode_type = UNKNOWN;
  175. return true;
  176. }