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:
- 8:c1b5893191fe
- Parent:
- 7:afd0ee36dcd1
diff -r afd0ee36dcd1 -r c1b5893191fe ManchesterMsg.h --- a/ManchesterMsg.h Sun Sep 03 09:04:18 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,132 +0,0 @@ -/* - ****************************************************************************** - * @file ManchesterMsg.h - * @author Zoltan Hudak - * @version - * @date 2017-May-16 - * @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 -{ - size_t _max; // Max length (capacity) of data field in bytes - -public: - char* data; // Data field - size_t len; // Length of data in bytes - - /** Creates empty message of specified capacity. - */ - ManchesterMsg(size_t 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) - */ - size_t maxLen(void) { return _max; } - - /** Clears message content - */ - void clear(void) { len = 0; memset(data, 0, _max); } - - /** Inserter operator: Appends data (value) to 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 array of char to message - */ - ManchesterMsg &operator<<(const char* str) { - size_t 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 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 array of char from message - */ - ManchesterMsg &operator>>(char* str) { - size_t 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