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