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