Skip to content

Commit dbfa275

Browse files
committed
Calibration for ESP32 joysticks.
1 parent fc1a950 commit dbfa275

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_WirelessController.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "Common/NintendoSwitch/NintendoSwitch_Protocol_ESP32.h"
1111
#include "NintendoSwitch_SerialPABotBase_Controller.h"
1212

13+
//#include <iostream>
14+
//using std::cout;
15+
//using std::endl;
16+
1317
namespace PokemonAutomation{
1418
namespace NintendoSwitch{
1519

@@ -40,6 +44,37 @@ class SerialPABotBase_WirelessController : public SerialPABotBase_Controller{
4044

4145

4246
protected:
47+
void encode_joystick(uint8_t data[3], uint8_t x, uint8_t y){
48+
const uint16_t min = 1897;
49+
const uint16_t max = 320;
50+
51+
const double SHIFT = 2048 - min;
52+
const double RATIO = (min - max) / 127.;
53+
54+
double dx = x - 128.;
55+
double dy = y - 128.;
56+
57+
dx *= RATIO;
58+
dy *= RATIO;
59+
60+
if (dx != 0){
61+
dx += dx >= 0 ? SHIFT : -SHIFT;
62+
}
63+
if (dy != 0){
64+
dy += dy >= 0 ? SHIFT : -SHIFT;
65+
}
66+
67+
uint16_t wx = (uint16_t)(2048 + dx + 0.5);
68+
uint16_t wy = (uint16_t)(2048 - dy + 0.5);
69+
70+
// wx = 320;
71+
// cout << "wx = " << wx << endl;
72+
73+
data[0] = (uint8_t)wx;
74+
data[1] = (uint8_t)(wx >> 8 | wy << 4);
75+
data[2] = (uint8_t)(wy >> 4);
76+
}
77+
4378
void issue_report(
4479
const Cancellable* cancellable,
4580
const ESP32Report0x30& report,

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_WirelessJoycon.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,8 @@ void SerialPABotBase_WirelessJoycon::push_state_left_joycon(const Cancellable* c
137137
.button3 = 0,
138138
.button4 = 0,
139139
.button5 = 0,
140-
.leftstick_x_lo = 0x00,
141-
.leftstick_x_hi = 0x08,
142-
.leftstick_y = 0x80,
143-
.rightstick_x_lo = 0x00,
144-
.rightstick_x_hi = 0x08,
145-
.rightstick_y = 0x80,
140+
.left_joystick = {0x00, 0x08, 0x80},
141+
.right_joystick = {0x00, 0x08, 0x80},
146142
.vibrator = 0x00,
147143
.gyro = {},
148144
};
@@ -190,9 +186,7 @@ void SerialPABotBase_WirelessJoycon::push_state_left_joycon(const Cancellable* c
190186

191187
// Left Stick
192188
if (m_left_joystick.is_busy()){
193-
report.leftstick_x_lo = (m_left_joystick.x << 4) & 0xf0;
194-
report.leftstick_x_hi = (m_left_joystick.x & 0xf0) >> 4;
195-
report.leftstick_y = 255 - m_left_joystick.y;
189+
encode_joystick(report.left_joystick, m_left_joystick.x, m_left_joystick.y);
196190
}
197191

198192
issue_report(cancellable, report, duration);
@@ -207,12 +201,8 @@ void SerialPABotBase_WirelessJoycon::push_state_right_joycon(const Cancellable*
207201
.button3 = 0,
208202
.button4 = 0,
209203
.button5 = 0,
210-
.leftstick_x_lo = 0x00,
211-
.leftstick_x_hi = 0x08,
212-
.leftstick_y = 0x80,
213-
.rightstick_x_lo = 0x00,
214-
.rightstick_x_hi = 0x08,
215-
.rightstick_y = 0x80,
204+
.left_joystick = {0x00, 0x08, 0x80},
205+
.right_joystick = {0x00, 0x08, 0x80},
216206
.vibrator = 0x00,
217207
.gyro = {},
218208
};
@@ -257,9 +247,7 @@ void SerialPABotBase_WirelessJoycon::push_state_right_joycon(const Cancellable*
257247

258248
// Right Stick
259249
if (m_right_joystick.is_busy()){
260-
report.rightstick_x_lo = (m_right_joystick.x << 4) & 0xf0;
261-
report.rightstick_x_hi = (m_right_joystick.x & 0xf0) >> 4;
262-
report.rightstick_y = 255 - m_right_joystick.y;
250+
encode_joystick(report.right_joystick, m_right_joystick.x, m_right_joystick.y);
263251
}
264252

265253
issue_report(cancellable, report, duration);

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_WirelessProController.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ void SerialPABotBase_WirelessProController::push_state(const Cancellable* cancel
4343
.button3 = 0,
4444
.button4 = 0,
4545
.button5 = 0,
46-
.leftstick_x_lo = 0x00,
47-
.leftstick_x_hi = 0x08,
48-
.leftstick_y = 0x80,
49-
.rightstick_x_lo = 0x00,
50-
.rightstick_x_hi = 0x08,
51-
.rightstick_y = 0x80,
46+
.left_joystick = {0x00, 0x08, 0x80},
47+
.right_joystick = {0x00, 0x08, 0x80},
5248
.vibrator = 0x00,
5349
.gyro = {},
5450
};
@@ -102,16 +98,12 @@ void SerialPABotBase_WirelessProController::push_state(const Cancellable* cancel
10298

10399
// Left Stick
104100
if (m_left_joystick.is_busy()){
105-
report.leftstick_x_lo = (m_left_joystick.x << 4) & 0xf0;
106-
report.leftstick_x_hi = (m_left_joystick.x & 0xf0) >> 4;
107-
report.leftstick_y = 255 - m_left_joystick.y;
101+
encode_joystick(report.left_joystick, m_left_joystick.x, m_left_joystick.y);
108102
}
109103

110104
// Right Stick
111105
if (m_right_joystick.is_busy()){
112-
report.rightstick_x_lo = (m_right_joystick.x << 4) & 0xf0;
113-
report.rightstick_x_hi = (m_right_joystick.x & 0xf0) >> 4;
114-
report.rightstick_y = 255 - m_right_joystick.y;
106+
encode_joystick(report.right_joystick, m_right_joystick.x, m_right_joystick.y);
115107
}
116108

117109
issue_report(cancellable, report, duration);

0 commit comments

Comments
 (0)