Skip to content

Commit 24a2a8b

Browse files
committed
New PABotBase wired controller firmware.
1 parent 37707d6 commit 24a2a8b

27 files changed

+285
-836
lines changed

Common/Cpp/RecursiveThrottler.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Recursive Throttler
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
* Used to suppress recursive logging.
6+
* Log only at the top level. And limit to 1 thread at a time.
7+
*
8+
*/
9+
10+
#ifndef PokemonAutomation_RecursiveThrottler_H
11+
#define PokemonAutomation_RecursiveThrottler_H
12+
13+
#include <mutex>
14+
15+
namespace PokemonAutomation{
16+
17+
class RecursiveThrottler{
18+
public:
19+
// Returns true if ok to run. (not throttled)
20+
operator bool() const{
21+
std::lock_guard<std::recursive_mutex> lg(m_lock);
22+
return m_depth == 0;
23+
}
24+
25+
private:
26+
friend class ThrottleScope;
27+
mutable std::recursive_mutex m_lock;
28+
size_t m_depth = 0;
29+
};
30+
31+
class ThrottleScope{
32+
ThrottleScope(const ThrottleScope&) = delete;
33+
void operator=(const ThrottleScope&) = delete;
34+
public:
35+
ThrottleScope(RecursiveThrottler& throttler)
36+
: m_throttler(throttler)
37+
, m_guard(throttler.m_lock)
38+
{
39+
throttler.m_depth++;
40+
}
41+
~ThrottleScope(){
42+
m_throttler.m_depth--;
43+
}
44+
45+
// Returns true if ok to run. (not throttled)
46+
operator bool() const{
47+
return m_throttler.m_depth == 1;
48+
}
49+
50+
private:
51+
RecursiveThrottler& m_throttler;
52+
std::lock_guard<std::recursive_mutex> m_guard;
53+
};
54+
55+
}
56+
#endif

Common/SerialPABotBase/SerialPABotBase_Protocol_IDs.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* SerialPABotBase Protocol IDs
2-
*
2+
*
33
* From: https://github.com/PokemonAutomation/
4-
*
4+
*
55
*/
66

77
#ifndef PokemonAutomation_SerialPABotBase_Protocol_IDs_H
@@ -10,9 +10,24 @@
1010

1111
// Program IDs
1212
#define PABB_PID_UNSPECIFIED 0x00
13+
14+
// Old AVR8
1315
#define PABB_PID_PABOTBASE_12KB 0x08
1416
#define PABB_PID_PABOTBASE_31KB 0x09
17+
18+
// New AVR8
19+
#define PABB_PID_PABOTBASE_ArduinoUnoR3 0x01
20+
#define PABB_PID_PABOTBASE_ArduinoLeonardo 0x02
21+
#define PABB_PID_PABOTBASE_ProMicro 0x03
22+
#define PABB_PID_PABOTBASE_Teensy2 0x04
23+
#define PABB_PID_PABOTBASE_TeensyPP2 0x05
24+
25+
// Misc.
26+
#define PABB_PID_PABOTBASE_CH552 0x0a
27+
28+
// ESP32
1529
#define PABB_PID_PABOTBASE_ESP32 0x10
30+
#define PABB_PID_PABOTBASE_ESP32S3 0x12
1631

1732

1833
// Controller IDs

