1// VirtualWire.h 2// 3// Virtual Wire implementation for Arduino 4// See the README file in this directory fdor 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.6 2013/02/14 22:02:11 mikem Exp mikem $ 9 10/// \mainpage VirtualWire library for Arduino 11/// 12/// This is the Arduino VirtualWire library. 13/// 14/// VirtualWire is an Arduino library 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. 20/// 21/// It is intended to be compatible with the RF Monolithics (www.rfm.com) 22/// Virtual Wire protocol, but this has not been tested. 23/// 24/// Does not use the Arduino UART. Messages are sent with a training preamble, 25/// message length and checksum. Messages are sent with 4-to-6 bit encoding 26/// for good DC balance, and a CRC checksum for message integrity. 27/// 28/// Why not just use the Arduino UART connected directly to the 29/// transmitter/receiver? As discussed in the RFM documentation, ASK receivers 30/// require a burst of training pulses to synchronize the transmitter and 31/// receiver, and also requires good balance between 0s and 1s in the message 32/// stream in order to maintain the DC balance of the message. UARTs do not 33/// provide these. They work a bit with ASK wireless, but not as well as this 34/// code. 35/// 36/// This library provides classes for 37/// - VirtualWire: unaddressed, unreliable messages 38/// 39/// Example Arduino programs are included to show the main modes of use. 40/// 41/// The version of the package that this documentation refers to can be downloaded 42/// from http://www.airspayce.com/mikem/arduino/VirtualWire/VirtualWire-1.15.zip 43/// You can find the latest version at http://www.airspayce.com/mikem/arduino/VirtualWire 44/// 45/// You can also find online help and disussion at http://groups.google.com/group/virtualwire 46/// Please use that group for all questions and discussions on this topic. 47/// Do not contact the author directly, unless it is to discuss commercial licensing. 48/// 49/// \par Supported Hardware 50/// A range of communications hardware is supported. The ones listed blow are 51/// available in common retail outlets in Australian and other countries for 52/// under $10 per unit. Many other modules may also work with this software. 53/// Runs on ATmega8/168 (Arduino Diecimila, Uno etc) and ATmega328 and possibly 54/// others. Also runs on on Energia with MSP430G2553 / G2452 and Arduino with 55/// ATMega328 (courtesy Yannick DEVOS - XV4Y). 56/// Also compiles and runs on ATtiny85 in Arduino environment, courtesy r4z0r7o3. 57/// 58/// - Receivers 59/// - RX-B1 (433.92MHz) (also known as ST-RX04-ASK) 60/// - Transmitters: 61/// - TX-C1 (433.92MHz) 62/// - Transceivers 63/// - DR3100 (433.92MHz) 64/// 65/// \par Installation 66/// To install, unzip the library into the libraries sub-directory of your 67/// Arduino application directory. Then launch the Arduino environment; you 68/// should see the library in the Sketch->Import Library menu, and example 69/// code in 70/// File->Sketchbook->Examples->VirtualWire menu. 71/// 72/// \par Open Source Licensing GPL V2 73/// 74/// This is the appropriate option if you want to share the source code of your 75/// application with everyone you distribute it to, and you also want to give them 76/// the right to share who uses it. If you wish to use this software under Open 77/// Source Licensing, you must contribute all your source code to the open source 78/// community in accordance with the GPL Version 2 when your application is 79/// distributed. See http://www.gnu.org/copyleft/gpl.html 80/// 81/// \par Commercial Licensing 82/// 83/// This is the appropriate option if you are creating proprietary applications 84/// and you are not prepared to distribute and share the source code of your 85/// application. Contact info@airspayce.com for details. 86/// 87/// \par Revision History 88/// \version 1.0 Original release 89/// 90/// \version 1.1 2008-06-24 91/// Now can compile for atmega8 92/// Reported by creatrope 93/// \version 1.2 2009-03-30 94/// Fixed a problem that prevented compiling with arduino-0015 95/// Reported by Jaime Castro 96/// \version 1.3 2009-04-01 97/// Fixed a compatibility problem with ATMEGA328 of the new arduino 98/// Now use SIGNAL(TIMER1_COMPA_vect) instead of ISR(SIG_OUTPUT_COMPARE1A) 99/// as discussed in 100/// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237714550/11 101/// and reported by Jaime Castro. 102/// \version 1.4 2010-01-29 103/// Added vx_tx_active(), suggested by Alan Burlison. 104/// \version 1.5 2011-09-09 105/// Added vx_tx_active() function. 106/// \version 1.6 2012-01-10 107/// Fixed a problem where the receiver was always reenabled after 108/// transmission. Reported by David Bath 109/// \version 1.9 2012-02-07 Documentation updates 110/// Documentation updates 111/// \version 1.10 Updated CHANGES file with changes since 1.4. 112/// \version 1.11 Converted documentation to Doxygen. Moved CHANGES log to this version history. 113/// Ensure vw_rx_pin is not accessed unless receiver is enabled 114/// \version 1.12 Compiles and runs on on Energia with MSP430G2553 / G2452 and Arduino with ATMega328. 115/// Patches contributed by Yannick DEVOS - XV4Y 116/// \version 1.13 util/crc16.h needed for compiling on Energia with MSP430G2553 / G2452 was accidentally 117/// left out of the distribution 118/// \version 1.14 Added support ATtiny85 on Arduino, patch provided by r4z0r7o3. 119/// \version 1.15 Updated author and distribution location details to airspayce.com 120/// 121/// \par Implementation Details 122/// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf 123/// 124/// \par Performance 125/// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf 126/// 127/// \par Connections 128/// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf 129/// 130/// \file VirtualWire.h 131/// \brief VirtualWire API 132/// 133/// To use the VirtualWire library, you must have 134/// \code 135/// #include <VirtualWire.h> 136/// \endcode 137/// At the top of your sketch. 138/// 139 140#ifndef VirtualWire_h 141#define VirtualWire_h 142 143#include <stdlib.h> 144#if defined(ARDUINO) 145#if ARDUINO >= 100 146#include <Arduino.h> 147#else 148#include <wiring.h> 149#endif 150#elif defined(__MSP430G2452__) || defined(__MSP430G2553__)// LaunchPad specific 151#include"legacymsp430.h" 152#include"Energia.h" 153#else// error 154#error Platform not defined 155#endif 156 157// These defs cause trouble on some versions of Arduino 158#undef abs 159#undef double 160#undef round 161 162/// Maximum number of bytes in a message, counting the byte count and FCS 163#define VW_MAX_MESSAGE_LEN 30 164 165/// The maximum payload length 166#define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3 167 168/// The size of the receiver ramp. Ramp wraps modulu this number 169#define VW_RX_RAMP_LEN 160 170 171/// Number of samples per bit 172#define VW_RX_SAMPLES_PER_BIT 8 173 174// Ramp adjustment parameters 175// Standard is if a transition occurs before VW_RAMP_TRANSITION (80) in the ramp, 176// the ramp is retarded by adding VW_RAMP_INC_RETARD (11) 177// else by adding VW_RAMP_INC_ADVANCE (29) 178// If there is no transition it is adjusted by VW_RAMP_INC (20) 179/// Internal ramp adjustment parameter 180#define VW_RAMP_INC (VW_RX_RAMP_LEN/VW_RX_SAMPLES_PER_BIT) 181/// Internal ramp adjustment parameter 182#define VW_RAMP_TRANSITION VW_RX_RAMP_LEN/2 183/// Internal ramp adjustment parameter 184#define VW_RAMP_ADJUST 9 185/// Internal ramp adjustment parameter 186#define VW_RAMP_INC_RETARD (VW_RAMP_INC-VW_RAMP_ADJUST) 187/// Internal ramp adjustment parameter 188#define VW_RAMP_INC_ADVANCE (VW_RAMP_INC+VW_RAMP_ADJUST) 189 190/// Outgoing message bits grouped as 6-bit words 191/// 36 alternating 1/0 bits, followed by 12 bits of start symbol 192/// Followed immediately by the 4-6 bit encoded byte count, 193/// message buffer and 2 byte FCS 194/// Each byte from the byte count on is translated into 2x6-bit words 195/// Caution, each symbol is transmitted LSBit first, 196/// but each byte is transmitted high nybble first 197#define VW_HEADER_LEN 8 198 199// Cant really do this as a real C++ class, since we need to have 200// an ISR 201extern"C" 202{ 203/// Set the digital IO pin to be for transmit data. 204/// This pin will only be accessed if 205/// the transmitter is enabled 206/// \param[in] pin The Arduino pin number for transmitting data. Defaults to 12. 207externvoidvw_set_tx_pin(uint8_t pin); 208 209/// Set the digital IO pin to be for receive data. 210/// This pin will only be accessed if 211/// the receiver is enabled 212/// \param[in] pin The Arduino pin number for receiving data. Defaults to 11. 213externvoidvw_set_rx_pin(uint8_t pin); 214 215// Set the digital IO pin to enable the transmitter (press to talk, PTT)' 216/// This pin will only be accessed if 217/// the transmitter is enabled 218/// \param[in] pin The Arduino pin number to enable the transmitter. Defaults to 10. 219externvoidvw_set_ptt_pin(uint8_t pin); 220 221/// By default the PTT pin goes high when the transmitter is enabled. 222/// This flag forces it low when the transmitter is enabled. 223/// \param[in] inverted True to invert PTT 224externvoidvw_set_ptt_inverted(uint8_t inverted); 225 226/// Initialise the VirtualWire software, to operate at speed bits per second 227/// Call this one in your setup() after any vw_set_* calls 228/// Must call vw_rx_start() before you will get any messages 229/// \param[in] speed Desired speed in bits per second 230externvoidvw_setup(uint16_t speed); 231 232/// Start the Phase Locked Loop listening to the receiver 233/// Must do this before you can receive any messages 234/// When a message is available (good checksum or not), vw_have_message(); 235/// will return true. 236externvoidvw_rx_start(); 237 238/// Stop the Phase Locked Loop listening to the receiver 239/// No messages will be received until vw_rx_start() is called again 240/// Saves interrupt processing cycles 241externvoidvw_rx_stop(); 242 243/// Returns the state of the 244/// transmitter 245/// \return true if the transmitter is active else false 246externuint8_tvx_tx_active(); 247 248/// Block until the transmitter is idle 249/// then returns 250externvoidvw_wait_tx(); 251 252/// Block until a message is available 253/// then returns 254externvoidvw_wait_rx(); 255 256/// Block until a message is available or for a max time 257/// \param[in] milliseconds Maximum time to wait in milliseconds. 258/// \return true if a message is available, false if the wait timed out. 259externuint8_tvw_wait_rx_max(unsigned long milliseconds); 260 261/// Send a message with the given length. Returns almost immediately, 262/// and message will be sent at the right timing by interrupts 263/// \param[in] buf Pointer to the data to transmit 264/// \param[in] len Number of octetes to transmit 265/// \return true if the message was accepted for transmission, false if the message is too long (>VW_MAX_MESSAGE_LEN - 3) 266externuint8_tvw_send(uint8_t* buf,uint8_t len); 267 268// Returns true if an unread message is available 269/// \return true if a message is available to read 270externuint8_tvw_have_message(); 271 272// If a message is available (good checksum or not), copies 273// up to *len octets to buf. 274/// \param[in] buf Pointer to location to save the read data (must be at least *len bytes. 275/// \param[in,out] len Available space in buf. Will be set to the actual number of octets read 276/// \return true if there was a message and the checksum was good 277externuint8_tvw_get_message(uint8_t* buf,uint8_t* len); 278} 279 280/// @example client.pde 281/// Client side of simple client/server pair using VirtualWire 282 283/// @example server.pde 284/// Server side of simple client/server pair using VirtualWire 285 286/// @example transmitter.pde 287/// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire 288 289/// @example receiver.pde 290/// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire 291 292#endif