Skip to content

Commit d01b17b

Browse files
committed
More work on controller infra. More work on sys-botbase.
1 parent c66f039 commit d01b17b

26 files changed

+528
-93
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,8 @@ file(GLOB MAIN_SOURCES
834834
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h
835835
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Descriptor.cpp
836836
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Descriptor.h
837+
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_ProController.cpp
838+
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_ProController.h
837839
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_SelectorWidget.h
838840
Source/NintendoSwitch/DevPrograms/BoxDraw.cpp
839841
Source/NintendoSwitch/DevPrograms/BoxDraw.h

SerialPrograms/SerialPrograms.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ SOURCES += \
413413
Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.cpp \
414414
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp \
415415
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Descriptor.cpp \
416+
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_ProController.cpp \
416417
Source/NintendoSwitch/DevPrograms/BoxDraw.cpp \
417418
Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp \
418419
Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp \
@@ -1568,6 +1569,7 @@ HEADERS += \
15681569
Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.h \
15691570
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h \
15701571
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Descriptor.h \
1572+
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_ProController.h \
15711573
Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_SelectorWidget.h \
15721574
Source/NintendoSwitch/DevPrograms/BoxDraw.h \
15731575
Source/NintendoSwitch/DevPrograms/TestProgramComputer.h \

SerialPrograms/Source/Controllers/ControllerConnection.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ class ControllerConnection{
3636

3737

3838
public:
39-
ControllerConnection(uint64_t sequence_number)
40-
: m_sequence_number(sequence_number)
41-
{}
4239
virtual ~ControllerConnection() = default;
4340

44-
uint64_t sequence_number() const{ return m_sequence_number; }
4541
bool is_ready() const{ return m_ready.load(std::memory_order_acquire); }
4642
std::string status_text() const;
4743

@@ -60,7 +56,6 @@ class ControllerConnection{
6056

6157

6258
protected:
63-
const uint64_t m_sequence_number;
6459
std::atomic<bool> m_ready;
6560

6661
private:

SerialPrograms/Source/Controllers/ControllerDescriptor.cpp

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#include "ControllerDescriptor.h"
1313
#include "NullController.h"
1414

15-
//#include <iostream>
16-
//using std::cout;
17-
//using std::endl;
15+
// REMOVE
16+
#include <iostream>
17+
using std::cout;
18+
using std::endl;
1819

1920
namespace PokemonAutomation{
2021

@@ -80,12 +81,28 @@ get_compatible_descriptors(const ControllerRequirements& requirements){
8081

8182

8283
ControllerOption::ControllerOption()
83-
: m_descriptor(new NullControllerDescriptor())
84-
, m_controller_type(ControllerType::None)
84+
: m_controller_type(ControllerType::None)
85+
, m_descriptor(new NullControllerDescriptor())
8586
{}
8687

88+
89+
void ControllerOption::set_descriptor(std::shared_ptr<const ControllerDescriptor> descriptor){
90+
m_descriptor_cache[descriptor->interface_type] = descriptor;
91+
m_descriptor = std::move(descriptor);
92+
}
93+
94+
std::shared_ptr<const ControllerDescriptor> ControllerOption::get_descriptor_from_cache(ControllerInterface interface_type) const{
95+
auto iter = m_descriptor_cache.find(interface_type);
96+
if (iter == m_descriptor_cache.end()){
97+
return nullptr;
98+
}
99+
return iter->second;
100+
}
101+
102+
103+
87104
void ControllerOption::load_json(const JsonValue& json){
88-
std::unique_ptr<const ControllerDescriptor> descriptor;
105+
std::shared_ptr<const ControllerDescriptor> descriptor;
89106
ControllerType controller_type = ControllerType::None;
90107
do{
91108
if (json.is_null()){
@@ -104,17 +121,26 @@ void ControllerOption::load_json(const JsonValue& json){
104121
if (controller != nullptr){
105122
controller_type = CONTROLLER_TYPE_STRINGS.get_enum(*controller, ControllerType::None);
106123
}
107-
const JsonValue* params = obj->get_value("Parameters");
108-
if (params == nullptr){
109-
break;
124+
125+
for (const auto& item : ALL_CONTROLLER_INTERFACES){
126+
const JsonValue* params = obj->get_value(CONTROLLER_INTERFACE_STRINGS.get_string(item.first));
127+
if (params == nullptr){
128+
continue;
129+
}
130+
m_descriptor_cache[item.first] = item.second->make(*params);
110131
}
111132

112-
auto iter = ALL_CONTROLLER_INTERFACES.find(CONTROLLER_INTERFACE_STRINGS.get_enum(*type, ControllerInterface::None));
113-
if (iter == ALL_CONTROLLER_INTERFACES.end()){
133+
// const JsonValue* params = obj->get_value("Parameters");
134+
// if (params == nullptr){
135+
// break;
136+
// }
137+
138+
auto iter = m_descriptor_cache.find(CONTROLLER_INTERFACE_STRINGS.get_enum(*type, ControllerInterface::None));
139+
if (iter == m_descriptor_cache.end()){
114140
break;
115141
}
116142

117-
descriptor = iter->second->make(*params);
143+
descriptor = iter->second;
118144

119145
}while (false);
120146

@@ -129,11 +155,16 @@ JsonValue ControllerOption::to_json() const{
129155
if (!m_descriptor){
130156
return JsonValue();
131157
}
132-
133158
JsonObject obj;
134159
obj["Interface"] = CONTROLLER_INTERFACE_STRINGS.get_string(m_descriptor->interface_type);
135160
obj["Controller"] = CONTROLLER_TYPE_STRINGS.get_string(m_controller_type);
136-
obj["Parameters"] = m_descriptor->to_json();
161+
// obj["Parameters"] = m_descriptor->to_json();
162+
163+
for (const auto& item : m_descriptor_cache){
164+
obj[CONTROLLER_INTERFACE_STRINGS.get_string(item.first)] = item.second->to_json();
165+
}
166+
167+
137168
return obj;
138169
}
139170

SerialPrograms/Source/Controllers/ControllerDescriptor.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string>
1111
#include <memory>
1212
#include "Common/Cpp/AbstractLogger.h"
13+
//#include "Common/Cpp/Json/JsonObject.h"
1314
#include "ControllerCapability.h"
1415
#include "Controller.h"
1516

@@ -97,9 +98,7 @@ class ControllerDescriptor{
9798
virtual void load_json(const JsonValue& json) = 0;
9899
virtual JsonValue to_json() const = 0;
99100

100-
virtual std::unique_ptr<ControllerConnection> open_connection(
101-
uint64_t sequence_number, Logger& logger
102-
) const = 0;
101+
virtual std::unique_ptr<ControllerConnection> open_connection(Logger& logger) const = 0;
103102
virtual std::unique_ptr<AbstractController> make_controller(
104103
Logger& logger,
105104
ControllerConnection& connection,
@@ -133,14 +132,24 @@ class ControllerOption{
133132
std::shared_ptr<const ControllerDescriptor> descriptor() const{
134133
return m_descriptor;
135134
}
135+
void set_descriptor(std::shared_ptr<const ControllerDescriptor> descriptor);
136136

137+
138+
std::shared_ptr<const ControllerDescriptor> get_descriptor_from_cache(ControllerInterface interface_type) const;
139+
140+
141+
public:
137142
void load_json(const JsonValue& json);
138143
JsonValue to_json() const;
139144

145+
146+
public:
147+
// friend class ControllerSession;
148+
ControllerType m_controller_type;
140149
private:
141-
friend class ControllerSession;
142150
std::shared_ptr<const ControllerDescriptor> m_descriptor;
143-
ControllerType m_controller_type;
151+
152+
std::map<ControllerInterface, std::shared_ptr<const ControllerDescriptor>> m_descriptor_cache;
144153
};
145154

146155

SerialPrograms/Source/Controllers/ControllerSelectorWidget.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
#include "SerialPABotBase/SerialPABotBase_SelectorWidget.h"
1414
#include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_SelectorWidget.h"
1515

16-
// REMOVE
17-
#include <iostream>
18-
using std::cout;
19-
using std::endl;
16+
//#include <iostream>
17+
//using std::cout;
18+
//using std::endl;
2019

2120
namespace PokemonAutomation{
2221

SerialPrograms/Source/Controllers/ControllerSession.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ControllerSession::ControllerSession(
5151
, m_options_locked(false)
5252
, m_connection_is_shutting_down(false)
5353
, m_descriptor(option.descriptor())
54-
, m_connection(m_descriptor->open_connection(0, logger))
54+
, m_connection(m_descriptor->open_connection(logger))
5555
{
5656
if (!m_connection){
5757
return;
@@ -166,7 +166,7 @@ void ControllerSession::make_controller(){
166166
bool ready = false;
167167
{
168168
std::lock_guard<std::mutex> lg(m_state_lock);
169-
m_connection = m_descriptor->open_connection(0, m_logger);
169+
m_connection = m_descriptor->open_connection(m_logger);
170170
if (m_connection){
171171
m_connection->add_status_listener(*this);
172172
ready = m_connection->is_ready();
@@ -204,6 +204,8 @@ bool ControllerSession::set_device(const std::shared_ptr<const ControllerDescrip
204204
controller = std::move(m_controller);
205205
connection = std::move(m_connection);
206206

207+
// cout << "setting..." << endl;
208+
m_option.set_descriptor(device);
207209
m_descriptor = device;
208210
}
209211

@@ -240,6 +242,8 @@ bool ControllerSession::set_controller(ControllerType controller_type){
240242
// Move these out to indicate that we should no longer access them.
241243
controller = std::move(m_controller);
242244
connection = std::move(m_connection);
245+
246+
m_option.m_controller_type = controller_type;
243247
}
244248

245249
// With the lock released, it is now safe to destroy them.
@@ -323,7 +327,8 @@ void ControllerSession::post_connection_ready(
323327
return;
324328
}
325329

326-
// We only show the "none" option when there are multiple controllers to choose from.
330+
// We only show the "none" option when there are multiple controllers
331+
// to choose from.
327332
if (controllers.size() > 1){
328333
available_controllers.emplace_back(ControllerType::None);
329334
}

SerialPrograms/Source/Controllers/NullController.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ void NullControllerDescriptor::load_json(const JsonValue& json){
2828
JsonValue NullControllerDescriptor::to_json() const{
2929
return JsonValue();
3030
}
31-
std::unique_ptr<ControllerConnection> NullControllerDescriptor::open_connection(
32-
uint64_t sequence_number, Logger& logger
33-
) const{
31+
std::unique_ptr<ControllerConnection> NullControllerDescriptor::open_connection(Logger& logger) const{
3432
return nullptr;
3533
}
3634
std::unique_ptr<AbstractController> NullControllerDescriptor::make_controller(

SerialPrograms/Source/Controllers/NullController.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class NullControllerDescriptor : public ControllerDescriptor{
2626
virtual void load_json(const JsonValue& json) override;
2727
virtual JsonValue to_json() const override;
2828

29-
virtual std::unique_ptr<ControllerConnection> open_connection(
30-
uint64_t sequence_number, Logger& logger
31-
) const override;
29+
virtual std::unique_ptr<ControllerConnection> open_connection(Logger& logger) const override;
3230
virtual std::unique_ptr<AbstractController> make_controller(
3331
Logger& logger,
3432
ControllerConnection& connection,

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ namespace SerialPABotBase{
3636

3737

3838
SerialPABotBase_Connection::SerialPABotBase_Connection(
39-
uint64_t sequence_number,
4039
Logger& logger,
4140
const QSerialPortInfo* port
4241
)
43-
: ControllerConnection(sequence_number)
44-
, m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
42+
: m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
4543
{
4644
set_label_text("Not Connected", COLOR_RED);
4745

0 commit comments

Comments
 (0)