Skip to content

Commit 9aec59c

Browse files
committed
Add ability to read the controller mode from the ESP32.
1 parent 7b84845 commit 9aec59c

17 files changed

+138
-57
lines changed

Common/Microcontroller/DeviceRoutines.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ uint8_t program_id(BotBaseController& botbase){
3939
).convert<PABB_MSG_ACK_REQUEST_I8>(botbase.logger(), response);
4040
return response.data;
4141
}
42+
uint32_t controller_mode(BotBaseController& botbase){
43+
pabb_MsgAckRequestI32 response;
44+
botbase.issue_request_and_wait(
45+
DeviceRequest_controller_mode()
46+
).convert<PABB_MSG_ACK_REQUEST_I32>(botbase.logger(), response);
47+
return response.data;
48+
}
4249

4350

4451
}

Common/Microcontroller/DeviceRoutines.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ uint32_t protocol_version(BotBaseController& botbase);
1818
uint32_t program_version(BotBaseController& botbase);
1919
uint8_t device_queue_size(BotBaseController& botbase);
2020
uint8_t program_id(BotBaseController& botbase);
21+
uint32_t controller_mode(BotBaseController& botbase);
2122

2223

2324
class DeviceRequest_seqnum_reset : public BotBaseRequest{
@@ -90,6 +91,16 @@ class DeviceRequest_program_id : public BotBaseRequest{
9091
return BotBaseMessage(PABB_MSG_REQUEST_PROGRAM_ID, params);
9192
}
9293
};
94+
class DeviceRequest_controller_mode : public BotBaseRequest{
95+
public:
96+
pabb_MsgRequestControllerMode params;
97+
DeviceRequest_controller_mode()
98+
: BotBaseRequest(false)
99+
{}
100+
virtual BotBaseMessage message() const override{
101+
return BotBaseMessage(PABB_MSG_REQUEST_CONTROLLER_MODE, params);
102+
}
103+
};
93104

94105

95106

Common/Microcontroller/MessageProtocol.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ typedef struct{
262262
uint32_t data;
263263
} PABB_PACK pabb_MsgAckRequestI32;
264264

265+
////////////////////////////////////////////////////////////////////////////////
266+
// Custom Info
267+
268+
#define PABB_MSG_INFO_I32 0x20
269+
typedef struct{
270+
uint8_t tag;
271+
uint32_t data;
272+
} PABB_PACK pabb_MsgInfoI32;
273+
265274
////////////////////////////////////////////////////////////////////////////////
266275
// Requests
267276
#define PABB_MSG_SEQNUM_RESET 0x40
@@ -313,14 +322,10 @@ typedef struct{
313322
seqnum_t seqnum;
314323
} PABB_PACK pabb_MsgRequestQueueSize;
315324

316-
////////////////////////////////////////////////////////////////////////////////
317-
// Custom Info
318-
319-
#define PABB_MSG_INFO_I32 0x20
325+
#define PABB_MSG_REQUEST_CONTROLLER_MODE 0x49
320326
typedef struct{
321-
uint8_t tag;
322-
uint32_t data;
323-
} PABB_PACK pabb_MsgInfoI32;
327+
seqnum_t seqnum;
328+
} PABB_PACK pabb_MsgRequestControllerMode;
324329

325330
////////////////////////////////////////////////////////////////////////////////
326331
// Commands

Common/PokemonSwSh/PokemonProgramIDs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#define PABB_PID_PABOTBASE_31KB 0x09
1414
#define PABB_PID_PABOTBASE_ESP32 0x10
1515

16+
#define PABB_CID_NONE 0
17+
#define PABB_CID_NINTENDO_SWITCH_WIRED_PRO_CONTROLLER 1
18+
#define PABB_CID_NINTENDO_SWITCH_WIRELESS_PRO_CONTROLLER 2
19+
#define PABB_CID_NINTENDO_SWITCH_LEFT_JOYCON 3
20+
#define PABB_CID_NINTENDO_SWITCH_RIGHT_JOYCON 4
1621

1722

1823
#endif

