Skip to content

Commit dc2451f

Browse files
authored
fix 3rd party classic controllers that don't send calibration data. (#100)
1 parent b51c517 commit dc2451f

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

wiiuse/classic.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "classic.h"
4848
#include "io.h"
4949

50+
static void fix_bad_calibration_values(struct joystick_t* js, short right_stick);
5051
static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now);
5152

5253
/**
@@ -88,16 +89,14 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, ubyt
8889
* Sometimes the data returned here is not correct.
8990
* This might happen because the wiimote is lagging
9091
* behind our initialization sequence.
91-
* To fix this just request the handshake again.
92+
* We won't request the handshake again - instead, we'll just use default calibration values.
9293
*
9394
* Other times it's just the first 16 bytes are 0xFF,
9495
* but since the next 16 bytes are the same, just use
9596
* those.
9697
*/
9798
if (data[offset + 16] == 0xFF) {
98-
/* get the calibration data again */
99-
WIIUSE_DEBUG("Classic controller handshake appears invalid, trying again.");
100-
wiiuse_read_data(wm, data, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN, wiiuse_handshake_expansion);
99+
// don't get the calibration data again
101100
} else
102101
offset += 16;
103102
}
@@ -121,6 +120,9 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, ubyt
121120
cc->rjs.max.y = data[9 + offset] / 8 == 0 ? 32 : data[9 + offset] / 8;
122121
cc->rjs.min.y = data[10 + offset] / 8;
123122
cc->rjs.center.y = data[11 + offset] / 8 == 0 ? 16 : data[11 + offset] / 8;
123+
124+
fix_bad_calibration_values(&cc->ljs, 0);
125+
fix_bad_calibration_values(&cc->rjs, 1);
124126
}
125127
/* handshake done */
126128
wm->event = WIIUSE_CLASSIC_CTRL_INSERTED;
@@ -200,6 +202,18 @@ void classic_ctrl_event(struct classic_ctrl_t* cc, ubyte* msg) {
200202
#endif
201203
}
202204

205+
static void fix_bad_calibration_values(struct joystick_t* js, short right_stick) {
206+
if ((js->min.x >= js->center.x) || (js->max.x <= js->center.x)) {
207+
js->min.x = 0;
208+
js->max.x = right_stick ? 32 : 64;
209+
js->center.x = right_stick ? 16 : 32;
210+
}
211+
if ((js->min.y >= js->center.y) || (js->max.y <= js->center.y)) {
212+
js->min.y = 0;
213+
js->max.y = right_stick ? 32 : 64;
214+
js->center.y = right_stick ? 16 : 32;
215+
}
216+
}
203217

204218
/**
205219
* @brief Find what buttons are pressed.

0 commit comments

Comments
 (0)