00001 // HRFMessage.h 00002 // 00003 /// \mainpage HopeRF Radio Transceiver Message Library for Arduino 00004 /// 00005 /// This is the Arduino HopeRF library for operating with HopeRF HM-TR transceivers 00006 /// (see http://www.hoperf.com) 00007 /// It should also work with other 'transparent' serial transceivers. 00008 /// See http://www.open.com.au/mikem/arduino/HopeRF/HopeRF.pdf 00009 /// for package download details, electrical details and documentation 00010 /// for connecting to HM-TR transceivers. 00011 /// 00012 /// The latest version of this documentation can be downloaded from 00013 /// http://www.open.com.au/mikem/arduino/HopeRF 00014 /// 00015 /// The version of the package that this documentation refers to can be downloaded 00016 /// from http://www.open.com.au/mikem/arduino/HopeRF/HopeRF-1.4.zip 00017 /// 00018 /// The HopeRF HM-TR transceiver is an inexpensive 433MHz 'transparent' serial transceiver. It handles 00019 /// internally all the issues of preamble, synchronisation etc (which is the bulk of the work in my other 00020 /// Arduino library, VirtualWire). Although the HM-TR is more expensive than the bare 433 MHz transceivers 00021 /// supported by VirtualWire, it requires much fewer compute resources from the Arduino (in fact the HM-TR has an 00022 /// ATMega on it specifically to do the serial-433MHz translation). However the HM-TR does not have any 00023 /// error detection built in. That is provided by this library. 00024 /// 00025 /// This library provides classes for 00026 /// \li unaddressed, unreliable messages 00027 /// \li addressed, unreliable messages 00028 /// \li addressed, reliable, retransmitted, acknowledged messages. 00029 /// 00030 /// The Library has been tested on Arduino Mega, which has several serial ports besides the USB connection 00031 /// to the host PC. However, it would be expected to work with any Arduino with a serial port. 00032 /// The only connection required between the Arduino and the HM-TR module is power, received and transmit. 00033 /// The library is wrtten in C++, and compiles on Linux. With only a little work it could be expected to run 00034 /// on Linux with a suitable HardwareSerial impoentation, and using HM-TR with RS-232 connections. 00035 /// 00036 /// This software is Copyright (C) 2009 Mike McCauley. Use is subject to license 00037 /// conditions. The main licensing options available are GPL V2 or Commercial: 00038 /// 00039 /// \par Open Source Licensing GPL V2 00040 /// This is the appropriate option if you want to share the source code of your 00041 /// application with everyone you distribute it to, and you also want to give them 00042 /// the right to share who uses it. If you wish to use this software under Open 00043 /// Source Licensing, you must contribute all your source code to the open source 00044 /// community in accordance with the GPL Version 2 when your application is 00045 /// distributed. See http://www.gnu.org/copyleft/gpl.html 00046 /// 00047 /// \par Commercial Licensing 00048 /// This is the appropriate option if you are creating proprietary applications 00049 /// and you are not prepared to distribute and share the source code of your 00050 /// application. Contact info@open.com.au for details. 00051 /// 00052 /// \par Revision History 00053 /// \version 1.4 Compiles on Arduino 1.0 00054 /// 00055 /// \author Mike McCauley (mikem@open.com.au) 00056 // Copyright (C) 2009 Mike McCauley 00057 // $Id: HRFMessage.h,v 1.1 2009/08/15 05:32:58 mikem Exp mikem $ 00058 00059 #ifndef HRFMessage_h 00060 #define HRFMessage_h 00061 00062 // When testing on Linux, the def of abs is broken by wiring.h 00063 #ifdef TEST 00064 #undef abs 00065 #endif 00066 00067 #include <stdlib.h> 00068 #if ARDUINO >= 100 00069 #include <Arduino.h> 00070 #else 00071 #include <wiring.h> 00072 #endif 00073 // These defs cause trouble on some versions of Arduino 00074 #undef abs 00075 #undef double 00076 #undef round 00077 00078 #include <HardwareSerial.h> 00079 00080 /// \def HRF_MAX_MESSAGE_LEN 00081 /// Maximum number of bytes in a message, counting the byte count and FCS 00082 /// Tests show that more than 64 octets in a single message can cause 00083 /// corrupted and missing characters, even though the Serial class has a buffer of 128, 00084 /// probably due to some undocumented buffer limitation in the HM-TR 00085 #define HRF_MAX_MESSAGE_LEN 64 00086 00087 /// \def HRF_MAX_PAYLOAD 00088 /// The maximum payload length 00089 #define HRF_MAX_PAYLOAD HRF_MAX_MESSAGE_LEN-3 00090 00091 ///////////////////////////////////////////////////////////////////// 00092 /// \class HRFMessage HRFMessage.h <HRFMessage.h> 00093 /// \brief Basic message class for HopeRF data transceivers 00094 /// 00095 /// Define low level unaddressed, unreliable message. 00096 /// HRFMessage have the format LEN payload FCS-LO FCS-HI. 00097 /// LEN is the total number of octets in the message, including the LEN and FCS octets 00098 /// FCS (16 bits) is the complement of CCITT CRC-16 of all octets in the message, including the LEN 00099 /// HRFMessage is unaddressed, and will be received by all HRFMessage nodes within range. 00100 /// 00101 /// This class and others in this library provide easy methods for sending and receiving messages using the HM-TR. 00102 /// Several layers of classes are provided above this 00103 /// to provide a range of features from unacknowledged broadcasts to 00104 /// acknowledged, reliable addressed messages. 00105 /// 00106 /// Although HopeRF HM-TR transceivers have an enable pin, this library make no use of that. Enabling 00107 /// and disabling of the transceiver when it is not required should be done by other code. 00108 /// 00109 /// Although HopeRF HM-TR transceivers have a Config pin, this library make no use of that, since it must be 00110 /// enabled when the transceiver powers up. 00111 /// This librray assumes the transceiver has already been configured, typically using the 00112 /// HopeRF configuration program. 00113 class HRFMessage 00114 { 00115 private: 00116 /// This is the instance of HardwareSerial that will be used to communicate with the HM-TR 00117 HardwareSerial* _serial; 00118 00119 /// Buffer for incoming messages 00120 uint8_t _rx_buf[HRF_MAX_MESSAGE_LEN]; 00121 00122 /// How many octets are currently in _rx_buf 00123 uint8_t _rx_len; 00124 00125 /// Number of bad messages received and dropped due to bad lengths 00126 uint8_t _rx_bad; 00127 00128 /// Number of good messages received 00129 uint8_t _rx_good; 00130 00131 public: 00132 /// You can have multiple instances for HM-TR modules on multiple HardwareSerial ports. 00133 /// \param[in] serial The instance of HardwareSerial to use for IO. Defaults to &Serial, 00134 /// the Arduino default serial port. 00135 HRFMessage(HardwareSerial* serial = &Serial); 00136 00137 /// Send a message with the given length. Blocks and returns after 00138 /// the entire message has been sent. Any binary data is permitted. 00139 /// \param[in] buf Pointer to the binary message to send 00140 /// \param[in] len Number of octets to send 00141 /// \return true if the message was transmitted 00142 /// \return false if the message is too long (>HRF_MAX_PAYLOAD) 00143 virtual uint8_t send(uint8_t* buf, uint8_t len); 00144 00145 /// \return message length if a complete unread message is available 00146 /// \return 0 (false) if no message is available yet) 00147 virtual uint8_t available(); 00148 00149 /// If a message is available (good checksum or not), copies 00150 /// up to *len octets to buf. 00151 /// If a message is copied, *len is set to the length 00152 /// \return true if there was a message copied _and_ the FCS checksum was good 00153 /// \param[in] buf Location to copy the received message 00154 /// \param[in] len Available space in buf. Set to the actual number of octets copied. 00155 virtual uint8_t recv(uint8_t* buf, uint8_t* len); 00156 00157 /// The bad message count starts at 0 at instantation time, and is incremented whenever 00158 /// a message with a bad length is received. This generally indicates a corrupt messages. 00159 /// \return the number of corrupt messages received. 00160 virtual uint8_t rx_bad(void); 00161 00162 /// The good message count starts at 0 at instantation time, and is incremented whenever 00163 /// a complete message is received, 00164 /// \return the number of good messages received. 00165 virtual uint8_t rx_good(void); 00166 00167 /// Wait until at least 1 octet is available on the serial device 00168 /// On Arduino, polls the device. On Linux, blocks 00169 virtual void waitAvailable(void); 00170 }; 00171 #endif