Manchester code (phase encoding) library.
Dependents: Manchester_Transmitter Manchester_Receiver
Manchester code (phase encoding) library
It implements Manchester code according to both IEEE 802.3 and G.E. Thomas' conventions.
- A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention.
- The transitions which signify '0' or '1' occur at the midpoint of a period.
- Transitions at the start of a period are overhead and don't signify data.
- Least significant bit is sent first
- There are synchronization pulses (the number can be set) at the begin of transmission
Select a convention to be used by commenting or uncommenting the line below in theManchester.h
header file.
Manchester.h
#define G_E_THOMAS 1
The IEEE 802.3 convention is used by default.
A Manchester encoded message (using G.E. Thomas' convention), with one sync pulse in the preamble, carrying four bytes:
ACKNOWLEDGEMENT: The code in this library was based on this article published by Robert Guastella.
Import programManchester_Transmitter
Manchester transmitter demo.
Import programManchester_Receiver
Manchester receiver demo.
NOTE: To perform a simple test (without radio modules) connect the txPin on transmitter board to the rxPin on the receiver board and make sure that grounds are also connected one another.
Diff: ManchesterMsg.h
- Revision:
- 2:de778df5892c
- Parent:
- 0:d5c75b0e5708
- Child:
- 4:f2c392191c74
--- a/ManchesterMsg.h Wed May 17 08:17:13 2017 +0000 +++ b/ManchesterMsg.h Wed May 17 21:02:49 2017 +0000 @@ -2,7 +2,7 @@ ****************************************************************************** * @file ManchesterMsg.h * @author Zoltan Hudak - * @version + * @version * @date 16-May-2017 * @brief Message container ****************************************************************************** @@ -25,7 +25,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ - #ifndef MANCHESTERMSG_H #define MANCHESTERMSG_H @@ -35,42 +34,36 @@ */ class ManchesterMsg { - unsigned char _max; // Max length (capacity) of data field in bytes + size_t _max; // Max length (capacity) of data field in bytes public: - char* data; // Data field - unsigned char len; // Length of data in bytes + char* data; // Data field + size_t len; // Length of data in bytes /** Creates empty Manchester message of specified capacity. */ - ManchesterMsg(unsigned char max) : - _max(max), - data(new char[max]) { len = 0; } + ManchesterMsg(size_t max) : _max(max), data(new char[max]) { len = 0; } - ~ManchesterMsg(void) { delete[] data; } + ~ ManchesterMsg(void) { delete[] data; } - /** Copy constructor. + /** Copy constructor. */ - ManchesterMsg(const ManchesterMsg& msg) { - len = msg.len; - memcpy(data, msg.data, msg.len); - } - - + ManchesterMsg(const ManchesterMsg& msg) { len = msg.len; memcpy(data, msg.data, msg.len); } + /** Returns message maximum length (capacity) */ - unsigned char maxLen(void) { return _max; } + size_t maxLen(void) { return _max; } /** Clears message content */ - void clear(void) { len = 0; memset(data, 0, _max); } + void clear(void) { len = 0; memset(data, 0, _max); } /** Inserter operator: Appends data (value) to Manchester message */ template<class T> ManchesterMsg &operator<<(const T val) { if(len + sizeof(T) <= _max) { - *reinterpret_cast <T*> (&data[len]) = val; + *reinterpret_cast < T * > (&data[len]) = val; len += sizeof(T); } @@ -85,10 +78,10 @@ /** Inserter operator: Appends string of char to Manchester message */ ManchesterMsg &operator<<(const char* str) { - unsigned char strLen = strlen(str); + size_t strLen = strlen(str); if(len + strLen + 1 <= _max) { memcpy(data + len, (char*)str, strLen); - len += strLen; + len += strLen; data[len++] = '\0'; } @@ -105,7 +98,7 @@ template<class T> ManchesterMsg &operator>>(T& val) { if(sizeof(T) <= len) { - val = *reinterpret_cast <T*> (&data[0]); + val = *reinterpret_cast < T * > (&data[0]); len -= sizeof(T); memcpy(data, data + sizeof(T), len); } @@ -117,10 +110,11 @@ #endif return *this; } + /** Extractor operator: Extracts string of char from CAN message */ ManchesterMsg &operator>>(char* str) { - unsigned char strLen = strlen(data); + size_t strLen = strlen(data); if(strLen <= len) { memcpy(str, data, strLen + 1); len -= (strLen + 1);