Skip to content

Commit 6ff6cd9

Browse files
committed
Refactor start-game related detectors with support Switch 2.
1 parent 2ecf9ec commit 6ff6cd9

19 files changed

+542
-248
lines changed

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Routines.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "NintendoSwitch_Commands_Routines.h"
1313
#include "NintendoSwitch_Commands_PushButtons.h"
1414
#include "NintendoSwitch_Commands_Superscalar.h"
15-
#include "NintendoSwitch/Inference/NintendoSwitch_DetectHome.h"
15+
#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h"
1616
//#include "NintendoSwitch_Messages_Routines.h"
1717

1818
//#include <iostream>
@@ -45,7 +45,7 @@ void close_game(VideoStream& stream, ProControllerContext& context){
4545
// send a second Home button press, if the first one is dropped
4646
bool video_available = (bool)stream.video().snapshot();
4747
if (video_available){
48-
HomeWatcher detector;
48+
HomeMenuWatcher detector;
4949
int ret = wait_until(
5050
stream, context,
5151
std::chrono::milliseconds(5000),
@@ -88,7 +88,7 @@ void close_game(VideoStream& stream, JoyconContext& context){
8888
pbf_press_button(context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0);
8989
context.wait_for_all_requests();
9090

91-
HomeWatcher detector;
91+
HomeMenuWatcher detector;
9292
int ret = wait_until(
9393
stream, context,
9494
std::chrono::milliseconds(5000),

SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ ControllerModeStatus TcpSysbotBase_Connection::controller_mode_status() const{
107107

108108
void TcpSysbotBase_Connection::write_data(const std::string& data){
109109
WriteSpinLock lg(m_send_lock);
110+
cout << "Sending: " << data << endl; // REMOVE
110111
m_socket.blocking_send(data.data(), data.size());
111112
}
112113

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
650650

651651
// ImageRGB32 image("20230427-200550386826-OperationFailedException.png");
652652

653-
// NintendoSwitch::HomeDetector detector;
653+
// NintendoSwitch::HomeMenuDetector detector;
654654
// cout << detector.detect(image) << endl;
655655

656656

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@
124124
#include "NintendoSwitch/Options/NintendoSwitch_ModelType.h"
125125
#include "NintendoSwitch/Programs/NintendoSwitch_Navigation.h"
126126
#include "NintendoSwitch/Inference/NintendoSwitch_ConsoleTypeDetector.h"
127-
#include "NintendoSwitch/Inference/NintendoSwitch_UpdateMenuDetector.h"
127+
#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h"
128+
#include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h"
129+
#include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h"
128130

129131
#include <QPixmap>
130132
#include <QVideoFrame>
@@ -320,6 +322,17 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
320322
VideoOverlaySet overlays(overlay);
321323

322324

325+
326+
HomeMenuDetector detector0;
327+
StartGameUserSelectDetector detector1;
328+
UpdatePopupDetector detector2;
329+
detector0.make_overlays(overlays);
330+
detector1.make_overlays(overlays);
331+
detector2.make_overlays(overlays);
332+
cout << detector1.detect(feed.snapshot()) << endl;
333+
334+
335+
323336
#if 0
324337
ConsoleTypeDetector_Home detector;
325338
detector.make_overlays(overlays);
@@ -329,8 +342,8 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
329342

330343

331344

332-
#if 1
333-
UpdateMenuDetector detector;
345+
#if 0
346+
UpdatePopupDetector detector;
334347
detector.make_overlays(overlays);
335348

336349
cout << detector.detect(feed.snapshot()) << endl;
@@ -2269,7 +2282,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
22692282

22702283

22712284
// YCommMenuDetector detector(true);
2272-
// HomeDetector detector;
2285+
// HomeMenuDetector detector;
22732286
// cout << detector.detect(image) << endl;
22742287
// cout << detector.detect(feed.snapshot()) << endl;
22752288

@@ -2336,8 +2349,8 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
23362349
#if 0
23372350
ImageRGB32 image("screenshot-20221107-210754107968.png");
23382351
// auto image = feed.snapshot();
2339-
HomeDetector detector;
2340-
// UpdateMenuDetector detector;
2352+
HomeMenuDetector detector;
2353+
// UpdatePopupDetector detector;
23412354
VideoOverlaySet overlays(overlay);
23422355
detector.make_overlays(overlays);
23432356
cout << detector.detect(image) << endl;
@@ -2346,7 +2359,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
23462359
// ImageRGB32 image("ExclamationFalsePositive.png");
23472360
// find_exclamation_marks(image);
23482361

2349-
// HomeDetector detector;
2362+
// HomeMenuDetector detector;
23502363
// cout << detector.detect(image) << endl;
23512364

23522365

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_ConsoleTypeDetector.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,31 @@ ConsoleTypeDetector_Home::ConsoleTypeDetector_Home(Color color)
2020
: m_color(color)
2121
, m_bottom_line(0.10, 0.88, 0.80, 0.03)
2222
{}
23-
2423
void ConsoleTypeDetector_Home::make_overlays(VideoOverlaySet& items) const{
2524
items.add(m_color, m_bottom_line);
2625
}
2726
ConsoleTypeDetection ConsoleTypeDetector_Home::detect(const ImageViewRGB32& screen) const{
2827
ImageStats stats = image_stats(extract_box_reference(screen, m_bottom_line));
29-
// cout << stats.stddev.sum() << endl;
28+
// cout << "ConsoleTypeDetector: " << stats.stddev.sum() << endl;
29+
if (stats.stddev.sum() < 10){
30+
return ConsoleTypeDetection::Switch2_Unknown;
31+
}else{
32+
return ConsoleTypeDetection::Switch1;
33+
}
34+
}
35+
36+
37+
38+
ConsoleTypeDetector_StartGameUserSelect::ConsoleTypeDetector_StartGameUserSelect(Color color)
39+
: m_color(color)
40+
, m_bottom_line(0.02, 0.53, 0.96, 0.03)
41+
{}
42+
void ConsoleTypeDetector_StartGameUserSelect::make_overlays(VideoOverlaySet& items) const{
43+
items.add(m_color, m_bottom_line);
44+
}
45+
ConsoleTypeDetection ConsoleTypeDetector_StartGameUserSelect::detect(const ImageViewRGB32& screen) const{
46+
ImageStats stats = image_stats(extract_box_reference(screen, m_bottom_line));
47+
// cout << "ConsoleTypeDetector: " << stats.stddev.sum() << endl;
3048
if (stats.stddev.sum() < 10){
3149
return ConsoleTypeDetection::Switch2_Unknown;
3250
}else{

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_ConsoleTypeDetector.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ class ConsoleTypeDetector_Home{
4545
ImageFloatBox m_bottom_line;
4646
};
4747

48+
class ConsoleTypeDetector_StartGameUserSelect{
49+
public:
50+
ConsoleTypeDetector_StartGameUserSelect(Color color = COLOR_RED);
51+
52+
void make_overlays(VideoOverlaySet& items) const;
53+
ConsoleTypeDetection detect(const ImageViewRGB32& screen) const;
54+
55+
private:
56+
Color m_color;
57+
ImageFloatBox m_bottom_line;
58+
};
59+
4860

4961

5062

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_DateReader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "NintendoSwitch_DateReader.h"
2323
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
2424
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
25-
#include "NintendoSwitch/Inference/NintendoSwitch_DetectHome.h"
25+
#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h"
2626
#include "NintendoSwitch/Programs/NintendoSwitch_Navigation.h"
2727

2828
//#include <iostream>
@@ -556,7 +556,7 @@ void change_date(
556556
while (true){
557557
context.wait_for_all_requests();
558558

559-
HomeWatcher home;
559+
HomeMenuWatcher home;
560560
DateChangeWatcher date_reader;
561561
int ret = wait_until(
562562
env.console, context, std::chrono::seconds(10),

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_DetectHome.cpp

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -15,163 +15,13 @@
1515
namespace PokemonAutomation{
1616
namespace NintendoSwitch{
1717

18-
HomeDetector::HomeDetector()
19-
: m_bottom_row(0.10, 0.92, 0.10, 0.05)
20-
, m_bottom_icons(0.70, 0.92, 0.28, 0.05)
21-
, m_bottom_left(0.02, 0.70, 0.15, 0.15)
22-
, m_bottom_right(0.83, 0.70, 0.15, 0.15)
23-
, m_user_icons(0.05, 0.05, 0.2, 0.08)
24-
, m_game_slot(0.08, 0.25, 0.10, 0.38)
25-
{}
26-
void HomeDetector::make_overlays(VideoOverlaySet& items) const{
27-
items.add(COLOR_RED, m_bottom_row);
28-
items.add(COLOR_RED, m_bottom_icons);
29-
items.add(COLOR_RED, m_bottom_left);
30-
items.add(COLOR_RED, m_bottom_right);
31-
items.add(COLOR_RED, m_user_icons);
32-
items.add(COLOR_RED, m_game_slot);
33-
}
34-
bool HomeDetector::detect(const ImageViewRGB32& screen) const{
35-
ImageStats stats_bottom_row = image_stats(extract_box_reference(screen, m_bottom_row));
36-
// cout << stats_bottom_row.average << stats_bottom_row.stddev << endl;
37-
bool white;
38-
// cout << "stats_bottom_row.average.sum() = " << stats_bottom_row.average.sum() << endl;
39-
if (stats_bottom_row.average.sum() < 200){
40-
white = false;
41-
}else if (stats_bottom_row.average.sum() > 500){
42-
white = true;
43-
}else{
44-
return false;
45-
}
46-
47-
// cout << "white: " << white << endl;
48-
49-
ImageStats stats_bottom_icons = image_stats(extract_box_reference(screen, m_bottom_icons));
50-
// cout << stats_bottom_icons.average << stats_bottom_icons.stddev << endl;
51-
if (stats_bottom_icons.stddev.sum() < 50){
52-
return false;
53-
}
54-
55-
ImageStats stats_bottom_left = image_stats(extract_box_reference(screen, m_bottom_left));
56-
ImageStats stats_bottom_right = image_stats(extract_box_reference(screen, m_bottom_right));
57-
// cout << stats_bottom_left.average << stats_bottom_left.stddev << endl;
58-
// cout << stats_bottom_right.average << stats_bottom_right.stddev << endl;
59-
if (white){
60-
if (!is_white(stats_bottom_left) || !is_white(stats_bottom_right)){
61-
// cout << "asdf" << endl;
62-
return false;
63-
}
64-
}else{
65-
if (!is_grey(stats_bottom_left, 0, 200) || !is_grey(stats_bottom_right, 0, 200)){
66-
// cout << "qwer" << endl;
67-
return false;
68-
}
69-
}
70-
71-
// cout << euclidean_distance(stats_bottom_row.average, stats_bottom_left.average) << endl;
72-
if (euclidean_distance(stats_bottom_row.average, stats_bottom_left.average) > 20){
73-
// cout << "qwer = " << euclidean_distance(stats_bottom_row.average, stats_bottom_left.average) << endl;
74-
return false;
75-
}
76-
// cout << euclidean_distance(stats_bottom_row.average, stats_bottom_right.average) << endl;
77-
if (euclidean_distance(stats_bottom_row.average, stats_bottom_right.average) > 20){
78-
// cout << "asdf" << endl;
79-
return false;
80-
}
81-
// cout << euclidean_distance(stats_bottom_left.average, stats_bottom_left.average) << endl;
82-
if (euclidean_distance(stats_bottom_left.average, stats_bottom_right.average) > 20){
83-
// cout << "zxcv" << endl;
84-
return false;
85-
}
86-
87-
ImageStats stats_user_icons = image_stats(extract_box_reference(screen, m_user_icons));
88-
// cout << stats_user_icons.stddev << endl;
89-
if (stats_user_icons.stddev.sum() < 40){
90-
return false;
91-
}
92-
ImageStats stats_game_slot = image_stats(extract_box_reference(screen, m_game_slot));
93-
// cout << stats_game_slot.stddev << endl;
94-
if (stats_game_slot.stddev.sum() < 50){
95-
return false;
96-
}
97-
98-
return true;
99-
}
10018

10119

10220

10321

10422

10523

10624

107-
StartGameUserSelectDetector::StartGameUserSelectDetector(Color color)
108-
: m_color(color)
109-
, m_bottom_row(0.10, 0.92, 0.10, 0.05)
110-
, m_bottom_icons(0.70, 0.92, 0.28, 0.05)
111-
, m_top_row(0.50, 0.47, 0.45, 0.05)
112-
, m_mid_row(0.10, 0.55, 0.80, 0.02)
113-
, m_user_slot(0.45, 0.55, 0.10, 0.35)
114-
{}
115-
void StartGameUserSelectDetector::make_overlays(VideoOverlaySet& items) const{
116-
items.add(m_color, m_bottom_row);
117-
items.add(m_color, m_bottom_icons);
118-
items.add(m_color, m_top_row);
119-
items.add(m_color, m_mid_row);
120-
items.add(m_color, m_user_slot);
121-
}
122-
bool StartGameUserSelectDetector::detect(const ImageViewRGB32& screen) const{
123-
ImageStats stats_bottom_row = image_stats(extract_box_reference(screen, m_bottom_row));
124-
// cout << stats_bottom_row.average << stats_bottom_row.stddev << endl;
125-
if (stats_bottom_row.stddev.sum() > 10){
126-
return false;
127-
}
128-
bool white;
129-
if (is_grey(stats_bottom_row, 50, 300)){
130-
white = false;
131-
}else if (stats_bottom_row.average.sum() > 500){
132-
white = true;
133-
}else{
134-
return false;
135-
}
136-
137-
ImageStats stats_bottom_icons = image_stats(extract_box_reference(screen, m_bottom_icons));
138-
// cout << stats_bottom_icons.average << stats_bottom_icons.stddev << endl;
139-
if (stats_bottom_icons.stddev.sum() < 50){
140-
return false;
141-
}
142-
143-
ImageStats stats_top_row = image_stats(extract_box_reference(screen, m_top_row));
144-
ImageStats stats_mid_row = image_stats(extract_box_reference(screen, m_mid_row));
145-
// cout << stats_top_row.average << stats_top_row.stddev << endl;
146-
if (stats_top_row.stddev.sum() > 10 || stats_mid_row.stddev.sum() > 10){
147-
return false;
148-
}
149-
if (white){
150-
if (!is_white(stats_top_row) || !is_white(stats_mid_row)){
151-
return false;
152-
}
153-
}else{
154-
if (!is_grey(stats_top_row, 50, 300) || !is_grey(stats_mid_row, 50, 300)){
155-
return false;
156-
}
157-
}
158-
159-
if (euclidean_distance(stats_bottom_row.average, stats_top_row.average) > 20){
160-
return false;
161-
}
162-
163-
ImageStats stats_user_slot = image_stats(extract_box_reference(screen, m_user_slot));
164-
// cout << stats_user_slot.average << stats_user_slot.stddev << endl;
165-
if (stats_user_slot.stddev.sum() < 50){
166-
return false;
167-
}
168-
169-
return true;
170-
}
171-
172-
173-
174-
17525

17626

17727

0 commit comments

Comments
 (0)