1010#include " Common/Cpp/Exceptions.h"
1111#include " Common/Cpp/PanicDump.h"
1212#include " Common/Microcontroller/DeviceRoutines.h"
13+ #include " Common/PokemonSwSh/PokemonProgramIDs.h"
1314#include " ClientSource/Libraries/MessageConverter.h"
1415#include " ClientSource/Connection/SerialConnection.h"
1516#include " ClientSource/Connection/PABotBase.h"
@@ -32,7 +33,8 @@ namespace SerialPABotBase{
3233
3334SerialPABotBase_Connection::SerialPABotBase_Connection (
3435 Logger& logger,
35- const QSerialPortInfo* port
36+ const QSerialPortInfo* port,
37+ std::optional<ControllerType> change_controller
3638)
3739 : m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
3840{
@@ -78,7 +80,11 @@ SerialPABotBase_Connection::SerialPABotBase_Connection(
7880 return ;
7981 }
8082
81- m_status_thread = std::thread (run_with_catch, " SerialPABotBase_Connection::thread_body()" , [this ]{ thread_body (); });
83+ m_status_thread = std::thread (
84+ run_with_catch,
85+ " SerialPABotBase_Connection::thread_body()" ,
86+ [=, this ]{ thread_body (change_controller); }
87+ );
8288}
8389SerialPABotBase_Connection::~SerialPABotBase_Connection (){
8490 m_ready.store (false , std::memory_order_release);
@@ -144,7 +150,9 @@ ControllerModeStatus SerialPABotBase_Connection::controller_mode_status() const{
144150}
145151
146152
147- ControllerModeStatus SerialPABotBase_Connection::read_device_specs (){
153+ ControllerModeStatus SerialPABotBase_Connection::read_device_specs (
154+ std::optional<ControllerType> change_controller
155+ ){
148156 Logger& logger = m_logger;
149157
150158
@@ -196,16 +204,34 @@ ControllerModeStatus SerialPABotBase_Connection::read_device_specs(){
196204 current_controller = program_iter->second .begin ()->first ;
197205 }else if (program_iter->second .size () > 1 ){
198206 uint32_t type_id = Microcontroller::read_controller_mode (*m_botbase);
199- current_controller = controller_type (type_id);
207+ current_controller = id_to_controller_type (type_id);
200208 }
201209 logger.log (" Reading Controller Mode... Mode = " + CONTROLLER_TYPE_STRINGS.get_string (current_controller));
202210
211+
212+ if (change_controller && program_iter->second .size () > 1 ){
213+ ControllerType desired_controller = change_controller.value ();
214+ uint32_t native_controller_id = controller_type_to_id (desired_controller);
215+ m_botbase->issue_request_and_wait (
216+ Microcontroller::DeviceRequest_change_controller_mode (native_controller_id),
217+ nullptr
218+ );
219+
220+ // Re-read the controller.
221+ logger.log (" Reading Controller Mode..." );
222+ uint32_t type_id = Microcontroller::read_controller_mode (*m_botbase);
223+ current_controller = id_to_controller_type (type_id);
224+ logger.log (" Reading Controller Mode... Mode = " + CONTROLLER_TYPE_STRINGS.get_string (current_controller));
225+ }
226+
203227 return {current_controller, program_iter->second };
204228}
205229
206230
207231
208- void SerialPABotBase_Connection::thread_body (){
232+ void SerialPABotBase_Connection::thread_body (
233+ std::optional<ControllerType> change_controller
234+ ){
209235 using namespace PokemonAutomation ;
210236
211237 m_botbase->set_sniffer (&m_logger);
@@ -236,7 +262,7 @@ void SerialPABotBase_Connection::thread_body(){
236262 ControllerModeStatus mode_status;
237263 std::string error;
238264 try {
239- mode_status = read_device_specs ();
265+ mode_status = read_device_specs (change_controller );
240266 std::lock_guard<std::mutex> lg (m_lock);
241267 m_mode_status = mode_status;
242268
0 commit comments