Skip to content

Commit 8626e13

Browse files
committed
Partial refactor of ConsoleHandle -> VideoStream when controller is not needed.
1 parent 008fbf0 commit 8626e13

File tree

208 files changed

+2662
-2182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+2662
-2182
lines changed

SerialPrograms/Source/CommonFramework/Tools/VideoStream.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
* From: https://github.com/PokemonAutomation/Arduino-Source
44
*
55
* Represents a single video stream. Includes video, audio, as well
6-
* as their inference pivots.
6+
* as their inference pivots. Also includes a logger and the video overlay.
7+
*
8+
* This is the class that should be passed into inference modules as it
9+
* provides all the information needed for inference.
10+
*
11+
* This class has been separated from ConsoleHandle which used to take this
12+
* role. But 95% of the uses don't require the controller. And the controller
13+
* is messy due to being heterogeneous.
714
*
815
*/
916

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Routines.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace PokemonAutomation{
1717
namespace NintendoSwitch{
1818

1919

20-
void close_game(ConsoleHandle& console, SwitchControllerContext& context){
20+
void close_game(VideoStream& stream, SwitchControllerContext& context){
2121
#if 0
2222
context.issue_request(
2323
DeviceRequest_close_game()
@@ -38,16 +38,16 @@ void close_game(ConsoleHandle& console, SwitchControllerContext& context){
3838
context.wait_for_all_requests();
3939

4040
// send a second Home button press, if the first one is dropped
41-
bool video_available = (bool)console.video().snapshot();
41+
bool video_available = (bool)stream.video().snapshot();
4242
if (video_available){
4343
HomeWatcher detector;
4444
int ret = wait_until(
45-
console, context,
45+
stream, context,
4646
std::chrono::milliseconds(5000),
4747
{ detector }
4848
);
4949
if (ret == 0){
50-
console.log("Detected Home screen.");
50+
stream.log("Detected Home screen.");
5151
}else{ // if game initially open. | if game initially closed
5252
// initial Home button press was dropped
5353
pbf_press_button(context, BUTTON_HOME, 50, 50); // - Does nothing. | - goes back to home screen, from opened app

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Routines.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
#ifndef PokemonAutomation_NintendoSwitch_Commands_Routines_H
88
#define PokemonAutomation_NintendoSwitch_Commands_Routines_H
99

10+
#include "CommonFramework/Tools/VideoStream.h"
1011
#include "NintendoSwitch/Controllers/NintendoSwitch_Controller.h"
11-
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
1212

1313
namespace PokemonAutomation{
1414
namespace NintendoSwitch{
1515

1616

17-
void close_game(ConsoleHandle& console, SwitchControllerContext& device);
17+
void close_game(VideoStream& stream, SwitchControllerContext& device);
1818

1919

2020

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_DateReader.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
2323
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
2424
#include "NintendoSwitch/Inference/NintendoSwitch_DetectHome.h"
25-
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
2625
#include "NintendoSwitch_DateReader.h"
2726
#include "PokemonSwSh/Commands/PokemonSwSh_Commands_DateSpam.h"
2827

@@ -165,17 +164,17 @@ int8_t DateReader::read_hours(Logger& logger, const ImageViewRGB32& screen) cons
165164

166165

167166
void DateReader::set_hours(
168-
const ProgramInfo& info, ConsoleHandle& console, SwitchControllerContext& context,
167+
const ProgramInfo& info, VideoStream& stream, SwitchControllerContext& context,
169168
uint8_t hour
170169
) const{
171170
context.wait_for_all_requests();
172171
{
173-
auto snapshot = console.video().snapshot();
172+
auto snapshot = stream.video().snapshot();
174173
if (!detect(snapshot)){
175174
throw_and_log<FatalProgramException>(
176-
console, ErrorReport::SEND_ERROR_REPORT,
175+
stream.logger(), ErrorReport::SEND_ERROR_REPORT,
177176
"Expected date change menu.",
178-
console
177+
stream
179178
);
180179
}
181180
}
@@ -189,15 +188,15 @@ void DateReader::set_hours(
189188
context.wait_for(std::chrono::milliseconds(250));
190189

191190
// Read the hour.
192-
VideoSnapshot snapshot = console.video().snapshot();
193-
// int8_t current_hour = read_hours(console, snapshot);
194-
int8_t current_hour = read_date(console, snapshot).second.hour;
191+
VideoSnapshot snapshot = stream.video().snapshot();
192+
// int8_t current_hour = read_hours(stream, snapshot);
193+
int8_t current_hour = read_date(stream.logger(), snapshot).second.hour;
195194

196195
if (current_hour < 0){
197196
throw_and_log<FatalProgramException>(
198-
console, ErrorReport::SEND_ERROR_REPORT,
197+
stream.logger(), ErrorReport::SEND_ERROR_REPORT,
199198
"Unable to read the hour.",
200-
console
199+
stream
201200
);
202201
}
203202

@@ -250,9 +249,9 @@ void DateReader::set_hours(
250249

251250
// auto snapshot = console.video().snapshot();
252251
throw_and_log<FatalProgramException>(
253-
console, ErrorReport::SEND_ERROR_REPORT,
252+
stream.logger(), ErrorReport::SEND_ERROR_REPORT,
254253
"Failed to set the hour after 10 attempts.",
255-
console
254+
stream
256255
);
257256
}
258257

@@ -483,18 +482,18 @@ void DateReader::adjust_minute(SwitchControllerContext& context, int current, in
483482
}
484483

485484
void DateReader::set_date(
486-
const ProgramInfo& info, ConsoleHandle& console, SwitchControllerContext& context,
485+
const ProgramInfo& info, VideoStream& stream, SwitchControllerContext& context,
487486
const DateTime& date
488487
) const{
489488
context.wait_for_all_requests();
490489

491490
{
492-
auto snapshot = console.video().snapshot();
491+
auto snapshot = stream.video().snapshot();
493492
if (!detect(snapshot)){
494493
throw_and_log<OperationFailedException>(
495-
console, ErrorReport::SEND_ERROR_REPORT,
494+
stream.logger(), ErrorReport::SEND_ERROR_REPORT,
496495
"Expected date change menu.",
497-
&console,
496+
&stream,
498497
snapshot
499498
);
500499
}
@@ -507,8 +506,8 @@ void DateReader::set_date(
507506
context.wait_for_all_requests();
508507
context.wait_for(std::chrono::milliseconds(250));
509508

510-
auto snapshot = console.video().snapshot();
511-
std::pair<DateFormat, DateTime> current = read_date(console, snapshot);
509+
auto snapshot = stream.video().snapshot();
510+
std::pair<DateFormat, DateTime> current = read_date(stream.logger(), snapshot);
512511
if (current.second.year == date.year &&
513512
current.second.month == date.month &&
514513
current.second.day == date.day &&
@@ -589,9 +588,9 @@ void DateReader::set_date(
589588

590589
ssf_flush_pipeline(context);
591590
throw_and_log<FatalProgramException>(
592-
console, ErrorReport::SEND_ERROR_REPORT,
591+
stream.logger(), ErrorReport::SEND_ERROR_REPORT,
593592
"Failed to set the hour after 10 attempts.",
594-
console
593+
stream
595594
);
596595
}
597596

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_DateReader.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ namespace PokemonAutomation{
1818
class Logger;
1919
namespace NintendoSwitch{
2020

21-
class ConsoleHandle;
22-
2321

2422
enum class DateFormat{
2523
US,
@@ -42,14 +40,14 @@ class DateReader : public StaticScreenDetector{
4240
// int8_t read_hours(Logger& logger, std::shared_ptr<const ImageRGB32> screen) const;
4341

4442
void set_hours(
45-
const ProgramInfo& info, ConsoleHandle& console, SwitchControllerContext& context,
43+
const ProgramInfo& info, VideoStream& stream, SwitchControllerContext& context,
4644
uint8_t hour // 0 - 23
4745
) const;
4846

4947

5048
std::pair<DateFormat, DateTime> read_date(Logger& logger, std::shared_ptr<const ImageRGB32> screen) const;
5149
void set_date(
52-
const ProgramInfo& info, ConsoleHandle& console, SwitchControllerContext& context,
50+
const ProgramInfo& info, VideoStream& stream, SwitchControllerContext& context,
5351
const DateTime& date // Seconds is ignored.
5452
) const;
5553

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.cpp

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ namespace NintendoSwitch{
2828

2929

3030
void resume_game_from_home(
31-
ConsoleHandle& console,
32-
SwitchControllerContext& context,
31+
VideoStream& stream, SwitchControllerContext& context,
3332
bool skip_home_press
3433
){
3534
if (!skip_home_press){
@@ -41,12 +40,12 @@ void resume_game_from_home(
4140
{
4241
UpdateMenuWatcher update_detector;
4342
int ret = wait_until(
44-
console, context,
43+
stream, context,
4544
std::chrono::milliseconds(1000),
4645
{ update_detector }
4746
);
4847
if (ret == 0){
49-
console.log("Detected update window.", COLOR_RED);
48+
stream.log("Detected update window.", COLOR_RED);
5049

5150
pbf_press_dpad(context, DPAD_UP, 5, 0);
5251
pbf_press_button(context, BUTTON_A, 10, 500);
@@ -57,8 +56,8 @@ void resume_game_from_home(
5756

5857
// In case we failed to enter the game.
5958
HomeWatcher home_detector;
60-
if (home_detector.detect(console.video().snapshot())){
61-
console.log("Failed to re-enter game. Trying again...", COLOR_RED);
59+
if (home_detector.detect(stream.video().snapshot())){
60+
stream.log("Failed to re-enter game. Trying again...", COLOR_RED);
6261
pbf_press_button(context, BUTTON_HOME, 10, 10);
6362
continue;
6463
}else{
@@ -91,8 +90,7 @@ void move_to_user(SwitchControllerContext& context, uint8_t user_slot){
9190

9291

9392
void start_game_from_home_with_inference(
94-
ConsoleHandle& console,
95-
SwitchControllerContext& context,
93+
VideoStream& stream, SwitchControllerContext& context,
9694
uint8_t game_slot,
9795
uint8_t user_slot,
9896
uint16_t start_game_wait
@@ -101,19 +99,19 @@ void start_game_from_home_with_inference(
10199
{
102100
HomeWatcher detector;
103101
int ret = run_until<SwitchControllerContext>(
104-
console, context,
102+
stream, context,
105103
[](SwitchControllerContext& context){
106104
pbf_mash_button(context, BUTTON_B, 10 * TICKS_PER_SECOND);
107105
},
108106
{ detector }
109107
);
110108
if (ret == 0){
111-
console.log("Detected Home screen.");
109+
stream.log("Detected Home screen.");
112110
}else{
113111
OperationFailedException::fire(
114112
ErrorReport::SEND_ERROR_REPORT,
115113
"start_game_from_home_with_inference(): Failed to detect Home screen after 10 seconds.",
116-
console
114+
stream
117115
);
118116
}
119117
context.wait_for(std::chrono::milliseconds(100));
@@ -137,7 +135,7 @@ void start_game_from_home_with_inference(
137135
BlackScreenWatcher black_screen(COLOR_BLUE);
138136
context.wait_for_all_requests();
139137
int ret = wait_until(
140-
console, context,
138+
stream, context,
141139
std::chrono::seconds(30),
142140
{
143141
home,
@@ -153,52 +151,51 @@ void start_game_from_home_with_inference(
153151

154152
switch (ret){
155153
case 0:
156-
console.log("Detected home screen (again).", COLOR_RED);
154+
stream.log("Detected home screen (again).", COLOR_RED);
157155
pbf_press_button(context, BUTTON_A, 20, 105);
158156
break;
159157
case 1:
160-
console.log("Detected user-select screen.");
158+
stream.log("Detected user-select screen.");
161159
move_to_user(context, user_slot);
162160
pbf_press_button(context, BUTTON_A, 10, start_game_wait);
163161
break;
164162
case 2:
165-
console.log("Detected update menu.", COLOR_RED);
163+
stream.log("Detected update menu.", COLOR_RED);
166164
pbf_press_dpad(context, DPAD_UP, 5, 0);
167165
pbf_press_button(context, BUTTON_A, 20, 105);
168166
break;
169167
case 3:
170-
console.log("Detected check online.", COLOR_RED);
168+
stream.log("Detected check online.", COLOR_RED);
171169
context.wait_for(std::chrono::seconds(1));
172170
break;
173171
case 4:
174-
console.log("Detected black screen. Game started...");
172+
stream.log("Detected black screen. Game started...");
175173
return;
176174
default:
177175
OperationFailedException::fire(
178176
ErrorReport::SEND_ERROR_REPORT,
179177
"start_game_from_home_with_inference(): No recognizable state after 30 seconds.",
180-
console
178+
stream
181179
);
182180
}
183181
}
184182
}
185183

186184

187185
void start_game_from_home(
188-
ConsoleHandle& console,
189-
SwitchControllerContext& context,
186+
VideoStream& stream, SwitchControllerContext& context,
190187
bool tolerate_update_menu,
191188
uint8_t game_slot,
192189
uint8_t user_slot,
193190
uint16_t start_game_mash
194191
){
195192
context.wait_for_all_requests();
196-
if (console.video().snapshot()){
197-
console.log("start_game_from_home(): Video capture available. Using inference...");
198-
start_game_from_home_with_inference(console, context, game_slot, user_slot, start_game_mash);
193+
if (stream.video().snapshot()){
194+
stream.log("start_game_from_home(): Video capture available. Using inference...");
195+
start_game_from_home_with_inference(stream, context, game_slot, user_slot, start_game_mash);
199196
return;
200197
}else{
201-
console.log("start_game_from_home(): Video capture not available.", COLOR_RED);
198+
stream.log("start_game_from_home(): Video capture not available.", COLOR_RED);
202199
}
203200

204201
if (game_slot != 0){
@@ -211,7 +208,7 @@ void start_game_from_home(
211208
if (tolerate_update_menu){
212209
if (ConsoleSettings::instance().START_GAME_REQUIRES_INTERNET){
213210
throw UserSetupError(
214-
console.logger(),
211+
stream.logger(),
215212
"Cannot have both \"Tolerate Update Menu\" and \"Start Game Requires Internet\" enabled at the same time without video feedback."
216213
);
217214
}
@@ -279,32 +276,32 @@ class GameLoadingDetector : public VisualInferenceCallback{
279276

280277

281278
bool openedgame_to_gamemenu(
282-
ConsoleHandle& console, SwitchControllerContext& context,
279+
VideoStream& stream, SwitchControllerContext& context,
283280
uint16_t timeout
284281
){
285282
{
286-
console.log("Waiting to load game...");
283+
stream.log("Waiting to load game...");
287284
GameLoadingDetector detector(false);
288285
int ret = wait_until(
289-
console, context,
286+
stream, context,
290287
std::chrono::milliseconds(timeout * (1000 / TICKS_PER_SECOND)),
291288
{{detector}}
292289
);
293290
if (ret < 0){
294-
console.log("Timed out waiting to enter game.", COLOR_RED);
291+
stream.log("Timed out waiting to enter game.", COLOR_RED);
295292
return false;
296293
}
297294
}
298295
{
299-
console.log("Waiting for game menu...");
296+
stream.log("Waiting for game menu...");
300297
GameLoadingDetector detector(true);
301298
int ret = wait_until(
302-
console, context,
299+
stream, context,
303300
std::chrono::milliseconds(timeout * (1000 / TICKS_PER_SECOND)),
304301
{{detector}}
305302
);
306303
if (ret < 0){
307-
console.log("Timed out waiting for game menu.", COLOR_RED);
304+
stream.log("Timed out waiting for game menu.", COLOR_RED);
308305
return false;
309306
}
310307
}

0 commit comments

Comments
 (0)