Skip to content

Commit a3ed2b7

Browse files
committed
Minor refactor for firmware.
1 parent 694ea91 commit a3ed2b7

13 files changed

+194
-60
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Nintendo Switch 2 - Wired Controller State
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <string.h>
8+
#include "NintendoSwitch2_WiredController_State.h"
9+
10+
11+
const pa_NintendoSwitch2_WiredController_State pa_NintendoSwitch2_WiredController_State_NEUTRAL_STATE = {
12+
.buttons0 = 0,
13+
.buttons1 = 0,
14+
.dpad_byte = 8,
15+
.left_joystick_x = 128,
16+
.left_joystick_y = 128,
17+
.right_joystick_x = 128,
18+
.right_joystick_y = 128,
19+
};
20+
21+
bool pa_NintendoSwitch2_WiredController_State_equals(
22+
const pa_NintendoSwitch2_WiredController_State* state0,
23+
const pa_NintendoSwitch2_WiredController_State* state1
24+
){
25+
return memcmp(state0, state1, sizeof(pa_NintendoSwitch2_WiredController_State)) == 0;
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Nintendo Switch 2 - Wired Controller State
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_NintendoSwitch2_WiredController_State_H
8+
#define PokemonAutomation_NintendoSwitch2_WiredController_State_H
9+
10+
#include <stdbool.h>
11+
#include <stdint.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
18+
19+
typedef struct{
20+
uint8_t buttons0;
21+
uint8_t buttons1;
22+
uint8_t dpad_byte;
23+
uint8_t left_joystick_x;
24+
uint8_t left_joystick_y;
25+
uint8_t right_joystick_x;
26+
uint8_t right_joystick_y;
27+
} pa_NintendoSwitch2_WiredController_State;
28+
29+
extern const pa_NintendoSwitch2_WiredController_State pa_NintendoSwitch2_WiredController_State_NEUTRAL_STATE;
30+
31+
bool pa_NintendoSwitch2_WiredController_State_equals(
32+
const pa_NintendoSwitch2_WiredController_State* state0,
33+
const pa_NintendoSwitch2_WiredController_State* state1
34+
);
35+
36+
37+
38+
#ifdef __cplusplus
39+
}
40+
#endif
41+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Nintendo Switch - Wireless Controller State
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
8+
#include <string.h>
9+
#include "NintendoSwitch_WirelessController_State.h"
10+
11+
12+
const pa_NintendoSwitch_WirelessController_State0x30 pa_NintendoSwitch_WirelessController_State0x30_NEUTRAL_STATE = {
13+
.buttons = {
14+
.button3 = 0,
15+
.button4 = 0,
16+
.button5 = 0,
17+
.left_joystick = {0x00, 0x08, 0x80},
18+
.right_joystick = {0x00, 0x08, 0x80},
19+
.vibrator = 0x80,
20+
},
21+
.gyro = {},
22+
};
23+
24+
bool pa_NintendoSwitch_WirelessController_State0x30_equals(
25+
const pa_NintendoSwitch_WirelessController_State0x30* state0,
26+
const pa_NintendoSwitch_WirelessController_State0x30* state1
27+
){
28+
return memcmp(state0, state1, sizeof(pa_NintendoSwitch_WirelessController_State0x30)) == 0;
29+
}
30+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Nintendo Switch - Wireless Controller State
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_NintendoSwitch_WirelessController_State_H
8+
#define PokemonAutomation_NintendoSwitch_WirelessController_State_H
9+
10+
#include <stdbool.h>
11+
#include <stdint.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
18+
19+
20+
typedef struct{
21+
uint8_t button3;
22+
uint8_t button4;
23+
uint8_t button5;
24+
uint8_t left_joystick[3];
25+
uint8_t right_joystick[3];
26+
uint8_t vibrator;
27+
} pa_NintendoSwitch_WirelessController_State0x30_Buttons;
28+
29+
typedef struct{
30+
int16_t accel_x;
31+
int16_t accel_y;
32+
int16_t accel_z;
33+
int16_t rotation_x;
34+
int16_t rotation_y;
35+
int16_t rotation_z;
36+
} pa_NintendoSwitch_WirelessController_State0x30_Gyro;
37+
38+
typedef struct{
39+
pa_NintendoSwitch_WirelessController_State0x30_Gyro time0;
40+
pa_NintendoSwitch_WirelessController_State0x30_Gyro time1;
41+
pa_NintendoSwitch_WirelessController_State0x30_Gyro time2;
42+
} pa_NintendoSwitch_WirelessController_State0x30_GyroX3;
43+
44+
typedef struct{
45+
pa_NintendoSwitch_WirelessController_State0x30_Buttons buttons;
46+
pa_NintendoSwitch_WirelessController_State0x30_GyroX3 gyro;
47+
} pa_NintendoSwitch_WirelessController_State0x30;
48+
49+
50+
51+
52+
extern const pa_NintendoSwitch_WirelessController_State0x30 pa_NintendoSwitch_WirelessController_State0x30_NEUTRAL_STATE;
53+
54+
bool pa_NintendoSwitch_WirelessController_State0x30_equals(
55+
const pa_NintendoSwitch_WirelessController_State0x30* state0,
56+
const pa_NintendoSwitch_WirelessController_State0x30* state1
57+
);
58+
59+
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif
64+
#endif

