HopeRF
HRFMessage.h
1 // HRFMessage.h
2 //
3 /// \mainpage HopeRF Radio Transceiver Message Library for Arduino
4 ///
5 /// This is the Arduino HopeRF library for operating with HopeRF HM-TR transceivers
6 /// (see http://www.hoperf.com)
7 /// It should also work with other 'transparent' serial transceivers.
8 /// See http://www.airspayce.com/mikem/arduino/HopeRF/HopeRF.pdf
9 /// for package download details, electrical details and documentation
10 /// for connecting to HM-TR transceivers.
11 ///
12 /// The latest version of this documentation can be downloaded from
13 /// http://www.airspayce.com/mikem/arduino/HopeRF
14 ///
15 /// The version of the package that this documentation refers to can be downloaded
16 /// from http://www.airspayce.com/mikem/arduino/HopeRF/HopeRF-1.5.zip
17 ///
18 /// The HopeRF HM-TR transceiver is an inexpensive 433MHz 'transparent' serial transceiver. It handles
19 /// internally all the issues of preamble, synchronisation etc (which is the bulk of the work in my other
20 /// Arduino library, VirtualWire). Although the HM-TR is more expensive than the bare 433 MHz transceivers
21 /// supported by VirtualWire, it requires much fewer compute resources from the Arduino (in fact the HM-TR has an
22 /// ATMega on it specifically to do the serial-433MHz translation). However the HM-TR does not have any
23 /// error detection built in. That is provided by this library.
24 ///
25 /// This library provides classes for
26 /// \li unaddressed, unreliable messages
27 /// \li addressed, unreliable messages
28 /// \li addressed, reliable, retransmitted, acknowledged messages.
29 ///
30 /// The Library has been tested on Arduino Mega, which has several serial ports besides the USB connection
31 /// to the host PC. However, it would be expected to work with any Arduino with a serial port.
32 /// The only connection required between the Arduino and the HM-TR module is power, received and transmit.
33 /// The library is wrtten in C++, and compiles on Linux. With only a little work it could be expected to run
34 /// on Linux with a suitable HardwareSerial impoentation, and using HM-TR with RS-232 connections.
35 ///
36 /// This software is Copyright (C) 2009 Mike McCauley. Use is subject to license
37 /// conditions. The main licensing options available are GPL V2 or Commercial:
38 ///
39 /// \par Open Source Licensing GPL V2
40 /// This is the appropriate option if you want to share the source code of your
41 /// application with everyone you distribute it to, and you also want to give them
42 /// the right to share who uses it. If you wish to use this software under Open
43 /// Source Licensing, you must contribute all your source code to the open source
44 /// community in accordance with the GPL Version 2 when your application is
45 /// distributed. See http://www.gnu.org/copyleft/gpl.html
46 ///
47 /// \par Commercial Licensing
48 /// This is the appropriate option if you are creating proprietary applications
49 /// and you are not prepared to distribute and share the source code of your
50 /// application. Contact info@airspayce.com for details.
51 ///
52 /// \par Revision History
53 /// \version 1.4 Compiles on Arduino 1.0
54 ///
55 /// \version 1.5 HRFReliableDatagram sendToWait() incorrectly could wait forever.
56 /// Reported by Nick Maddock.
57 /// \version 1.6 Updated author and distribution location details to airspayce.com
58 ///
59 /// \author Mike McCauley (mikem@airspayce.com)
60 // Copyright (C) 2009 Mike McCauley
61 // $Id: HRFMessage.h,v 1.1 2009/08/15 05:32:58 mikem Exp mikem $
62 
63 #ifndef HRFMessage_h
64 #define HRFMessage_h
65 
66 // When testing on Linux, the def of abs is broken by wiring.h
67 #ifdef TEST
68 #undef abs
69 #endif
70 
71 #include <stdlib.h>
72 #if ARDUINO >= 100
73 #include <Arduino.h>
74 #else
75 #include <wiring.h>
76 #endif
77 // These defs cause trouble on some versions of Arduino
78 #undef abs
79 #undef double
80 #undef round
81 
82 #include <HardwareSerial.h>
83 
84 /// \def HRF_MAX_MESSAGE_LEN
85 /// Maximum number of bytes in a message, counting the byte count and FCS
86 /// Tests show that more than 64 octets in a single message can cause
87 /// corrupted and missing characters, even though the Serial class has a buffer of 128,
88 /// probably due to some undocumented buffer limitation in the HM-TR
89 #define HRF_MAX_MESSAGE_LEN 64
90 
91 /// \def HRF_MAX_PAYLOAD
92 /// The maximum payload length
93 #define HRF_MAX_PAYLOAD HRF_MAX_MESSAGE_LEN-3
94 
95 /////////////////////////////////////////////////////////////////////
96 /// \class HRFMessage HRFMessage.h <HRFMessage.h>
97 /// \brief Basic message class for HopeRF data transceivers
98 ///
99 /// Define low level unaddressed, unreliable message.
100 /// HRFMessage have the format LEN payload FCS-LO FCS-HI.
101 /// LEN is the total number of octets in the message, including the LEN and FCS octets
102 /// FCS (16 bits) is the complement of CCITT CRC-16 of all octets in the message, including the LEN
103 /// HRFMessage is unaddressed, and will be received by all HRFMessage nodes within range.
104 ///
105 /// This class and others in this library provide easy methods for sending and receiving messages using the HM-TR.
106 /// Several layers of classes are provided above this
107 /// to provide a range of features from unacknowledged broadcasts to
108 /// acknowledged, reliable addressed messages.
109 ///
110 /// Although HopeRF HM-TR transceivers have an enable pin, this library make no use of that. Enabling
111 /// and disabling of the transceiver when it is not required should be done by other code.
112 ///
113 /// Although HopeRF HM-TR transceivers have a Config pin, this library make no use of that, since it must be
114 /// enabled when the transceiver powers up.
115 /// This librray assumes the transceiver has already been configured, typically using the
116 /// HopeRF configuration program.
118 {
119 private:
120  /// This is the instance of HardwareSerial that will be used to communicate with the HM-TR
121  HardwareSerial* _serial;
122 
123  /// Buffer for incoming messages
124  uint8_t _rx_buf[HRF_MAX_MESSAGE_LEN];
125 
126  /// How many octets are currently in _rx_buf
127  uint8_t _rx_len;
128 
129  /// Number of bad messages received and dropped due to bad lengths
130  uint8_t _rx_bad;
131 
132  /// Number of good messages received
133  uint8_t _rx_good;
134 
135 public:
136  /// You can have multiple instances for HM-TR modules on multiple HardwareSerial ports.
137  /// \param[in] serial The instance of HardwareSerial to use for IO. Defaults to &Serial,
138  /// the Arduino default serial port.
139  HRFMessage(HardwareSerial* serial = &Serial);
140 
141  /// Send a message with the given length. Blocks and returns after
142  /// the entire message has been sent. Any binary data is permitted.
143  /// \param[in] buf Pointer to the binary message to send
144  /// \param[in] len Number of octets to send
145  /// \return true if the message was transmitted
146  /// \return false if the message is too long (>HRF_MAX_PAYLOAD)
147  virtual uint8_t send(uint8_t* buf, uint8_t len);
148 
149  /// \return message length if a complete unread message is available
150  /// \return 0 (false) if no message is available yet)
151  virtual uint8_t available();
152 
153  /// If a message is available (good checksum or not), copies
154  /// up to *len octets to buf.
155  /// If a message is copied, *len is set to the length
156  /// \return true if there was a message copied _and_ the FCS checksum was good
157  /// \param[in] buf Location to copy the received message
158  /// \param[in] len Available space in buf. Set to the actual number of octets copied.
159  virtual uint8_t recv(uint8_t* buf, uint8_t* len);
160 
161  /// The bad message count starts at 0 at instantation time, and is incremented whenever
162  /// a message with a bad length is received. This generally indicates a corrupt messages.
163  /// \return the number of corrupt messages received.
164  virtual uint8_t rx_bad(void);
165 
166  /// The good message count starts at 0 at instantation time, and is incremented whenever
167  /// a complete message is received,
168  /// \return the number of good messages received.
169  virtual uint8_t rx_good(void);
170 
171  /// Wait until at least 1 octet is available on the serial device
172  /// On Arduino, polls the device. On Linux, blocks
173  virtual void waitAvailable(void);
174 };
175 #endif