Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Manchester_Transmitter Manchester_Receiver ManchesterUART_Transmitter ManchesterUART_Receiver
ManchesterMsg.h
00001 /* 00002 ****************************************************************************** 00003 * @file ManchesterMsg.h 00004 * @author Zoltan Hudak 00005 * @version 00006 * @date 2017-May-16 00007 * @brief Message container 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com> 00012 * 00013 * All rights reserved. 00014 00015 This program is free software: you can redistribute it and/or modify 00016 it under the terms of the GNU General Public License as published by 00017 the Free Software Foundation, either version 3 of the License, or 00018 (at your option) any later version. 00019 00020 This program is distributed in the hope that it will be useful, 00021 but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 GNU General Public License for more details. 00024 00025 You should have received a copy of the GNU General Public License 00026 along with this program. If not, see <http://www.gnu.org/licenses/>. 00027 */ 00028 #ifndef MANCHESTERMSG_H 00029 #define MANCHESTERMSG_H 00030 00031 //#define DEBUG 1 00032 00033 /** ManchesterMsg class 00034 */ 00035 class ManchesterMsg 00036 { 00037 size_t _max; // Max length (capacity) of data field in bytes 00038 00039 public: 00040 char* data; // Data field 00041 size_t len; // Length of data in bytes 00042 00043 /** Creates empty message of specified capacity. 00044 */ 00045 ManchesterMsg(size_t max) : _max(max), data(new char[max]) { len = 0; } 00046 00047 ~ ManchesterMsg(void) { delete[] data; } 00048 00049 /** Copy constructor. 00050 */ 00051 ManchesterMsg(const ManchesterMsg& msg) { len = msg.len; memcpy(data, msg.data, msg.len); } 00052 00053 /** Returns message maximum length (capacity) 00054 */ 00055 size_t maxLen(void) { return _max; } 00056 00057 /** Clears message content 00058 */ 00059 void clear(void) { len = 0; memset(data, 0, _max); } 00060 00061 /** Inserter operator: Appends data (value) to message 00062 */ 00063 template<class T> 00064 ManchesterMsg &operator<<(const T val) { 00065 if(len + sizeof(T) <= _max) { 00066 memcpy(&data[len], &val, sizeof(T)); 00067 len += sizeof(T); 00068 } 00069 00070 #if DEBUG 00071 else { 00072 printf("Error: Cannot append data. Exceeding max data length!\r\n"); 00073 } 00074 #endif 00075 return *this; 00076 } 00077 00078 /** Inserter operator: Appends array of char to message 00079 */ 00080 ManchesterMsg &operator<<(const char* str) { 00081 size_t strLen = strlen(str); 00082 if(len + strLen + 1 <= _max) { 00083 memcpy(data + len, (char*)str, strLen); 00084 len += strLen; 00085 data[len++] = '\0'; 00086 } 00087 00088 #if DEBUG 00089 else { 00090 printf("Error: Cannot append data. Exceeding max data length!\r\n"); 00091 } 00092 #endif 00093 return *this; 00094 } 00095 00096 /** Extractor operator: Extracts data (value) from message 00097 */ 00098 template<class T> 00099 ManchesterMsg &operator>>(T& val) { 00100 if(sizeof(T) <= len) { 00101 val = *reinterpret_cast < T * > (&data[0]); 00102 len -= sizeof(T); 00103 memcpy(data, data + sizeof(T), len); 00104 } 00105 00106 #if DEBUG 00107 else { 00108 printf("Error: Cannot extract data. Exceeding data length!\r\n"); 00109 } 00110 #endif 00111 return *this; 00112 } 00113 00114 /** Extractor operator: Extracts array of char from message 00115 */ 00116 ManchesterMsg &operator>>(char* str) { 00117 size_t strLen = strlen(data); 00118 if(strLen <= len) { 00119 memcpy(str, data, strLen + 1); 00120 len -= (strLen + 1); 00121 memcpy(data, data + strLen + 1, len); 00122 } 00123 00124 #if DEBUG 00125 else { 00126 printf("Error: Cannot extract data. Exceeding data length!\r\n"); 00127 } 00128 #endif 00129 return *this; 00130 } 00131 }; 00132 #endif // MANCHESTERMSG_H 00133
Generated on Thu Jul 14 2022 05:00:10 by
