-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProController.Test.cpp
More file actions
218 lines (178 loc) · 5.72 KB
/
Copy pathProController.Test.cpp
File metadata and controls
218 lines (178 loc) · 5.72 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
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
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <conio.h>
#endif
#include <memory>
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <thread>
#include <mutex>
#include "hidio.h"
#include "ProController.h"
#define LF "\n\x1b[2K"
bool enable_imu_sensor = true;
bool enable_packet_dump = true;
static void SetupConsoleWindow();
static void WaitEscapeOrCtrlC();
static void logger_output(const std::string& text)
{
static std::mutex console;
std::lock_guard lock(console);
std::cout << text + LF;
}
static size_t count_of_lines(std::string_view s)
{
return std::count(s.begin(), s.end(), '\n');
}
using namespace ProControllerHid;
struct ControllerTest
{
std::unique_ptr<ProController> controller{};
InputStatus input{};
RawInputStatus raw{};
ControllerTest(std::unique_ptr<ProController> connected)
: controller(std::move(connected))
{
// input status
controller->SetInputStatusCallback([this](const InputStatus& input) { this->input = input; });
// input to rumble/led output
controller->SetRawInputStatusCallback([this](const RawInputStatus& raw)
{
this->raw = raw;
// Rumble output
auto lf = static_cast<uint8_t>(raw.LeftStick.AxisX >> 4);
auto la = static_cast<uint8_t>(raw.LeftStick.AxisY >> 4);
auto hf = static_cast<uint8_t>(raw.RightStick.AxisX >> 4);
auto ha = static_cast<uint8_t>(raw.RightStick.AxisY >> 4);
this->controller->SetRumbleBasic(
raw.Buttons.LZButton ? la : 0,
raw.Buttons.RZButton ? la : 0,
raw.Buttons.LButton ? ha : 0,
raw.Buttons.RButton ? ha : 0,
lf, lf,
hf, hf);
// Player status led output
auto led = static_cast<uint8_t>(
raw.Buttons.AButton << 0 |
raw.Buttons.BButton << 1 |
raw.Buttons.XButton << 2 |
raw.Buttons.YButton << 3);
this->controller->SetPlayerLed(led);
});
}
[[nodiscard]] std::string GetStatus() const
{
std::ostringstream output;
output << " Input >";
output << " Clock=" << std::fixed << std::setprecision(3) << std::chrono::duration_cast<std::chrono::duration<double>>(raw.Timestamp.time_since_epoch()).count();
output << " Report=" + DumpInputStatusAsString(raw) << LF;
output << " - Parsed > " << InputStatusAsString(raw) << LF;
output << " - Parsed > " << ImuSensorStatusAsString(raw) << LF;
output << " - Corrected > " << InputStatusAsString(input) << LF;
output << " - Corrected > " << ImuSensorStatusAsString(input) << LF;
output << " Output > Vibration Test (L/R Button): ";
output << " lf/la=" << std::to_string(raw.LeftStick.AxisX >> 4) << "/" << std::to_string(raw.LeftStick.AxisY >> 4);
output << " hf/ha=" << std::to_string(raw.RightStick.AxisX >> 4) << "/" << std::to_string(raw.RightStick.AxisY >> 4);
output << LF;
output << " Output > LED Test (ABXY Button): ";
output << (raw.Buttons.AButton ? "*" : "_");
output << (raw.Buttons.BButton ? "*" : "_");
output << (raw.Buttons.XButton ? "*" : "_");
output << (raw.Buttons.YButton ? "*" : "_");
output << LF;
return output.str();
}
};
int main()
{
SetupConsoleWindow();
std::vector<std::unique_ptr<ControllerTest>> controllers;
std::cout << "Finding all ProControllers..." LF;
auto devices = hidio::enumerate_devices(ProController::DeviceVendorID, ProController::DeviceProductID);
for (size_t i = 0; i < devices.size(); i++)
{
const auto& device = devices[i];
std::cout << "Device found: index = " << i << std::endl;
std::cout << " Path: " << device.device_path << std::endl;
std::wcout << L" Manufacture: " << device.manufacture_string << std::endl;
std::wcout << L" Product: " << device.product_string << std::endl;
std::cout << " Opening device..." << std::endl;
if (auto controller = ProController::Connect(
device.device_path.c_str(),
enable_imu_sensor,
[i](const char* text) { logger_output("device[" + std::to_string(i) + "]: " + text); },
enable_packet_dump))
{
controllers.emplace_back(std::make_unique<ControllerTest>(std::move(controller)));
}
else
{
std::cout << " Failed to open controller..." LF;
}
}
if (controllers.empty())
{
std::cout << "No Pro Controllers found." LF;
return 1;
}
std::cout << std::endl;
// Start
std::cout << "Controller starting..." LF LF;
std::atomic_flag thread_running{};
std::thread thread = std::thread([&]
{
thread_running.test_and_set();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
while (thread_running.test_and_set())
{
std::ostringstream output;
output << LF;
for (size_t i = 0; i < controllers.size(); i++)
{
output << "---- Controller " << i << LF << controllers[i]->GetStatus();
}
for (size_t i = 0, count = count_of_lines(output.str()); i < count + 1; i++)
{
output << "\x1b[1A"; // cursor move up
}
logger_output(output.str());
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
});
WaitEscapeOrCtrlC();
thread_running.clear();
thread.join();
std::cout << "Closing controllers..." << LF;
controllers.clear();
std::cout << "Closed." << LF;
}
static void SetupConsoleWindow()
{
#ifdef _WIN32
DWORD mode = {};
GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
info.dwSize.X = std::max(info.dwSize.X, SHORT{120});
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), info.dwSize);
#endif
}
static void WaitEscapeOrCtrlC()
{
#ifdef _WIN32
std::cout << "Press Ctrl+C or ESCAPE to exit.\n" << std::endl;
while (int ch = _getch())
{
if (ch == 3 || ch == 27) { break; }
}
#else
std::cout << "Press Enter to exit.\n" << std::endl;
(void)getchar();
#endif
}