Skip to content

Commit 6053238

Browse files
committed
Move Switch-specific button globals into namespace. Make DpadPosition an enum.
1 parent d324fec commit 6053238

17 files changed

+148
-25
lines changed

Common/NintendoSwitch/NintendoSwitch_ControllerDefs.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,68 @@
1010
#include <stdbool.h>
1111
#include <stdint.h>
1212

13+
14+
#ifdef __cplusplus
15+
namespace PokemonAutomation{
16+
namespace NintendoSwitch{
17+
18+
// One second = 125 ticks. Thus each tick is 8 milliseconds.
19+
constexpr uint16_t TICKS_PER_SECOND = 125;
20+
21+
// Buttons
22+
using Button = uint16_t;
23+
constexpr Button BUTTON_Y = ((uint16_t)1 << 0);
24+
constexpr Button BUTTON_B = ((uint16_t)1 << 1);
25+
constexpr Button BUTTON_A = ((uint16_t)1 << 2);
26+
constexpr Button BUTTON_X = ((uint16_t)1 << 3);
27+
constexpr Button BUTTON_L = ((uint16_t)1 << 4);
28+
constexpr Button BUTTON_R = ((uint16_t)1 << 5);
29+
constexpr Button BUTTON_ZL = ((uint16_t)1 << 6);
30+
constexpr Button BUTTON_ZR = ((uint16_t)1 << 7);
31+
constexpr Button BUTTON_MINUS = ((uint16_t)1 << 8);
32+
constexpr Button BUTTON_PLUS = ((uint16_t)1 << 9);
33+
constexpr Button BUTTON_LCLICK = ((uint16_t)1 << 10);
34+
constexpr Button BUTTON_RCLICK = ((uint16_t)1 << 11);
35+
constexpr Button BUTTON_HOME = ((uint16_t)1 << 12);
36+
constexpr Button BUTTON_CAPTURE = ((uint16_t)1 << 13);
37+
38+
// Dpad
39+
enum DpadPosition : uint8_t{
40+
DPAD_UP = 0,
41+
DPAD_UP_RIGHT = 1,
42+
DPAD_RIGHT = 2,
43+
DPAD_DOWN_RIGHT = 3,
44+
DPAD_DOWN = 4,
45+
DPAD_DOWN_LEFT = 5,
46+
DPAD_LEFT = 6,
47+
DPAD_UP_LEFT = 7,
48+
DPAD_NONE = 8,
49+
};
50+
51+
#if 0
52+
using DpadPosition = uint8_t;
53+
constexpr DpadPosition DPAD_UP = 0;
54+
constexpr DpadPosition DPAD_UP_RIGHT = 1;
55+
constexpr DpadPosition DPAD_RIGHT = 2;
56+
constexpr DpadPosition DPAD_DOWN_RIGHT = 3;
57+
constexpr DpadPosition DPAD_DOWN = 4;
58+
constexpr DpadPosition DPAD_DOWN_LEFT = 5;
59+
constexpr DpadPosition DPAD_LEFT = 6;
60+
constexpr DpadPosition DPAD_UP_LEFT = 7;
61+
constexpr DpadPosition DPAD_NONE = 8;
62+
#endif
63+
64+
// Joysticks
65+
constexpr uint8_t STICK_MIN = 0x00;
66+
constexpr uint8_t STICK_CENTER = 0x80;
67+
constexpr uint8_t STICK_MAX = 0xff;
68+
69+
70+
}
71+
}
72+
73+
#else
74+
1375
// One second = 125 ticks. Thus each tick is 8 milliseconds.
1476
#define TICKS_PER_SECOND 125
1577

@@ -47,5 +109,11 @@
47109
#define STICK_CENTER 0x80
48110
#define STICK_MAX 0xff
49111

112+
#endif
113+
114+
115+
116+
117+
50118

51119
#endif

Common/NintendoSwitch/NintendoSwitch_Protocol_PushButtons.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#endif
2222

2323