Common/Cpp/Concurrency/ComputationThreadPoolCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define PokemonAutomation_ComputationThreadPoolCore_H
1818

1919
#include <functional>
20-
#include <list>
20+
//#include <list>
2121
#include <deque>
2222
#include <thread>
2323
#include "Common/Cpp/CpuUtilization/CpuUtilization.h"

Common/SerialPABotBase/SerialPABotBase_Messages_ESP32.h

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef PokemonAutomation_SerialPABotBase_Messages_ESP32_H
88
#define PokemonAutomation_SerialPABotBase_Messages_ESP32_H
99

10+
#include "../Controllers/NintendoSwitch_WirelessController_State.h"
1011
#include "SerialPABotBase_Protocol.h"
1112

1213
#if _WIN32
@@ -32,30 +33,6 @@ typedef struct{
3233
uint8_t right_grip[3];
3334
} PABB_NintendoSwitch_ControllerColors;
3435

35-
typedef struct{
36-
uint8_t button3;
37-
uint8_t button4;
38-
uint8_t button5;
39-
uint8_t left_joystick[3];
40-
uint8_t right_joystick[3];
41-
uint8_t vibrator;
42-
} PABB_NintendoSwitch_ButtonState;
43-
44-
typedef struct{
45-
int16_t accel_x;
46-
int16_t accel_y;
47-
int16_t accel_z;
48-
int16_t rotation_x;
49-
int16_t rotation_y;
50-
int16_t rotation_z;
51-
} PABB_NintendoSwitch_GyroState;
52-
53-
typedef struct{
54-
PABB_NintendoSwitch_GyroState time0;
55-
PABB_NintendoSwitch_GyroState time1;
56-
PABB_NintendoSwitch_GyroState time2;
57-
} PABB_NintendoSwitch_GyroStateX3;
58-
5936

6037

6138

