Skip to content

Commit 665fa9d

Browse files
committed
Fix bot integration for button presses.
1 parent 6053238 commit 665fa9d

File tree

9 files changed

+120
-74
lines changed

9 files changed

+120
-74
lines changed

SerialPrograms/Source/Controllers/ControllerSession.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#include "Common/Cpp/Exceptions.h"
88
#include "ControllerSession.h"
99

10-
//#include <iostream>
11-
//using std::cout;
12-
//using std::endl;
10+
// REMOVE
11+
#include <iostream>
12+
using std::cout;
13+
using std::endl;
1314

1415
namespace PokemonAutomation{
1516

@@ -105,6 +106,7 @@ std::string ControllerSession::user_input_blocked() const{
105106
}
106107
void ControllerSession::set_user_input_blocked(std::string disallow_reason){
107108
std::lock_guard<std::mutex> lg(m_state_lock);
109+
// cout << "set_user_input_blocked() = " << disallow_reason << endl;
108110
m_user_input_disallow_reason = std::move(disallow_reason);
109111
}
110112

SerialPrograms/Source/Controllers/ControllerSession.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#define PokemonAutomation_Controllers_ControllerSession_H
99

1010
#include <mutex>
11-
#include "Common/Cpp/ListenerSet.h"
1211
#include "Common/Cpp/Exceptions.h"
12+
#include "Common/Cpp/ListenerSet.h"
13+
//#include "Common/Cpp/Exceptions.h"
1314
#include "ControllerDescriptor.h"
1415
#include "ControllerConnection.h"
1516

@@ -76,25 +77,30 @@ class ControllerSession{
7677
std::string reset();
7778

7879
// Try to run the following lambda on the underlying controller type.
79-
// Returns true if successful.
80+
// Returns empty string if successful. Otherwise returns error message.
8081
template <typename ControllerType, typename Lambda>
81-
bool try_run(Lambda&& function) noexcept{
82+
std::string try_run(Lambda&& function) noexcept{
8283
std::lock_guard<std::mutex> lg(m_state_lock);
8384
if (!m_connection){
84-
return false;
85+
return "Connection is null.";
8586
}
8687
try{
8788
// This will be a cross-cast in most cases.
8889
ControllerType* child = dynamic_cast<ControllerType*>(m_connection.get());
8990
if (child == nullptr){
9091
m_logger.log("ControllerSession::try_run(): Incompatible controller type cast.", COLOR_RED);
91-
return false;
92+
return "Incompatible controller type cast.";
93+
}
94+
if (!m_user_input_disallow_reason.empty()){
95+
return m_user_input_disallow_reason;
9296
}
9397
function(*child);
98+
}catch (Exception& e){
99+
return e.to_str();
94100
}catch (...){
95-
return false;
101+
return "Unknown exception.";
96102
}
97-
return true;
103+
return "";
98104
}
99105

100106

SerialPrograms/Source/Controllers/SuperscalarScheduler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ void SuperscalarScheduler::clear() noexcept{
5252
}
5353

5454
bool SuperscalarScheduler::iterate_schedule(const Cancellable* cancellable){
55-
cancellable->throw_if_cancelled();
55+
if (cancellable){
56+
cancellable->throw_if_cancelled();
57+
}
5658

5759
if (m_state_changes.empty()){
5860
m_device_sent_time = m_device_issue_time;

SerialPrograms/Source/Integrations/ProgramTracker.cpp

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "CommonFramework/Logging/Logger.h"
88
#include "CommonFramework/ImageTypes/ImageRGB32.h"
99
#include "CommonFramework/VideoPipeline/VideoFeed.h"
10+
#include "Controllers/ControllerSession.h"
1011
#include "Controllers/SerialPABotBase/SerialPABotBase_Handle.h"
12+
#include "NintendoSwitch/Controllers/NintendoSwitch_Controller.h"
1113
#include "NintendoSwitch/Commands/NintendoSwitch_Messages_Superscalar.h"
1214
#include "ProgramTracker.h"
1315

@@ -17,6 +19,8 @@ using std::endl;
1719

1820
namespace PokemonAutomation{
1921

22+
using namespace std::chrono_literals;
23+
2024

2125
ProgramTracker& ProgramTracker::instance(){
2226
static ProgramTracker obj;
@@ -109,6 +113,7 @@ std::string ProgramTracker::stop_program(uint64_t program_id){
109113
return "";
110114
}
111115
std::string ProgramTracker::nsw_press_button(uint64_t console_id, NintendoSwitch::Button button, uint16_t ticks){
116+
using namespace NintendoSwitch;
112117
std::string header = "press_button(ID = " + std::to_string(console_id) + ")";
113118
std::lock_guard<std::mutex> lg(m_lock);
114119
auto iter = m_consoles.find(console_id);
@@ -117,19 +122,28 @@ std::string ProgramTracker::nsw_press_button(uint64_t console_id, NintendoSwitch
117122
global_logger_tagged().log("SwitchProgramTracker::" + error, COLOR_RED);
118123
return error;
119124
}
120-
const char* err = iter->second.first->sender().try_send_request(
121-
// NintendoSwitch::DeviceRequest_pbf_press_button(button, ticks, 0)
122-
NintendoSwitch::DeviceRequest_ssf_press_button(button, ticks, ticks, 0)
123-
);
124-
if (err){
125-
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
126-
return err;
127-
}else{
125+
Milliseconds duration = ticks * 8ms;
126+
std::string err;
127+
try{
128+
err = iter->second.first->controller().try_run<SwitchController>(
129+
[=](SwitchController& controller){
130+
controller.issue_buttons(nullptr, button, duration, duration, 0ms);
131+
}
132+
);
133+
}catch (Exception& e){
134+
e.log(global_logger_tagged());
135+
err = e.to_str();
136+
}
137+
if (err.empty()){
128138
global_logger_tagged().log("SwitchProgramTracker::" + header, COLOR_BLUE);
129139
return "";
140+
}else{
141+
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
142+
return err;
130143
}
131144
}
132145
std::string ProgramTracker::nsw_press_dpad(uint64_t console_id, NintendoSwitch::DpadPosition position, uint16_t ticks){
146+
using namespace NintendoSwitch;
133147
std::string header = "press_dpad(ID = " + std::to_string(console_id) + ")";
134148
std::lock_guard<std::mutex> lg(m_lock);
135149
auto iter = m_consoles.find(console_id);
@@ -138,19 +152,28 @@ std::string ProgramTracker::nsw_press_dpad(uint64_t console_id, NintendoSwitch::
138152
global_logger_tagged().log("SwitchProgramTracker::" + error, COLOR_RED);
139153
return error;
140154
}
141-
const char* err = iter->second.first->sender().try_send_request(
142-
// NintendoSwitch::DeviceRequest_pbf_press_dpad(position, ticks, 0)
143-
NintendoSwitch::DeviceRequest_ssf_press_dpad(position, ticks, ticks, 0)
144-
);
145-
if (err){
146-
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
147-
return err;
148-
}else{
155+
Milliseconds duration = ticks * 8ms;
156+
std::string err;
157+
try{
158+
err = iter->second.first->controller().try_run<SwitchController>(
159+
[=](SwitchController& controller){
160+
controller.issue_dpad(nullptr, position, duration, duration, 0ms);
161+
}
162+
);
163+
}catch (Exception& e){
164+
e.log(global_logger_tagged());
165+
err = e.to_str();
166+
}
167+
if (err.empty()){
149168
global_logger_tagged().log("SwitchProgramTracker::" + header, COLOR_BLUE);
150169
return "";
170+
}else{
171+
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
172+
return err;
151173
}
152174
}
153175
std::string ProgramTracker::nsw_press_left_joystick(uint64_t console_id, uint8_t x, uint8_t y, uint16_t ticks){
176+
using namespace NintendoSwitch;
154177
std::string header = "press_left_joystick(ID = " + std::to_string(console_id) + ")";
155178
std::lock_guard<std::mutex> lg(m_lock);
156179
auto iter = m_consoles.find(console_id);
@@ -159,19 +182,28 @@ std::string ProgramTracker::nsw_press_left_joystick(uint64_t console_id, uint8_t
159182
global_logger_tagged().log("SwitchProgramTracker::" + error, COLOR_RED);
160183
return error;
161184
}
162-
const char* err = iter->second.first->sender().try_send_request(
163-
// NintendoSwitch::DeviceRequest_pbf_move_left_joystick(x, y, ticks, 0)
164-
NintendoSwitch::DeviceRequest_ssf_press_joystick(true, x, y, ticks, ticks, 0)
165-
);
166-
if (err){
167-
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
168-
return err;
169-
}else{
185+
Milliseconds duration = ticks * 8ms;
186+
std::string err;
187+
try{
188+
err = iter->second.first->controller().try_run<SwitchController>(
189+
[=](SwitchController& controller){
190+
controller.issue_left_joystick(nullptr, x, y, duration, duration, 0ms);
191+
}
192+
);
193+
}catch (Exception& e){
194+
e.log(global_logger_tagged());
195+
err = e.to_str();
196+
}
197+
if (err.empty()){
170198
global_logger_tagged().log("SwitchProgramTracker::" + header, COLOR_BLUE);
171199
return "";
200+
}else{
201+
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
202+
return err;
172203
}
173204
}
174205
std::string ProgramTracker::nsw_press_right_joystick(uint64_t console_id, uint8_t x, uint8_t y, uint16_t ticks){
206+
using namespace NintendoSwitch;
175207
std::string header = "press_right_joystick(ID = " + std::to_string(console_id) + ")";
176208
std::lock_guard<std::mutex> lg(m_lock);
177209
auto iter = m_consoles.find(console_id);
@@ -180,16 +212,24 @@ std::string ProgramTracker::nsw_press_right_joystick(uint64_t console_id, uint8_
180212
global_logger_tagged().log("SwitchProgramTracker::" + error, COLOR_RED);
181213
return error;
182214
}
183-
const char* err = iter->second.first->sender().try_send_request(
184-
// NintendoSwitch::DeviceRequest_pbf_move_right_joystick(x, y, ticks, 0)
185-
NintendoSwitch::DeviceRequest_ssf_press_joystick(false, x, y, ticks, ticks, 0)
186-
);
187-
if (err){
188-
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
189-
return err;
190-
}else{
215+
Milliseconds duration = ticks * 8ms;
216+
std::string err;
217+
try{
218+
err = iter->second.first->controller().try_run<SwitchController>(
219+
[=](SwitchController& controller){
220+
controller.issue_right_joystick(nullptr, x, y, duration, duration, 0ms);
221+
}
222+
);
223+
}catch (Exception& e){
224+
e.log(global_logger_tagged());
225+
err = e.to_str();
226+
}
227+
if (err.empty()){
191228
global_logger_tagged().log("SwitchProgramTracker::" + header, COLOR_BLUE);
192229
return "";
230+
}else{
231+
global_logger_tagged().log("SwitchProgramTracker::" + header + ": " + err, COLOR_RED);
232+
return err;
193233
}
194234
}
195235

SerialPrograms/Source/Integrations/ProgramTrackerInterfaces.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515

1616
namespace PokemonAutomation{
1717

18-
class BotBaseHandle;
1918
class VideoFeed;
2019
class AudioFeed;
20+
class ControllerSession;
21+
class BotBaseHandle;
2122

2223

2324
class TrackableConsole{
2425
public:
2526
virtual BotBaseHandle& sender() = 0;
2627
virtual VideoFeed& video() = 0;
2728
virtual AudioFeed& audio() = 0;
29+
virtual ControllerSession& controller() = 0;
2830
};
2931

3032
class TrackableProgram{

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void SwitchController_SerialPABotBase::issue_left_joystick(
377377
m_left_joystick.x = x;
378378
m_left_joystick.y = y;
379379
this->issue_to_resource(cancellable, m_left_joystick, delay, hold, cooldown);
380-
// cout << "(" << (unsigned)x << "," << (unsigned)y << "), hold = " << hold / 8 << endl; // REMOVE
380+
// cout << "(" << (unsigned)x << "," << (unsigned)y << "), hold = " << hold / 8 << endl;
381381
}
382382
void SwitchController_SerialPABotBase::issue_right_joystick(
383383
const Cancellable* cancellable,

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemSession.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ class SwitchSystemSession final : public TrackableConsole{
5454
bool allow_commands_while_running() const{ return m_option.m_allow_commands_while_running; }
5555

5656
Logger& logger(){ return m_logger; }
57-
virtual BotBaseHandle& sender() override;
5857
virtual VideoFeed& video() override{ return *m_camera; }
5958
virtual AudioFeed& audio() override{ return m_audio; }
59+
virtual BotBaseHandle& sender() override;
60+
virtual ControllerSession& controller() override{ return m_controller; };
6061
VideoOverlay& overlay(){ return m_overlay; }
6162
const StreamHistorySession& stream_history() const{ return m_history; }
6263

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,16 @@ bool VirtualController::on_key_release(Qt::Key key){
168168

169169

170170
bool VirtualController::try_stop_commands(){
171-
return m_session.try_run<SwitchController>([](SwitchController& controller){
171+
std::string error = m_session.try_run<SwitchController>([](SwitchController& controller){
172172
controller.cancel_all(nullptr);
173173
});
174+
return error.empty();
174175
}
175176
bool VirtualController::try_next_interrupt(){
176-
return m_session.try_run<SwitchController>([](SwitchController& controller){
177+
std::string error = m_session.try_run<SwitchController>([](SwitchController& controller){
177178
controller.replace_on_next_command(nullptr);
178179
});
180+
return error.empty();
179181
}
180182

181183

@@ -251,8 +253,7 @@ void VirtualController::thread_loop(){
251253
")",
252254
COLOR_DARKGREEN
253255
);
254-
bool success = false;
255-
success = m_session.try_run<SwitchController>([=](SwitchController& controller){
256+
std::string error = m_session.try_run<SwitchController>([=](SwitchController& controller){
256257
controller.issue_controller_state(
257258
nullptr,
258259
state.buttons,
@@ -264,7 +265,7 @@ void VirtualController::thread_loop(){
264265
255*8ms
265266
);
266267
});
267-
if (!success){
268+
if (!error.empty()){
268269
next_wake = now + std::chrono::milliseconds(PABB_RETRANSMIT_DELAY_MILLIS);
269270
break;
270271
}

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_SwitchSystemWidget.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -169,30 +169,22 @@ SwitchSystemWidget::SwitchSystemWidget(
169169

170170

171171
void SwitchSystemWidget::update_ui(ProgramState state){
172-
#if 0
173-
if (!m_session.allow_commands_while_running()){
174-
m_session.set_allow_user_commands(
175-
state == ProgramState::STOPPED
176-
? ""
177-
: "<font color=\"orange\">Program is not ready.</font>"
178-
);
179-
}
180-
switch (state){
181-
case ProgramState::NOT_READY:
182-
m_session.set_allow_user_commands("<font color=\"orange\">Program is not ready.</font>");
183-
// m_serial_widget->set_options_enabled(false);
184-
break;
185-
case ProgramState::STOPPED:
186-
m_session.set_allow_user_commands(true);
187-
// m_serial_widget->set_options_enabled(true);
188-
break;
189-
case ProgramState::RUNNING:
190-
case ProgramState::STOPPING:
191-
m_session.set_allow_user_commands(false);
192-
// m_serial_widget->set_options_enabled(false);
193-
break;
172+
if (m_session.allow_commands_while_running()){
173+
m_session.set_allow_user_commands("");
174+
}else{
175+
switch (state){
176+
case ProgramState::NOT_READY:
177+
m_session.set_allow_user_commands("Program is not ready.");
178+
break;
179+
case ProgramState::STOPPED:
180+
m_session.set_allow_user_commands("");
181+
break;
182+
case ProgramState::RUNNING:
183+
case ProgramState::STOPPING:
184+
m_session.set_allow_user_commands("Program is running.");
185+
break;
186+
}
194187
}
195-
#endif
196188
m_command->on_state_changed(state);
197189
}
198190

0 commit comments

Comments
 (0)