From 5d8fbfdcd2eb3f0140ed54f2ce4b588e8461db96 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Thu, 19 May 2016 16:30:23 +1000 Subject: [PATCH] Added more libraries --- libraries/VirtualWire/CHANGES | 17 +- libraries/VirtualWire/MANIFEST | 16 +- libraries/VirtualWire/VirtualWire.cpp | 312 ++++++++++++++---- libraries/VirtualWire/VirtualWire.h | 265 ++++++++++++--- .../VirtualWire/examples/client/client.pde | 8 +- .../examples/receiver/receiver.pde | 27 +- .../VirtualWire/examples/server/server.pde | 8 +- .../examples/transmitter/transmitter.pde | 38 ++- 8 files changed, 529 insertions(+), 162 deletions(-) diff --git a/libraries/VirtualWire/CHANGES b/libraries/VirtualWire/CHANGES index 3a12f6e..592d0e0 100644 --- a/libraries/VirtualWire/CHANGES +++ b/libraries/VirtualWire/CHANGES @@ -1,16 +1 @@ -1.3 2009-04-01 - Fixed a compatibility problem with ATMEGA328 of the new arduino - Now use SIGNAL(TIMER1_COMPA_vect) instead of ISR(SIG_OUTPUT_COMPARE1A) - as discussed in - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237714550/11 - and reported by Jaime Castro. - -1.2 2009-03-30 - Fixed a problem that prevented compiling with arduino-0015 - Reported by Jaime Castro - -1.1 2008-06-24 - Now can compile for atmega8 - Reported by creatrope - -1.0 Original release +See VirtulWire.h for latest change log diff --git a/libraries/VirtualWire/MANIFEST b/libraries/VirtualWire/MANIFEST index dd9c5ae..805a14e 100644 --- a/libraries/VirtualWire/MANIFEST +++ b/libraries/VirtualWire/MANIFEST @@ -1,12 +1,14 @@ -VirtualWire/MANIFEST -VirtualWire/README +VirtualWire/doc VirtualWire/LICENSE -VirtualWire/CHANGES +VirtualWire/README +VirtualWire/Makefile VirtualWire/VirtualWire.cpp VirtualWire/VirtualWire.h +VirtualWire/CHANGES +VirtualWire/MANIFEST +VirtualWire/keywords.txt +VirtualWire/util/crc16.h VirtualWire/examples/client/client.pde -VirtualWire/examples/server/server.pde -VirtualWire/examples/receiver/receiver.pde VirtualWire/examples/transmitter/transmitter.pde - - +VirtualWire/examples/receiver/receiver.pde +VirtualWire/examples/server/server.pde diff --git a/libraries/VirtualWire/VirtualWire.cpp b/libraries/VirtualWire/VirtualWire.cpp index 7e90873..7c11c95 100644 --- a/libraries/VirtualWire/VirtualWire.cpp +++ b/libraries/VirtualWire/VirtualWire.cpp @@ -2,19 +2,36 @@ // // Virtual Wire implementation for Arduino // See the README file in this directory fdor documentation +// See also +// ASH Transceiver Software Designer's Guide of 2002.08.07 +// http://www.rfm.com/products/apnotes/tr_swg05.pdf // // Changes: -// 2008-05-25: fixed a bug that could prevent messages with certain +// 1.5 2008-05-25: fixed a bug that could prevent messages with certain // bytes sequences being received (false message start detected) +// 1.6 2011-09-10: Patch from David Bath to prevent unconditional reenabling of the receiver +// at end of transmission. // -// Author: Mike McCauley (mikem@open.com.au) +// Author: Mike McCauley (mikem@airspayce.com) // Copyright (C) 2008 Mike McCauley -// $Id: VirtualWire.cpp,v 1.4 2009/03/31 20:49:41 mikem Exp mikem $ +// $Id: VirtualWire.cpp,v 1.9 2013/02/14 22:02:11 mikem Exp mikem $ + + +#if defined(ARDUINO) + #if (ARDUINO < 100) + #include "WProgram.h" + #endif +#elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific + #include "legacymsp430.h" + #include "Energia.h" +#else // error + #error Platform not defined +#endif -#include "WProgram.h" #include "VirtualWire.h" #include + static uint8_t vw_tx_buf[(VW_MAX_MESSAGE_LEN * 2) + VW_HEADER_LEN] = {0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x38, 0x2c}; @@ -96,7 +113,7 @@ static uint8_t vw_rx_good = 0; // 4 bit to 6 bit symbol converter table // Used to convert the high and low nybbles of the transmitted data // into 6 bit symbols for transmission. Each 6-bit symbol has 3 1s and 3 0s -// with at most 2 consecutive identical bits +// with at most 3 consecutive identical bits static uint8_t symbols[] = { 0xd, 0xe, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c, @@ -246,20 +263,139 @@ void vw_pll() } } +// Common function for setting timer ticks @ prescaler values for speed +// Returns prescaler index into {0, 1, 8, 64, 256, 1024} array +// and sets nticks to compare-match value if lower than max_ticks +// returns 0 & nticks = 0 on fault +static uint8_t _timer_calc(uint16_t speed, uint16_t max_ticks, uint16_t *nticks) +{ + // Clock divider (prescaler) values - 0/3333: error flag + uint16_t prescalers[] = {0, 1, 8, 64, 256, 1024, 3333}; + uint8_t prescaler=0; // index into array & return bit value + unsigned long ulticks; // calculate by ntick overflow + + // Div-by-zero protection + if (speed == 0) + { + // signal fault + *nticks = 0; + return 0; + } + + // test increasing prescaler (divisor), decreasing ulticks until no overflow + for (prescaler=1; prescaler < 7; prescaler += 1) + { + // Amount of time per CPU clock tick (in seconds) + float clock_time = (1.0 / (float(F_CPU) / float(prescalers[prescaler]))); + // Fraction of second needed to xmit one bit + float bit_time = ((1.0 / float(speed)) / 8.0); + // number of prescaled ticks needed to handle bit time @ speed + ulticks = long(bit_time / clock_time); + // Test if ulticks fits in nticks bitwidth (with 1-tick safety margin) + if ((ulticks > 1) && (ulticks < max_ticks)) + { + break; // found prescaler + } + // Won't fit, check with next prescaler value + } + + // Check for error + if ((prescaler == 6) || (ulticks < 2) || (ulticks > max_ticks)) + { + // signal fault + *nticks = 0; + return 0; + } + + *nticks = ulticks; + return prescaler; +} + +#if defined(__arm__) && defined(CORE_TEENSY) + // This allows the AVR interrupt code below to be run from an + // IntervalTimer object. It must be above vw_setup(), so the + // the TIMER1_COMPA_vect function name is defined. + #ifdef SIGNAL + #undef SIGNAL + #endif + #define SIGNAL(f) void f(void) + #ifdef TIMER1_COMPA_vect + #undef TIMER1_COMPA_vect + #endif + void TIMER1_COMPA_vect(void); +#endif + + // Speed is in bits per sec RF rate +#if defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific void vw_setup(uint16_t speed) { - // Calculate the OCR1A overflow count based on the required bit speed - // and CPU clock rate - uint16_t ocr1a = (F_CPU / 8UL) / speed; - -#ifndef TEST - // Set up timer1 for a tick every 62.50 microseconds - // for 2000 bits per sec - TCCR1A = 0; - TCCR1B = _BV(WGM12) | _BV(CS10); + // Calculate the counter overflow count based on the required bit speed + // and CPU clock rate + uint16_t ocr1a = (F_CPU / 8UL) / speed; + + // This code is for Energia/MSP430 + TA0CCR0 = ocr1a; // Ticks for 62,5 us + TA0CTL = TASSEL_2 + MC_1; // SMCLK, up mode + TA0CCTL0 |= CCIE; // CCR0 interrupt enabled + + // Set up digital IO pins + pinMode(vw_tx_pin, OUTPUT); + pinMode(vw_rx_pin, INPUT); + pinMode(vw_ptt_pin, OUTPUT); + digitalWrite(vw_ptt_pin, vw_ptt_inverted); +} + +#elif defined (ARDUINO) // Arduino specific +void vw_setup(uint16_t speed) +{ + uint16_t nticks; // number of prescaled ticks needed + uint8_t prescaler; // Bit values for CS0[2:0] + +#ifdef __AVR_ATtiny85__ + // figure out prescaler value and counter match value + prescaler = _timer_calc(speed, (uint8_t)-1, &nticks); + if (!prescaler) + { + return; // fault + } + + TCCR0A = 0; + TCCR0A = _BV(WGM01); // Turn on CTC mode / Output Compare pins disconnected + + // convert prescaler index to TCCRnB prescaler bits CS00, CS01, CS02 + TCCR0B = 0; + TCCR0B = prescaler; // set CS00, CS01, CS02 (other bits not needed) + + // Number of ticks to count before firing interrupt + OCR0A = uint8_t(nticks); + + // Set mask to fire interrupt when OCF0A bit is set in TIFR0 + TIMSK |= _BV(OCIE0A); + +#elif defined(__arm__) && defined(CORE_TEENSY) + // on Teensy 3.0 (32 bit ARM), use an interval timer + IntervalTimer *t = new IntervalTimer(); + t->begin(TIMER1_COMPA_vect, 125000.0 / (float)(speed)); + +#else // ARDUINO + // This is the path for most Arduinos + // figure out prescaler value and counter match value + prescaler = _timer_calc(speed, (uint16_t)-1, &nticks); + if (!prescaler) + { + return; // fault + } + + TCCR1A = 0; // Output Compare pins disconnected + TCCR1B = _BV(WGM12); // Turn on CTC mode + + // convert prescaler index to TCCRnB prescaler bits CS10, CS11, CS12 + TCCR1B |= prescaler; + // Caution: special procedures for setting 16 bit regs - OCR1A = ocr1a; + // is handled by the compiler + OCR1A = nticks; // Enable interrupt #ifdef TIMSK1 // atmega168 @@ -267,9 +403,9 @@ void vw_setup(uint16_t speed) #else // others TIMSK |= _BV(OCIE1A); -#endif +#endif // TIMSK1 -#endif +#endif // __AVR_ATtiny85__ // Set up digital IO pins pinMode(vw_tx_pin, OUTPUT); @@ -278,6 +414,8 @@ void vw_setup(uint16_t speed) digitalWrite(vw_ptt_pin, vw_ptt_inverted); } +#endif // ARDUINO + // Start the transmitter, call when the tx buffer is ready to go and vw_tx_len is // set to the total number of symbols to send void vw_tx_start() @@ -286,9 +424,6 @@ void vw_tx_start() vw_tx_bit = 0; vw_tx_sample = 0; - // Disable the receiver PLL - vw_rx_enabled = false; - // Enable the transmitter hardware digitalWrite(vw_ptt_pin, true ^ vw_ptt_inverted); @@ -305,9 +440,6 @@ void vw_tx_stop() // No more ticks for the transmitter vw_tx_enabled = false; - - // Enable the receiver PLL - vw_rx_enabled = true; } // Enable the receiver. When a message becomes available, vw_rx_done flag @@ -327,6 +459,12 @@ void vw_rx_stop() vw_rx_enabled = false; } +// Return true if the transmitter is active +uint8_t vx_tx_active() +{ + return vw_tx_enabled; +} + // Wait for the transmitter to become available // Busy-wait loop until the ISR says the message has been sent void vw_wait_tx() @@ -405,74 +543,126 @@ uint8_t vw_send(uint8_t* buf, uint8_t len) return true; } +// Return true if there is a message available +uint8_t vw_have_message() +{ + return vw_rx_done; +} + +// Get the last message received (without byte count or FCS) +// Copy at most *len bytes, set *len to the actual number copied +// Return true if there is a message and the FCS is OK +uint8_t vw_get_message(uint8_t* buf, uint8_t* len) +{ + uint8_t rxlen; + + // Message available? + if (!vw_rx_done) + return false; + + // Wait until vw_rx_done is set before reading vw_rx_len + // then remove bytecount and FCS + rxlen = vw_rx_len - 3; + + // Copy message (good or bad) + if (*len > rxlen) + *len = rxlen; + memcpy(buf, vw_rx_buf + 1, *len); + + vw_rx_done = false; // OK, got that message thanks + + // Check the FCS, return goodness + return (vw_crc(vw_rx_buf, vw_rx_len) == 0xf0b8); // FCS OK? +} + // This is the interrupt service routine called when timer1 overflows // Its job is to output the next bit from the transmitter (every 8 calls) // and to call the PLL code if the receiver is enabled //ISR(SIG_OUTPUT_COMPARE1A) +#if defined (ARDUINO) // Arduino specific + +#ifdef __AVR_ATtiny85__ +SIGNAL(TIM0_COMPA_vect) +#else // Assume Arduino Uno (328p or similar) + SIGNAL(TIMER1_COMPA_vect) -{ - vw_rx_sample = digitalRead(vw_rx_pin); +#endif // __AVR_ATtiny85__ +{ + if (vw_rx_enabled && !vw_tx_enabled) + vw_rx_sample = digitalRead(vw_rx_pin); + // Do transmitter stuff first to reduce transmitter bit jitter due // to variable receiver processing if (vw_tx_enabled && vw_tx_sample++ == 0) { - // Send next bit + // Send next bit // Symbols are sent LSB first - // Finished sending the whole message? (after waiting one bit period + // Finished sending the whole message? (after waiting one bit period // since the last bit) - if (vw_tx_index >= vw_tx_len) + if (vw_tx_index >= vw_tx_len) { vw_tx_stop(); vw_tx_msg_count++; } - else - { + else + { digitalWrite(vw_tx_pin, vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++)); if (vw_tx_bit >= 6) { - vw_tx_bit = 0; - vw_tx_index++; + vw_tx_bit = 0; + vw_tx_index++; } - } + } } if (vw_tx_sample > 7) vw_tx_sample = 0; - - if (vw_rx_enabled) + + if (vw_rx_enabled && !vw_tx_enabled) vw_pll(); } - -// Return true if there is a message available -uint8_t vw_have_message() +#elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific +void vw_Int_Handler() { - return vw_rx_done; + if (vw_rx_enabled && !vw_tx_enabled) + vw_rx_sample = digitalRead(vw_rx_pin); + + // Do transmitter stuff first to reduce transmitter bit jitter due + // to variable receiver processing + if (vw_tx_enabled && vw_tx_sample++ == 0) + { + // Send next bit + // Symbols are sent LSB first + // Finished sending the whole message? (after waiting one bit period + // since the last bit) + if (vw_tx_index >= vw_tx_len) + { + vw_tx_stop(); + vw_tx_msg_count++; + } + else + { + digitalWrite(vw_tx_pin, vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++)); + if (vw_tx_bit >= 6) + { + vw_tx_bit = 0; + vw_tx_index++; + } + } + } + if (vw_tx_sample > 7) + vw_tx_sample = 0; + + if (vw_rx_enabled && !vw_tx_enabled) + vw_pll(); } -// Get the last message received (without byte count or FCS) -// Copy at most *len bytes, set *len to the actual number copied -// Return true if there is a message and the FCS is OK -uint8_t vw_get_message(uint8_t* buf, uint8_t* len) +interrupt(TIMER0_A0_VECTOR) Timer_A_int(void) { - uint8_t rxlen; - - // Message available? - if (!vw_rx_done) - return false; - - // Wait until vw_rx_done is set before reading vw_rx_len - // then remove bytecount and FCS - rxlen = vw_rx_len - 3; - - // Copy message (good or bad) - if (*len > rxlen) - *len = rxlen; - memcpy(buf, vw_rx_buf + 1, *len); + vw_Int_Handler(); +}; - vw_rx_done = false; // OK, got that message thanks +#endif - // Check the FCS, return goodness - return (vw_crc(vw_rx_buf, vw_rx_len) == 0xf0b8); // FCS OK? -} } diff --git a/libraries/VirtualWire/VirtualWire.h b/libraries/VirtualWire/VirtualWire.h index 63f0b41..5fb9233 100644 --- a/libraries/VirtualWire/VirtualWire.h +++ b/libraries/VirtualWire/VirtualWire.h @@ -3,26 +3,172 @@ // Virtual Wire implementation for Arduino // See the README file in this directory fdor documentation // -// Author: Mike McCauley (mikem@open.com.au) +// Author: Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS // Copyright (C) 2008 Mike McCauley -// $Id: VirtualWire.h,v 1.3 2009/03/30 00:07:24 mikem Exp $ +// $Id: VirtualWire.h,v 1.6 2013/02/14 22:02:11 mikem Exp mikem $ + +/// \mainpage VirtualWire library for Arduino +/// +/// This is the Arduino VirtualWire library. +/// +/// VirtualWire is an Arduino library that provides features to send short +/// messages, without addressing, retransmit or acknowledgment, a bit like UDP +/// over wireless, using ASK (amplitude shift keying). Supports a number of +/// inexpensive radio transmitters and receivers. All that is required is +/// transmit data, receive data and (for transmitters, optionally) a PTT +/// transmitter enable. +/// +/// It is intended to be compatible with the RF Monolithics (www.rfm.com) +/// Virtual Wire protocol, but this has not been tested. +/// +/// Does not use the Arduino UART. Messages are sent with a training preamble, +/// message length and checksum. Messages are sent with 4-to-6 bit encoding +/// for good DC balance, and a CRC checksum for message integrity. +/// +/// Why not just use the Arduino UART connected directly to the +/// transmitter/receiver? As discussed in the RFM documentation, ASK receivers +/// require a burst of training pulses to synchronize the transmitter and +/// receiver, and also requires good balance between 0s and 1s in the message +/// stream in order to maintain the DC balance of the message. UARTs do not +/// provide these. They work a bit with ASK wireless, but not as well as this +/// code. +/// +/// This library provides classes for +/// - VirtualWire: unaddressed, unreliable messages +/// +/// Example Arduino programs are included to show the main modes of use. +/// +/// The version of the package that this documentation refers to can be downloaded +/// from http://www.airspayce.com/mikem/arduino/VirtualWire/VirtualWire-1.15.zip +/// You can find the latest version at http://www.airspayce.com/mikem/arduino/VirtualWire +/// +/// You can also find online help and disussion at http://groups.google.com/group/virtualwire +/// Please use that group for all questions and discussions on this topic. +/// Do not contact the author directly, unless it is to discuss commercial licensing. +/// +/// \par Supported Hardware +/// A range of communications hardware is supported. The ones listed blow are +/// available in common retail outlets in Australian and other countries for +/// under $10 per unit. Many other modules may also work with this software. +/// Runs on ATmega8/168 (Arduino Diecimila, Uno etc) and ATmega328 and possibly +/// others. Also runs on on Energia with MSP430G2553 / G2452 and Arduino with +/// ATMega328 (courtesy Yannick DEVOS - XV4Y). +/// Also compiles and runs on ATtiny85 in Arduino environment, courtesy r4z0r7o3. +/// +/// - Receivers +/// - RX-B1 (433.92MHz) (also known as ST-RX04-ASK) +/// - Transmitters: +/// - TX-C1 (433.92MHz) +/// - Transceivers +/// - DR3100 (433.92MHz) +/// +/// \par Installation +/// To install, unzip the library into the libraries sub-directory of your +/// Arduino application directory. Then launch the Arduino environment; you +/// should see the library in the Sketch->Import Library menu, and example +/// code in +/// File->Sketchbook->Examples->VirtualWire menu. +/// +/// \par Open Source Licensing GPL V2 +/// +/// This is the appropriate option if you want to share the source code of your +/// application with everyone you distribute it to, and you also want to give them +/// the right to share who uses it. If you wish to use this software under Open +/// Source Licensing, you must contribute all your source code to the open source +/// community in accordance with the GPL Version 2 when your application is +/// distributed. See http://www.gnu.org/copyleft/gpl.html +/// +/// \par Commercial Licensing +/// +/// This is the appropriate option if you are creating proprietary applications +/// and you are not prepared to distribute and share the source code of your +/// application. Contact info@airspayce.com for details. +/// +/// \par Revision History +/// \version 1.0 Original release +/// +/// \version 1.1 2008-06-24 +/// Now can compile for atmega8 +/// Reported by creatrope +/// \version 1.2 2009-03-30 +/// Fixed a problem that prevented compiling with arduino-0015 +/// Reported by Jaime Castro +/// \version 1.3 2009-04-01 +/// Fixed a compatibility problem with ATMEGA328 of the new arduino +/// Now use SIGNAL(TIMER1_COMPA_vect) instead of ISR(SIG_OUTPUT_COMPARE1A) +/// as discussed in +/// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237714550/11 +/// and reported by Jaime Castro. +/// \version 1.4 2010-01-29 +/// Added vx_tx_active(), suggested by Alan Burlison. +/// \version 1.5 2011-09-09 +/// Added vx_tx_active() function. +/// \version 1.6 2012-01-10 +/// Fixed a problem where the receiver was always reenabled after +/// transmission. Reported by David Bath +/// \version 1.9 2012-02-07 Documentation updates +/// Documentation updates +/// \version 1.10 Updated CHANGES file with changes since 1.4. +/// \version 1.11 Converted documentation to Doxygen. Moved CHANGES log to this version history. +/// Ensure vw_rx_pin is not accessed unless receiver is enabled +/// \version 1.12 Compiles and runs on on Energia with MSP430G2553 / G2452 and Arduino with ATMega328. +/// Patches contributed by Yannick DEVOS - XV4Y +/// \version 1.13 util/crc16.h needed for compiling on Energia with MSP430G2553 / G2452 was accidentally +/// left out of the distribution +/// \version 1.14 Added support ATtiny85 on Arduino, patch provided by r4z0r7o3. +/// \version 1.15 Updated author and distribution location details to airspayce.com +/// +/// \par Implementation Details +/// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf +/// +/// \par Performance +/// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf +/// +/// \par Connections +/// See: http://www.airspayce.com/mikem/arduino/VirtualWire.pdf +/// +/// \file VirtualWire.h +/// \brief VirtualWire API +/// +/// To use the VirtualWire library, you must have +/// \code +/// #include +/// \endcode +/// At the top of your sketch. +/// #ifndef VirtualWire_h #define VirtualWire_h #include -#include +#if defined(ARDUINO) + #if ARDUINO >= 100 + #include + #else + #include + #endif +#elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific + #include "legacymsp430.h" + #include "Energia.h" +#else // error + #error Platform not defined +#endif + +// These defs cause trouble on some versions of Arduino +#undef abs +#undef double +#undef round -// Maximum number of bytes in a message, counting the byte count and FCS +/// Maximum number of bytes in a message, counting the byte count and FCS #define VW_MAX_MESSAGE_LEN 30 -// The maximum payload length +/// The maximum payload length #define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3 -// The size of the receiver ramp. Ramp wraps modulu this number +/// The size of the receiver ramp. Ramp wraps modulu this number #define VW_RX_RAMP_LEN 160 -// Number of samples per bit +/// Number of samples per bit #define VW_RX_SAMPLES_PER_BIT 8 // Ramp adjustment parameters @@ -30,78 +176,117 @@ // the ramp is retarded by adding VW_RAMP_INC_RETARD (11) // else by adding VW_RAMP_INC_ADVANCE (29) // If there is no transition it is adjusted by VW_RAMP_INC (20) +/// Internal ramp adjustment parameter #define VW_RAMP_INC (VW_RX_RAMP_LEN/VW_RX_SAMPLES_PER_BIT) +/// Internal ramp adjustment parameter #define VW_RAMP_TRANSITION VW_RX_RAMP_LEN/2 +/// Internal ramp adjustment parameter #define VW_RAMP_ADJUST 9 +/// Internal ramp adjustment parameter #define VW_RAMP_INC_RETARD (VW_RAMP_INC-VW_RAMP_ADJUST) +/// Internal ramp adjustment parameter #define VW_RAMP_INC_ADVANCE (VW_RAMP_INC+VW_RAMP_ADJUST) -// Outgoing message bits grouped as 6-bit words -// 36 alternating 1/0 bits, followed by 12 bits of start symbol -// Followed immediately by the 4-6 bit encoded byte count, -// message buffer and 2 byte FCS -// Each byte from the byte count on is translated into 2x6-bit words -// Caution, each symbol is transmitted LSBit first, -// but each byte is transmitted high nybble first +/// Outgoing message bits grouped as 6-bit words +/// 36 alternating 1/0 bits, followed by 12 bits of start symbol +/// Followed immediately by the 4-6 bit encoded byte count, +/// message buffer and 2 byte FCS +/// Each byte from the byte count on is translated into 2x6-bit words +/// Caution, each symbol is transmitted LSBit first, +/// but each byte is transmitted high nybble first #define VW_HEADER_LEN 8 // Cant really do this as a real C++ class, since we need to have // an ISR extern "C" { - // Set the digital IO pin to be for transmit data - // Defaults to 12 + /// Set the digital IO pin to be for transmit data. + /// This pin will only be accessed if + /// the transmitter is enabled + /// \param[in] pin The Arduino pin number for transmitting data. Defaults to 12. extern void vw_set_tx_pin(uint8_t pin); - // Set the digital IO pin to be for receive data - // Defaults to 11 + /// Set the digital IO pin to be for receive data. + /// This pin will only be accessed if + /// the receiver is enabled + /// \param[in] pin The Arduino pin number for receiving data. Defaults to 11. extern void vw_set_rx_pin(uint8_t pin); - // Set the digital IO pin to enable the transmitter (press to talk) - // Defaults to 10 + // Set the digital IO pin to enable the transmitter (press to talk, PTT)' + /// This pin will only be accessed if + /// the transmitter is enabled + /// \param[in] pin The Arduino pin number to enable the transmitter. Defaults to 10. extern void vw_set_ptt_pin(uint8_t pin); - // By default the PTT pin goes high when the transmitter is enabled. - // This flag forces it low when the transmitter is enabled. + /// By default the PTT pin goes high when the transmitter is enabled. + /// This flag forces it low when the transmitter is enabled. + /// \param[in] inverted True to invert PTT extern void vw_set_ptt_inverted(uint8_t inverted); - // Initialise the VirtualWire software, to operate at speed bits per second - // Call this one in your setup() after any vw_set_* calls - // Must call vw_rx_start() before you will get any messages + /// Initialise the VirtualWire software, to operate at speed bits per second + /// Call this one in your setup() after any vw_set_* calls + /// Must call vw_rx_start() before you will get any messages + /// \param[in] speed Desired speed in bits per second extern void vw_setup(uint16_t speed); - // Start the Phase Locked Loop listening to the receiver - // Must do this before you can receive any messages - // When a message is available (good checksum or not), vw_have_message(); - // will return true. + /// Start the Phase Locked Loop listening to the receiver + /// Must do this before you can receive any messages + /// When a message is available (good checksum or not), vw_have_message(); + /// will return true. extern void vw_rx_start(); - // Stop the Phase Locked Loop listening to the receiver - // No messages will be received until vw_rx_start() is called again - // Saves interrupt processing cycles + /// Stop the Phase Locked Loop listening to the receiver + /// No messages will be received until vw_rx_start() is called again + /// Saves interrupt processing cycles extern void vw_rx_stop(); - // Block until the transmitter is idle + /// Returns the state of the + /// transmitter + /// \return true if the transmitter is active else false + extern uint8_t vx_tx_active(); + + /// Block until the transmitter is idle + /// then returns extern void vw_wait_tx(); - // Block until a message is available + /// Block until a message is available + /// then returns extern void vw_wait_rx(); - // or for a max time + + /// Block until a message is available or for a max time + /// \param[in] milliseconds Maximum time to wait in milliseconds. + /// \return true if a message is available, false if the wait timed out. extern uint8_t vw_wait_rx_max(unsigned long milliseconds); - // Send a message with the given length. Returns almost immediately, - // and message will be sent at the right timing by interrupts - // Returns true if the message was accepted for transmissions - // Returns false if the message is too long (>VW_MAX_MESSAGE_LEN - 3) + /// Send a message with the given length. Returns almost immediately, + /// and message will be sent at the right timing by interrupts + /// \param[in] buf Pointer to the data to transmit + /// \param[in] len Number of octetes to transmit + /// \return true if the message was accepted for transmission, false if the message is too long (>VW_MAX_MESSAGE_LEN - 3) extern uint8_t vw_send(uint8_t* buf, uint8_t len); // Returns true if an unread message is available + /// \return true if a message is available to read extern uint8_t vw_have_message(); // If a message is available (good checksum or not), copies // up to *len octets to buf. - // Returns true if there was a message and the checksum was good + /// \param[in] buf Pointer to location to save the read data (must be at least *len bytes. + /// \param[in,out] len Available space in buf. Will be set to the actual number of octets read + /// \return true if there was a message and the checksum was good extern uint8_t vw_get_message(uint8_t* buf, uint8_t* len); } +/// @example client.pde +/// Client side of simple client/server pair using VirtualWire + +/// @example server.pde +/// Server side of simple client/server pair using VirtualWire + +/// @example transmitter.pde +/// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire + +/// @example receiver.pde +/// Transmitter side of simple one-way transmitter->receiver pair using VirtualWire + #endif diff --git a/libraries/VirtualWire/examples/client/client.pde b/libraries/VirtualWire/examples/client/client.pde index 5c0fa58..018cb01 100644 --- a/libraries/VirtualWire/examples/client/client.pde +++ b/libraries/VirtualWire/examples/client/client.pde @@ -6,16 +6,12 @@ // should send a reply, which we will check // // See VirtualWire.h for detailed API docs -// Author: Mike McCauley (mikem@open.com.au) +// Author: Mike McCauley (mikem@airspayce.com) // Copyright (C) 2008 Mike McCauley // $Id: client.pde,v 1.1 2008/04/20 09:24:17 mikem Exp $ #include -#undef int -#undef abs -#undef double -#undef float -#undef round + void setup() { Serial.begin(9600); // Debugging only diff --git a/libraries/VirtualWire/examples/receiver/receiver.pde b/libraries/VirtualWire/examples/receiver/receiver.pde index 3291d74..100e6eb 100644 --- a/libraries/VirtualWire/examples/receiver/receiver.pde +++ b/libraries/VirtualWire/examples/receiver/receiver.pde @@ -4,26 +4,33 @@ // Implements a simplex (one-way) receiver with an Rx-B1 module // // See VirtualWire.h for detailed API docs -// Author: Mike McCauley (mikem@open.com.au) +// Author: Mike McCauley (mikem@airspayce.com) // Copyright (C) 2008 Mike McCauley // $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $ #include -#undef int -#undef abs -#undef double -#undef float -#undef round + +const int led_pin = 13; +const int transmit_pin = 12; +const int receive_pin = 11; +const int transmit_en_pin = 3; + void setup() { + delay(1000); Serial.begin(9600); // Debugging only Serial.println("setup"); // Initialise the IO and ISR + vw_set_tx_pin(transmit_pin); + vw_set_rx_pin(receive_pin); + vw_set_ptt_pin(transmit_en_pin); vw_set_ptt_inverted(true); // Required for DR3100 vw_setup(2000); // Bits per sec vw_rx_start(); // Start the receiver PLL running + + pinMode(led_pin, OUTPUT); } void loop() @@ -35,16 +42,16 @@ void loop() { int i; - digitalWrite(13, true); // Flash a light to show received good message + digitalWrite(led_pin, HIGH); // Flash a light to show received good message // Message with a good checksum received, dump it. Serial.print("Got: "); for (i = 0; i < buflen; i++) { Serial.print(buf[i], HEX); - Serial.print(" "); + Serial.print(' '); } - Serial.println(""); - digitalWrite(13, false); + Serial.println(); + digitalWrite(led_pin, LOW); } } diff --git a/libraries/VirtualWire/examples/server/server.pde b/libraries/VirtualWire/examples/server/server.pde index f03502d..ead67f0 100644 --- a/libraries/VirtualWire/examples/server/server.pde +++ b/libraries/VirtualWire/examples/server/server.pde @@ -7,16 +7,12 @@ // You can use this as the basis of a remote control/remote sensing system // // See VirtualWire.h for detailed API docs -// Author: Mike McCauley (mikem@open.com.au) +// Author: Mike McCauley (mikem@airspayce.com) // Copyright (C) 2008 Mike McCauley // $Id: server.pde,v 1.1 2008/04/20 09:24:17 mikem Exp $ #include -#undef int -#undef abs -#undef double -#undef float -#undef round + void setup() { Serial.begin(9600); // Debugging only diff --git a/libraries/VirtualWire/examples/transmitter/transmitter.pde b/libraries/VirtualWire/examples/transmitter/transmitter.pde index a7ef3ed..357c674 100644 --- a/libraries/VirtualWire/examples/transmitter/transmitter.pde +++ b/libraries/VirtualWire/examples/transmitter/transmitter.pde @@ -4,33 +4,39 @@ // Implements a simplex (one-way) transmitter with an TX-C1 module // // See VirtualWire.h for detailed API docs -// Author: Mike McCauley (mikem@open.com.au) +// Author: Mike McCauley (mikem@airspayce.com) // Copyright (C) 2008 Mike McCauley // $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $ #include -#undef int -#undef abs -#undef double -#undef float -#undef round + +const int led_pin = 11; +const int transmit_pin = 12; +const int receive_pin = 2; +const int transmit_en_pin = 3; + void setup() { - Serial.begin(9600); // Debugging only - Serial.println("setup"); - // Initialise the IO and ISR + vw_set_tx_pin(transmit_pin); + vw_set_rx_pin(receive_pin); + vw_set_ptt_pin(transmit_en_pin); vw_set_ptt_inverted(true); // Required for DR3100 - vw_setup(2000); // Bits per sec + vw_setup(2000); // Bits per sec + pinMode(led_pin, OUTPUT); } +byte count = 1; + void loop() { - const char *msg = "hello"; + char msg[7] = {'h','e','l','l','o',' ','#'}; - digitalWrite(13, true); // Flash a light to show transmitting - vw_send((uint8_t *)msg, strlen(msg)); - vw_wait_tx(); // Wait until the whole message is gone - digitalWrite(13, false); - delay(200); + msg[6] = count; + digitalWrite(led_pin, HIGH); // Flash a light to show transmitting + vw_send((uint8_t *)msg, 7); + vw_wait_tx(); // Wait until the whole message is gone + digitalWrite(led_pin, LOW); + delay(1000); + count = count + 1; } -- 2.43.2