@@ -82,16 +59,15 @@ typedef struct{
8259
typedef struct{
8360
seqnum_t seqnum;
8461
uint16_t milliseconds;
85-
PABB_NintendoSwitch_ButtonState buttons;
62+
pa_NintendoSwitch_WirelessController_State0x30_Buttons buttons;
8663
} PABB_PACK pabb_Message_ESP32_CommandButtonState;
8764

8865

8966
#define PABB_MSG_ESP32_CONTROLLER_STATE_FULL 0xa1
9067
typedef struct{
9168
seqnum_t seqnum;
9269
uint16_t milliseconds;
93-
PABB_NintendoSwitch_ButtonState buttons;
94-
PABB_NintendoSwitch_GyroStateX3 gyro;
70+
pa_NintendoSwitch_WirelessController_State0x30 state;
9571
} PABB_PACK pabb_Message_ESP32_CommandFullState;
9672

9773

Common/SerialPABotBase/SerialPABotBase_Messages_NS2_WiredController.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef PokemonAutomation_SerialPABotBase_Messages_NS2_WiredController_H
88
#define PokemonAutomation_SerialPABotBase_Messages_NS2_WiredController_H
99

10+
#include "../Controllers/NintendoSwitch2_WiredController_State.h"
1011
#include "SerialPABotBase_Protocol.h"
1112

1213
#if _WIN32
@@ -26,19 +27,10 @@ namespace SerialPABotBase{
2627

2728

2829
#define PABB_MSG_REPORT_NS2_WIRED_CONTROLLER 0x90
29-
typedef struct{
30-
uint8_t buttons0;
31-
uint8_t buttons1;
32-
uint8_t dpad_byte;
33-
uint8_t left_joystick_x;
34-
uint8_t left_joystick_y;
35-
uint8_t right_joystick_x;
36-
uint8_t right_joystick_y;
37-
} pabb_Report_NS2_WiredController;
3830
typedef struct{
3931
seqnum_t seqnum;
4032
uint16_t milliseconds;
41-
pabb_Report_NS2_WiredController report;
33+
pa_NintendoSwitch2_WiredController_State report;
4234
} PABB_PACK pabb_Message_Report_NS2_WiredController;
4335

4436

SerialPrograms/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ file(GLOB MAIN_SOURCES
6262
../Common/CRC32.cpp
6363
../Common/CRC32.h
6464
../Common/Compiler.h
65+
../Common/Controllers/NintendoSwitch2_WiredController_State.h
66+
../Common/Controllers/NintendoSwitch_WirelessController_State.h
6567
../Common/Cpp/AbstractLogger.h
6668
../Common/Cpp/BitmapConversion.cpp
6769
../Common/Cpp/BitmapConversion.h

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_ESP32.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ class MessageControllerWriteSpi : public BotBaseRequest{
8484
class MessageControllerStateButtons : public BotBaseRequest{
8585
public:
8686
pabb_Message_ESP32_CommandButtonState params;
87-
MessageControllerStateButtons(uint16_t milliseconds, const PABB_NintendoSwitch_ButtonState& state)
87+
MessageControllerStateButtons(
88+
uint16_t milliseconds,
89+
const pa_NintendoSwitch_WirelessController_State0x30_Buttons& state
90+
)
8891
: BotBaseRequest(true)
8992
{
9093
params.seqnum = 0;
@@ -100,15 +103,15 @@ class MessageControllerStateFull : public BotBaseRequest{
100103
pabb_Message_ESP32_CommandFullState params;
101104
MessageControllerStateFull(
102105
uint16_t milliseconds,
103-
const PABB_NintendoSwitch_ButtonState& buttons,
104-
const PABB_NintendoSwitch_GyroStateX3& gyro
106+
const pa_NintendoSwitch_WirelessController_State0x30_Buttons& buttons,
107+
const pa_NintendoSwitch_WirelessController_State0x30_GyroX3& gyro
105108
)
106109
: BotBaseRequest(true)
107110
{
108111
params.seqnum = 0;
109112
params.milliseconds = milliseconds;
110-
params.buttons = buttons;
111-
params.gyro = gyro;
113+
params.state.buttons = buttons;
114+
params.state.gyro = gyro;
112115
}
113116
virtual BotBaseMessage message() const override{
114117
return BotBaseMessage(PABB_MSG_ESP32_CONTROLLER_STATE_FULL, params);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void SerialPABotBase_WirelessController::stop(){
6464

6565

6666

67-
Button SerialPABotBase_WirelessController::populate_report_buttons(PABB_NintendoSwitch_ButtonState& buttons){
67+
Button SerialPABotBase_WirelessController::populate_report_buttons(pa_NintendoSwitch_WirelessController_State0x30_Buttons& buttons){
6868
// https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/bluetooth_hid_notes.md
6969

7070
Button all_buttons = BUTTON_NONE;
@@ -108,7 +108,7 @@ Button SerialPABotBase_WirelessController::populate_report_buttons(PABB_Nintendo
108108
}
109109
return all_buttons;
110110
}
111-
bool SerialPABotBase_WirelessController::populate_report_gyro(PABB_NintendoSwitch_GyroState& gyro){
111+
bool SerialPABotBase_WirelessController::populate_report_gyro(pa_NintendoSwitch_WirelessController_State0x30_Gyro& gyro){
112112
bool gyro_active = false;
113113
{
114114
if (m_accel_x.is_busy()){
@@ -143,7 +143,7 @@ bool SerialPABotBase_WirelessController::populate_report_gyro(PABB_NintendoSwitc
143143
void SerialPABotBase_WirelessController::issue_report(
144144
const Cancellable* cancellable,
145145
WallDuration duration,
146-
const PABB_NintendoSwitch_ButtonState& buttons
146+
const pa_NintendoSwitch_WirelessController_State0x30_Buttons& buttons
147147
){
148148
// Release the state lock since we are no longer touching state.
149149
// This loop can block indefinitely if the command queue is full.
@@ -170,15 +170,15 @@ void SerialPABotBase_WirelessController::issue_report(
170170
void SerialPABotBase_WirelessController::issue_report(
171171
const Cancellable* cancellable,
172172
WallDuration duration,
173-
const PABB_NintendoSwitch_ButtonState& buttons,
174-
const PABB_NintendoSwitch_GyroState& gyro
173+
const pa_NintendoSwitch_WirelessController_State0x30_Buttons& buttons,
174+
const pa_NintendoSwitch_WirelessController_State0x30_Gyro& gyro
175175
){
176176
// Release the state lock since we are no longer touching state.
177177
// This loop can block indefinitely if the command queue is full.
178178
ReverseLockGuard<std::mutex> lg(m_state_lock);
179179

180180
// TODO: For now we duplicate the gyro data to all 3 5ms segments.
181-
PABB_NintendoSwitch_GyroStateX3 gyro3{
181+
pa_NintendoSwitch_WirelessController_State0x30_GyroX3 gyro3{
182182
gyro, gyro, gyro
183183
};
184184

0 commit comments

Comments
 (0)