-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.h
162 lines (125 loc) · 3.37 KB
/
packet.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef PACKET_H
#define PACKET_H
#include <memory>
#include <QString>
#include <QStringList>
#include <QMetaType>
#define ERROR_PACKET 'E'
#define MEASUREMENT_RESULT_PACKET 'M'
#define CURRENT_POSITION_PACKET 'P'
#define ENDSTOP_STATE_PACKET 'S'
#define HOMED_PACKET 'H'
#define MEASURE_PACKET 'm'
#define LASER_STATE_PACKET 'l'
#define VOLTAGE_PACKET 'v'
#define THRESHOLD_PACKET 't'
#define PHOTOMULTIPLIER_STATE_PACKET 'a'
#define TARGET_POSITION_PACKET 'g'
class Packet
{
public:
static std::shared_ptr<Packet> parsePacket(QString);
virtual ~Packet() = default;
char type;
protected:
Packet(char type);
};
Q_DECLARE_METATYPE(std::shared_ptr<Packet>);
class SendablePacket : public Packet
{
public:
virtual QString serialize() = 0;
QString appendType(QString);
protected:
SendablePacket(char type);
};
Q_DECLARE_METATYPE(std::shared_ptr<SendablePacket>);
class ErrorPacket : public Packet {
public:
ErrorPacket(QString string) : Packet(ERROR_PACKET) {
error = string.right(string.size()-2);
}
QString error;
};
class MeasuremetResultPacket : public Packet {
public:
MeasuremetResultPacket(QString string) : Packet(MEASUREMENT_RESULT_PACKET) {
value = string.split(" ")[1].toInt();
}
unsigned long value;
};
class CurrentPositionPacket : public Packet {
public:
CurrentPositionPacket(QString string) : Packet(CURRENT_POSITION_PACKET) {
position = string.split(" ")[1].toInt();
}
unsigned long position;
};
class EndstopStatePacket : public Packet {
public:
EndstopStatePacket(QString string) : Packet(ENDSTOP_STATE_PACKET) {
state = (string.split(" ")[1].toInt() == 1);
}
bool state;
};
class TargetPositionPacket : public SendablePacket {
public:
TargetPositionPacket(int position) : SendablePacket(TARGET_POSITION_PACKET) {
this->position = position;
}
QString serialize() override {
return appendType(QString::number(position));
}
int position;
};
class VoltagePacket : public SendablePacket {
public:
VoltagePacket(int voltage) : SendablePacket(VOLTAGE_PACKET) {
this->voltage = voltage;
}
QString serialize() override {
return appendType(QString::number(voltage));
}
int voltage;
};
class ThresholdPacket : public SendablePacket {
public:
ThresholdPacket(int threshold) : SendablePacket(THRESHOLD_PACKET) {
this->threshold = threshold;
}
QString serialize() override {
return appendType(QString::number(threshold));
}
int threshold;
};
class LaserPacket : public SendablePacket {
public:
LaserPacket(bool state) : SendablePacket(LASER_STATE_PACKET) {
this->state = state;
}
QString serialize() override {
return appendType(state ? "1" : "0");
}
bool state;
};
class AmplifierPacket : public SendablePacket {
public:
AmplifierPacket(bool state) : SendablePacket(PHOTOMULTIPLIER_STATE_PACKET) {
this->state = state;
}
QString serialize() override {
return appendType(state ? "1" : "0");
}
bool state;
};
class MeasurePacket : public SendablePacket {
public:
MeasurePacket(int miliseconds) : SendablePacket(MEASURE_PACKET) {
this->miliseconds = miliseconds;
}
QString serialize() override {
return appendType(QString::number(miliseconds));
}
int miliseconds;
};
#endif // PACKET_H