|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Include the IRremote library headers |
| 3 | +// |
| 4 | +#include <IRremote.h> |
| 5 | +#include <IRremoteInt.h> |
| 6 | + |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | +// Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838) |
| 9 | +// |
| 10 | +IRrecv irrecv(6); |
| 11 | + |
| 12 | +//+============================================================================= |
| 13 | +// Configure the Arduino |
| 14 | +// |
| 15 | +void setup ( ) |
| 16 | +{ |
| 17 | + Serial.begin(9600); // Status message will be sent to PC at 9600 baud |
| 18 | + irrecv.enableIRIn(); // Start the receiver |
| 19 | +} |
| 20 | + |
| 21 | +//+============================================================================= |
| 22 | +// Display IR code |
| 23 | +// |
| 24 | +void ircode (decode_results *results) |
| 25 | +{ |
| 26 | + // Panasonic has an Address |
| 27 | + if (results->decode_type == PANASONIC) { |
| 28 | + Serial.print(results->address, HEX); |
| 29 | + Serial.print(":"); |
| 30 | + } |
| 31 | + |
| 32 | + // Print Code |
| 33 | + Serial.print(results->value, HEX); |
| 34 | +} |
| 35 | + |
| 36 | +//+============================================================================= |
| 37 | +// Display encoding type |
| 38 | +// |
| 39 | +void encoding (decode_results *results) |
| 40 | +{ |
| 41 | + switch (results->decode_type) { |
| 42 | + default: |
| 43 | + case UNKNOWN: Serial.print("UNKNOWN"); break ; |
| 44 | + case NEC: Serial.print("NEC"); break ; |
| 45 | + case SONY: Serial.print("SONY"); break ; |
| 46 | + case RC5: Serial.print("RC5"); break ; |
| 47 | + case RC6: Serial.print("RC6"); break ; |
| 48 | + case DISH: Serial.print("DISH"); break ; |
| 49 | + case SHARP: Serial.print("SHARP"); break ; |
| 50 | + case JVC: Serial.print("JVC"); break ; |
| 51 | + case SANYO: Serial.print("SANYO"); break ; |
| 52 | + case MITSUBISHI: Serial.print("MITSUBISHI"); break ; |
| 53 | + case SAMSUNG: Serial.print("SAMSUNG"); break ; |
| 54 | + case LG: Serial.print("LG"); break ; |
| 55 | + case WHYNTER: Serial.print("WHYNTER"); break ; |
| 56 | + case AIWA_RC_T501: Serial.print("AIWA_RC_T501"); break ; |
| 57 | + case PANASONIC: Serial.print("PANASONIC"); break ; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +//+============================================================================= |
| 62 | +// Dump out the decode_results structure. |
| 63 | +// |
| 64 | +void dumpInfo (decode_results *results) |
| 65 | +{ |
| 66 | + // Check if the buffer overflowed |
| 67 | + if (results->overflow) { |
| 68 | + Serial.println("IR code too long. Edit IRremoteInt.h and increase RAWLEN"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Show Encoding standard |
| 73 | + Serial.print("Encoding : "); |
| 74 | + encoding(results); |
| 75 | + Serial.println(""); |
| 76 | + |
| 77 | + // Show Code & length |
| 78 | + Serial.print("Code : "); |
| 79 | + ircode(results); |
| 80 | + Serial.print(" ("); |
| 81 | + Serial.print(results->bits, DEC); |
| 82 | + Serial.println(" bits)"); |
| 83 | +} |
| 84 | + |
| 85 | +//+============================================================================= |
| 86 | +// Dump out the decode_results structure. |
| 87 | +// |
| 88 | +void dumpRaw (decode_results *results) |
| 89 | +{ |
| 90 | + // Print Raw data |
| 91 | + Serial.print("Timing["); |
| 92 | + Serial.print(results->rawlen, DEC); |
| 93 | + Serial.println("]: "); |
| 94 | + Serial.print(" -"); |
| 95 | + Serial.println(results->rawbuf[0] * USECPERTICK, DEC); |
| 96 | + for (int i = 1; i < results->rawlen; i++) { |
| 97 | + int x = results->rawbuf[i] * USECPERTICK; |
| 98 | + if (!(i & 1)) { // even |
| 99 | + Serial.print("-"); |
| 100 | + if (x < 1000) Serial.print(" ") ; |
| 101 | + if (x < 100) Serial.print(" ") ; |
| 102 | + Serial.print(x, DEC); |
| 103 | + } else { // odd |
| 104 | + Serial.print(" "); |
| 105 | + Serial.print("+"); |
| 106 | + if (x < 1000) Serial.print(" ") ; |
| 107 | + if (x < 100) Serial.print(" ") ; |
| 108 | + Serial.print(x, DEC); |
| 109 | + Serial.print(", "); |
| 110 | + } |
| 111 | + if (!(i%8)) Serial.println(""); |
| 112 | + } |
| 113 | + Serial.println(""); // Newline |
| 114 | +} |
| 115 | + |
| 116 | +//+============================================================================= |
| 117 | +// Dump out the decode_results structure. |
| 118 | +// |
| 119 | +void dumpCode (decode_results *results) |
| 120 | +{ |
| 121 | + // Start declaration |
| 122 | + Serial.print("unsigned int "); // variable type |
| 123 | + Serial.print("rawData["); // array name |
| 124 | + Serial.print(results->rawlen + 1, DEC); // array size |
| 125 | + Serial.print("] = {"); // Start declaration |
| 126 | + |
| 127 | + // Dump data |
| 128 | + for (int i = 0; i < results->rawlen; i++) { |
| 129 | + Serial.print(results->rawbuf[i], DEC); |
| 130 | + Serial.print(","); |
| 131 | + if (!(i&1)) Serial.print(" "); |
| 132 | + } |
| 133 | + |
| 134 | + // End declaration |
| 135 | + Serial.print("0};"); // Turn LED off at the end |
| 136 | + |
| 137 | + // Comment |
| 138 | + Serial.print(" // "); |
| 139 | + encoding(results); |
| 140 | + Serial.print(" "); |
| 141 | + ircode(results); |
| 142 | + |
| 143 | + // Newline |
| 144 | + Serial.println(""); |
| 145 | +} |
| 146 | + |
| 147 | +//+============================================================================= |
| 148 | +// The repeating section of the code |
| 149 | +// |
| 150 | +void loop ( ) |
| 151 | +{ |
| 152 | + decode_results results; // Somewhere to store the results |
| 153 | + |
| 154 | + if (irrecv.decode(&results)) { // Grab an IR code |
| 155 | + dumpInfo(&results); // Output the results |
| 156 | + dumpRaw(&results); // Output the results in RAW format |
| 157 | + dumpCode(&results); // Output the results as source code |
| 158 | + Serial.println(""); // Blank line between entries |
| 159 | + irrecv.resume(); // Prepare for the next value |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments