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 the Manchester.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:

/media/uploads/hudakz/manchester01.png

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.

Committer:
hudakz
Date:
Wed May 17 21:02:49 2017 +0000
Revision:
2:de778df5892c
Parent:
0:d5c75b0e5708
Child:
4:f2c392191c74
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:d5c75b0e5708 1 /*
hudakz 0:d5c75b0e5708 2 ******************************************************************************
hudakz 0:d5c75b0e5708 3 * @file ManchesterMsg.h
hudakz 0:d5c75b0e5708 4 * @author Zoltan Hudak
hudakz 2:de778df5892c 5 * @version
hudakz 0:d5c75b0e5708 6 * @date 16-May-2017
hudakz 0:d5c75b0e5708 7 * @brief Message container
hudakz 0:d5c75b0e5708 8 ******************************************************************************
hudakz 0:d5c75b0e5708 9 * @attention
hudakz 0:d5c75b0e5708 10 *
hudakz 0:d5c75b0e5708 11 * <h2><center>&copy; COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com>
hudakz 0:d5c75b0e5708 12 *
hudakz 0:d5c75b0e5708 13 * All rights reserved.
hudakz 0:d5c75b0e5708 14
hudakz 0:d5c75b0e5708 15 This program is free software: you can redistribute it and/or modify
hudakz 0:d5c75b0e5708 16 it under the terms of the GNU General Public License as published by
hudakz 0:d5c75b0e5708 17 the Free Software Foundation, either version 3 of the License, or
hudakz 0:d5c75b0e5708 18 (at your option) any later version.
hudakz 0:d5c75b0e5708 19
hudakz 0:d5c75b0e5708 20 This program is distributed in the hope that it will be useful,
hudakz 0:d5c75b0e5708 21 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:d5c75b0e5708 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:d5c75b0e5708 23 GNU General Public License for more details.
hudakz 0:d5c75b0e5708 24
hudakz 0:d5c75b0e5708 25 You should have received a copy of the GNU General Public License
hudakz 0:d5c75b0e5708 26 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:d5c75b0e5708 27 */
hudakz 0:d5c75b0e5708 28 #ifndef MANCHESTERMSG_H
hudakz 0:d5c75b0e5708 29 #define MANCHESTERMSG_H
hudakz 0:d5c75b0e5708 30
hudakz 0:d5c75b0e5708 31 //#define DEBUG 1
hudakz 0:d5c75b0e5708 32
hudakz 0:d5c75b0e5708 33 /** ManchesterMsg class
hudakz 0:d5c75b0e5708 34 */
hudakz 0:d5c75b0e5708 35 class ManchesterMsg
hudakz 0:d5c75b0e5708 36 {
hudakz 2:de778df5892c 37 size_t _max; // Max length (capacity) of data field in bytes
hudakz 0:d5c75b0e5708 38
hudakz 0:d5c75b0e5708 39 public:
hudakz 2:de778df5892c 40 char* data; // Data field
hudakz 2:de778df5892c 41 size_t len; // Length of data in bytes
hudakz 0:d5c75b0e5708 42
hudakz 0:d5c75b0e5708 43 /** Creates empty Manchester message of specified capacity.
hudakz 0:d5c75b0e5708 44 */
hudakz 2:de778df5892c 45 ManchesterMsg(size_t max) : _max(max), data(new char[max]) { len = 0; }
hudakz 0:d5c75b0e5708 46
hudakz 2:de778df5892c 47 ~ ManchesterMsg(void) { delete[] data; }
hudakz 0:d5c75b0e5708 48
hudakz 2:de778df5892c 49 /** Copy constructor.
hudakz 0:d5c75b0e5708 50 */
hudakz 2:de778df5892c 51 ManchesterMsg(const ManchesterMsg& msg) { len = msg.len; memcpy(data, msg.data, msg.len); }
hudakz 2:de778df5892c 52
hudakz 0:d5c75b0e5708 53 /** Returns message maximum length (capacity)
hudakz 0:d5c75b0e5708 54 */
hudakz 2:de778df5892c 55 size_t maxLen(void) { return _max; }
hudakz 0:d5c75b0e5708 56
hudakz 0:d5c75b0e5708 57 /** Clears message content
hudakz 0:d5c75b0e5708 58 */
hudakz 2:de778df5892c 59 void clear(void) { len = 0; memset(data, 0, _max); }
hudakz 0:d5c75b0e5708 60
hudakz 0:d5c75b0e5708 61 /** Inserter operator: Appends data (value) to Manchester message
hudakz 0:d5c75b0e5708 62 */
hudakz 0:d5c75b0e5708 63 template<class T>
hudakz 0:d5c75b0e5708 64 ManchesterMsg &operator<<(const T val) {
hudakz 0:d5c75b0e5708 65 if(len + sizeof(T) <= _max) {
hudakz 2:de778df5892c 66 *reinterpret_cast < T * > (&data[len]) = val;
hudakz 0:d5c75b0e5708 67 len += sizeof(T);
hudakz 0:d5c75b0e5708 68 }
hudakz 0:d5c75b0e5708 69
hudakz 0:d5c75b0e5708 70 #if DEBUG
hudakz 0:d5c75b0e5708 71 else {
hudakz 0:d5c75b0e5708 72 printf("Error: Cannot append data. Exceeding max data length!\r\n");
hudakz 0:d5c75b0e5708 73 }
hudakz 0:d5c75b0e5708 74 #endif
hudakz 0:d5c75b0e5708 75 return *this;
hudakz 0:d5c75b0e5708 76 }
hudakz 0:d5c75b0e5708 77
hudakz 0:d5c75b0e5708 78 /** Inserter operator: Appends string of char to Manchester message
hudakz 0:d5c75b0e5708 79 */
hudakz 0:d5c75b0e5708 80 ManchesterMsg &operator<<(const char* str) {
hudakz 2:de778df5892c 81 size_t strLen = strlen(str);
hudakz 0:d5c75b0e5708 82 if(len + strLen + 1 <= _max) {
hudakz 0:d5c75b0e5708 83 memcpy(data + len, (char*)str, strLen);
hudakz 2:de778df5892c 84 len += strLen;
hudakz 0:d5c75b0e5708 85 data[len++] = '\0';
hudakz 0:d5c75b0e5708 86 }
hudakz 0:d5c75b0e5708 87
hudakz 0:d5c75b0e5708 88 #if DEBUG
hudakz 0:d5c75b0e5708 89 else {
hudakz 0:d5c75b0e5708 90 printf("Error: Cannot append data. Exceeding max data length!\r\n");
hudakz 0:d5c75b0e5708 91 }
hudakz 0:d5c75b0e5708 92 #endif
hudakz 0:d5c75b0e5708 93 return *this;
hudakz 0:d5c75b0e5708 94 }
hudakz 0:d5c75b0e5708 95
hudakz 0:d5c75b0e5708 96 /** Extractor operator: Extracts data (value) from CAN message
hudakz 0:d5c75b0e5708 97 */
hudakz 0:d5c75b0e5708 98 template<class T>
hudakz 0:d5c75b0e5708 99 ManchesterMsg &operator>>(T& val) {
hudakz 0:d5c75b0e5708 100 if(sizeof(T) <= len) {
hudakz 2:de778df5892c 101 val = *reinterpret_cast < T * > (&data[0]);
hudakz 0:d5c75b0e5708 102 len -= sizeof(T);
hudakz 0:d5c75b0e5708 103 memcpy(data, data + sizeof(T), len);
hudakz 0:d5c75b0e5708 104 }
hudakz 0:d5c75b0e5708 105
hudakz 0:d5c75b0e5708 106 #if DEBUG
hudakz 0:d5c75b0e5708 107 else {
hudakz 0:d5c75b0e5708 108 printf("Error: Cannot extract data. Exceeding data length!\r\n");
hudakz 0:d5c75b0e5708 109 }
hudakz 0:d5c75b0e5708 110 #endif
hudakz 0:d5c75b0e5708 111 return *this;
hudakz 0:d5c75b0e5708 112 }
hudakz 2:de778df5892c 113
hudakz 0:d5c75b0e5708 114 /** Extractor operator: Extracts string of char from CAN message
hudakz 0:d5c75b0e5708 115 */
hudakz 0:d5c75b0e5708 116 ManchesterMsg &operator>>(char* str) {
hudakz 2:de778df5892c 117 size_t strLen = strlen(data);
hudakz 0:d5c75b0e5708 118 if(strLen <= len) {
hudakz 0:d5c75b0e5708 119 memcpy(str, data, strLen + 1);
hudakz 0:d5c75b0e5708 120 len -= (strLen + 1);
hudakz 0:d5c75b0e5708 121 memcpy(data, data + strLen + 1, len);
hudakz 0:d5c75b0e5708 122 }
hudakz 0:d5c75b0e5708 123
hudakz 0:d5c75b0e5708 124 #if DEBUG
hudakz 0:d5c75b0e5708 125 else {
hudakz 0:d5c75b0e5708 126 printf("Error: Cannot extract data. Exceeding data length!\r\n");
hudakz 0:d5c75b0e5708 127 }
hudakz 0:d5c75b0e5708 128 #endif
hudakz 0:d5c75b0e5708 129 return *this;
hudakz 0:d5c75b0e5708 130 }
hudakz 0:d5c75b0e5708 131 };
hudakz 0:d5c75b0e5708 132 #endif // MANCHESTERMSG_H