IRrc
IRrc.h
1 // IRrc.h
2 //
3 // IRrc class for infra-red control
4 //
5 // Copyright (C) 2010 Mike McCauley
6 // $Id: IRrc.h,v 1.1 2010/07/18 21:34:33 mikem Exp mikem $
7 
8 ///\mainpage IRrc library for Arduino
9 ///
10 /// IRrc provides classes for controlling a range of infra-red controlled devices such as 3 channel
11 /// IR model helicopters and others. Such models all use a similar IR encoding scheme which is
12 /// implemented in the IRrc class. Subclasses such as IRheli provide specific implementations and
13 /// timings for a specific type of model or other device.
14 ///
15 /// Videos explaining how it works and what you can do with it can be found at:
16 /// \li http://www.youtube.com/watch?v=CHZbIvcC5I0
17 /// \li http://www.youtube.com/watch?v=lzRpyqnD6_M
18 ///
19 /// The version of the package that this documentation refers to can be downloaded
20 /// from http://www.airspayce.com/mikem/arduino/IRrc/IRrc-1.4.zip
21 /// You can find the latest version at http://www.airspayce.com/mikem/arduino/IRrc
22 ///
23 /// Tested on Arduino Duemilanove, Diecimila, Mega and Asynclabs Yellowjacket
24 /// with arduino-0018 on OpenSuSE 11.1
25 /// and avr-libc-1.6.2-5.11,
26 /// cross-avr-binutils-2.19-9.1 and cross-avr-gcc43-4.3.3_20081022-9.3.
27 ///
28 /// IRrc can also interoperate with the RCKit libbary at http://www.airspayce.com/mikem/arduino/RCKit to
29 /// create infra-red models that can be controlled from your iPhone with the RCTx app from the Apple App Store
30 /// at http://itunes.apple.com/app/rctx/id377833472?mt=8
31 ///
32 /// The Infra-Red protocol is a 38kHz carrier, modulated with a preamble, a variable number of octets
33 /// followed by a postamble. Timings are specific for a particular model.
34 ///
35 /// The IRheli class provides a specific implementation for a Chinese 3 channel co-axial model helicopter.
36 ///
37 /// \par Basic usage
38 ///
39 /// You must instantiate a subclass of the IRrc class, such as the IRheli class. By default it will control
40 /// an infra-red transmitter connected to Arduino digital output pin 3 (pin 9 on Arduino Mega).
41 /// You then call sendMessage()
42 /// (or a derivative) to send messages to the controlled device. IRrc will then modulate the infra-red
43 /// tranmitter on the output pin according to the timings defined by the subclass.
44 ///
45 /// The carrier output is from PWM timer 2, with 50% duty cycle. It is turned on and off according to the
46 /// bit timings and the bits in the message to be transmitted.
47 ///
48 /// \par Example Sketches
49 ///
50 /// 2 Example Arduino sketches are provided in the examples directory of the distribution
51 /// to demonstrate the use of these classes:
52 ///
53 /// \li HeliDemo
54 /// Receives simple commands on the Serial port and turns them into Infr-red commands for a
55 /// coaxial helicopter using the IRheli class. A corresponding program js.pl, a Perl program for Linux,
56 /// reads a Logitech or similar
57 /// joystick and sends complying commands on a Serial port. This allows a
58 /// coaxial helicopter to be controlled using a Logitech joystick connected to a Linux computer.
59 /// js.pl can be found in the perl directory of the IRrc distribution.
60 ///
61 /// \li HeliRCRx
62 /// Implements a RCRx Wi-Fi receiver, that receives RCOIP commands from an RCOIP compliant
63 /// controller such as the iPhone RCTx transmitter and controls a
64 /// coaxial helicopter using the IRheli class. Requires the RCRx library available from
65 /// http://www/.airspayce.com/mikem/arduino/RCRx and the RCTx iPhone app from
66 /// http://itunes.apple.com/app/rctx/id377833472?mt=8
67 /// This is the sketch that was used in the video http://www.youtube.com/watch?v=lzRpyqnD6_M
68 ///
69 /// \li USBJoystickHeli
70 /// Controls a helicopter with a Logitech USB game pad connected to a Sparkfun USB Host shield.
71 /// Requires the USB_Host_Shield library. The game pad is
72 /// used as a mode 2 controller: throttle on left stick, elevator and rudder on right stick.
73 /// Includes elevator and rudder trim buttons with button 1, 2, 3, 4.
74 /// This is the sketch that was used in the video http://www.youtube.com/watch?v=CHZbIvcC5I0
75 ///
76 /// \par Electrical
77 ///
78 /// You can get a short range IR output by connecting an IR LED directly to an Arduino output pin and ground.
79 /// This will provide about 25mA of drive for the LED, which is about 1/40th of what a commonly
80 /// available IR LED is capable of, but will work for short ranges.
81 ///
82 /// A better alternative for greater range is to use a transistor driver to drive a higher current
83 /// through several IR LEDs. See <a href="LED-Output.pdf">LED-Output.pdf</a> for
84 /// sample circuit diagrams for LED connections.
85 ///
86 /// \par Installation
87 ///
88 /// Install in the usual way: unzip the distribution zip file to the libraries
89 /// sub-folder of your sketchbook.
90 ///
91 /// \author Mike McCauley (mikem@airspayce.com)
92 ///
93 /// This software and is Copyright (C) 2010 Mike McCauley. Use is subject to license
94 /// conditions. The main licensing options available are GPL V2 or Commercial:
95 ///
96 /// \par Open Source Licensing GPL V2
97 /// This is the appropriate option if you want to share the source code of your
98 /// application with everyone you distribute it to, and you also want to give them
99 /// the right to share who uses it. If you wish to use this software under Open
100 /// Source Licensing, you must contribute all your source code to the open source
101 /// community in accordance with the GPL Version 2 when your application is
102 /// distributed. See http://www.gnu.org/copyleft/gpl.html
103 ///
104 /// \par Commercial Licensing
105 /// This is the appropriate option if you are creating proprietary applications
106 /// and you are not prepared to distribute and share the source code of your
107 /// application. Contact info@airspayce.com for details.
108 ///
109 /// \par Revision History
110 /// \version 1.0 Initial release
111 /// \version 1.1 Added videos and USBJoystickHeli example
112 /// \version 1.2 Compiles under Ardiono 1.0
113 /// \version 1.3 Updated HeliRCRx example for latest version of RCKit.
114 /// \version 1.4 Updated author and distribution location details to airspayce.com
115 
116 #ifndef IRrc_h
117 #define IRrc_h
118 #include <inttypes.h>
119 
120 #define SYSCLOCK 16000000 // main Arduino clock
121 
122 /////////////////////////////////////////////////////////////////////
123 /// \class IRrc IRrc.h <IRrc.h>
124 /// \brief Low level infa-red output modulator, compatible with a number of infra-red controlled toys
125 /// This is an abstract class that must be subclassed.
126 /// Subclasses customise timings etc for particular toys and protocols.
127 ///
128 class IRrc
129 {
130 public:
131  /// Constructor.
132  IRrc();
133 
134  /// Sends the message pointed to by msg, with length len octets. Message is precended by the preamble
135  /// with sendPreamble() and followed by the postamble with sendPostamble().
136  /// Each bit in the message is sent with the carrier enabled for the on time and disabled for the off time.
137  /// Each octet is sent most significant bit first.
138  /// Caution: this call blocks until the postamble has been sent
139  /// \param[in] msg Pointer to the octets to be sent
140  /// \param[in] len Message length in octets
141  void sendMessage(const uint8_t* msg, uint8_t len);
142 
143  /// Sends the preamble, which consists of the carrier on for the _preamble time, followed by carrier off
144  /// for the _preambleGap time.
145  void sendPreamble();
146 
147  /// Send the postamble which consists by default of a single One bit sent with sendOne()
148  void sendPostamble();
149 
150  /// Encodes and sends a Zero bit, with the carrier on for _zeroOnTime microseconds, followed by
151  /// off for _zeroOffTime microseconds.
152  void sendZero();
153 
154  /// Encodes and sends a One bit, with the carrier on for _oneOnTime microseconds, followed by
155  /// off for _oneOffTime microseconds.
156  void sendOne();
157 
158  /// Enables IR output. The _carrierFrequency value controls the carrier frequency in Hertz.
159  /// The IR output will be on pin 3 (OC2B) on most platforms, pin 9 on Arduino Mega.
160  void enableCarrier();
161 
162  /// Disables the IR output, if necessary.
163  /// The default implmentation does nothing.
164  void disableCarrier();
165 
166  /// Turns the carrier on for the specified time.
167  /// Blocks until complete.
168  /// \param[in] time Time on in microseconds
169  void carrierOn(uint16_t time);
170 
171  /// Turns the carrier off for the specified time.
172  /// Blocks until complete.
173  /// \param[in] time
174  void carrierOff(uint16_t time);
175 
176 protected:
177  // Subclasses have to set these in their constructor
178 
179  /// IR Carrier frequence in Hertz
181 
182  /// Length of the preamble carrier on time in microseconds
183  uint16_t _preamble;
184 
185  /// Length of the preamble gap off time in microseconds
186  uint16_t _preambleGap;
187 
188  /// Time that the carrier is on at the start of a Zero bit in microseconds
189  uint16_t _zeroOnTime;
190 
191  /// Time that the carrier is off at the end of a Zero bit in microseconds
192  uint16_t _zeroOffTime;
193 
194  /// Time that the carrier is on at the start of a One bit in microseconds
195  uint16_t _oneOnTime;
196 
197  /// Time that the carrier is off at the end of a One bit in microseconds
198  uint16_t _oneOffTime;
199 
200 private:
201 };
202 
203 #endif