Skip to content

Commit bd6b0de

Browse files
committed
add run_until callback to Turbomacro
1 parent 54b9a80 commit bd6b0de

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_TurboMacro.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66

77
#include "NintendoSwitch_TurboMacro.h"
8-
8+
#include "CommonTools/Async/InferenceRoutines.h"
9+
#include "PokemonLA/Inference/Sounds/PokemonLA_ShinySoundDetector.h"
10+
#include "PokemonLZA/Options/PokemonLZA_ShinyDetectedAction.h"
11+
#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h"
912
//#include <iostream>
1013
//using std::cout;
1114
//using std::endl;
@@ -40,22 +43,70 @@ TurboMacro::TurboMacro()
4043
ControllerClass::NintendoSwitch_RightJoycon,
4144
}
4245
)
46+
, RUN_UNTIL_CALLBACK(
47+
"Trigger to stop program:",
48+
{
49+
{RunUntilCallback::NONE, "none", "None"},
50+
{RunUntilCallback::LZA_SHINY_SOUND, "lza-shiny-sound", "LZA: Shiny Sound Detected"},
51+
},
52+
LockMode::LOCK_WHILE_RUNNING,
53+
RunUntilCallback::NONE
54+
)
4355
{
4456
PA_ADD_OPTION(LOOP);
57+
PA_ADD_OPTION(RUN_UNTIL_CALLBACK);
4558
PA_ADD_OPTION(TABLE);
4659
}
4760

4861

4962
void TurboMacro::program(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
63+
switch (RUN_UNTIL_CALLBACK){
64+
case RunUntilCallback::NONE:
65+
run_table(env, scope);
66+
break;
67+
case RunUntilCallback::LZA_SHINY_SOUND:
68+
run_table_stop_when_lza_shiny_sound(env, scope);
69+
break;
70+
default:
71+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "TurboMacro::program(): Unknown RunUntilCallback");
72+
73+
}
74+
}
5075

51-
// Connect the controller.
52-
//pbf_press_button(context, BUTTON_LCLICK, 40ms, 40ms);
5376

77+
void TurboMacro::run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
5478
for (uint32_t c = 0; c < LOOP; c++){
5579
TABLE.run(scope, env.console.controller());
5680
}
5781
}
5882

83+
void TurboMacro::run_table_stop_when_lza_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
84+
PokemonLZA::ShinySoundDetectedActionOption shiny_detected_option("Shiny Detected", "", "1000 ms", PokemonLZA::ShinySoundDetectedAction::NOTIFY_ON_FIRST_ONLY);
85+
PokemonLZA::ShinySoundHandler shiny_sound_handler(shiny_detected_option);
86+
PokemonLA::ShinySoundDetector shiny_detector(env.console, [&](float error_coefficient) -> bool {
87+
// Warning: This callback will be run from a different thread than this function.
88+
// env.console.overlay().add_log("Shiny Sound Detected!", COLOR_YELLOW);
89+
return shiny_sound_handler.on_shiny_sound(
90+
env, env.console,
91+
0,
92+
error_coefficient
93+
);
94+
});
95+
96+
ProControllerContext context(scope, env.console.controller<ProController>());
97+
98+
int ret = run_until(
99+
env.console, scope,
100+
[&](CancellableScope& scope){
101+
run_table(env, scope);
102+
},
103+
{shiny_detector}
104+
);
105+
106+
if (ret == 0){
107+
go_home(env.console, context);
108+
}
109+
}
59110

60111
}
61112
}

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_TurboMacro.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ class TurboMacro : public SingleSwitchProgramInstance{
2727

2828
virtual void program(SingleSwitchProgramEnvironment& env, CancellableScope& scope) override;
2929

30+
void run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope);
31+
32+
void run_table_stop_when_lza_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope);
33+
3034

3135
private:
3236
SimpleIntegerOption<uint32_t> LOOP;
3337
ControllerCommandTables TABLE;
38+
39+
enum class RunUntilCallback{
40+
NONE,
41+
LZA_SHINY_SOUND,
42+
};
43+
EnumDropdownOption<RunUntilCallback> RUN_UNTIL_CALLBACK;
3444
};
3545

3646

0 commit comments

Comments
 (0)