forked from JanekOstendorf/THOMAS-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoystickControl.cpp
More file actions
106 lines (78 loc) · 2.35 KB
/
JoystickControl.cpp
File metadata and controls
106 lines (78 loc) · 2.35 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
99
100
101
102
103
104
105
106
/*
-- JOYSTICK-CONTROL-KLASSE :: IMPLEMENTIERUNG --
*/
/* INCLUDES */
// Klassenheader
#include "JoystickControl.h"
using namespace THOMAS;
// THOMASException-Klasse
#include "THOMASException.h"
// C-String-Funktionen
// Hier ist u.a. die memcpy()-Funktion definiert.
#include <cstring>
/* FUNKTIONEN */
JoystickControl::JoystickControl()
{
// Verbindung zum Joystick herstellen
_joystick = new Joystick(&JoystickControl::ComputeJoystickDataWrapper, static_cast<void *>(this));
// Joystick-Daten abrufen
_joystickAxisCount = _joystick->GetAxisCount();
_joystickButtonCount = _joystick->GetButtonCount();
// Sendepuffer-Länge berechnen
_joystickDataSendBuffLength = 1 + sizeof(short) * _joystickAxisCount + _joystickButtonCount;
// Server-Verbindungs-Objekt erstellen
_serverCon = new TCPClient();
}
JoystickControl::~JoystickControl()
{
// Ggf. Steuerung beenden
if(_running)
Stop();
// Joystick-Verbindung löschen
delete _joystick;
// Server-Verbindung löschen
delete _serverCon;
}
void JoystickControl::Run(const char* ip)
{
// Läuft die Steuerung bereits?
if(_running)
throw THOMASException("Fehler: Die Steuerung läuft bereits!");
// Mit Server verbinden
_serverCon->Connect(ip, 4242);
// Joystick-Daten an Server übermitteln (JOYSTICK_HEADER)
BYTE sendBuff[4] = {3, 1, _joystickAxisCount, _joystickButtonCount};
_serverCon->Send(sendBuff, 4);
// Steuerung läuft
_running = true;
// Joystick-Empfangsmodus starten
_joystick->BeginReceive();
}
void JoystickControl::Stop()
{
// Läuft die Steuerung?
if(!_running)
throw THOMASException("Fehler: Die Steuerung läuft nicht!");
// Steuerung läuft nicht mehr
_running = false;
// Joystick-Empfangsmodus beenden
_joystick->EndReceive();
// Server-Verbindung beenden
_serverCon->Disconnect();
}
void JoystickControl::ComputeJoystickData(short *axis, BYTE *buttons)
{
// Paketlänge ermitteln
int dataLength = 1 + _joystickDataSendBuffLength;
// Sende-Puffer erstellen
BYTE buff[dataLength];
// Paketheader (Größe des Datenpaketes)
buff[0] = _joystickDataSendBuffLength;
// Kommandobyte (JOYSTICK_DATA)
buff[1] = 2;
// Joystick-Daten in Puffer kopieren
memcpy(&buff[2], axis, sizeof(short) * _joystickAxisCount);
memcpy(&buff[2 + sizeof(short) * _joystickAxisCount], buttons, _joystickButtonCount);
// Daten an Server senden
_serverCon->Send(buff, dataLength);
}