Skip to content

Commit ca87cb6

Browse files
authored
Merge branch 'PokemonAutomation:main' into main
2 parents 3cdd426 + e014116 commit ca87cb6

10 files changed

Lines changed: 535 additions & 2 deletions

SerialPrograms/Source/Controllers/ControllerSelectorWidget.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
#include <QKeyEvent>
88
#include <QHBoxLayout>
99
#include "Common/Qt/NoWheelComboBox.h"
10-
//#include "CommonFramework/GlobalSettingsPanel.h"
10+
#include "CommonFramework/GlobalSettingsPanel.h"
1111
#include "CommonFramework/Panels/ConsoleSettingsStretch.h"
1212
#include "Controllers/ControllerTypeStrings.h"
1313
#include "ControllerSelectorWidget.h"
1414
//#include "NintendoSwitch/NintendoSwitch_Settings.h"
1515

1616
#include "SerialPABotBase/SerialPABotBase_SelectorWidget.h"
17+
#include "SerialPABotBase/SerialPABotBase2_SelectorWidget.h"
1718
#include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_SelectorWidget.h"
1819

1920
//#include <iostream>
@@ -49,6 +50,9 @@ ControllerSelectorWidget::ControllerSelectorWidget(QWidget& parent, ControllerSe
4950
m_dropdowns->addWidget(interface_dropdown);
5051

5152
interface_dropdown->addItem(QString::fromStdString(CONTROLLER_INTERFACE_STRINGS.get_string(ControllerInterface::SerialPABotBase)));
53+
if (PreloadSettings::instance().DEVELOPER_MODE){
54+
interface_dropdown->addItem(QString::fromStdString(CONTROLLER_INTERFACE_STRINGS.get_string(ControllerInterface::SerialPABotBase2)));
55+
}
5256
interface_dropdown->addItem(QString::fromStdString(CONTROLLER_INTERFACE_STRINGS.get_string(ControllerInterface::TcpSysbotBase)));
5357
// interface_dropdown->addItem(QString::fromStdString(CONTROLLER_INTERFACE_STRINGS.get_string(ControllerInterface::UsbSysbotBase)));
5458

@@ -161,6 +165,11 @@ void ControllerSelectorWidget::refresh_selection(ControllerInterface interface_t
161165
m_dropdowns->insertWidget(1, m_selector, 1);
162166
break;
163167

168+
case ControllerInterface::SerialPABotBase2:
169+
m_selector = new SerialPABotBase::SerialPABotBase2_SelectorWidget(*this, m_session.descriptor().get());
170+
m_dropdowns->insertWidget(1, m_selector, 1);
171+
break;
172+
164173
case ControllerInterface::TcpSysbotBase:
165174
m_selector = new SysbotBase::TcpSysbotBase_SelectorWidget(*this, m_session.descriptor().get());
166175
m_dropdowns->insertWidget(1, m_selector, 1);

SerialPrograms/Source/Controllers/ControllerTypeStrings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace PokemonAutomation{
1313
const EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS{
1414
{ControllerInterface::None, "None"},
1515
{ControllerInterface::SerialPABotBase, "Serial: PABotBase"},
16+
{ControllerInterface::SerialPABotBase2, "Serial: PABotBase2"},
1617
{ControllerInterface::TcpSysbotBase, "TCP: sys-botbase"},
1718
{ControllerInterface::UsbSysbotBase, "USB: sys-botbase"},
1819
};

SerialPrograms/Source/Controllers/ControllerTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace PokemonAutomation{
1414
enum class ControllerInterface{
1515
None,
1616
SerialPABotBase,
17+
SerialPABotBase2,
1718
TcpSysbotBase,
1819
UsbSysbotBase,
1920
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* Serial Port (PABotBase) Connection
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <QSerialPortInfo>
8+
#include <QMessageBox>
9+
#include "Common/Cpp/PanicDump.h"
10+
#include "CommonFramework/GlobalSettingsPanel.h"
11+
#include "CommonFramework/Tools/GlobalThreadPools.h"
12+
#include "SerialPABotBase2_Connection.h"
13+
14+
namespace PokemonAutomation{
15+
namespace SerialPABotBase{
16+
17+
18+
19+
20+
SerialPABotBase2_Connection::SerialPABotBase2_Connection(
21+
Logger& logger,
22+
std::string name,
23+
bool set_to_null_controller
24+
)
25+
: m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
26+
, m_device_name(std::move(name))
27+
{
28+
set_status_line0("Not Connected", COLOR_RED);
29+
30+
m_connect_thread = GlobalThreadPools::unlimited_normal().dispatch_now_blocking([=, this]{
31+
run_with_catch(
32+
"SerialPABotBase_Connection::connect_thread_body()",
33+
[=, this]{ connect_thread_body(set_to_null_controller); }
34+
);
35+
});
36+
};
37+
SerialPABotBase2_Connection::~SerialPABotBase2_Connection(){
38+
cancel(nullptr);
39+
m_ready.store(false, std::memory_order_release);
40+
41+
42+
m_connect_thread.wait_and_ignore_exceptions();
43+
}
44+
45+
46+
47+
48+
void SerialPABotBase2_Connection::connect_thread_body(bool set_to_null_controller){
49+
{
50+
std::string text = "Opening serial port...";
51+
m_logger.log(text);
52+
set_status_line0(text, COLOR_DARKGREEN);
53+
}
54+
55+
if (cancelled()){
56+
return;
57+
}
58+
QSerialPortInfo info(QString::fromStdString(m_device_name));
59+
60+
// Port is invalid.
61+
if (info.isNull()){
62+
std::string text = "Serial port " + m_device_name + " is invalid.";
63+
m_logger.log(text, COLOR_RED);
64+
set_status_line0(text, COLOR_RED);
65+
return;
66+
}
67+
68+
// Prolific is banned
69+
if (info.description().indexOf("Prolific") != -1){
70+
QMessageBox box;
71+
box.critical(
72+
nullptr,
73+
"Error",
74+
"Cannot select Prolific controller.<br><br>"
75+
"Prolific controllers do not work for Arduino and similar microcontrollers.<br>"
76+
"You were warned of this in the setup instructions. Please buy a CP210x controller instead."
77+
);
78+
std::string text = "Cannot connect to Prolific controller.";
79+
m_logger.log(text, COLOR_RED);
80+
set_status_line0(text, COLOR_RED);
81+
return;
82+
}
83+
84+
if (cancelled()){
85+
return;
86+
}
87+
m_unreliable_connection = std::make_unique<SerialConnection>(
88+
GlobalThreadPools::unlimited_realtime(),
89+
info.systemLocation().toStdString(),
90+
115200 // Temporary. Will increase to 460800 in the future.
91+
);
92+
93+
{
94+
std::string text = "Opening up reliable channel...";
95+
m_logger.log(text);
96+
set_status_line0(text, COLOR_DARKGREEN);
97+
}
98+
m_stream_connection = std::make_unique<ReliableStreamConnection>(
99+
this,
100+
m_logger, GlobalSettings::instance().LOG_EVERYTHING,
101+
GlobalThreadPools::unlimited_realtime(),
102+
*m_unreliable_connection,
103+
std::chrono::milliseconds(80),
104+
nullptr
105+
);
106+
107+
m_logger.log("Resetting device connection state...");
108+
m_stream_connection->reset();
109+
110+
// TODO
111+
112+
}
113+
114+
115+
116+
117+
}
118+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Serial Port (PABotBase2) Connection
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Controllers_SerialPABotBase2_Connection_H
8+
#define PokemonAutomation_Controllers_SerialPABotBase2_Connection_H
9+
10+
#include "Common/Cpp/CancellableScope.h"
11+
#include "Common/Cpp/Concurrency/AsyncTask.h"
12+
#include "Common/Cpp/SerialConnection/SerialConnection.h"
13+
#include "Common/Cpp/StreamConnections/ReliableStreamConnection.h"
14+
#include "Controllers/SerialPABotBase/Connection/MessageLogger.h"
15+
#include "Controllers/ControllerConnection.h"
16+
17+
namespace PokemonAutomation{
18+
namespace SerialPABotBase{
19+
20+
21+
class SerialPABotBase2_Connection final : public ControllerConnection, public CancellableScope{
22+
public:
23+
SerialPABotBase2_Connection(
24+
Logger& logger,
25+
std::string name,
26+
bool set_to_null_controller
27+
);
28+
~SerialPABotBase2_Connection();
29+
30+
31+
private:
32+
void connect_thread_body(bool set_to_null_controller);
33+
34+
35+
private:
36+
SerialLogger m_logger;
37+
std::string m_device_name;
38+
39+
AsyncTask m_connect_thread;
40+
std::unique_ptr<SerialConnection> m_unreliable_connection;
41+
std::unique_ptr<ReliableStreamConnection> m_stream_connection;
42+
43+
};
44+
45+
46+
47+
}
48+
}
49+
#endif
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/* Serial Port (PABotBase2) Interface
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <QSerialPortInfo>
8+
#include <QWidget>
9+
#include "Common/Cpp/Json/JsonValue.h"
10+
#include "Controllers/ControllerTypeStrings.h"
11+
#include "SerialPABotBase2_Connection.h"
12+
#include "SerialPABotBase2_SelectorWidget.h"
13+
#include "SerialPABotBase2_Descriptor.h"
14+
15+
//#include "Controllers/StandardHid/StandardHid_Keyboard_SerialPABotBase.h"
16+
//#include "NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_WiredController.h"
17+
//#include "NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_ProController.h"
18+
//#include "NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_Joycon.h"
19+
20+
//#include <iostream>
21+
//using std::cout;
22+
//using std::endl;
23+
24+
namespace PokemonAutomation{
25+
26+
template class InterfaceType_t<SerialPABotBase::SerialPABotBase2_Descriptor>;
27+
28+
namespace SerialPABotBase{
29+
30+
31+
32+
33+
bool SerialPABotBase2_Descriptor::operator==(const ControllerDescriptor& x) const{
34+
if (typeid(*this) != typeid(x)){
35+
return false;
36+
}
37+
return m_name == static_cast<const SerialPABotBase2_Descriptor&>(x).m_name;
38+
}
39+
40+
41+
std::string SerialPABotBase2_Descriptor::display_name() const{
42+
#if 0
43+
QSerialPortInfo info(QString::fromStdString(m_name));
44+
if (info.isNull()){
45+
return m_name;
46+
}
47+
return m_name + " - " + info.manufacturer().toStdString();
48+
#else
49+
return m_name;
50+
#endif
51+
}
52+
void SerialPABotBase2_Descriptor::load_json(const JsonValue& json){
53+
const std::string* name = json.to_string();
54+
if (name == nullptr || name->empty()){
55+
return;
56+
}
57+
m_name = *name;
58+
}
59+
JsonValue SerialPABotBase2_Descriptor::to_json() const{
60+
return m_name;
61+
}
62+
63+
std::unique_ptr<ControllerConnection> SerialPABotBase2_Descriptor::open_connection(
64+
Logger& logger,
65+
bool set_to_null_controller
66+
) const{
67+
return std::unique_ptr<ControllerConnection>(
68+
new SerialPABotBase2_Connection(logger, m_name, set_to_null_controller)
69+
);
70+
}
71+
std::unique_ptr<AbstractController> SerialPABotBase2_Descriptor::make_controller(
72+
Logger& logger,
73+
ControllerConnection& connection,
74+
ControllerType controller_type,
75+
ControllerResetMode reset_mode
76+
) const{
77+
#if 0
78+
switch (controller_type){
79+
#if 0
80+
case ControllerType::HID_Keyboard:
81+
return std::unique_ptr<AbstractController>(
82+
new PokemonAutomation::StandardHid::SerialPABotBase_Keyboard(
83+
logger,
84+
static_cast<SerialPABotBase_Connection&>(connection),
85+
reset_mode
86+
)
87+
);
88+
#endif
89+
#if 0
90+
case ControllerType::NintendoSwitch_WiredController:
91+
case ControllerType::NintendoSwitch2_WiredController:
92+
return std::unique_ptr<AbstractController>(
93+
new PokemonAutomation::NintendoSwitch::SerialPABotBase_WiredController(
94+
logger,
95+
static_cast<SerialPABotBase_Connection&>(connection),
96+
controller_type,
97+
reset_mode
98+
)
99+
);
100+
101+
case ControllerType::NintendoSwitch_WiredProController:
102+
case ControllerType::NintendoSwitch_WirelessProController:
103+
return std::unique_ptr<AbstractController>(
104+
new PokemonAutomation::NintendoSwitch::SerialPABotBase_ProController(
105+
logger,
106+
static_cast<SerialPABotBase_Connection&>(connection),
107+
controller_type,
108+
reset_mode
109+
)
110+
);
111+
112+
case ControllerType::NintendoSwitch_WiredLeftJoycon:
113+
case ControllerType::NintendoSwitch_WirelessLeftJoycon:
114+
return std::unique_ptr<AbstractController>(
115+
new PokemonAutomation::NintendoSwitch::SerialPABotBase_LeftJoycon(
116+
logger,
117+
static_cast<SerialPABotBase_Connection&>(connection),
118+
controller_type,
119+
reset_mode
120+
)
121+
);
122+
123+
case ControllerType::NintendoSwitch_WiredRightJoycon:
124+
case ControllerType::NintendoSwitch_WirelessRightJoycon:
125+
return std::unique_ptr<AbstractController>(
126+
new PokemonAutomation::NintendoSwitch::SerialPABotBase_RightJoycon(
127+
logger,
128+
static_cast<SerialPABotBase_Connection&>(connection),
129+
controller_type,
130+
reset_mode
131+
)
132+
);
133+
#endif
134+
135+
default:;
136+
}
137+
#endif
138+
139+
logger.log(
140+
std::string("Unsupported Controller Type: ") + CONTROLLER_TYPE_STRINGS.get_string(controller_type),
141+
COLOR_RED
142+
);
143+
return nullptr;
144+
}
145+
146+
147+
148+
QWidget* SerialPABotBase2_Descriptor::make_selector_QtWidget(ControllerSelectorWidget& parent) const{
149+
return new SerialPABotBase2_SelectorWidget(parent, this);
150+
}
151+
152+
153+
154+
155+
}
156+
}

0 commit comments

Comments
 (0)