summaryrefslogtreecommitdiff
path: root/src/input_common/udp/client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/udp/client.cpp')
-rw-r--r--src/input_common/udp/client.cpp140
1 files changed, 63 insertions, 77 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index befa4c86d..da5227058 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -59,8 +59,7 @@ public:
59 void StartReceive() { 59 void StartReceive() {
60 socket.async_receive_from( 60 socket.async_receive_from(
61 boost::asio::buffer(receive_buffer), receive_endpoint, 61 boost::asio::buffer(receive_buffer), receive_endpoint,
62 [this](const boost::system::error_code& error, std::size_t bytes_transferred) 62 [this](const boost::system::error_code& error, std::size_t bytes_transferred) {
63 {
64 HandleReceive(error, bytes_transferred); 63 HandleReceive(error, bytes_transferred);
65 }); 64 });
66 } 65 }
@@ -212,27 +211,21 @@ void Client::StartCommunication(const std::string& host, u16 port, u8 pad_index,
212void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 211void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id,
213 std::function<void()> success_callback, 212 std::function<void()> success_callback,
214 std::function<void()> failure_callback) { 213 std::function<void()> failure_callback) {
215 std::thread([=] 214 std::thread([=] {
216 { 215 Common::Event success_event;
217 Common::Event success_event; 216 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
218 SocketCallback callback{[](Response::Version version) 217 [&](Response::PadData data) { success_event.Set(); }};
219 { 218 Socket socket{host, port, pad_index, client_id, std::move(callback)};
220 }, 219 std::thread worker_thread{SocketLoop, &socket};
221 [](Response::PortInfo info) 220 bool result = success_event.WaitFor(std::chrono::seconds(8));
222 { 221 socket.Stop();
223 }, 222 worker_thread.join();
224 [&](Response::PadData data) { success_event.Set(); }}; 223 if (result) {
225 Socket socket{host, port, pad_index, client_id, std::move(callback)}; 224 success_callback();
226 std::thread worker_thread{SocketLoop, &socket}; 225 } else {
227 bool result = success_event.WaitFor(std::chrono::seconds(8)); 226 failure_callback();
228 socket.Stop(); 227 }
229 worker_thread.join(); 228 })
230 if (result) {
231 success_callback();
232 } else {
233 failure_callback();
234 }
235 })
236 .detach(); 229 .detach();
237} 230}
238 231
@@ -241,60 +234,53 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
241 std::function<void(Status)> status_callback, 234 std::function<void(Status)> status_callback,
242 std::function<void(u16, u16, u16, u16)> data_callback) { 235 std::function<void(u16, u16, u16, u16)> data_callback) {
243 236
244 std::thread([=] 237 std::thread([=] {
245 { 238 constexpr u16 CALIBRATION_THRESHOLD = 100;
246 constexpr u16 CALIBRATION_THRESHOLD = 100; 239
247 240 u16 min_x{UINT16_MAX};
248 u16 min_x{UINT16_MAX}; 241 u16 min_y{UINT16_MAX};
249 u16 min_y{UINT16_MAX}; 242 u16 max_x{};
250 u16 max_x{}; 243 u16 max_y{};
251 u16 max_y{}; 244
252 245 Status current_status{Status::Initialized};
253 Status current_status{Status::Initialized}; 246 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
254 SocketCallback callback{[](Response::Version version) 247 [&](Response::PadData data) {
255 { 248 if (current_status == Status::Initialized) {
256 }, 249 // Receiving data means the communication is ready now
257 [](Response::PortInfo info) 250 current_status = Status::Ready;
258 { 251 status_callback(current_status);
259 }, 252 }
260 [&](Response::PadData data) 253 if (!data.touch_1.is_active) {
261 { 254 return;
262 if (current_status == Status::Initialized) { 255 }
263 // Receiving data means the communication is ready now 256 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x,
264 current_status = Status::Ready; 257 data.touch_1.y);
265 status_callback(current_status); 258 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x));
266 } 259 min_y = std::min(min_y, static_cast<u16>(data.touch_1.y));
267 if (!data.touch_1.is_active) { 260 if (current_status == Status::Ready) {
268 return; 261 // First touch - min data (min_x/min_y)
269 } 262 current_status = Status::Stage1Completed;
270 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 263 status_callback(current_status);
271 data.touch_1.y); 264 }
272 min_x = std::min(min_x, static_cast<u16>(data.touch_1.x)); 265 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD &&
273 min_y = std::min(min_y, static_cast<u16>(data.touch_1.y)); 266 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) {
274 if (current_status == Status::Ready) { 267 // Set the current position as max value and finishes
275 // First touch - min data (min_x/min_y) 268 // configuration
276 current_status = Status::Stage1Completed; 269 max_x = data.touch_1.x;
277 status_callback(current_status); 270 max_y = data.touch_1.y;
278 } 271 current_status = Status::Completed;
279 if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && 272 data_callback(min_x, min_y, max_x, max_y);
280 data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { 273 status_callback(current_status);
281 // Set the current position as max value and finishes 274
282 // configuration 275 complete_event.Set();
283 max_x = data.touch_1.x; 276 }
284 max_y = data.touch_1.y; 277 }};
285 current_status = Status::Completed; 278 Socket socket{host, port, pad_index, client_id, std::move(callback)};
286 data_callback(min_x, min_y, max_x, max_y); 279 std::thread worker_thread{SocketLoop, &socket};
287 status_callback(current_status); 280 complete_event.Wait();
288 281 socket.Stop();
289 complete_event.Set(); 282 worker_thread.join();
290 } 283 })
291 }};
292 Socket socket{host, port, pad_index, client_id, std::move(callback)};
293 std::thread worker_thread{SocketLoop, &socket};
294 complete_event.Wait();
295 socket.Stop();
296 worker_thread.join();
297 })
298 .detach(); 284 .detach();
299} 285}
300 286