ir_JVC.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2015 Kristian Lauszus
  2. // Copyright 2017 David Conran
  3. #include <algorithm>
  4. #include "IRrecv.h"
  5. #include "IRsend.h"
  6. #include "IRtimer.h"
  7. #include "IRutils.h"
  8. // JJJJJ V V CCCC
  9. // J V V C
  10. // J V V C
  11. // J J V V C
  12. // J V CCCC
  13. // JVC originally added by Kristian Lauszus
  14. // (Thanks to zenwheel and other people at the original blog post)
  15. // Constants
  16. // Ref:
  17. // http://www.sbprojects.com/knowledge/ir/jvc.php
  18. const uint16_t kJvcTick = 75;
  19. const uint16_t kJvcHdrMarkTicks = 112;
  20. const uint16_t kJvcHdrMark = kJvcHdrMarkTicks * kJvcTick;
  21. const uint16_t kJvcHdrSpaceTicks = 56;
  22. const uint16_t kJvcHdrSpace = kJvcHdrSpaceTicks * kJvcTick;
  23. const uint16_t kJvcBitMarkTicks = 7;
  24. const uint16_t kJvcBitMark = kJvcBitMarkTicks * kJvcTick;
  25. const uint16_t kJvcOneSpaceTicks = 23;
  26. const uint16_t kJvcOneSpace = kJvcOneSpaceTicks * kJvcTick;
  27. const uint16_t kJvcZeroSpaceTicks = 7;
  28. const uint16_t kJvcZeroSpace = kJvcZeroSpaceTicks * kJvcTick;
  29. const uint16_t kJvcRptLengthTicks = 800;
  30. const uint16_t kJvcRptLength = kJvcRptLengthTicks * kJvcTick;
  31. const uint16_t kJvcMinGapTicks =
  32. kJvcRptLengthTicks -
  33. (kJvcHdrMarkTicks + kJvcHdrSpaceTicks +
  34. kJvcBits * (kJvcBitMarkTicks + kJvcOneSpaceTicks) + kJvcBitMarkTicks);
  35. const uint16_t kJvcMinGap = kJvcMinGapTicks * kJvcTick;
  36. #if SEND_JVC
  37. // Send a JVC message.
  38. //
  39. // Args:
  40. // data: The contents of the command you want to send.
  41. // nbits: The bit size of the command being sent. (kJvcBits)
  42. // repeat: The number of times you want the command to be repeated.
  43. //
  44. // Status: STABLE.
  45. //
  46. // Ref:
  47. // http://www.sbprojects.com/knowledge/ir/jvc.php
  48. void IRsend::sendJVC(uint64_t data, uint16_t nbits, uint16_t repeat) {
  49. // Set 38kHz IR carrier frequency & a 1/3 (33%) duty cycle.
  50. enableIROut(38, 33);
  51. IRtimer usecs = IRtimer();
  52. // Header
  53. // Only sent for the first message.
  54. mark(kJvcHdrMark);
  55. space(kJvcHdrSpace);
  56. // We always send the data & footer at least once, hence '<= repeat'.
  57. for (uint16_t i = 0; i <= repeat; i++) {
  58. sendGeneric(0, 0, // No Header
  59. kJvcBitMark, kJvcOneSpace, kJvcBitMark, kJvcZeroSpace,
  60. kJvcBitMark, kJvcMinGap, data, nbits, 38, true,
  61. 0, // Repeats are handles elsewhere.
  62. 33);
  63. // Wait till the end of the repeat time window before we send another code.
  64. uint32_t elapsed = usecs.elapsed();
  65. // Avoid potential unsigned integer underflow.
  66. // e.g. when elapsed > kJvcRptLength.
  67. if (elapsed < kJvcRptLength) space(kJvcRptLength - elapsed);
  68. usecs.reset();
  69. }
  70. }
  71. // Calculate the raw JVC data based on address and command.
  72. //
  73. // Args:
  74. // address: An 8-bit address value.
  75. // command: An 8-bit command value.
  76. // Returns:
  77. // A raw JVC message.
  78. //
  79. // Status: BETA / Should work fine.
  80. //
  81. // Ref:
  82. // http://www.sbprojects.com/knowledge/ir/jvc.php
  83. uint16_t IRsend::encodeJVC(uint8_t address, uint8_t command) {
  84. return reverseBits((command << 8) | address, 16);
  85. }
  86. #endif
  87. #if DECODE_JVC
  88. // Decode the supplied JVC message.
  89. //
  90. // Args:
  91. // results: Ptr to the data to decode and where to store the decode result.
  92. // nbits: Nr. of bits of data to expect. Typically kJvcBits.
  93. // strict: Flag indicating if we should perform strict matching.
  94. // Returns:
  95. // boolean: True if it can decode it, false if it can't.
  96. //
  97. // Status: STABLE
  98. //
  99. // Note:
  100. // JVC repeat codes don't have a header.
  101. // Ref:
  102. // http://www.sbprojects.com/knowledge/ir/jvc.php
  103. bool IRrecv::decodeJVC(decode_results *results, uint16_t nbits, bool strict) {
  104. if (strict && nbits != kJvcBits)
  105. return false; // Must be called with the correct nr. of bits.
  106. if (results->rawlen < 2 * nbits + kFooter - 1)
  107. return false; // Can't possibly be a valid JVC message.
  108. uint64_t data = 0;
  109. uint16_t offset = kStartOffset;
  110. bool isRepeat = true;
  111. uint32_t m_tick;
  112. uint32_t s_tick;
  113. // Header
  114. // (Optional as repeat codes don't have the header)
  115. if (matchMark(results->rawbuf[offset], kJvcHdrMark)) {
  116. isRepeat = false;
  117. m_tick = results->rawbuf[offset++] * kRawTick / kJvcHdrMarkTicks;
  118. if (results->rawlen < 2 * nbits + 4)
  119. return false; // Can't possibly be a valid JVC message with a header.
  120. if (!matchSpace(results->rawbuf[offset], kJvcHdrSpace)) return false;
  121. s_tick = results->rawbuf[offset++] * kRawTick / kJvcHdrSpaceTicks;
  122. } else {
  123. // We can't easily auto-calibrate as there is no header, so assume
  124. // the default tick time.
  125. m_tick = kJvcTick;
  126. s_tick = kJvcTick;
  127. }
  128. // Data
  129. match_result_t data_result =
  130. matchData(&(results->rawbuf[offset]), nbits, kJvcBitMarkTicks * m_tick,
  131. kJvcOneSpaceTicks * s_tick, kJvcBitMarkTicks * m_tick,
  132. kJvcZeroSpaceTicks * s_tick);
  133. if (data_result.success == false) return false;
  134. data = data_result.data;
  135. offset += data_result.used;
  136. // Footer
  137. if (!matchMark(results->rawbuf[offset++], kJvcBitMarkTicks * m_tick))
  138. return false;
  139. if (offset < results->rawlen &&
  140. !matchAtLeast(results->rawbuf[offset], kJvcMinGapTicks * s_tick))
  141. return false;
  142. // Success
  143. results->decode_type = JVC;
  144. results->bits = nbits;
  145. results->value = data;
  146. // command & address are transmitted LSB first, so we need to reverse them.
  147. results->address = reverseBits(data >> 8, 8); // The first 8 bits sent.
  148. results->command = reverseBits(data & 0xFF, 8); // The last 8 bits sent.
  149. results->repeat = isRepeat;
  150. return true;
  151. }
  152. #endif