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.
ManchesterMsg.h
- Committer:
- hudakz
- Date:
- 2017-05-17
- Revision:
- 0:d5c75b0e5708
- Child:
- 2:de778df5892c
File content as of revision 0:d5c75b0e5708:
/* ****************************************************************************** * @file ManchesterMsg.h * @author Zoltan Hudak * @version * @date 16-May-2017 * @brief Message container ****************************************************************************** * @attention * * <h2><center>© COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com> * * All rights reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 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 //#define DEBUG 1 /** ManchesterMsg class */ class ManchesterMsg { unsigned char _max; // Max length (capacity) of data field in bytes public: char* data; // Data field unsigned char 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(void) { delete[] data; } /** Copy constructor. */ ManchesterMsg(const ManchesterMsg& msg) { len = msg.len; memcpy(data, msg.data, msg.len); } /** Returns message maximum length (capacity) */ unsigned char maxLen(void) { return _max; } /** Clears message content */ 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; len += sizeof(T); } #if DEBUG else { printf("Error: Cannot append data. Exceeding max data length!\r\n"); } #endif return *this; } /** Inserter operator: Appends string of char to Manchester message */ ManchesterMsg &operator<<(const char* str) { unsigned char strLen = strlen(str); if(len + strLen + 1 <= _max) { memcpy(data + len, (char*)str, strLen); len += strLen; data[len++] = '\0'; } #if DEBUG else { printf("Error: Cannot append data. Exceeding max data length!\r\n"); } #endif return *this; } /** Extractor operator: Extracts data (value) from CAN message */ template<class T> ManchesterMsg &operator>>(T& val) { if(sizeof(T) <= len) { val = *reinterpret_cast <T*> (&data[0]); len -= sizeof(T); memcpy(data, data + sizeof(T), len); } #if DEBUG else { printf("Error: Cannot extract data. Exceeding data length!\r\n"); } #endif return *this; } /** Extractor operator: Extracts string of char from CAN message */ ManchesterMsg &operator>>(char* str) { unsigned char strLen = strlen(data); if(strLen <= len) { memcpy(str, data, strLen + 1); len -= (strLen + 1); memcpy(data, data + strLen + 1, len); } #if DEBUG else { printf("Error: Cannot extract data. Exceeding data length!\r\n"); } #endif return *this; } }; #endif // MANCHESTERMSG_H