forked from PokemonAutomation/Arduino-Source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPybindSwitchController.cpp
More file actions
230 lines (198 loc) · 7.62 KB
/
Copy pathPybindSwitchController.cpp
File metadata and controls
230 lines (198 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Pybind Switch Controller
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Concurrency/Mutex.h"
#include "Common/Cpp/Concurrency/ConditionVariable.h"
#include "Common/Cpp/Logging/TaggedLogger.h"
#include "CommonFramework/Logging/Logger.h"
#include "Controllers/ControllerConnection.h"
#include "Controllers/PABotBase2/SerialPABotBase2_Descriptor.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
#include "PybindSwitchController.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace NintendoSwitch{
class PybindSwitchProControllerInternal final : public ControllerConnection::StatusListener{
public:
PybindSwitchProControllerInternal(const std::string& name)
: m_logger(global_logger_command_line(), "Pybind")
, m_descriptor(name)
, m_connection(m_descriptor.open_connection(m_logger))
{
m_connection->add_status_listener(*this);
}
~PybindSwitchProControllerInternal(){
m_connection->remove_status_listener(*this);
}
bool wait_for_ready(uint64_t timeout_millis){
std::unique_lock<Mutex> lg(m_lock);
m_cv.wait_for(lg, Milliseconds(timeout_millis), [this]{
return m_connected;
});
ProController* procon = m_procon.load(std::memory_order_relaxed);
return procon != nullptr && procon->is_ready();
}
virtual void post_connection_ready(ControllerConnection& connection) override{
// cout << "post_connection_ready()" << endl;
m_controller = m_descriptor.make_controller(
m_logger,
connection,
connection.current_controller()
);
ProController* procon = dynamic_cast<ProController*>(m_controller.get());
if (procon == nullptr){
// cout << "post_connection_ready() - incompatible" << endl;
m_connection->set_status_line1("Incompatible controller type.", COLOR_RED);
}else{
// cout << "post_connection_ready() - good" << endl;
m_procon.store(procon, std::memory_order_release);
}
{
std::unique_lock<Mutex> lg(m_lock);
m_connected = true;
}
m_cv.notify_all();
}
ProController* controller(){
return m_procon.load(std::memory_order_acquire);
}
public:
TaggedLogger m_logger;
SerialPABotBase::SerialPABotBase2_Descriptor m_descriptor;
std::unique_ptr<ControllerConnection> m_connection;
std::unique_ptr<AbstractController> m_controller;
std::atomic<ProController*> m_procon;
bool m_connected = false;
Mutex m_lock;
ConditionVariable m_cv;
};
PybindSwitchProController::PybindSwitchProController(const std::string& port_name){
PybindSwitchProControllerInternal* internal = new PybindSwitchProControllerInternal(port_name);
m_internals = internal;
}
PybindSwitchProController::~PybindSwitchProController(){
delete (PybindSwitchProControllerInternal*)m_internals;
}
bool PybindSwitchProController::wait_for_ready(uint64_t timeout_millis){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
return internal->wait_for_ready(timeout_millis);
}
bool PybindSwitchProController::is_ready() const{
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
return false;
}
return controller->is_ready();
}
std::string PybindSwitchProController::current_status() const{
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
return internal->m_connection->status_text();
}
void PybindSwitchProController::wait_for_all_requests(){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->wait_for_all(nullptr);
}
void PybindSwitchProController::wait(uint64_t duration){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->issue_nop(nullptr, Milliseconds(duration));
}
void PybindSwitchProController::push_button(uint64_t delay, uint64_t hold, uint64_t release, uint32_t bitfield){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->issue_buttons(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
(Button)bitfield
);
}
void PybindSwitchProController::push_dpad(uint64_t delay, uint64_t hold, uint64_t release, uint8_t position){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->issue_dpad(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
(DpadPosition)position
);
}
void PybindSwitchProController::push_left_joystick(uint64_t delay, uint64_t hold, uint64_t release, double x, double y){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->issue_left_joystick(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
{x, y}
);
}
void PybindSwitchProController::push_right_joystick(uint64_t delay, uint64_t hold, uint64_t release, double x, double y){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->issue_right_joystick(
nullptr,
Milliseconds(delay),
Milliseconds(hold),
Milliseconds(release),
{x, y}
);
}
void PybindSwitchProController::controller_state(
uint64_t duration,
uint32_t button_bitfield,
uint8_t dpad_position,
double left_x, double left_y,
double right_x, double right_y
){
PybindSwitchProControllerInternal* internal = (PybindSwitchProControllerInternal*)m_internals;
ProController* controller = internal->controller();
if (controller == nullptr){
internal->m_logger.log("Controller is not ready.", COLOR_RED);
return;
}
controller->issue_full_controller_state(
nullptr,
true,
Milliseconds(duration),
(Button)button_bitfield,
(DpadPosition)dpad_position,
{left_x, left_y},
{right_x, right_y}
);
}
}
}