40 lines
955 B
C++
40 lines
955 B
C++
#include "TextMsg.h"
|
|
|
|
namespace RoboidControl {
|
|
|
|
TextMsg::TextMsg(const char* text, unsigned char textLength) {
|
|
this->text = text;
|
|
this->textLength = textLength;
|
|
}
|
|
|
|
TextMsg::TextMsg(char* buffer) {
|
|
unsigned char ix = 1; // first byte is msg id
|
|
|
|
this->textLength = buffer[ix++];
|
|
char* text = new char[this->textLength + 1];
|
|
for (int i = 0; i < this->textLength; i++)
|
|
text[i] = buffer[ix++];
|
|
text[this->textLength] = '\0';
|
|
this->text = text;
|
|
}
|
|
|
|
TextMsg::~TextMsg() {}
|
|
|
|
unsigned char TextMsg::Serialize(char* buffer) {
|
|
if (this->textLength == 0 || this->text == nullptr)
|
|
return 0;
|
|
|
|
#if defined(DEBUG)
|
|
std::cout << "Send TextMsg " << (int)this->textLength << " " << this->text << std::endl;
|
|
#endif
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->textLength;
|
|
for (int nameIx = 0; nameIx < this->textLength; nameIx++)
|
|
buffer[ix++] = this->text[nameIx];
|
|
|
|
return ix;
|
|
}
|
|
|
|
} // namespace RoboidControl
|