Container for sending and receiving messages using the Manchester and ManchesterUART libararies.

Dependents:   Manchester_Transmitter Manchester_Receiver ManchesterUART_Transmitter ManchesterUART_Receiver

Container for sending and receiving messages using the Manchester and ManchesterUART libararies.

Committer:
hudakz
Date:
Mon Jul 30 09:38:57 2018 +0000
Revision:
1:001100cee73a
Parent:
0:260cfaa4e8b5
Updated.

Who changed what in which revision?

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