BrickUp API Service for Docker version.

ir_Template.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Assuming the protocol we are adding is for the (imaginary) manufacturer: Shuzu
  3. Our fantasy protocol is a standard protocol, so we can use this standard
  4. template without too much work. Some protocols are quite unique and will require
  5. considerably more work in this file! It is way beyond the scope of this text to
  6. explain how to reverse engineer "unusual" IR protocols. But, unless you own an
  7. oscilloscope, the starting point is probably to use the rawDump.ino sketch and
  8. try to spot the pattern!
  9. Before you start, make sure the IR library is working OK:
  10. # Open up the Arduino IDE
  11. # Load up the rawDump.ino example sketch
  12. # Run it
  13. Now we can start to add our new protocol...
  14. 1. Copy this file to : ir_Shuzu.cpp
  15. 2. Replace all occurrences of "Shuzu" with the name of your protocol.
  16. 3. Tweak the #defines to suit your protocol.
  17. 4. If you're lucky, tweaking the #defines will make the default send() function
  18. work.
  19. 5. Again, if you're lucky, tweaking the #defines will have made the default
  20. decode() function work.
  21. You have written the code to support your new protocol!
  22. Now you must do a few things to add it to the IRremote system:
  23. 1. Open IRremote.h and make the following changes:
  24. REMEMEBER to change occurences of "SHUZU" with the name of your protocol
  25. A. At the top, in the section "Supported Protocols", add:
  26. #define DECODE_SHUZU 1
  27. #define SEND_SHUZU 1
  28. B. In the section "enumerated list of all supported formats", add:
  29. SHUZU,
  30. to the end of the list (notice there is a comma after the protocol name)
  31. C. Further down in "Main class for receiving IR", add:
  32. //......................................................................
  33. #if DECODE_SHUZU
  34. bool decodeShuzu (decode_results *results) ;
  35. #endif
  36. D. Further down in "Main class for sending IR", add:
  37. //......................................................................
  38. #if SEND_SHUZU
  39. void sendShuzu (unsigned long data, int nbits) ;
  40. #endif
  41. E. Save your changes and close the file
  42. 2. Now open irRecv.cpp and make the following change:
  43. A. In the function IRrecv::decode(), add:
  44. #ifdef DECODE_NEC
  45. DBG_PRINTLN("Attempting Shuzu decode");
  46. if (decodeShuzu(results)) return true ;
  47. #endif
  48. B. Save your changes and close the file
  49. You will probably want to add your new protocol to the example sketch
  50. 3. Open MyDocuments\Arduino\libraries\IRremote\examples\IRrecvDumpV2.ino
  51. A. In the encoding() function, add:
  52. case SHUZU: Serial.print("SHUZU"); break ;
  53. Now open the Arduino IDE, load up the rawDump.ino sketch, and run it.
  54. Hopefully it will compile and upload.
  55. If it doesn't, you've done something wrong. Check your work.
  56. If you can't get it to work - seek help from somewhere.
  57. If you get this far, I will assume you have successfully added your new protocol
  58. There is one last thing to do.
  59. 1. Delete this giant instructional comment.
  60. 2. Send a copy of your work to us so we can include it in the library and
  61. others may benefit from your hard work and maybe even write a song about how
  62. great you are for helping them! :)
  63. Regards,
  64. BlueChip
  65. */
  66. #include "IRremote.h"
  67. #include "IRremoteInt.h"
  68. //==============================================================================
  69. //
  70. //
  71. // S H U Z U
  72. //
  73. //
  74. //==============================================================================
  75. #define BITS 32 // The number of bits in the command
  76. #define HDR_MARK 1000 // The length of the Header:Mark
  77. #define HDR_SPACE 2000 // The lenght of the Header:Space
  78. #define BIT_MARK 3000 // The length of a Bit:Mark
  79. #define ONE_SPACE 4000 // The length of a Bit:Space for 1's
  80. #define ZERO_SPACE 5000 // The length of a Bit:Space for 0's
  81. #define OTHER 1234 // Other things you may need to define
  82. //+=============================================================================
  83. //
  84. #if SEND_SHUZU
  85. void IRsend::sendShuzu (unsigned long data, int nbits)
  86. {
  87. // Set IR carrier frequency
  88. enableIROut(38);
  89. // Header
  90. mark (HDR_MARK);
  91. space(HDR_SPACE);
  92. // Data
  93. for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1) {
  94. if (data & mask) {
  95. mark (BIT_MARK);
  96. space(ONE_SPACE);
  97. } else {
  98. mark (BIT_MARK);
  99. space(ZERO_SPACE);
  100. }
  101. }
  102. // Footer
  103. mark(BIT_MARK);
  104. space(0); // Always end with the LED off
  105. }
  106. #endif
  107. //+=============================================================================
  108. //
  109. #if DECODE_SHUZU
  110. bool IRrecv::decodeShuzu (decode_results *results)
  111. {
  112. unsigned long data = 0; // Somewhere to build our code
  113. int offset = 1; // Skip the Gap reading
  114. // Check we have the right amount of data
  115. if (irparams.rawlen != 1 + 2 + (2 * BITS) + 1) return false ;
  116. // Check initial Mark+Space match
  117. if (!MATCH_MARK (results->rawbuf[offset++], HDR_MARK )) return false ;
  118. if (!MATCH_SPACE(results->rawbuf[offset++], HDR_SPACE)) return false ;
  119. // Read the bits in
  120. for (int i = 0; i < SHUZU_BITS; i++) {
  121. // Each bit looks like: MARK + SPACE_1 -> 1
  122. // or : MARK + SPACE_0 -> 0
  123. if (!MATCH_MARK(results->rawbuf[offset++], BIT_MARK)) return false ;
  124. // IR data is big-endian, so we shuffle it in from the right:
  125. if (MATCH_SPACE(results->rawbuf[offset], ONE_SPACE)) data = (data << 1) | 1 ;
  126. else if (MATCH_SPACE(results->rawbuf[offset], ZERO_SPACE)) data = (data << 1) | 0 ;
  127. else return false ;
  128. offset++;
  129. }
  130. // Success
  131. results->bits = BITS;
  132. results->value = data;
  133. results->decode_type = SHUZU;
  134. return true;
  135. }
  136. #endif