88#include " CommonFramework/Exceptions/OperationFailedException.h"
99#include " NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
1010#include " NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
11+ #include " NintendoSwitch/Inference/NintendoSwitch_SelectedSettingDetector.h"
12+ #include " CommonTools/Async/InferenceRoutines.h"
1113#include " CommonFramework/ImageTools/ImageStats.h"
1214#include " CommonFramework/VideoPipeline/VideoFeed.h"
1315#include " CommonFramework/VideoPipeline/VideoOverlayScopes.h"
@@ -72,6 +74,8 @@ bool is_setting_selected(VideoStream& stream, ProControllerContext& context, Ima
7274}
7375
7476void home_to_date_time (VideoStream& stream, ProControllerContext& context, bool to_date_change, bool fast){
77+ size_t max_attempts = 5 ;
78+ for (size_t i = 0 ; i < max_attempts; i++){
7579 switch (context->performance_class ()){
7680 case ControllerPerformanceClass::SerialPABotBase_Wired_125Hz:{
7781 ssf_issue_scroll (context, SSF_SCROLL_RIGHT, 4 );
@@ -84,31 +88,9 @@ void home_to_date_time(VideoStream& stream, ProControllerContext& context, bool
8488
8589 ssf_issue_scroll (context, SSF_SCROLL_LEFT, 0 );
8690
87- // we expect to be at Settings icon
88- ImageFloatBox system_settings (0.685 , 0.69 , 0.05 , 0.03 );
89- ImageFloatBox other_setting1 (0.615 , 0.69 , 0.05 , 0.03 );
90- ImageFloatBox other_setting2 (0.545 , 0.69 , 0.05 , 0.03 );
91- if (!is_setting_selected (stream, context, system_settings, other_setting1, other_setting2)){
92- // not at Settings Icon
93- // mash down, mash right. then left one
94- size_t iterations = Milliseconds (1200 ) / 24ms + 1 ;
95- for (size_t i = 0 ; i < iterations; i++){
96- ssf_issue_scroll (context, SSF_SCROLL_DOWN, 24ms);
97- }
98-
99- for (size_t i = 0 ; i < iterations; i++){
100- ssf_issue_scroll (context, SSF_SCROLL_RIGHT, 24ms);
101- }
102- ssf_issue_scroll (context, SSF_SCROLL_LEFT, 0 );
103-
104- if (!is_setting_selected (stream, context, system_settings, other_setting1, other_setting2)){
105- OperationFailedException::fire (
106- ErrorReport::SEND_ERROR_REPORT,
107- " home_to_date_time(): Failed to reach settings gear on Home page after 2 attempts." ,
108- stream
109- );
110- }
111- }
91+ // ImageFloatBox system_icon(0.685, 0.69, 0.05, 0.03);
92+ // ImageFloatBox other_setting1(0.615, 0.69, 0.05, 0.03);
93+ // ImageFloatBox other_setting2(0.545, 0.69, 0.05, 0.03);
11294
11395 // Two A presses in case we drop the 1st one.
11496 ssf_press_button (context, BUTTON_A, 3 );
@@ -122,6 +104,25 @@ void home_to_date_time(VideoStream& stream, ProControllerContext& context, bool
122104 }while (--iterations);
123105 }
124106
107+ // Should now be in System Settings, with System highlighted
108+ ImageFloatBox system_setting_box (0.056 , 0.74 , 0.01 , 0.1 );
109+ ImageFloatBox other_setting1 (0.04 , 0.74 , 0.01 , 0.1 );
110+ ImageFloatBox other_setting2 (0.02 , 0.74 , 0.01 , 0.1 );
111+ SelectedSettingWatcher system_setting (system_setting_box, other_setting1, other_setting2);
112+ int ret = run_until<ProControllerContext>(
113+ stream, context,
114+ [](ProControllerContext& context){
115+ for (int i = 0 ; i < 10 ; i++){
116+ ssf_issue_scroll (context, SSF_SCROLL_DOWN, 24ms);
117+ }
118+ },
119+ {system_setting}
120+ );
121+ if (ret < 0 ){ // failed to detect System highlighted. press home and re-try
122+ pbf_press_button (context, BUTTON_HOME, 100ms, 100ms);
123+ continue ;
124+ }
125+
125126 // Scroll left and press A to exit the sleep menu if we happened to
126127 // land there.
127128 ssf_issue_scroll (context, SSF_SCROLL_LEFT, 3 );
@@ -148,39 +149,20 @@ void home_to_date_time(VideoStream& stream, ProControllerContext& context, bool
148149
149150 context.wait_for_all_requests ();
150151 // we expect to be within "Date and Time", with "Synchronize Clock via Internet" being highlighted
151- ImageFloatBox sync_clock (0.168 , 0.185 , 0.01 , 0.1 );
152+ ImageFloatBox sync_clock_box (0.168 , 0.185 , 0.01 , 0.1 );
152153 ImageFloatBox other_setting3 (0.1 , 0.185 , 0.01 , 0.1 );
153154 ImageFloatBox other_setting4 (0.05 , 0.185 , 0.01 , 0.1 );
154- if (!is_setting_selected (stream, context, sync_clock, other_setting3, other_setting4)){
155- // press B once, then mash Up. then try again to scroll down to Date and Time
156- pbf_press_button (context, BUTTON_B, 100ms, 100ms);
157- pbf_move_left_joystick (context, 128 , 0 , 3000ms, 100ms);
158-
159- size_t iterations = Milliseconds (1200 ) / 24ms + 1 ;
160- for (size_t i = 0 ; i < iterations; i++){
161- ssf_issue_scroll (context, SSF_SCROLL_UP, 24ms);
162- }
163-
164- ssf_issue_scroll (context, SSF_SCROLL_DOWN, 3 );
165- ssf_issue_scroll (context, SSF_SCROLL_DOWN, 3 );
166- ssf_issue_scroll (context, SSF_SCROLL_DOWN, 10 );
167- ssf_press_dpad (context, DPAD_DOWN, 45 , 40 );
168- ssf_issue_scroll (context, SSF_SCROLL_DOWN, 3 );
169- ssf_issue_scroll (context, SSF_SCROLL_DOWN, 3 );
170-
171- // double up this A press to make sure it gets through.
172- ssf_press_button (context, BUTTON_A, 3 );
173- ssf_press_button (context, BUTTON_A, 3 );
174-
175- if (!is_setting_selected (stream, context, sync_clock, other_setting3, other_setting4)){
176- OperationFailedException::fire (
177- ErrorReport::SEND_ERROR_REPORT,
178- " home_to_date_time(): Failed to reach 'Synchronize Clock via Internet', within 'Date and Time', after 2 attempts." ,
179- stream
180- );
181- }
155+ SelectedSettingWatcher sync_clock (sync_clock_box, other_setting3, other_setting4);
156+ ret = wait_until (
157+ stream, context,
158+ Milliseconds (2000 ),
159+ {sync_clock}
160+ );
161+ if (ret < 0 ){ // failed to detect System highlighted. press home and re-try
162+ pbf_press_button (context, BUTTON_HOME, 100ms, 100ms);
163+ continue ;
164+ }
182165
183- }
184166
185167 if (!to_date_change){
186168 return ;
@@ -198,7 +180,7 @@ void home_to_date_time(VideoStream& stream, ProControllerContext& context, bool
198180 // confirmation menus.
199181 ssf_issue_scroll (context, SSF_SCROLL_LEFT, 0ms);
200182
201- break ;
183+ return ;
202184 }
203185 case ControllerPerformanceClass::SerialPABotBase_Wireless_ESP32:{
204186 Milliseconds tv = context->timing_variation ();
@@ -269,7 +251,7 @@ void home_to_date_time(VideoStream& stream, ProControllerContext& context, bool
269251 // confirmation menus.
270252 ssf_issue_scroll (context, SSF_SCROLL_LEFT, 0ms, 2 *unit, unit);
271253
272- break ;
254+ return ;
273255 }
274256 default :{
275257 // Slow version for tick-imprecise controllers.
@@ -308,10 +290,16 @@ void home_to_date_time(VideoStream& stream, ProControllerContext& context, bool
308290 ssf_press_button_ptv (context, BUTTON_A, 1000ms);
309291 ssf_issue_scroll_ptv (context, SSF_SCROLL_DOWN);
310292 ssf_issue_scroll_ptv (context, SSF_SCROLL_DOWN);
293+ return ;
294+ }
311295 }
312296 }
313297
314-
298+ OperationFailedException::fire (
299+ ErrorReport::SEND_ERROR_REPORT,
300+ " home_to_date_time(): Failed to reach Date and Time after several attempts." ,
301+ stream
302+ );
315303
316304}
317305
0 commit comments