-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cpp
174 lines (151 loc) · 3.78 KB
/
controller.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
#include "controller.hpp"
Controller::Controller(QObject *parent, QString initialIp, quint16 initalPort)
: QObject(parent)
{
m_socket = new QUdpSocket(this);
m_ip = QHostAddress(initialIp);
m_port = initalPort;
m_sequenceNumber = 0;
}
void Controller::setIp(QString givenIp)
{
m_ip = QHostAddress(givenIp);
}
void Controller::setPort(int givenPort)
{
m_port = (quint16)givenPort;
}
unsigned long Controller::nextSequenceNumber()
{
return m_sequenceNumber++;
}
void Controller::sendCommand(QByteArray body)
{
int bodyLength = body.size();
unsigned long seqNum = nextSequenceNumber();
QByteArray command;
// Command type
command.append("\x01\x00", 2);
// Body length (one byte at a time)
command.append((bodyLength >> 8) & 0xFF);
command.append(bodyLength);
// Sequence number (one byte at a time)
command.append((seqNum >> 24) & 0xFF);
command.append((seqNum >> 16) & 0xFF);
command.append((seqNum >> 8) & 0xFF);
command.append(seqNum);
command.append(body);
m_socket->writeDatagram(command, m_ip, m_port);
}
void Controller::powerOn()
{
sendCommand(QByteArray("\x81\x01\x04\x00\x02\xFF", 6));
qDebug() << "Powered on";
}
void Controller::powerOff()
{
sendCommand(QByteArray("\x81\x01\x04\x00\x03\xFF", 6));
qDebug() << "Powered off";
}
void Controller::moveStop()
{
sendCommand(QByteArray("\x81\x01\x06\x01\x00\x00\x03\x03\xFF", 9));
}
void Controller::moveUp(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x03\x01\xFF");
sendCommand(command);
}
void Controller::moveUpRight(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x02\x01\xFF");
sendCommand(command);
}
void Controller::moveRight(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x02\x03\xFF");
sendCommand(command);
}
void Controller::moveDownRight(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x02\x02\xFF");
sendCommand(command);
}
void Controller::moveDown(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x03\x02\xFF");
sendCommand(command);
}
void Controller::moveDownLeft(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x01\x02\xFF");
sendCommand(command);
}
void Controller::moveLeft(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x01\x03\xFF");
sendCommand(command);
}
void Controller::moveUpLeft(unsigned short speed)
{
QByteArray command("\x81\x01\x06\x01");
command.append(speed);
command.append(speed);
command.append("\x01\x01\xFF");
sendCommand(command);
}
void Controller::zoomStop()
{
sendCommand(QByteArray("\x81\x01\x04\x07\x00\xFF", 6));
}
void Controller::zoomIn(unsigned short speed)
{
QByteArray command("\x81\x01\x04\x07");
speed += 32; // Zoom in speed is formatted in hex as "2[speed]"
command.append(speed);
command.append("\xFF");
sendCommand(command);
}
void Controller::zoomOut(unsigned short speed)
{
QByteArray command("\x81\x01\x04\x07");
speed += 48; // Zoom out speed is formatted in hex as "3[speed]"
command.append(speed);
command.append("\xFF");
sendCommand(command);
}
void Controller::presetSet(unsigned short preset)
{
QByteArray command("\x81\x01\x04\x3F\x01");
command.append(preset);
command.append("\xFF");
sendCommand(command);
}
void Controller::presetRecall(unsigned short preset)
{
QByteArray command("\x81\x01\x04\x3F\x02");
command.append(preset);
command.append("\xFF");
sendCommand(command);
}