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.
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