SerialPrograms/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ file(GLOB MAIN_SOURCES
172172
../Common/Cpp/PrintDebuggers.h
173173
../Common/Cpp/Rectangle.h
174174
../Common/Cpp/Rectangle.tpp
175+
../Common/Cpp/RecursiveThrottler.h
175176
../Common/Cpp/Sockets/AbstractClientSocket.h
176177
../Common/Cpp/Sockets/ClientSocket.cpp
177178
../Common/Cpp/Sockets/ClientSocket.h
@@ -1794,15 +1795,12 @@ file(GLOB MAIN_SOURCES
17941795
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_AutoHosts.h
17951796
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DateSpam.cpp
17961797
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DateSpam.h
1797-
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DaySkippers.cpp
1798-
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DaySkippers.h
17991798
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_EggRoutines.cpp
18001799
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_EggRoutines.h
18011800
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_GameEntry.cpp
18021801
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_GameEntry.h
18031802
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_Misc.cpp
18041803
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_Misc.h
1805-
Source/PokemonSwSh/Commands/PokemonSwSh_Messages_DaySkippers.h
18061804
Source/PokemonSwSh/Inference/Battles/PokemonSwSh_BattleBallReader.cpp
18071805
Source/PokemonSwSh/Inference/Battles/PokemonSwSh_BattleBallReader.h
18081806
Source/PokemonSwSh/Inference/Battles/PokemonSwSh_BattleDialogDetector.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,6 @@ SOURCES += \
882882
Source/PokemonSV/Resources/PokemonSV_TournamentPrizeNames.cpp \
883883
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_AutoHosts.cpp \
884884
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DateSpam.cpp \
885-
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DaySkippers.cpp \
886885
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_EggRoutines.cpp \
887886
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_GameEntry.cpp \
888887
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_Misc.cpp \
@@ -1234,6 +1233,7 @@ HEADERS += \
12341233
../Common/Cpp/PrettyPrint.h \
12351234
../Common/Cpp/PrintDebuggers.h \
12361235
../Common/Cpp/Rectangle.h \
1236+
../Common/Cpp/RecursiveThrottler.h \
12371237
../Common/Cpp/SIMDDebuggers.h \
12381238
../Common/Cpp/Sockets/AbstractClientSocket.h \
12391239
../Common/Cpp/Sockets/ClientSocket.h \
@@ -2080,11 +2080,9 @@ HEADERS += \
20802080
Source/PokemonSV/Resources/PokemonSV_TournamentPrizeNames.h \
20812081
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_AutoHosts.h \
20822082
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DateSpam.h \
2083-
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_DaySkippers.h \
20842083
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_EggRoutines.h \
20852084
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_GameEntry.h \
20862085
Source/PokemonSwSh/Commands/PokemonSwSh_Commands_Misc.h \
2087-
Source/PokemonSwSh/Commands/PokemonSwSh_Messages_DaySkippers.h \
20882086
Source/PokemonSwSh/Inference/Battles/PokemonSwSh_BattleBallReader.h \
20892087
Source/PokemonSwSh/Inference/Battles/PokemonSwSh_BattleDialogDetector.h \
20902088
Source/PokemonSwSh/Inference/Battles/PokemonSwSh_BattleDialogTracker.h \

SerialPrograms/Source/Controllers/Controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class QKeyEvent;
1616

1717
namespace PokemonAutomation{
1818

19+
class RecursiveThrottler;
1920
enum class ControllerType;
2021
enum class ControllerPerformanceClass;
2122
class ControllerFeatures;
@@ -37,6 +38,7 @@ class AbstractController{
3738
virtual ~AbstractController() = default;
3839

3940
virtual Logger& logger() = 0;
41+
virtual RecursiveThrottler& logging_throttler() = 0;
4042

4143

4244
public:

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase.cpp

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,24 @@ const ControllerFeatures OLD_NINTENDO_SWITCH_DEFAULT_REQUIREMENTS{
2424

2525
std::string program_name(uint8_t id){
2626
switch (id){
27-
case PABB_PID_UNSPECIFIED: return "Microcontroller Program";
28-
case PABB_PID_PABOTBASE_12KB: return "PABotBase-AVR8-12KB";
29-
case PABB_PID_PABOTBASE_31KB: return "PABotBase-AVR8-31KB";
30-
case PABB_PID_PABOTBASE_ESP32: return "PABotBase-ESP32";
27+
case PABB_PID_UNSPECIFIED: return "Microcontroller Program";
28+
29+
// Old (fat) PABotBase with scheduler.
30+
case PABB_PID_PABOTBASE_12KB: return "PABotBase-AVR8-12KB";
31+
case PABB_PID_PABOTBASE_31KB: return "PABotBase-AVR8-31KB";
32+
33+
// New (slim) PABotBase.
34+
case PABB_PID_PABOTBASE_ArduinoUnoR3: return "PABotBase-UnoR3";
35+
case PABB_PID_PABOTBASE_ArduinoLeonardo: return "PABotBase-Leonardo";
36+
case PABB_PID_PABOTBASE_ProMicro: return "PABotBase-ProMicro";
37+
case PABB_PID_PABOTBASE_Teensy2: return "PABotBase-Teensy2.0";
38+
case PABB_PID_PABOTBASE_TeensyPP2: return "PABotBase-Teensy++2.0";
39+
40+
case PABB_PID_PABOTBASE_CH552: return "PABotBase-CH552";
41+
42+
case PABB_PID_PABOTBASE_ESP32: return "PABotBase-ESP32";
43+
case PABB_PID_PABOTBASE_ESP32S3: return "PABotBase-ESP32-S3";
44+
3145
default: return "Unknown ID";
3246
}
3347
}
@@ -71,6 +85,7 @@ const std::map<
7185
std::map<ControllerType, ControllerFeatures>
7286
>
7387
> SUPPORTED_VERSIONS{
88+
#if 0
7489
{2021052600, {
7590
{PABB_PID_UNSPECIFIED, {{ControllerType::None, {}}}},
7691
{PABB_PID_PABOTBASE_12KB, {
@@ -106,6 +121,7 @@ const std::map<
106121
}},
107122
}},
108123
}},
124+
#endif
109125
{2025033000, {
110126
{PABB_PID_PABOTBASE_ESP32, {
111127
{ControllerType::NintendoSwitch_WirelessProController, {
@@ -125,6 +141,48 @@ const std::map<
125141
}},
126142
}},
127143
}},
144+
{2025040200, {
145+
{PABB_PID_PABOTBASE_ArduinoUnoR3, {
146+
{ControllerType::NintendoSwitch_WiredProController, {
147+
ControllerFeature::TickPrecise,
148+
ControllerFeature::QueryCommandQueueSize,
149+
ControllerFeature::NintendoSwitch_ProController,
150+
ControllerFeature::NintendoSwitch_DateSkip,
151+
}},
152+
}},
153+
{PABB_PID_PABOTBASE_ArduinoLeonardo, {
154+
{ControllerType::NintendoSwitch_WiredProController, {
155+
ControllerFeature::TickPrecise,
156+
ControllerFeature::QueryCommandQueueSize,
157+
ControllerFeature::NintendoSwitch_ProController,
158+
ControllerFeature::NintendoSwitch_DateSkip,
159+
}},
160+
}},
161+
{PABB_PID_PABOTBASE_ProMicro, {
162+
{ControllerType::NintendoSwitch_WiredProController, {
163+
ControllerFeature::TickPrecise,
164+
ControllerFeature::QueryCommandQueueSize,
165+
ControllerFeature::NintendoSwitch_ProController,
166+
ControllerFeature::NintendoSwitch_DateSkip,
167+
}},
168+
}},
169+
{PABB_PID_PABOTBASE_Teensy2, {
170+
{ControllerType::NintendoSwitch_WiredProController, {
171+
ControllerFeature::TickPrecise,
172+
ControllerFeature::QueryCommandQueueSize,
173+
ControllerFeature::NintendoSwitch_ProController,
174+
ControllerFeature::NintendoSwitch_DateSkip,
175+
}},
176+
}},
177+
{PABB_PID_PABOTBASE_TeensyPP2, {
178+
{ControllerType::NintendoSwitch_WiredProController, {
179+
ControllerFeature::TickPrecise,
180+
ControllerFeature::QueryCommandQueueSize,
181+
ControllerFeature::NintendoSwitch_ProController,
182+
ControllerFeature::NintendoSwitch_DateSkip,
183+
}},
184+
}},
185+
}},
128186
};
129187

130188

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerWithScheduler.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ControllerWithScheduler::ControllerWithScheduler(Logger& logger)
2323
make_resource_list()
2424
)
2525
, m_logger(logger)
26-
, m_logging_suppress(0)
26+
// , m_logging_suppress(0)
2727
{}
2828

2929

@@ -32,7 +32,7 @@ void ControllerWithScheduler::issue_barrier(const Cancellable* cancellable){
3232
std::lock_guard<std::mutex> lg0(m_issue_lock);
3333
std::lock_guard<std::mutex> lg1(m_state_lock);
3434
this->issue_wait_for_all(cancellable);
35-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
35+
if (m_logging_throttler){
3636
m_logger.log("issue_barrier()", COLOR_DARKGREEN);
3737
}
3838
}
@@ -43,7 +43,7 @@ void ControllerWithScheduler::issue_nop(const Cancellable* cancellable, Millisec
4343
cancellable->throw_if_cancelled();
4444
}
4545
this->SuperscalarScheduler::issue_nop(cancellable, WallDuration(duration));
46-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
46+
if (m_logging_throttler){
4747
m_logger.log(
4848
"issue_nop(): duration = " + std::to_string(duration.count()) + "ms",
4949
COLOR_DARKGREEN
@@ -78,7 +78,7 @@ void ControllerWithScheduler::issue_buttons(
7878
}
7979
this->SuperscalarScheduler::issue_nop(cancellable, delay);
8080

81-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
81+
if (m_logging_throttler){
8282
m_logger.log(
8383
"issue_buttons(): " + button_to_string(button) +
8484
", delay = " + std::to_string(delay.count()) + "ms" +
@@ -103,7 +103,7 @@ void ControllerWithScheduler::issue_dpad(
103103
m_dpad.position = position;
104104
this->issue_to_resource(cancellable, m_dpad, delay, hold, cooldown);
105105

106-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
106+
if (m_logging_throttler){
107107
m_logger.log(
108108
"issue_dpad(): " + dpad_to_string(position) +
109109
", delay = " + std::to_string(delay.count()) + "ms" +
@@ -130,7 +130,7 @@ void ControllerWithScheduler::issue_left_joystick(
130130
m_left_joystick.y = y;
131131
this->issue_to_resource(cancellable, m_left_joystick, delay, hold, cooldown);
132132

133-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
133+
if (m_logging_throttler){
134134
m_logger.log(
135135
"issue_left_joystick(): (" + std::to_string(x) + "," + std::to_string(y) + ")" +
136136
", delay = " + std::to_string(delay.count()) + "ms" +
@@ -156,7 +156,7 @@ void ControllerWithScheduler::issue_right_joystick(
156156
m_right_joystick.y = y;
157157
this->issue_to_resource(cancellable, m_right_joystick, delay, hold, cooldown);
158158

159-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
159+
if (m_logging_throttler){
160160
m_logger.log(
161161
"issue_right_joystick(): (" + std::to_string(x) + "," + std::to_string(y) + ")" +
162162
", delay = " + std::to_string(delay.count()) + "ms" +
@@ -218,7 +218,7 @@ void ControllerWithScheduler::issue_full_controller_state(
218218
hold, hold, WallDuration::zero()
219219
);
220220

221-
if (m_logging_suppress.load(std::memory_order_relaxed) == 0){
221+
if (m_logging_throttler){
222222
m_logger.log(
223223
"issue_controller_state(): (" + button_to_string(button) +
224224
"), dpad(" + dpad_to_string(position) +
@@ -238,15 +238,15 @@ void ControllerWithScheduler::issue_mash_button(
238238
if (cancellable){
239239
cancellable->throw_if_cancelled();
240240
}
241-
LoggingSuppressScope scope(m_logging_suppress);
241+
ThrottleScope scope(m_logging_throttler);
242242
bool log = true;
243243
while (duration > Milliseconds::zero()){
244244
issue_buttons(cancellable, button, 8*8ms, 5*8ms, 3*8ms);
245245

246246
// We never log before the first issue to avoid delaying the critical path.
247247
// But we do want to log before the mash spam. So we log after the first
248248
// issue, but before the second.
249-
if (log && m_logging_suppress.load(std::memory_order_relaxed) == 1){
249+
if (log && scope){
250250
m_logger.log(
251251
"issue_mash_button(): " + button_to_string(button) +
252252
", duration = " + std::to_string(duration.count()) + "ms",
@@ -267,7 +267,7 @@ void ControllerWithScheduler::issue_mash_button(
267267
if (cancellable){
268268
cancellable->throw_if_cancelled();
269269
}
270-
LoggingSuppressScope scope(m_logging_suppress);
270+
ThrottleScope scope(m_logging_throttler);
271271
bool log = true;
272272
while (duration > Milliseconds::zero()){
273273
issue_buttons(cancellable, button0, Milliseconds(4*8), 5*8ms, 3*8ms);
@@ -276,7 +276,7 @@ void ControllerWithScheduler::issue_mash_button(
276276
// We never log before the first issue to avoid delaying the critical path.
277277
// But we do want to log before the mash spam. So we log after the first
278278
// issue, but before the second.
279-
if (log && m_logging_suppress.load(std::memory_order_relaxed) == 1){
279+
if (log && scope){
280280
m_logger.log(
281281
"issue_mash_button(): (" + button_to_string(button0) +
282282
"), (" + button_to_string(button1) +
@@ -296,7 +296,7 @@ void ControllerWithScheduler::issue_mash_AZs(
296296
if (cancellable){
297297
cancellable->throw_if_cancelled();
298298
}
299-
LoggingSuppressScope scope(m_logging_suppress);
299+
ThrottleScope scope(m_logging_throttler);
300300
bool log = true;
301301
while (true){
302302
if (duration <= Milliseconds::zero()){
@@ -307,7 +307,7 @@ void ControllerWithScheduler::issue_mash_AZs(
307307
// We never log before the first issue to avoid delaying the critical path.
308308
// But we do want to log before the mash spam. So we log after the first
309309
// issue, but before the second.
310-
if (log && m_logging_suppress.load(std::memory_order_relaxed) == 1){
310+
if (log && scope){
311311
m_logger.log(
312312
"issue_mash_AZs(): duration = " + std::to_string(duration.count()) + "ms",
313313
COLOR_DARKGREEN
@@ -338,7 +338,7 @@ void ControllerWithScheduler::issue_system_scroll(
338338
cancellable->throw_if_cancelled();
339339
}
340340

341-
LoggingSuppressScope scope(m_logging_suppress);
341+
ThrottleScope scope(m_logging_throttler);
342342

343343
WallClock dpad = m_dpad.free_time();
344344
WallClock left_joystick = m_left_joystick.free_time();
@@ -403,7 +403,7 @@ void ControllerWithScheduler::issue_system_scroll(
403403
}
404404
}while (false);
405405

406-
if (m_logging_suppress.load(std::memory_order_relaxed) == 1){
406+
if (scope){
407407
m_logger.log(
408408
"issue_system_scroll(): " + dpad_to_string(direction) +
409409
", delay = " + std::to_string(delay.count()) + "ms" +

0 commit comments

Comments
 (0)