24+
#ifdef __cplusplus
25+
namespace PokemonAutomation{
26+
namespace NintendoSwitch{
27+
#endif
28+
29+
2430
#define PABB_MSG_COMMAND_PBF_WAIT 0x90
2531
typedef struct{
2632
seqnum_t seqnum;
@@ -73,6 +79,12 @@ typedef struct{
7379
} PABB_PACK pabb_controller_state;
7480

7581

82+
#ifdef __cplusplus
83+
}
84+
}
85+
#endif
86+
87+
7688
#if _WIN32
7789
#pragma pack(pop)
7890
#endif

Common/NintendoSwitch/NintendoSwitch_Protocol_Superscalar.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#endif
1919

2020

21+
#ifdef __cplusplus
22+
namespace PokemonAutomation{
23+
namespace NintendoSwitch{
24+
#endif
25+
26+
2127
#define PABB_MSG_COMMAND_DEBUG_I32 0xf0
2228
typedef struct{
2329
seqnum_t seqnum;
@@ -99,6 +105,12 @@ typedef struct{
99105
} PABB_PACK pabb_ssf_issue_scroll;
100106

101107

108+
#ifdef __cplusplus
109+
}
110+
}
111+
#endif
112+
113+
102114
#if _WIN32
103115
#pragma pack(pop)
104116
#endif

Common/PokemonSwSh/PokemonSwSh_Protocol_DaySkippers.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#else
2727
#define PABB_PACK __attribute__((packed))
2828
#endif
29+
#ifdef __cplusplus
30+
namespace PokemonAutomation{
31+
namespace NintendoSwitch{
32+
#endif
2933
////////////////////////////////////////////////////////////////////////////////
3034

3135
#define PABB_MSG_COMMAND_SKIPPER_INIT_VIEW 0xbd
@@ -72,6 +76,10 @@ typedef struct{
7276
} PABB_PACK pabb_skipper_increment_all_rollback;
7377

7478
////////////////////////////////////////////////////////////////////////////////
79+
#ifdef __cplusplus
80+
}
81+
}
82+
#endif
7583
#if _WIN32
7684
#pragma pack(pop)
7785
#endif

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Handle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void BotBaseHandle::thread_body(){
412412

413413
// uint32_t wallclock = NintendoSwitch::system_clock(context);
414414
// cout << "system_clock() - done" << endl;
415-
str = ticks_to_time(TICKS_PER_SECOND, wallclock);
415+
str = ticks_to_time(NintendoSwitch::TICKS_PER_SECOND, wallclock);
416416
}catch (InvalidConnectionStateException&){
417417
break;
418418
}catch (SerialProtocolException& e){

SerialPrograms/Source/Integrations/IntegrationsAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void pai_nsw_press_button(DllSafeString& error, uint64_t console_id, uint16_t bu
109109
error = ProgramTracker::instance().nsw_press_button(console_id, button, ticks);
110110
}
111111
void pai_nsw_press_dpad(DllSafeString& error, uint64_t console_id, uint8_t position, uint16_t ticks){
112-
error = ProgramTracker::instance().nsw_press_dpad(console_id, position, ticks);
112+
error = ProgramTracker::instance().nsw_press_dpad(console_id, (NintendoSwitch::DpadPosition)position, ticks);
113113
}
114114
void pai_nsw_press_left_joystick(DllSafeString& error, uint64_t console_id, uint8_t x, uint8_t y, uint16_t ticks){
115115
error = ProgramTracker::instance().nsw_press_left_joystick(console_id, x, y, ticks);

SerialPrograms/Source/Integrations/ProgramTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ std::string ProgramTracker::stop_program(uint64_t program_id){
108108
iter->second->program.async_stop();
109109
return "";
110110
}
111-
std::string ProgramTracker::nsw_press_button(uint64_t console_id, Button button, uint16_t ticks){
111+
std::string ProgramTracker::nsw_press_button(uint64_t console_id, NintendoSwitch::Button button, uint16_t ticks){
112112
std::string header = "press_button(ID = " + std::to_string(console_id) + ")";
113113
std::lock_guard<std::mutex> lg(m_lock);
114114
auto iter = m_consoles.find(console_id);
@@ -129,7 +129,7 @@ std::string ProgramTracker::nsw_press_button(uint64_t console_id, Button button,
129129
return "";
130130
}
131131
}
132-
std::string ProgramTracker::nsw_press_dpad(uint64_t console_id, DpadPosition position, uint16_t ticks){
132+
std::string ProgramTracker::nsw_press_dpad(uint64_t console_id, NintendoSwitch::DpadPosition position, uint16_t ticks){
133133
std::string header = "press_dpad(ID = " + std::to_string(console_id) + ")";
134134
std::lock_guard<std::mutex> lg(m_lock);
135135
auto iter = m_consoles.find(console_id);

SerialPrograms/Source/Integrations/ProgramTracker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class ProgramTracker{
5151

5252
public:
5353
// Nintendo Switch
54-
std::string nsw_press_button (uint64_t console_id, Button button, uint16_t ticks);
55-
std::string nsw_press_dpad (uint64_t console_id, DpadPosition position, uint16_t ticks);
54+
std::string nsw_press_button (uint64_t console_id, NintendoSwitch::Button button, uint16_t ticks);
55+
std::string nsw_press_dpad (uint64_t console_id, NintendoSwitch::DpadPosition position, uint16_t ticks);
5656
std::string nsw_press_left_joystick (uint64_t console_id, uint8_t x, uint8_t y, uint16_t ticks);
5757
std::string nsw_press_right_joystick(uint64_t console_id, uint8_t x, uint8_t y, uint16_t ticks);
5858

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_DigitEntry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace NintendoSwitch{
3737
const uint8_t XCORD[] = {1, 0, 1, 2, 0, 1, 2, 0, 1, 2};
3838
const uint8_t YCORD[] = {3, 0, 0, 0, 1, 1, 1, 2, 2, 2};
3939

40-
void code_entry_scroll(SwitchControllerContext& context, ssf_ScrollDirection direction){
40+
void code_entry_scroll(SwitchControllerContext& context, DpadPosition direction){
4141
pbf_wait(context, CODE_DELAY);
4242
ssf_issue_scroll(context, direction, 0);
4343
}

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,20 @@ void ssf_mash_AZs(SwitchControllerContext& context, Milliseconds duration){
117117
}
118118
void ssf_issue_scroll(
119119
SwitchControllerContext& context,
120-
ssf_ScrollDirection direction,
120+
DpadPosition direction,
121121
uint16_t delay, uint16_t hold, uint8_t cool
122122
){
123123
context->issue_system_scroll(
124-
&context, (DpadPosition)direction,
124+
&context, direction,
125125
delay*8ms, hold*8ms, cool*8ms
126126
);
127127
}
128128
void ssf_issue_scroll(
129129
SwitchControllerContext& context,
130-
ssf_ScrollDirection direction,
130+
DpadPosition direction,
131131
Milliseconds delay, Milliseconds hold, Milliseconds cool
132132
){
133-
context->issue_system_scroll(&context, (DpadPosition)direction, delay, hold, cool);
133+
context->issue_system_scroll(&context, direction, delay, hold, cool);
134134
}
135135

136136

0 commit comments

Comments
 (0)