SerialPrograms/Source/Controllers/ControllerConnection.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace PokemonAutomation{
1818
void ControllerConnection::add_status_listener(StatusListener& listener){
1919
m_status_listeners.add(listener);
2020
if (m_ready.load(std::memory_order_acquire)){
21-
listener.post_connection_ready(*this, supported_controllers());
21+
listener.post_connection_ready(*this, controller_mode_status());
2222
}
2323
}
2424
void ControllerConnection::remove_status_listener(StatusListener& listener){
@@ -51,17 +51,17 @@ void ControllerConnection::set_status_line1(const std::string& text, Color color
5151
}
5252
signal_status_text_changed(status_text());
5353
}
54-
void ControllerConnection::declare_ready(const std::map<ControllerType, std::set<ControllerFeature>>& controllers){
54+
void ControllerConnection::declare_ready(const ControllerModeStatus& mode_status){
5555
m_ready.store(true, std::memory_order_release);
56-
signal_post_ready(controllers);
56+
signal_post_ready(mode_status);
5757
}
5858

5959

6060
//void ControllerConnection::signal_pre_not_ready(){
6161
// m_status_listeners.run_method_unique(&StatusListener::pre_connection_not_ready, *this);
6262
//}
63-
void ControllerConnection::signal_post_ready(const std::map<ControllerType, std::set<ControllerFeature>>& controllers){
64-
m_status_listeners.run_method_unique(&StatusListener::post_connection_ready, *this, controllers);
63+
void ControllerConnection::signal_post_ready(const ControllerModeStatus& mode_status){
64+
m_status_listeners.run_method_unique(&StatusListener::post_connection_ready, *this, mode_status);
6565
}
6666
void ControllerConnection::signal_status_text_changed(const std::string& text){
6767
// cout << "m_status_listeners.size() = " << m_status_listeners.count_unique() << endl;

SerialPrograms/Source/Controllers/ControllerConnection.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
namespace PokemonAutomation{
1616

1717

18+
19+
struct ControllerModeStatus{
20+
ControllerType current_controller = ControllerType::None;
21+
std::map<ControllerType, std::set<ControllerFeature>> supported_controllers;
22+
};
23+
24+
25+
1826
class ControllerConnection{
1927
public:
2028
struct StatusListener{
2129
// virtual void pre_connection_not_ready(ControllerConnection& connection){}
2230
virtual void post_connection_ready(
2331
ControllerConnection& connection,
24-
const std::map<ControllerType, std::set<ControllerFeature>>& controllers
32+
const ControllerModeStatus& mode_status
2533
){}
2634
virtual void status_text_changed(
2735
ControllerConnection& connection, const std::string& text
@@ -41,7 +49,10 @@ class ControllerConnection{
4149
bool is_ready() const{ return m_ready.load(std::memory_order_acquire); }
4250
std::string status_text() const;
4351

44-
virtual std::map<ControllerType, std::set<ControllerFeature>> supported_controllers() const = 0;
52+
// Returns the current controller type and the list of supported controllers.
53+
// The current controller may be "None" if there is only one supported
54+
// controller since it is implied to be that.
55+
virtual ControllerModeStatus controller_mode_status() const = 0;
4556

4657

4758
public:
@@ -50,12 +61,12 @@ class ControllerConnection{
5061

5162

5263
protected:
53-
void declare_ready(const std::map<ControllerType, std::set<ControllerFeature>>& controllers);
64+
void declare_ready(const ControllerModeStatus& mode_status);
5465

5566

5667
private:
5768
// void signal_pre_not_ready();
58-
void signal_post_ready(const std::map<ControllerType, std::set<ControllerFeature>>& controllers);
69+
void signal_post_ready(const ControllerModeStatus& mode_status);
5970
void signal_status_text_changed(const std::string& text);
6071
void signal_error(const std::string& text);
6172

SerialPrograms/Source/Controllers/ControllerSession.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ ControllerSession::ControllerSession(
6262
// If we already missed it, run it ourselves.
6363
if (m_connection->is_ready()){
6464
ControllerSession::post_connection_ready(
65-
*m_connection, m_connection->supported_controllers()
65+
*m_connection,
66+
m_connection->controller_mode_status()
6667
);
6768
}
6869
}catch (...){
@@ -180,7 +181,8 @@ void ControllerSession::make_controller(){
180181
// If we already missed it, run it ourselves.
181182
if (ready){
182183
ControllerSession::post_connection_ready(
183-
*m_connection, m_connection->supported_controllers()
184+
*m_connection,
185+
m_connection->controller_mode_status()
184186
);
185187
}
186188
}
@@ -305,18 +307,21 @@ std::string ControllerSession::reset(){
305307
//}
306308
void ControllerSession::post_connection_ready(
307309
ControllerConnection& connection,
308-
const std::map<ControllerType, std::set<ControllerFeature>>& controllers
310+
const ControllerModeStatus& mode_status
309311
){
310-
if (controllers.empty()){
312+
const std::map<ControllerType, std::set<ControllerFeature>>& supported_controllers = mode_status.supported_controllers;
313+
if (supported_controllers.empty()){
311314
return;
312315
}
313316

317+
ControllerType current_controller = mode_status.current_controller;
318+
314319
// cout << "sleeping" << endl;
315320
// Sleep(10000);
316321

317322

318323
std::vector<ControllerType> available_controllers;
319-
ControllerType selected_controller = ControllerType::None;
324+
// ControllerType selected_controller = ControllerType::None;
320325
bool ready;
321326
{
322327
std::lock_guard<std::mutex> lg(m_state_lock);
@@ -333,10 +338,10 @@ void ControllerSession::post_connection_ready(
333338

334339
// We only show the "none" option when there are multiple controllers
335340
// to choose from.
336-
if (controllers.size() > 1){
341+
if (supported_controllers.size() > 1){
337342
available_controllers.emplace_back(ControllerType::None);
338343
}
339-
for (const auto& item : controllers){
344+
for (const auto& item : supported_controllers){
340345
available_controllers.emplace_back(item.first);
341346
}
342347

@@ -345,30 +350,30 @@ void ControllerSession::post_connection_ready(
345350
m_available_controllers = available_controllers;
346351

347352

348-
auto iter = controllers.begin();
349-
if (controllers.size() == 1){
353+
auto iter = supported_controllers.begin();
354+
if (supported_controllers.size() == 1){
350355
// Only one controller available. Force the option to it.
351-
selected_controller = iter->first;
356+
current_controller = iter->first;
352357
}else{
353358
// Keep the current controller only if it exists.
354-
iter = controllers.find(m_option.m_controller_type);
355-
if (iter != controllers.end()){
356-
selected_controller = m_option.m_controller_type;
359+
iter = supported_controllers.find(m_option.m_controller_type);
360+
if (iter != supported_controllers.end()){
361+
current_controller = m_option.m_controller_type;
357362
}
358363
}
359364

360365
// Construct the controller.
361-
if (selected_controller != ControllerType::None){
366+
if (current_controller != ControllerType::None){
362367
m_controller = m_descriptor->make_controller(
363368
m_logger,
364369
*m_connection,
365-
selected_controller,
370+
current_controller,
366371
m_requirements
367372
);
368373
}
369374

370375
// Commit all changes.
371-
m_option.m_controller_type = selected_controller;
376+
m_option.m_controller_type = current_controller;
372377
ready = m_controller && m_controller->is_ready();
373378

374379
WriteSpinLock lg1(m_message_lock);
@@ -377,7 +382,7 @@ void ControllerSession::post_connection_ready(
377382
}
378383
}
379384

380-
signal_controller_changed(selected_controller, available_controllers);
385+
signal_controller_changed(current_controller, available_controllers);
381386
signal_ready_changed(ready);
382387
signal_status_text_changed(status_text());
383388
}

SerialPrograms/Source/Controllers/ControllerSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ControllerSession : private ControllerConnection::StatusListener{
123123
// virtual void pre_connection_not_ready(ControllerConnection& connection) override;
124124
virtual void post_connection_ready(
125125
ControllerConnection& connection,
126-
const std::map<ControllerType, std::set<ControllerFeature>>& controllers
126+
const ControllerModeStatus& mode_status
127127
) override;
128128
virtual void status_text_changed(
129129
ControllerConnection& connection,

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ std::string program_name(uint8_t id){
3131
default: return "Unknown ID";
3232
}
3333
}
34+
ControllerType controller_type(uint32_t id){
35+
switch (id){
36+
case PABB_CID_NONE:
37+
return ControllerType::None;
38+
case PABB_CID_NINTENDO_SWITCH_WIRED_PRO_CONTROLLER:
39+
return ControllerType::NintendoSwitch_WiredProController;
40+
case PABB_CID_NINTENDO_SWITCH_WIRELESS_PRO_CONTROLLER:
41+
return ControllerType::NintendoSwitch_WirelessProController;
42+
case PABB_CID_NINTENDO_SWITCH_LEFT_JOYCON:
43+
return ControllerType::NintendoSwitch_LeftJoycon;
44+
case PABB_CID_NINTENDO_SWITCH_RIGHT_JOYCON:
45+
return ControllerType::NintendoSwitch_RightJoycon;
46+
default:
47+
return ControllerType::None;
48+
}
49+
}
3450

3551

3652

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern const ControllerRequirements OLD_NINTENDO_SWITCH_DEFAULT_REQUIREMENTS;
2424

2525

2626
std::string program_name(uint8_t id);
27+
ControllerType controller_type(uint32_t id);
2728

2829

2930

0 commit comments

Comments
 (0)