-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.cpp
More file actions
executable file
·99 lines (74 loc) · 3.07 KB
/
commands.cpp
File metadata and controls
executable file
·99 lines (74 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "commands.h"
//const CommandDescriptor CommandDescriptor::descriptors[]={CommandDescriptor(UPDOWN,2),CommandDescriptor(MOVETO,9),CommandDescriptor(RESET,1)};
UpDown::UpDown(const ByteArray& data){
upDown = data[1];
}
Reset::Reset(const ByteArray& data){
(data);
};
MoveTo::MoveTo(const ByteArray& data){
x =*reinterpret_cast<const int32_t*>(data.cdata()+1); //+current - смещение на текущую позицию (пропустить success и up)
y =*reinterpret_cast<const int32_t*>(data.cdata()+5);
};
Status::Status(bool success, bool up, int32_t x, int32_t y){
m_success = success;
m_up = up;
m_x = x;
m_y = y;
};
ByteArray Status::serialize(){
ByteArray data(11); // 11 - размер массива, status = <byte cmdtype=STATUS><byte результат команды><byte updown><int32 x><int32 y>.
int current = 0;
data[current++] = STATUS;
data[current++] = m_success;
data[current++] = m_up;
char* xBytes = reinterpret_cast<char*>(&m_x); //Берем адрес переменной x и преобразуем его к типу char*, а затем присваиваем его переменной xBytes
for (unsigned int i=0; i<sizeof(int32_t);i++){
data[current++]=xBytes[i];
}
char* yBytes = reinterpret_cast<char*>(&m_y);
for (unsigned int i=0; i<sizeof(int32_t);i++){
data[current++]=yBytes[i];
};
return data;
};
Config::Config(int32_t maxX, int32_t maxY):Status(true,0,maxX,maxY),m_maxX(maxX), m_maxY(maxY){
}
ByteArray Config::serialize(){
ByteArray data(19); // 9 - размер массива, .
int current = 0;
data[current++] = CONFIG;
data[current++] = m_success;
data[current++] = m_up;
char* bytes = reinterpret_cast<char*>(&m_x); //Берем адрес переменной x и преобразуем его к типу char*, а затем присваиваем его переменной xBytes
for (unsigned int i=0; i<sizeof(int32_t);i++){
data[current++]=bytes[i];
}
bytes = reinterpret_cast<char*>(&m_y);
for (unsigned int i=0; i<sizeof(int32_t);i++){
data[current++]=bytes[i];
};
bytes = reinterpret_cast<char*>(&m_maxX); //Берем адрес переменной x и преобразуем его к типу char*, а затем присваиваем его переменной xBytes
for (unsigned int i=0; i<sizeof(int32_t);i++){
data[current++]=bytes[i];
}
bytes = reinterpret_cast<char*>(&m_maxY);
for (unsigned int i=0; i<sizeof(int32_t);i++){
data[current++]=bytes[i];
};
return data;
};
Commands Status::getType(){return STATUS;}
Commands Config::getType(){return CONFIG;}
IncomingCommand* commandFactory(const Commands type, const ByteArray& data){
switch(type){
case UPDOWN:
return new UpDown(data);
case MOVETO:
return new MoveTo(data);
case RESET:
return new Reset(data);
default:
return 0;
};
}