turn off display before updating rows
[Arduino] / libraries / VirtualWire / VirtualWire.h
1 // VirtualWire.h
2 //
3 // Virtual Wire implementation for Arduino and other boards
4 // See the README file in this directory for documentation
5 // 
6 // Author: Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS
7 // Copyright (C) 2008 Mike McCauley
8 // $Id: VirtualWire.h,v 1.10 2013/08/06 23:43:41 mikem Exp mikem $
9
10 /// \mainpage VirtualWire library for Arduino and other boards
11 ///
12 /// This is the VirtualWire library.
13 ///
14 /// VirtualWire is an library for Arduino, Maple and others that provides features to send short
15 /// messages, without addressing, retransmit or acknowledgment, a bit like UDP
16 /// over wireless, using ASK (amplitude shift keying). Supports a number of
17 /// inexpensive radio transmitters and receivers. All that is required is
18 /// transmit data, receive data and (for transmitters, optionally) a PTT
19 /// transmitter enable. Can also be used over various analog connections (not just a data radio), 
20 /// such as the audio channel of an A/V sender
21 ///
22 /// It is intended to be compatible with the RF Monolithics (www.rfm.com)
23 /// Virtual Wire protocol, but this has not been tested.
24 ///
25 /// Does not use the Arduino UART. Messages are sent with a training preamble,
26 /// message length and checksum. Messages are sent with 4-to-6 bit encoding
27 /// for good DC balance, and a CRC checksum for message integrity.
28 ///
29 /// Why not just use a UART connected directly to the
30 /// transmitter/receiver? As discussed in the RFM documentation, ASK receivers
31 /// require a burst of training pulses to synchronize the transmitter and
32 /// receiver, and also requires good balance between 0s and 1s in the message
33 /// stream in order to maintain the DC balance of the message. UARTs do not
34 /// provide these. They work a bit with ASK wireless, but not as well as this
35 /// code.
36 ///
37 /// This library provides classes for 
38 /// - VirtualWire: unaddressed, unreliable messages
39 ///
40 /// Example Arduino programs are included to show the main modes of use.
41 ///
42 /// The version of the package that this documentation refers to can be downloaded 
43 /// from http://www.airspayce.com/mikem/arduino/VirtualWire/VirtualWire-1.19.zip
44 /// You can find the latest version at http://www.airspayce.com/mikem/arduino/VirtualWire
45 ///
46 /// You can also find online help and disussion at http://groups.google.com/group/virtualwire
47 /// Please use that group for all questions and discussions on this topic. 
48 /// Do not contact the author directly, unless it is to discuss commercial licensing.
49 ///
50 /// \par Supported Hardware
51 /// A range of communications hardware is supported. The ones listed below are
52 /// available in common retail outlets in Australian and other countries for
53 /// under $10 per unit. Many other modules may also work with this software.
54 /// Runs on ATmega8/168 (Arduino Diecimila, Uno etc) and ATmega328 and possibly
55 /// others. Also runs on on Energia with MSP430G2553 / G2452 and Arduino with 
56 /// ATMega328 (courtesy Yannick DEVOS - XV4Y), but untested by us.
57 /// It also runs on Teensy 3.0 (courtesy of Paul Stoffregen), but untested by us.
58 /// Also compiles and runs on ATtiny85 in Arduino environment, courtesy r4z0r7o3.
59 /// Also compiles on maple-ide-v0.0.12, and runs on Maple, flymaple 1.1 etc.
60 ///
61 /// - Receivers
62 ///  - RX-B1 (433.92MHz) (also known as ST-RX04-ASK)
63 /// - Transmitters: 
64 ///  - TX-C1 (433.92MHz)
65 /// - Transceivers
66 ///  - DR3100 (433.92MHz)
67 ///
68 /// For testing purposes you can connect 2 VirtualWire instances directly, by
69 /// connecting pin 12 of one to 11 of the other and vice versa, like this for a duplex connection:
70 ///
71 /// <code>
72 /// Arduino 1         wires         Arduino 1
73 ///  D11-----------------------------D12
74 ///  D12-----------------------------D11
75 ///  GND-----------------------------GND
76 /// </code>
77 ///
78 /// You can also connect 2 VirtualWire instances over a suitable analog
79 /// transmitter/receiver, such as the audio channel of an A/V transmitter/receiver. You may need
80 /// buffers at each end of the connection to convert the 0-5V digital output to a suitable analog voltage.
81 ///
82 /// \par Installation
83 /// To install, unzip the library into the libraries sub-directory of your
84 /// Arduino application directory. Then launch the Arduino environment; you
85 /// should see the library in the Sketch->Import Library menu, and example
86 /// code in
87 /// File->Sketchbook->Examples->VirtualWire menu.
88 ///
89 /// \par Open Source Licensing GPL V2
90 ///
91 /// This is the appropriate option if you want to share the source code of your
92 /// application with everyone you distribute it to, and you also want to give them
93 /// the right to share who uses it. If you wish to use this software under Open
94 /// Source Licensing, you must contribute all your source code to the open source
95 /// community in accordance with the GPL Version 2 when your application is
96 /// distributed. See http://www.gnu.org/copyleft/gpl.html
97 /// 
98 /// \par Commercial Licensing
99 ///
100 /// This is the appropriate option if you are creating proprietary applications
101 /// and you are not prepared to distribute and share the source code of your
102 /// application. Contact info@airspayce.com for details.
103 ///
104 /// \par Revision History
105 /// \version 1.0 Original release
106 /// 
107 /// \version 1.1 2008-06-24
108 ///     Now can compile for atmega8
109 ///     Reported by creatrope
110 /// \version 1.2 2009-03-30
111 ///     Fixed a problem that prevented compiling with arduino-0015
112 ///     Reported by Jaime Castro
113 /// \version 1.3 2009-04-01
114 ///     Fixed a compatibility problem with ATMEGA328 of the new arduino
115 ///     Now use SIGNAL(TIMER1_COMPA_vect) instead of ISR(SIG_OUTPUT_COMPARE1A)
116 ///     as discussed in
117 ///     http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237714550/11
118 ///     and reported by Jaime Castro.
119 /// \version 1.4 2010-01-29
120 ///     Added vx_tx_active(), suggested by Alan Burlison.
121 /// \version 1.5 2011-09-09
122 ///     Added vx_tx_active() function.
123 /// \version 1.6 2012-01-10
124 ///     Fixed a problem where the receiver was always reenabled after
125 ///     transmission. Reported by David Bath
126 /// \version 1.9 2012-02-07 Documentation updates
127 ///     Documentation updates
128 /// \version 1.10 Updated CHANGES file with changes since 1.4.
129 /// \version 1.11 Converted documentation to Doxygen. Moved CHANGES log to this version history.
130 ///     Ensure vw_rx_pin is not accessed unless receiver is enabled
131 /// \version 1.12 Compiles and runs on on Energia with MSP430G2553 / G2452 and Arduino with ATMega328. 
132 ///     Patches contributed by Yannick DEVOS - XV4Y
133 /// \version 1.13 util/crc16.h needed for compiling on  Energia with MSP430G2553 / G2452 was accidentally
134 ///     left out of the distribution
135 /// \version 1.14 Added support ATtiny85 on Arduino, patch provided by r4z0r7o3.
136 /// \version 1.15 Updated author and distribution location details to airspayce.com
137 /// \version 1.16 Added support for Teensy 3.0, contributed by Paul Stoffregen.
138 /// \version 1.17 Increase default MAX_MESSAGE_LENGTH to 80. Added vw_get_rx_good() and vw_get_rx_bad()
139 ///               functions.
140 /// \version 1.18 Added support for Maple, Flymaple etc with STM32F103RE processor using timer 1.
141 ///               Tested with Flymaple 1.1 and maple-ide-v0.0.12
142 /// \version 1.19 Added new function vw_rx_inverted(), to allow the incoming RX to be inverted (normally high).
143 ///               Minor improvements to timer setup for Maple. Name vw_tx_active() changed from incorrect
144 ///               vx_tx_active()
145 ///
146 /// \par Implementation Details
147 /// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
148 ///
149 /// \par Performance
150 /// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
151 ///
152 /// \par Connections
153 /// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
154 ///
155 /// \file VirtualWire.h
156 /// \brief VirtualWire API
157 ///
158 /// To use the VirtualWire library, you must have
159 /// \code
160 /// #include <VirtualWire.h>
161 /// \endcode
162 /// At the top of your sketch.
163 /// 
164
165 #ifndef VirtualWire_h
166 #define VirtualWire_h
167
168 #include <stdlib.h>
169 #if defined(ARDUINO)
170  #if ARDUINO >= 100
171   #include <Arduino.h>
172  #else
173   #include <wiring.h>
174  #endif
175 #elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific
176  #include "legacymsp430.h"
177  #include "Energia.h"
178 #elif defined(MCU_STM32F103RE) // Maple etc
179  #include <wirish.h>
180  #include <string.h>
181  #include <stdint.h>
182 // Defines which timer to use on Maple
183 #define MAPLE_TIMER 1
184 #else // error
185  #error Platform not defined
186 #endif
187
188 // These defs cause trouble on some versions of Arduino
189 #undef abs
190 #undef double
191 #undef round
192
193 /// Maximum number of bytes in a message, counting the byte count and FCS
194 #define VW_MAX_MESSAGE_LEN 80
195
196 /// The maximum payload length
197 #define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3
198
199 /// The size of the receiver ramp. Ramp wraps modulu this number
200 #define VW_RX_RAMP_LEN 160
201
202 /// Number of samples per bit
203 #define VW_RX_SAMPLES_PER_BIT 8
204
205 // Ramp adjustment parameters
206 // Standard is if a transition occurs before VW_RAMP_TRANSITION (80) in the ramp,
207 // the ramp is retarded by adding VW_RAMP_INC_RETARD (11)
208 // else by adding VW_RAMP_INC_ADVANCE (29)
209 // If there is no transition it is adjusted by VW_RAMP_INC (20)
210 /// Internal ramp adjustment parameter
211 #define VW_RAMP_INC (VW_RX_RAMP_LEN/VW_RX_SAMPLES_PER_BIT)
212 /// Internal ramp adjustment parameter
213 #define VW_RAMP_TRANSITION VW_RX_RAMP_LEN/2
214 /// Internal ramp adjustment parameter
215 #define VW_RAMP_ADJUST 9
216 /// Internal ramp adjustment parameter
217 #define VW_RAMP_INC_RETARD (VW_RAMP_INC-VW_RAMP_ADJUST)
218 /// Internal ramp adjustment parameter
219 #define VW_RAMP_INC_ADVANCE (VW_RAMP_INC+VW_RAMP_ADJUST)
220
221 /// Outgoing message bits grouped as 6-bit words
222 /// 36 alternating 1/0 bits, followed by 12 bits of start symbol
223 /// Followed immediately by the 4-6 bit encoded byte count, 
224 /// message buffer and 2 byte FCS
225 /// Each byte from the byte count on is translated into 2x6-bit words
226 /// Caution, each symbol is transmitted LSBit first, 
227 /// but each byte is transmitted high nybble first
228 #define VW_HEADER_LEN 8
229
230 // Cant really do this as a real C++ class, since we need to have 
231 // an ISR
232 extern "C"
233 {
234     /// Set the digital IO pin to be for transmit data. 
235     /// This pin will only be accessed if
236     /// the transmitter is enabled
237     /// \param[in] pin The Arduino pin number for transmitting data. Defaults to 12.
238     extern void vw_set_tx_pin(uint8_t pin);
239
240     /// Set the digital IO pin to be for receive data.
241     /// This pin will only be accessed if
242     /// the receiver is enabled
243     /// \param[in] pin The Arduino pin number for receiving data. Defaults to 11.
244     extern void vw_set_rx_pin(uint8_t pin);
245
246     /// By default the RX pin is expected to be low when idle, and to pulse high 
247     /// for each data pulse.
248     /// This flag forces it to be inverted. This may be necessary if your transport medium
249     /// inverts the logic of your signal, such as happens with some types of A/V tramsmitter.
250     /// \param[in] inverted True to invert sense of receiver input
251     extern void vw_set_rx_inverted(uint8_t inverted);
252
253     // Set the digital IO pin to enable the transmitter (press to talk, PTT)'
254     /// This pin will only be accessed if
255     /// the transmitter is enabled
256     /// \param[in] pin The Arduino pin number to enable the transmitter. Defaults to 10.
257     extern void vw_set_ptt_pin(uint8_t pin);
258
259     /// By default the PTT pin goes high when the transmitter is enabled.
260     /// This flag forces it low when the transmitter is enabled.
261     /// \param[in] inverted True to invert PTT
262     extern void vw_set_ptt_inverted(uint8_t inverted);
263
264     /// Initialise the VirtualWire software, to operate at speed bits per second
265     /// Call this one in your setup() after any vw_set_* calls
266     /// Must call vw_rx_start() before you will get any messages
267     /// \param[in] speed Desired speed in bits per second
268     extern void vw_setup(uint16_t speed);
269
270     /// Start the Phase Locked Loop listening to the receiver
271     /// Must do this before you can receive any messages
272     /// When a message is available (good checksum or not), vw_have_message();
273     /// will return true.
274     extern void vw_rx_start();
275
276     /// Stop the Phase Locked Loop listening to the receiver
277     /// No messages will be received until vw_rx_start() is called again
278     /// Saves interrupt processing cycles
279     extern void vw_rx_stop();
280
281     /// Returns the state of the
282     /// transmitter
283     /// \return true if the transmitter is active else false
284     extern uint8_t vw_tx_active();
285
286     /// Block until the transmitter is idle
287     /// then returns
288     extern void vw_wait_tx();
289
290     /// Block until a message is available
291     /// then returns
292     extern void vw_wait_rx();
293
294     /// Block until a message is available or for a max time
295     /// \param[in] milliseconds Maximum time to wait in milliseconds.
296     /// \return true if a message is available, false if the wait timed out.
297     extern uint8_t vw_wait_rx_max(unsigned long milliseconds);
298
299     /// Send a message with the given length. Returns almost immediately,
300     /// and message will be sent at the right timing by interrupts
301     /// \param[in] buf Pointer to the data to transmit
302     /// \param[in] len Number of octetes to transmit
303     /// \return true if the message was accepted for transmission, false if the message is too long (>VW_MAX_MESSAGE_LEN - 3)
304     extern uint8_t vw_send(uint8_t* buf, uint8_t len);
305
306     /// Returns true if an unread message is available
307     /// \return true if a message is available to read
308     extern uint8_t vw_have_message();
309
310     /// If a message is available (good checksum or not), copies
311     /// up to *len octets to buf.
312     /// \param[in] buf Pointer to location to save the read data (must be at least *len bytes.
313     /// \param[in,out] len Available space in buf. Will be set to the actual number of octets read
314     /// \return true if there was a message and the checksum was good
315     extern uint8_t vw_get_message(uint8_t* buf, uint8_t* len);
316
317     /// Returns the count of good messages received
318     /// Caution,: this is an 8 bit count and can easily overflow
319     /// \return Count of good messages received
320     extern uint8_t vw_get_rx_good();
321
322     /// Returns the count of bad messages received, ie
323     /// messages with bogus lengths, indicating corruption
324     /// or lost octets.
325     /// Caution,: this is an 8 bit count and can easily overflow
326     /// \return Count of bad messages received
327     extern uint8_t vw_get_rx_bad();
328 }
329
330 /// @example client.pde
331 /// Client side of simple client/server pair using VirtualWire
332
333 /// @example server.pde
334 /// Server side of simple client/server pair using VirtualWire
335
336 /// @example transmitter.pde
337 /// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire
338
339 /// @example receiver.pde
340 /// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire
341
342 #endif