libraries / VirtualWire / VirtualWire.hon commit Added final version of code (649c546)
   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@open.com.au)
   7// Copyright (C) 2008 Mike McCauley
   8// $Id: VirtualWire.h,v 1.3 2009/03/30 00:07:24 mikem Exp $
   9
  10#ifndef VirtualWire_h
  11#define VirtualWire_h
  12
  13#include <stdlib.h>
  14#include <wiring.h>
  15
  16// Maximum number of bytes in a message, counting the byte count and FCS
  17#define VW_MAX_MESSAGE_LEN 30
  18
  19// The maximum payload length
  20#define VW_MAX_PAYLOAD VW_MAX_MESSAGE_LEN-3
  21
  22// The size of the receiver ramp. Ramp wraps modulu this number
  23#define VW_RX_RAMP_LEN 160
  24
  25// Number of samples per bit
  26#define VW_RX_SAMPLES_PER_BIT 8
  27
  28// Ramp adjustment parameters
  29// Standard is if a transition occurs before VW_RAMP_TRANSITION (80) in the ramp,
  30// the ramp is retarded by adding VW_RAMP_INC_RETARD (11)
  31// else by adding VW_RAMP_INC_ADVANCE (29)
  32// If there is no transition it is adjusted by VW_RAMP_INC (20)
  33#define VW_RAMP_INC (VW_RX_RAMP_LEN/VW_RX_SAMPLES_PER_BIT)
  34#define VW_RAMP_TRANSITION VW_RX_RAMP_LEN/2
  35#define VW_RAMP_ADJUST 9
  36#define VW_RAMP_INC_RETARD (VW_RAMP_INC-VW_RAMP_ADJUST)
  37#define VW_RAMP_INC_ADVANCE (VW_RAMP_INC+VW_RAMP_ADJUST)
  38
  39// Outgoing message bits grouped as 6-bit words
  40// 36 alternating 1/0 bits, followed by 12 bits of start symbol
  41// Followed immediately by the 4-6 bit encoded byte count, 
  42// message buffer and 2 byte FCS
  43// Each byte from the byte count on is translated into 2x6-bit words
  44// Caution, each symbol is transmitted LSBit first, 
  45// but each byte is transmitted high nybble first
  46#define VW_HEADER_LEN 8
  47
  48// Cant really do this as a real C++ class, since we need to have 
  49// an ISR
  50extern "C"
  51{
  52    // Set the digital IO pin to be for transmit data
  53    // Defaults to 12
  54    extern void vw_set_tx_pin(uint8_t pin);
  55
  56    // Set the digital IO pin to be for receive data
  57    // Defaults to 11
  58    extern void vw_set_rx_pin(uint8_t pin);
  59
  60    // Set the digital IO pin to enable the transmitter (press to talk)
  61    // Defaults to 10
  62    extern void vw_set_ptt_pin(uint8_t pin);
  63
  64    // By default the PTT pin goes high when the transmitter is enabled.
  65    // This flag forces it low when the transmitter is enabled.
  66    extern void vw_set_ptt_inverted(uint8_t inverted);
  67
  68    // Initialise the VirtualWire software, to operate at speed bits per second
  69    // Call this one in your setup() after any vw_set_* calls
  70    // Must call vw_rx_start() before you will get any messages
  71    extern void vw_setup(uint16_t speed);
  72
  73    // Start the Phase Locked Loop listening to the receiver
  74    // Must do this before you can receive any messages
  75    // When a message is available (good checksum or not), vw_have_message();
  76    // will return true.
  77    extern void vw_rx_start();
  78
  79    // Stop the Phase Locked Loop listening to the receiver
  80    // No messages will be received until vw_rx_start() is called again
  81    // Saves interrupt processing cycles
  82    extern void vw_rx_stop();
  83
  84    // Block until the transmitter is idle
  85    extern void vw_wait_tx();
  86
  87    // Block until a message is available
  88    extern void vw_wait_rx();
  89    // or for a max time
  90    extern uint8_t vw_wait_rx_max(unsigned long milliseconds);
  91
  92    // Send a message with the given length. Returns almost immediately,
  93    // and message will be sent at the right timing by interrupts
  94    // Returns true if the message was accepted for transmissions
  95    // Returns false if the message is too long (>VW_MAX_MESSAGE_LEN - 3)
  96    extern uint8_t vw_send(uint8_t* buf, uint8_t len);
  97
  98    // Returns true if an unread message is available
  99    extern uint8_t vw_have_message();
 100
 101    // If a message is available (good checksum or not), copies
 102    // up to *len octets to buf.
 103    // Returns true if there was a message and the checksum was good
 104    extern uint8_t vw_get_message(uint8_t* buf, uint8_t* len);
 105}
 106
 107#endif