Skip to content

Commit 4765fc8

Browse files
committed
More joystick calibration tools.
1 parent 2b24ddb commit 4765fc8

File tree

5 files changed

+129
-26
lines changed

5 files changed

+129
-26
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ file(GLOB MAIN_SOURCES
127127
../Common/Cpp/Options/BooleanCheckBoxOption.h
128128
../Common/Cpp/Options/ButtonOption.cpp
129129
../Common/Cpp/Options/ButtonOption.h
130+
../Common/Cpp/Options/ColorOption.cpp
131+
../Common/Cpp/Options/ColorOption.h
130132
../Common/Cpp/Options/ConfigOption.cpp
131133
../Common/Cpp/Options/ConfigOption.h
132134
../Common/Cpp/Options/DateOption.cpp
@@ -212,6 +214,8 @@ file(GLOB MAIN_SOURCES
212214
../Common/Qt/Options/BooleanCheckBoxWidget.h
213215
../Common/Qt/Options/ButtonWidget.cpp
214216
../Common/Qt/Options/ButtonWidget.h
217+
../Common/Qt/Options/ColorWidget.cpp
218+
../Common/Qt/Options/ColorWidget.h
215219
../Common/Qt/Options/ConfigWidget.cpp
216220
../Common/Qt/Options/ConfigWidget.h
217221
../Common/Qt/Options/DateWidget.cpp
@@ -634,6 +638,7 @@ file(GLOB MAIN_SOURCES
634638
Source/Controllers/ControllerTypes.h
635639
Source/Controllers/ControllerTypeStrings.cpp
636640
Source/Controllers/ControllerTypeStrings.h
641+
Source/Controllers/JoystickTools.h
637642
Source/Controllers/KeyboardInput/GlobalQtKeyMap.cpp
638643
Source/Controllers/KeyboardInput/GlobalQtKeyMap.h
639644
Source/Controllers/KeyboardInput/KeyboardInput.cpp
@@ -829,6 +834,8 @@ file(GLOB MAIN_SOURCES
829834
Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h
830835
Source/NintendoSwitch/Commands/NintendoSwitch_Messages_Device.h
831836
Source/NintendoSwitch/Commands/NintendoSwitch_Messages_PushButtons.h
837+
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerSettings.cpp
838+
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerSettings.h
832839
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerState.cpp
833840
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerState.h
834841
Source/NintendoSwitch/Controllers/NintendoSwitch_KeyboardMapping.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ SOURCES += \
103103
../Common/Cpp/Options/BatchOption.cpp \
104104
../Common/Cpp/Options/BooleanCheckBoxOption.cpp \
105105
../Common/Cpp/Options/ButtonOption.cpp \
106+
../Common/Cpp/Options/ColorOption.cpp \
106107
../Common/Cpp/Options/ConfigOption.cpp \
107108
../Common/Cpp/Options/DateOption.cpp \
108109
../Common/Cpp/Options/EditableTableOption.cpp \
@@ -137,6 +138,7 @@ SOURCES += \
137138
../Common/Qt/Options/BatchWidget.cpp \
138139
../Common/Qt/Options/BooleanCheckBoxWidget.cpp \
139140
../Common/Qt/Options/ButtonWidget.cpp \
141+
../Common/Qt/Options/ColorWidget.cpp \
140142
../Common/Qt/Options/ConfigWidget.cpp \
141143
../Common/Qt/Options/DateWidget.cpp \
142144
../Common/Qt/Options/EditableTableWidget.cpp \
@@ -1199,6 +1201,7 @@ HEADERS += \
11991201
../Common/Cpp/Options/BatchOption.h \
12001202
../Common/Cpp/Options/BooleanCheckBoxOption.h \
12011203
../Common/Cpp/Options/ButtonOption.h \
1204+
../Common/Cpp/Options/ColorOption.h \
12021205
../Common/Cpp/Options/ConfigOption.h \
12031206
../Common/Cpp/Options/DateOption.h \
12041207
../Common/Cpp/Options/EditableTableOption.h \
@@ -1248,6 +1251,7 @@ HEADERS += \
12481251
../Common/Qt/Options/BatchWidget.h \
12491252
../Common/Qt/Options/BooleanCheckBoxWidget.h \
12501253
../Common/Qt/Options/ButtonWidget.h \
1254+
../Common/Qt/Options/ColorWidget.h \
12511255
../Common/Qt/Options/ConfigWidget.h \
12521256
../Common/Qt/Options/DateWidget.h \
12531257
../Common/Qt/Options/EditableTableWidget.h \
@@ -1480,6 +1484,7 @@ HEADERS += \
14801484
Source/Controllers/ControllerDescriptor.h \
14811485
Source/Controllers/ControllerTypeStrings.h \
14821486
Source/Controllers/ControllerTypes.h \
1487+
Source/Controllers/JoystickTools.h \
14831488
Source/Controllers/KeyboardInput/GlobalQtKeyMap.h \
14841489
Source/Controllers/KeyboardInput/KeyboardInput.h \
14851490
Source/Controllers/KeyboardInput/KeyboardStateTracker.h \
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Joystick Tools
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Controllers_JoystickTools_H
8+
#define PokemonAutomation_Controllers_JoystickTools_H
9+
10+
#include <stdint.h>
11+
12+
namespace PokemonAutomation{
13+
namespace JoystickTools{
14+
15+
16+
17+
inline double linear_u8_to_float(uint8_t x){
18+
if (x <= 128){
19+
return (x - 128) * (1. / 128);
20+
}else{
21+
return (x - 128) * (1. / 127);
22+
}
23+
}
24+
inline uint8_t linear_float_to_u8(double f){
25+
if (f <= 0){
26+
if (f <= -1){
27+
return 0;
28+
}
29+
f = f * 128 + 128;
30+
return (uint8_t)(f + 0.5);
31+
}else{
32+
if (f >= 1){
33+
return 255;
34+
}
35+
f = f * 127 + 128;
36+
return (uint8_t)(f + 0.5);
37+
}
38+
}
39+
inline int16_t linear_float_to_s16(double f){
40+
if (f <= 0){
41+
if (f <= -1){
42+
return -32768;
43+
}
44+
f = f * 32768;
45+
return (int16_t)(f + 0.5);
46+
}else{
47+
if (f >= 1){
48+
return 32767;
49+
}
50+
f = f * 32767;
51+
return (int16_t)(f + 0.5);
52+
}
53+
}
54+
inline uint16_t linear_float_to_u16(double f){
55+
if (f <= 0){
56+
if (f <= -1){
57+
return 0;
58+
}
59+
f = f * 32768 + 32768;
60+
return (uint16_t)(f + 0.5);
61+
}else{
62+
if (f >= 1){
63+
return 65535;
64+
}
65+
f = f * 32767 + 32768;
66+
return (uint16_t)(f + 0.5);
67+
}
68+
}
69+
inline uint16_t linear_float_to_u12(double lo, double hi, double f){
70+
if (f == 0){
71+
return 2048;
72+
}else if (f <= 0){
73+
if (f <= -1){
74+
return 0;
75+
}
76+
f = f * (hi - lo) - lo;
77+
f = f * 2048 + 2048;
78+
return (uint16_t)(f + 0.5);
79+
}else{
80+
if (f >= 1){
81+
return 4095;
82+
}
83+
f = f * (hi - lo) + lo;
84+
f = f * 2047 + 2048;
85+
return (uint16_t)(f + 0.5);
86+
}
87+
}
88+
89+
90+
91+
}
92+
}
93+
#endif

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_NintendoSwitch_SerialPABotBase_WirelessController_H
99

1010
#include "Common/NintendoSwitch/NintendoSwitch_Protocol_ESP32.h"
11+
#include "Controllers/JoystickTools.h"
1112
#include "NintendoSwitch_SerialPABotBase_Controller.h"
1213

1314
//#include <iostream>
@@ -60,26 +61,15 @@ class SerialPABotBase_WirelessController : public SerialPABotBase_Controller{
6061
const uint16_t min = 1897;
6162
const uint16_t max = 320;
6263

63-
const double SHIFT = 2048 - min;
64-
const double RATIO = (min - max) / 127.;
64+
const double lo = 1 - min / 2048.;
65+
const double hi = 1 - max / 2048.;
6566

66-
double dx = x - 128.;
67-
double dy = y - 128.;
67+
double fx = JoystickTools::linear_u8_to_float(x);
68+
double fy = -JoystickTools::linear_u8_to_float(y);
69+
// cout << "fx = " << fx << ", fy = " << fy << endl;
6870

69-
dx *= RATIO;
70-
dy *= RATIO;
71-
72-
if (dx != 0){
73-
dx += dx >= 0 ? SHIFT : -SHIFT;
74-
}
75-
if (dy != 0){
76-
dy += dy >= 0 ? SHIFT : -SHIFT;
77-
}
78-
79-
uint16_t wx = (uint16_t)(2048 + dx + 0.5);
80-
uint16_t wy = (uint16_t)(2048 - dy + 0.5);
81-
82-
// wx = 320;
71+
uint16_t wx = JoystickTools::linear_float_to_u12(lo, hi, fx);
72+
uint16_t wy = JoystickTools::linear_float_to_u12(lo, hi, fy);
8373
// cout << "wx = " << wx << ", wy = " << wy << endl;
8474

8575
data[0] = (uint8_t)wx;

SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_ProController.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
*/
66

77
#include "Common/Cpp/Exceptions.h"
8+
#include "Common/Cpp/Concurrency/SpinPause.h"
89
#include "CommonFramework/GlobalSettingsPanel.h"
910
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
10-
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
11-
#include "Controllers/ControllerTypeStrings.h"
11+
#include "Controllers/JoystickTools.h"
1212
#include "SysbotBase_ProController.h"
1313

14-
#include "Common/Cpp/Concurrency/SpinPause.h"
15-
1614
//#include <iostream>
1715
//using std::cout;
1816
//using std::endl;
@@ -273,19 +271,29 @@ void ProController_SysbotBase::send_diff(
273271
if (old_state.left_x != new_state.left_x ||
274272
old_state.left_y != new_state.left_y
275273
){
274+
double fx = JoystickTools::linear_u8_to_float(new_state.left_x);
275+
double fy = -JoystickTools::linear_u8_to_float(new_state.left_y);
276+
// cout << "fx = " << fx << ", fy = " << fy << endl;
277+
int16_t ix = JoystickTools::linear_float_to_s16(fx);
278+
int16_t iy = JoystickTools::linear_float_to_s16(fy);
279+
// cout << "ix = " << ix << ", iy = " << iy << endl;
276280
message += "setStick LEFT ";
277-
message += std::to_string(((uint16_t)new_state.left_x - 128) << 8);
281+
message += std::to_string(ix);
278282
message += " ";
279-
message += std::to_string(((uint16_t)128 - new_state.left_y) << 8);
283+
message += std::to_string(iy);
280284
message += "\n";
281285
}
282286
if (old_state.right_x != new_state.right_x ||
283287
old_state.right_y != new_state.right_y
284288
){
289+
double fx = JoystickTools::linear_u8_to_float(new_state.right_x);
290+
double fy = -JoystickTools::linear_u8_to_float(new_state.right_y);
291+
int16_t ix = JoystickTools::linear_float_to_s16(fx);
292+
int16_t iy = JoystickTools::linear_float_to_s16(fy);
285293
message += "setStick RIGHT ";
286-
message += std::to_string(((uint16_t)new_state.right_x - 128) << 8);
294+
message += std::to_string(ix);
287295
message += " ";
288-
message += std::to_string(((uint16_t)128 - new_state.right_y) << 8);
296+
message += std::to_string(iy);
289297
message += "\n";
290298
}
291299

0 commit comments

Comments
 (0)