Skip to content

Commit 87a96a6

Browse files
committed
Refactor for ESP32.
1 parent 0786d40 commit 87a96a6

14 files changed

+403
-121
lines changed

ClientSource/Connection/PABotBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ uint64_t PABotBase::try_issue_request(
563563
if (message.body.size() < sizeof(uint32_t)){
564564
throw InternalProgramError(&m_logger, PA_CURRENT_FUNCTION, "Message is too short.");
565565
}
566-
if (message.body.size() > PABB_MAX_MESSAGE_SIZE){
566+
if (message.body.size() > MAX_MESSAGE_SIZE){
567567
throw InternalProgramError(&m_logger, PA_CURRENT_FUNCTION, "Message is too long.");
568568
}
569569

@@ -628,7 +628,7 @@ uint64_t PABotBase::try_issue_command(
628628
if (message.body.size() < sizeof(uint32_t)){
629629
throw InternalProgramError(&m_logger, PA_CURRENT_FUNCTION, "Message is too short.");
630630
}
631-
if (message.body.size() > PABB_MAX_MESSAGE_SIZE){
631+
if (message.body.size() > MAX_MESSAGE_SIZE){
632632
throw InternalProgramError(&m_logger, PA_CURRENT_FUNCTION, "Message is too long.");
633633
}
634634

ClientSource/Connection/PABotBaseConnection.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include "BotBaseMessage.h"
1212
#include "PABotBaseConnection.h"
1313

14-
#include <iostream>
15-
using std::cout;
16-
using std::endl;
14+
//#include <iostream>
15+
//using std::cout;
16+
//using std::endl;
1717

1818
namespace PokemonAutomation{
1919

@@ -67,7 +67,7 @@ void PABotBaseConnection::send_message(const BotBaseMessage& message, bool is_re
6767
m_sniffer->on_send(message, is_retransmit);
6868

6969
size_t total_bytes = PABB_PROTOCOL_OVERHEAD + message.body.size();
70-
if (total_bytes > PABB_MAX_PACKET_SIZE){
70+
if (total_bytes > MAX_MESSAGE_SIZE){
7171
throw InternalProgramError(&m_logger, PA_CURRENT_FUNCTION, "Message is too long.");
7272
}
7373

@@ -105,7 +105,7 @@ void PABotBaseConnection::on_recv(const void* data, size_t bytes){
105105
}
106106

107107
// Message is too long.
108-
if (length > PABB_MAX_PACKET_SIZE){
108+
if (length > MAX_MESSAGE_SIZE){
109109
m_sniffer->log("Message is too long: bytes = " + std::to_string(length));
110110
m_recv_buffer.pop_front();
111111
continue;

ClientSource/Connection/PABotBaseConnection.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ namespace PokemonAutomation{
2727
// None the functions in this class are thread-safe. It is up to
2828
// the child class to wrap and make them thread-safe.
2929
class PABotBaseConnection : public StreamListener{
30+
public:
31+
static const size_t MAX_MESSAGE_SIZE = 64;
32+
3033
public:
3134
PABotBaseConnection(Logger& logger, std::unique_ptr<StreamConnection> connection);
3235
virtual ~PABotBaseConnection();
3336

3437
void set_sniffer(MessageSniffer* sniffer);
3538

3639
public:
37-
void send_zeros(uint8_t bytes = PABB_MAX_PACKET_SIZE);
40+
void send_zeros(uint8_t bytes = MAX_MESSAGE_SIZE);
3841
void send_message(const BotBaseMessage& message, bool is_retransmit);
3942

4043
protected:

Common/Microcontroller/MessageProtocol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@
172172
#define PABB_BAUD_RATE 115200
173173
#define PABB_RETRANSMIT_DELAY_MILLIS 80
174174

175+
// This limit of 12 is only for AVR8. ESP32 will go much larger. So we ignore
176+
// this number and allow large messages on the computer side.
175177
#define PABB_MAX_MESSAGE_SIZE 12
176178
#define PABB_PROTOCOL_OVERHEAD (2 + sizeof(uint32_t))
177179
#define PABB_MAX_PACKET_SIZE (PABB_MAX_MESSAGE_SIZE + PABB_PROTOCOL_OVERHEAD)

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Descriptor.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include "SerialPABotBase_Descriptor.h"
1111
#include "SerialPABotBase_SelectorWidget.h"
1212

13-
#include "NintendoSwitch/Controllers/NintendoSwitch_ProController_SerialPABotBase.h"
13+
#include "NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_PokkenController.h"
14+
#include "NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_WirelessProController.h"
1415

1516
//#include <iostream>
1617
//using std::cout;
@@ -67,11 +68,18 @@ std::unique_ptr<AbstractController> SerialPABotBase_Descriptor::make_controller(
6768
) const{
6869
switch (controller_type){
6970
case ControllerType::NintendoSwitch_WiredProController:
71+
return std::unique_ptr<AbstractController>(
72+
new NintendoSwitch::SerialPABotBase_PokkenController(
73+
logger,
74+
static_cast<SerialPABotBase_Connection&>(connection),
75+
requirements
76+
)
77+
);
78+
7079
case ControllerType::NintendoSwitch_WirelessProController:
7180
return std::unique_ptr<AbstractController>(
72-
new NintendoSwitch::ProController_SerialPABotBase(
81+
new NintendoSwitch::SerialPABotBase_WirelessProController(
7382
logger,
74-
controller_type,
7583
static_cast<SerialPABotBase_Connection&>(connection),
7684
requirements
7785
)

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ProControllerWithScheduler.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define PokemonAutomation_NintendoSwitch_ControllerWithScheduler_H
1212

1313
#include <mutex>
14-
#include "Common/Cpp/Concurrency/SpinLock.h"
1514
#include "Controllers/SuperscalarScheduler.h"
1615
#include "NintendoSwitch_ProController.h"
1716

@@ -38,6 +37,35 @@ struct ProControllerSchedulerState{
3837
};
3938

4039

40+
struct SplitDpad{
41+
bool up = false;
42+
bool right = false;
43+
bool down = false;
44+
bool left = false;
45+
};
46+
inline SplitDpad convert_unified_to_split_dpad(DpadPosition dpad){
47+
switch (dpad){
48+
case DpadPosition::DPAD_UP:
49+
return {true, false, false, false};
50+
case DpadPosition::DPAD_UP_RIGHT:
51+
return {true, true, false, false};
52+
case DpadPosition::DPAD_RIGHT:
53+
return {false, true, false, false};
54+
case DpadPosition::DPAD_DOWN_RIGHT:
55+
return {false, true, true, false};
56+
case DpadPosition::DPAD_DOWN:
57+
return {false, false, true, false};
58+
case DpadPosition::DPAD_DOWN_LEFT:
59+
return {false, false, true, true};
60+
case DpadPosition::DPAD_LEFT:
61+
return {false, false, false, true};
62+
case DpadPosition::DPAD_UP_LEFT:
63+
return {true, false, false, true};
64+
default:
65+
return {false, false, false, false};
66+
}
67+
}
68+
4169

4270

4371

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Pokken Controller
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#include "Common/Cpp/Exceptions.h"
8+
#include "Common/Cpp/Concurrency/ReverseLockGuard.h"
9+
#include "Controllers/ControllerCapability.h"
10+
#include "NintendoSwitch/Commands/NintendoSwitch_Messages_PushButtons.h"
11+
#include "NintendoSwitch_PokkenController.h"
12+
13+
//#include <iostream>
14+
//using std::cout;
15+
//using std::endl;
16+
17+
namespace PokemonAutomation{
18+
namespace NintendoSwitch{
19+
20+
using namespace std::chrono_literals;
21+
22+
23+
24+
25+
SerialPABotBase_PokkenController::SerialPABotBase_PokkenController(
26+
Logger& logger,
27+
SerialPABotBase::SerialPABotBase_Connection& connection,
28+
const ControllerRequirements& requirements
29+
)
30+
: SerialPABotBase_ProController(
31+
logger,
32+
ControllerType::NintendoSwitch_WiredProController,
33+
connection,
34+
requirements
35+
)
36+
{}
37+
SerialPABotBase_PokkenController::~SerialPABotBase_PokkenController(){
38+
stop();
39+
}
40+
41+
void SerialPABotBase_PokkenController::push_state(const Cancellable* cancellable, WallDuration duration){
42+
// Must be called inside "m_state_lock".
43+
44+
if (!is_ready()){
45+
throw InvalidConnectionStateException();
46+
}
47+
48+
Button buttons = BUTTON_NONE;
49+
for (size_t c = 0; c < 14; c++){
50+
buttons |= m_buttons[c].is_busy()
51+
? (Button)((uint16_t)1 << c)
52+
: BUTTON_NONE;
53+
}
54+
55+
DpadPosition dpad = m_dpad.is_busy() ? m_dpad.position : DPAD_NONE;
56+
57+
uint8_t left_x = 128;
58+
uint8_t left_y = 128;
59+
uint8_t right_x = 128;
60+
uint8_t right_y = 128;
61+
if (m_left_joystick.is_busy()){
62+
left_x = m_left_joystick.x;
63+
left_y = m_left_joystick.y;
64+
}
65+
if (m_right_joystick.is_busy()){
66+
right_x = m_right_joystick.x;
67+
right_y = m_right_joystick.y;
68+
}
69+
70+
71+
// Release the state lock since we are no longer touching state.
72+
// This loop can block indefinitely if the command queue is full.
73+
ReverseLockGuard<std::mutex> lg(m_state_lock);
74+
75+
// Divide the controller state into smaller chunks of 255 ticks.
76+
Milliseconds time_left = std::chrono::duration_cast<Milliseconds>(duration);
77+
while (time_left > Milliseconds::zero()){
78+
Milliseconds current_ms = std::min(time_left, 255 * 8ms);
79+
uint8_t current_ticks = (uint8_t)milliseconds_to_ticks_8ms(current_ms.count());
80+
m_serial->issue_request(
81+
DeviceRequest_controller_state(buttons, dpad, left_x, left_y, right_x, right_y, current_ticks),
82+
cancellable
83+
);
84+
time_left -= current_ms;
85+
}
86+
}
87+
88+
89+
90+
91+
92+
93+
94+
95+
}
96+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Pokken Controller
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_NintendoSwitch_PokkenController_H
8+
#define PokemonAutomation_NintendoSwitch_PokkenController_H
9+
10+
#include "NintendoSwitch_ProController.h"
11+
12+
namespace PokemonAutomation{
13+
namespace NintendoSwitch{
14+
15+
16+
class SerialPABotBase_PokkenController final : public SerialPABotBase_ProController{
17+
public:
18+
using ContextType = ProControllerContext;
19+
20+
public:
21+
SerialPABotBase_PokkenController(
22+
Logger& logger,
23+
SerialPABotBase::SerialPABotBase_Connection& connection,
24+
const ControllerRequirements& requirements
25+
);
26+
~SerialPABotBase_PokkenController();
27+
28+
29+
private:
30+
template <typename Type>
31+
PA_FORCE_INLINE Type milliseconds_to_ticks_8ms(Type milliseconds){
32+
return milliseconds / 8 + (milliseconds % 8 + 7) / 8;
33+
}
34+
virtual void push_state(const Cancellable* cancellable, WallDuration duration) override;
35+
};
36+
37+
38+
39+
40+
}
41+
}
42+
#endif

0 commit comments

Comments
 (0)