-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_physical.cpp
223 lines (202 loc) · 8.7 KB
/
udp_physical.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* PS Move API - An interface for the PS Move Motion Controller
* Copyright (c) 2011 Thomas Perl <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
#include "move_udp_server.h"
#include "udp_physical.h"
#include <cstring>
#ifndef WIN32
#include <unistd.h>
#endif
// Formats move button presses into a simple 8 bit integer.
int format_buttons(unsigned int currButtons)
{
int btnsToReturn = 0;
if(currButtons & Btn_CROSS) btnsToReturn += (1 << 7);
if(currButtons & Btn_SQUARE) btnsToReturn += (1 << 6);
if(currButtons & Btn_TRIANGLE) btnsToReturn += (1 << 5);
if(currButtons & Btn_CIRCLE) btnsToReturn += (1 << 4);
if(currButtons & Btn_MOVE) btnsToReturn += (1 << 3);
if(currButtons & Btn_START) btnsToReturn += (1 << 2);
if(currButtons & Btn_SELECT) btnsToReturn += (1 << 1);
if(currButtons & Btn_PS) btnsToReturn += (1);
return btnsToReturn;
}
UDP_Physical::UDP_Physical(PSENDTHREADDATA data,
std::vector<MoveState*> & stateList) :
Thread()
{
_physicalData = data;
_stateList = stateList;
}
UDP_Physical::~UDP_Physical()
{
}
void UDP_Physical::run()
{
// ----- physicalData variables -----
int totalConnectedMoves = _physicalData->totalConnectedMoves;
PSMove** controllers = _physicalData->controllers;
// Sending address/socket is defined by udp_recv.cpp once a client connects.
SOCKADDR_IN* sendAddress = _physicalData->sendAddress;
SOCKET* udpSocket = _physicalData->udpSocket;
int* okayToSend = _physicalData->okayToSend;
int* trackingEnabled = _physicalData->trackingEnabled;
// ControllerData can be changed by 'udp_recv.cpp' messages and also altered here.
// Protected by the controllerMutex.
ControllerData* controllerData = _physicalData->controllerData;
int currButtons = 0;
int analogVal = 0;
int orientationEnabled = 0;
int currPoll = 0;
int msgNo = 0;
int c;
float ax, ay, az, gx, gy, gz, mx, my, mz, qx, qy, qz, qw;
int server_length = sizeof(struct sockaddr_in);
char sendMes[512];
PSMove* move;
while(1)
{
for(c = 0; c < totalConnectedMoves; c++)
{
move = controllers[c];
// Need to poll for new Move data. Returns 0 if unsucessful poll.
currPoll = psmove_poll(move);
if(currPoll)
{
// Controller mutex
controllerMutex->lock();
// Set the move light to the tracker set value.
if(controllerData[c].trackerLight)
{
controllerData[c].trackerLight = 0;
controllerData[c].changeLight = 0;
if(*trackingEnabled)
{
controllerData[c].r = controllerData[c].tr;
controllerData[c].g = controllerData[c].tg;
controllerData[c].b = controllerData[c].tb;
}
psmove_set_leds(move, controllerData[c].r,
controllerData[c].g, controllerData[c].b);
}
// Set the move light to a user defined light (not recommended if tracking)
else if(controllerData[c].changeLight)
{
controllerData[c].changeLight = 0;
psmove_set_leds(move, controllerData[c].r,
controllerData[c].g, controllerData[c].b);
}
// Rumble the controller. Only runs for a certain amount of ticks, client needs to send multiple packets to keep it going.
if(controllerData[c].rumbleTimeout > 0)
{
if(controllerData[c].rumbleTimeout == RUMBLE_TIMEOUT)
{
psmove_set_rumble(move, controllerData[c].rumble);
}
controllerData[c].rumbleTimeout -= 1;
}
// Set the rumble on the controller to 0. (timeout goes to -1 to stop needlessly setting rumble to 0)
else
{
if(controllerData[c].rumbleTimeout == 0)
{
psmove_set_rumble(move, 0);
controllerData[c].rumbleTimeout = -1;
}
}
// Reset the orientation (allows user to do so in application)
if(controllerData[c].resetOrientation)
{
controllerData[c].resetOrientation = 0;
psmove_reset_orientation(move);
printf("\nController %d has been calibrated.\n >", c);
}
controllerMutex->unlock();
// Controller mutex end
// Read values from the controller.
analogVal = psmove_get_trigger(move);
unsigned int rawButtons = psmove_get_buttons(move);
currButtons = format_buttons(rawButtons);
psmove_get_accelerometer_frame(move, Frame_SecondHalf, &ax, &ay,
&az);
psmove_get_gyroscope_frame(move, Frame_SecondHalf, &gx, &gy,
&gz);
psmove_get_magnetometer_vector(move, &mx, &my, &mz);
psmove_update_leds(move);
// Check for orientation and get new values
if(psmove_has_orientation(move))
{
orientationEnabled = 1;
psmove_get_orientation(move, &qw, &qx, &qy, &qz);
}
else
{
orientationEnabled = 0;
}
_stateList[c]->lock->lock();
_stateList[c]->buttons = rawButtons;
_stateList[c]->qx = qx;
_stateList[c]->qy = qy;
_stateList[c]->qz = qz;
_stateList[c]->qw = qw;
_stateList[c]->trigger = ((float)analogVal) / 255.0f;
_stateList[c]->lock->unlock();
if(*okayToSend == 1)
{
// Stream data for controller to the client.
sprintf(sendMes,
"a %d %d %d %d %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %d %.3f %.3f %.3f %.3f %d %d %d",
msgNo, c, currButtons, analogVal, ax, ay, az, gx,
gy, gz, mx, my, mz, orientationEnabled, qw, qx, qy,
qz, controllerData[c].r, controllerData[c].g,
controllerData[c].b);
//printf("%s\n", sendMes);
sendto(*udpSocket, sendMes, strlen(sendMes), 0,
(SOCKADDR*)sendAddress, server_length);
//sprintf(sendMes, "%.3f %.3f %.3f", mx, my, mz);
//printf("%s\n", sendMes);
}
}
}
_quitMutex->lock();
if(_quit)
{
_quitMutex->unlock();
break;
}
_quitMutex->unlock();
#ifdef WIN32
Sleep(10);
#else
usleep(10000);
#endif
if(*okayToSend)
{
msgNo++;
}
}
}