diff --git a/runtime/amiibo.c b/runtime/amiibo.c index 8b2c6a95..e44773d2 100644 --- a/runtime/amiibo.c +++ b/runtime/amiibo.c @@ -107,17 +107,25 @@ bool amiibo_is_device_present(void) { return res; } +// While the game waits for a tag it stops and starts detection again several times a second (tens with the menu open), +// so the log names the first few and then a count every 200th start. +static uint64_t g_detect_starts; +static uint64_t g_detect_stop_ns; // the last stop: the menu holds "searching" across the game's restarts + void amiibo_start_detection(void) { plat_mutex_lock(&g_amiibo_lock); g_detecting = true; - rt_log("amiibo: start detection"); + uint64_t n = ++g_detect_starts; + if (n <= 3) rt_log("amiibo: start detection"); + else if (n % 200 == 0) rt_log("amiibo: detection started %llu times so far", (unsigned long long)n); plat_mutex_unlock(&g_amiibo_lock); } void amiibo_stop_detection(void) { plat_mutex_lock(&g_amiibo_lock); + if (g_detecting) g_detect_stop_ns = plat_now_ns(); g_detecting = false; - rt_log("amiibo: stop detection"); + if (g_detect_starts <= 3) rt_log("amiibo: stop detection"); plat_mutex_unlock(&g_amiibo_lock); } @@ -176,6 +184,15 @@ AmiiboDeviceState amiibo_get_device_state(void) { return AMIIBO_DEV_STATE_INITIALIZED; } +AmiiboDeviceState amiibo_get_reader_state(void) { + AmiiboDeviceState state = amiibo_get_device_state(); + if (state != AMIIBO_DEV_STATE_INITIALIZED) return state; + plat_mutex_lock(&g_amiibo_lock); + bool recent = g_detect_stop_ns && plat_now_ns() - g_detect_stop_ns < 1000000000ull; + plat_mutex_unlock(&g_amiibo_lock); + return recent ? AMIIBO_DEV_STATE_SEARCHING : state; +} + bool amiibo_load_file(const char *path) { if (!path || !*path) return false; uint64_t size = 0; diff --git a/runtime/amiibo.h b/runtime/amiibo.h index 11597f8a..5a3e96d4 100644 --- a/runtime/amiibo.h +++ b/runtime/amiibo.h @@ -51,6 +51,9 @@ bool amiibo_is_mounted(void); // State query AmiiboDeviceState amiibo_get_device_state(void); +// The state the menu shows: searching also within a second after a stop, since the game restarts detection many times +// a second while it waits for a tag. The game reads amiibo_get_device_state. +AmiiboDeviceState amiibo_get_reader_state(void); // File loading & triggering actions bool amiibo_load_file(const char *path); diff --git a/runtime/ui/amiibo_page.cpp b/runtime/ui/amiibo_page.cpp index 4a4e44d2..2f2ed996 100644 --- a/runtime/ui/amiibo_page.cpp +++ b/runtime/ui/amiibo_page.cpp @@ -227,7 +227,7 @@ void tab_amiibo(float scale) { ImGui::TextColored(ImVec4(0.93f, 0.76f, 0.33f, 1.0f), "%s", menu_text(UI_MENU_AMIIBO_READER)); ImGui::Separator(); - AmiiboDeviceState state = amiibo_get_device_state(); + AmiiboDeviceState state = amiibo_get_reader_state(); UiTextId state_id = UI_MENU_AMIIBO_STATE_IDLE; ImVec4 state_col = ImVec4(0.8f, 0.8f, 0.8f, 1.0f); switch (state) {