Skip to content

Commit 13c1bb5

Browse files
committed
Remove change timeout because disonnect detection is working
1 parent a1d728a commit 13c1bb5

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ Add the following lines to `/boot/config.txt`
145145

146146
### xpadneo
147147
1. `sudo apt install dkms linux-headers`
148-
2. `git clone https://github.com/atar-axis/xpadneo.git`
149-
3. `cd xpadneo`
150-
4. `sudo ./install.sh`
148+
2. Reboot
149+
3. `git clone https://github.com/atar-axis/xpadneo.git`
150+
4. `cd xpadneo`
151+
5. `sudo ./install.sh`
151152

152153
## Bare Minimum To Pair XBox Series Controller
153154

src/XBoxController/XBoxController.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ bool XBoxController::Initialize() {
8181
SDL_GameControllerEventState(SDL_ENABLE);
8282
m_pJoystick = candidateJoystick;
8383
return true;
84+
} else if (num_axes == 7 && num_buttons == 15 && num_hats == 1) {
85+
std::cout << "Connected to new XBox Series controller\n";
86+
SDL_GameControllerEventState(SDL_ENABLE);
87+
m_pJoystick = candidateJoystick;
88+
return true;
8489
} else {
8590
std::cout << "VendorID: " << SDL_GameControllerGetVendor(m_pJoystick)
8691
<< ", ProductID: " << SDL_GameControllerGetProduct(m_pJoystick) << '\n';
@@ -101,15 +106,21 @@ void XBoxController::Deinitialize() {
101106
}
102107

103108
std::optional<XBoxController::ControllerState> XBoxController::CurrentState() {
109+
SDL_JoystickUpdate();
110+
104111
// Try getting controller if it was lost
105112
if (m_pJoystick == nullptr) {
106113
Initialize();
107114
}
108115

109116
if (m_pJoystick != nullptr) {
117+
if (!SDL_GameControllerGetAttached(m_pJoystick)) {
118+
std::cout << "Game controller gone\n";
119+
Deinitialize();
120+
return std::nullopt;
121+
}
110122
SDL_Event event;
111123
while (SDL_PollEvent(&event)) {
112-
m_lastHeartbeat = std::chrono::steady_clock::now();
113124
switch (event.type) {
114125
case SDL_QUIT:
115126
Deinitialize();
@@ -121,6 +132,12 @@ std::optional<XBoxController::ControllerState> XBoxController::CurrentState() {
121132
std::cout << "DEVICEADDED cdevice.which = " << event.cdevice.which << std::endl;
122133
break;
123134

135+
case SDL_CONTROLLERDEVICEREMOVED:
136+
std::cout << "DEVICEREMOVED" << std::endl;
137+
Deinitialize();
138+
return std::nullopt;
139+
break;
140+
124141
// If a controller button is pressed
125142
case SDL_CONTROLLERBUTTONDOWN:
126143
// Looking for the button that was pressed
@@ -260,10 +277,6 @@ std::optional<XBoxController::ControllerState> XBoxController::CurrentState() {
260277
}
261278

262279
UpdateVibration();
263-
if (std::chrono::steady_clock::now() - m_lastHeartbeat > std::chrono::milliseconds(2000)) {
264-
printf("Timeout!\n");
265-
return std::nullopt;
266-
}
267280

268281
return m_latestState;
269282
}

src/XBoxController/XBoxController.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ class XBoxController {
8989
SDL_GameController* m_pJoystick;
9090
ControllerState m_latestState;
9191

92-
std::chrono::steady_clock::time_point m_lastHeartbeat;
93-
9492
ArgosLib::VibrationModel m_vibrationModel;
9593

9694
constexpr static auto JsIntToPct = [](int jsVal) { return static_cast<double>(jsVal) / 32767.0; };

0 commit comments

Comments
 (0)