diff --git a/runtime/cfg.c b/runtime/cfg.c index bcbaadc3..ee59d716 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -49,7 +49,8 @@ static const char *const known[] = { "GAMEPLAY_MAP_ZOOM", "GAMEPLAY_MAP_ZOOM_SENSITIVITY", "GAMEPLAY_ROTATION_SENSITIVITY", "GAMEPLAY_TWEAKS_ENABLED", "GAMEPLAY_ULTRAHAND_C_AXIS", "GAMEPLAY_ULTRAHAND_DISTANCE", "GAMEPLAY_ULTRAHAND_ROTATE", "GAMEPLAY_WEAPON_SENSITIVITY", "GAMEPLAY_WEAPON_WHEEL", - "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "GRASS_RANGE", "HDR", "HDR_GAME_NITS", "HDR_HUE", + "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "GRASS_RANGE", + "GYRO_BIAS_PAD", "GYRO_BIAS_X", "GYRO_BIAS_Y", "GYRO_BIAS_Z", "GYRO_CALIBRATION", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MEMX", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG", "MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", @@ -91,7 +92,8 @@ static const struct { const char *key, *section; } sections[] = { {"TEX_HEAP_EXTRA_MB", "mods"}, {"MENU_LANG", "language"}, {"MENU_FONT_FONTCONFIG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, {"VOLUME", "audio"}, {"VOLUME_MUSIC", "audio"}, {"VOLUME_SFX", "audio"}, {"VOLUME_VOICE", "audio"}, {"AUDIO_CHANNELS", "audio"}, - {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, + {"MOTION", "input"}, {"GYRO_CALIBRATION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, + {"GYRO_BIAS_X", "input"}, {"GYRO_BIAS_Y", "input"}, {"GYRO_BIAS_Z", "input"}, {"GYRO_BIAS_PAD", "input"}, {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, {"STICK_SENS", "input"}, {"MENU_CURSOR", "input"}, {"MAP_CLICK", "input"}, {"DIALOGUE_CLICK", "input"}, {"POINTER_IMAGE", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"MOUSE_AIM_SCALE", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, @@ -162,7 +164,12 @@ static const CfgMeta metas[] = { {"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c) // Sound, Controls, Menu {"VOLUME", "100", "0..100", LIVE}, {"VOLUME_MUSIC", "100", "0..100", LIVE}, {"VOLUME_SFX", "100", "0..100", LIVE}, {"VOLUME_VOICE", "100", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART}, - {"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, + {"MOTION", "1", "0|1", LIVE}, {"GYRO_CALIBRATION", "1", "0|1", LIVE}, + {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, + // Written by the Calibrate gyro row (motion_fusion.c), not by hand; read at start when gyro_bias_pad names the + // connected controller. + {"GYRO_BIAS_X", "", NULL, RESTART}, {"GYRO_BIAS_Y", "", NULL, RESTART}, {"GYRO_BIAS_Z", "", NULL, RESTART}, + {"GYRO_BIAS_PAD", "", NULL, RESTART}, {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "auto", "auto|nintendo|xbox", LIVE}, {"PROMPTS", "nintendo", "nintendo|xbox|pc", RESTART}, {"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART}, diff --git a/runtime/input.c b/runtime/input.c index 65f4de0a..465b0d9c 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -46,6 +46,7 @@ static volatile int32_t pad_layout_shown = -1; // pad_layout for the menu (any static SDL_JoystickID sensor_pad; // the gamepad whose sensors the motion thread set up, 0 none static int sensor_ok; // ... and it has both sensors, enabled static int motion_thread; // 0 not started, 1 running, -1 off (TOTK_MOTION=0, no SDL) +static char calib_pad_name[128]; // the controller the bias belongs to; empty when there is none // SDL buttons of the gamepad inputs (input_map.h PAD_*), by position; PAD_ZL and PAD_ZR are the triggers (axes). static const SDL_GamepadButton pad_sdl[PAD_COUNT] = { @@ -959,6 +960,12 @@ void input_motion_axes(const float a[3], const float g[3], double acc[3], double static int sensors_ready(void) { SDL_JoystickID id = SDL_GetGamepadID(pad); if (id == sensor_pad) return sensor_ok; + // A different controller: the old one's bias estimate does not apply. Only on a change, so the first call does + // not drop a bias that input_motion_start loaded from totk.ini for this controller. + if (sensor_pad) { + motion_bias_reset(motion_global()); + calib_pad_name[0] = 0; + } sensor_pad = id; // Nothing drains the SDL event queue: no event per sample (the data still updates, SDL_SendJoystickSensor) SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_SENSOR_UPDATE, false); @@ -1007,10 +1014,37 @@ static void motion_main(void *arg) { } } +// Copy a controller name into cap bytes without splitting a UTF-8 sequence: a name cut mid-character would show as +// garbage in the menu and be written to totk.ini the same way. +static void copy_name(char *dst, size_t cap, const char *src) { + if (!src) src = ""; + size_t n = strlen(src); + if (n >= cap) { + n = cap - 1; + while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; // back off to the start of the character + } + memcpy(dst, src, n); + dst[n] = 0; +} + void input_motion_start(void) { plat_mutex_lock(&lock); if (!state) start(); if (!motion_thread) { + // Load the calibrated bias from totk.ini when it was measured for the controller that is connected now and + // the switch is on; the keys stay in the file either way, so turning the switch back on restores the bias. + const char *name = pad && SDL_GamepadConnected(pad) ? SDL_GetGamepadName(pad) : NULL; + const char *calib_pad = cfg_get("TOTK_GYRO_BIAS_PAD"); + if (name && calib_pad && strcmp(calib_pad, name) == 0 && motion_calibration_enabled()) { + motion_bias_load(motion_global()); + copy_name(calib_pad_name, sizeof calib_pad_name, name); + rt_log("input: gyro bias loaded from totk.ini for %s", name); + } else { + // No usable bias for this controller: drop whatever an earlier start left behind, so the row does not + // report another controller's calibration. + calib_pad_name[0] = 0; + motion_bias_reset(motion_global()); + } if (!input_motion_enabled()) rt_log("input: motion off (TOTK_MOTION=0); it can be turned on while the game runs"); if (state != 1) motion_thread = -1; else { @@ -1020,3 +1054,34 @@ void input_motion_start(void) { } plat_mutex_unlock(&lock); } + +void input_motion_calibrate(void) { + plat_mutex_lock(&lock); + if (!input_gyro_calibration_enabled()) { + rt_log("input: calibrate gyro ignored: gyro_calibration is off"); + } else if (motion_thread != 1 || !pad) { + // Nothing would collect the samples; say so rather than leaving the row looking pressed. + rt_log("input: calibrate gyro ignored: %s", motion_thread != 1 ? "motion is off (TOTK_MOTION=0)" : "no gamepad"); + } else { + const char *name = SDL_GetGamepadName(pad); + copy_name(calib_pad_name, sizeof calib_pad_name, name); + motion_calibrate_start(motion_global(), calib_pad_name); + } + plat_mutex_unlock(&lock); +} + +int input_gyro_calibration_enabled(void) { return motion_calibration_enabled(); } + +int input_motion_calib_state(char *pad, size_t cap) { + plat_mutex_lock(&lock); + int st = 0; + if (motion_thread == 1) { + st = motion_calib_poll(motion_global(), kern_now_ns()); // also ends an attempt whose thread stalled + // Loaded from totk.ini at start, no attempt in this run; with the switch off the bias does not apply, so + // the row reads as before any calibration. + if (st == 0 && calib_pad_name[0] && input_gyro_calibration_enabled()) st = 2; + } + if (pad && cap) snprintf(pad, cap, "%s", calib_pad_name); + plat_mutex_unlock(&lock); + return st; +} diff --git a/runtime/input.h b/runtime/input.h index 86f26fa7..f2162fc4 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -114,6 +114,16 @@ int input_rumble_triggers_enabled(void); void input_motion_start(void); // TOTK_MOTION is not "0" (a live setting: read again when cfg_version changes). Off: the sensor reads at rest. int input_motion_enabled(void); +void input_motion_calibrate(void); // start collecting resting gyro samples for a bias estimate +// What the Controls tab's Calibrate gyro row shows: 0 no bias for this controller, 1 collecting, 2 calibrated +// (by this run or loaded from totk.ini), 3 the last attempt timed out. pad receives the controller the bias belongs +// to (empty when none), copied so the caller does not read a buffer another thread may be rewriting. +int input_motion_calib_state(char *pad, size_t cap); +// The bias's own switch (gyro_calibration, live). Off: no collection, the fusion takes the raw gyro, and the row's +// state reads as before any calibration while the saved keys stay in totk.ini. +int input_gyro_calibration_enabled(void); +// The fusion's own read of the same switch (motion_fusion.c); input.c reaches it for the load and the row's state. +int motion_calibration_enabled(void); // SDL sensor values (accelerometer m/s^2, gyro rad/s, SDL gamepad axes) to the Switch controller frame motion_feed // takes (acceleration in G as gravity reads, rotation rad/s). The mapping is documented in input_win.c. void input_motion_axes(const float sdl_acc[3], const float sdl_gyro[3], double acc_g[3], double gyro[3]); @@ -131,3 +141,7 @@ MotionFusion *motion_global(void); // the gamepad's, fed by input_win.c void motion_feed(MotionFusion *m, const double acc_g[3], const double gyro_rad_s[3], uint64_t now_ns); int motion_read(MotionFusion *m, MotionState *out, int max, uint64_t now_ns); // newest first; 0 = none in 200 ms void motion_restart(MotionFusion *m); // angle and direction start over at the next sample +void motion_calibrate_start(MotionFusion *m, const char *pad_name); // start collecting; pad_name saved with the bias +int motion_calib_poll(MotionFusion *m, uint64_t now_ns); // the timeout, for when motion_feed is not called +void motion_bias_load(MotionFusion *m); // load the bias from totk.ini (TOTK_GYRO_BIAS_*) +void motion_bias_reset(MotionFusion *m); // zero the bias (new controller) diff --git a/runtime/motion_fusion.c b/runtime/motion_fusion.c index 7bbf3be6..71552219 100644 --- a/runtime/motion_fusion.c +++ b/runtime/motion_fusion.c @@ -14,12 +14,14 @@ #include #include +#include "cfg.h" #include "host.h" #include "input.h" #include "platform/platform.h" -enum { MOTION_RING = 64 }; +enum { MOTION_RING = 64, CALIB_SAMPLES = 250 }; #define STALE_NS 200000000ull // no sample for 200 ms: no motion +#define CALIB_TIMEOUT_NS 5000000000ull // 5 s: give up if sensor data stops arriving mid-calibration struct MotionFusion { PlatMutex lock; @@ -29,6 +31,14 @@ struct MotionFusion { double R[3][3]; // controller -> reference; row i = reference axis i in controller coordinates double angle[3]; int restart; + double gyro_bias[3]; // estimated resting bias (rad/s), subtracted from the gyro + int calibrating; // 1: collecting resting samples for a new bias + double calib_sum[3]; // accumulated gyro samples during calibration + int calib_count; // samples collected so far + uint64_t calib_start_ns; // when calibration started (timeout check) + char calib_pad[128]; // controller name that was calibrated (for loading the bias on the next run) + int calib_result; // 0 no attempt or one in progress, 1 the last attempt finished, -1 it timed out + int bias_valid; // 1 when gyro_bias holds a real estimate (loaded or measured), so a zero is a value }; MotionFusion *motion_new(void) { @@ -107,11 +117,111 @@ static void integrate(double R[3][3], const double gyro[3], const double acc[3], orthonormalize(R); } -void motion_feed(MotionFusion *m, const double acc[3], const double gyro[3], uint64_t now) { +// Write the calibrated bias to totk.ini. Called with m->lock released: cfg_set takes cfg.c's own lock and cfg_save +// rewrites the file, and neither belongs inside the sensor thread's critical section. +static void save_bias(const double bias[3], const char *pad, int samples) { + char buf[32]; + snprintf(buf, sizeof buf, "%.6f", bias[0]); + cfg_set("TOTK_GYRO_BIAS_X", buf); + snprintf(buf, sizeof buf, "%.6f", bias[1]); + cfg_set("TOTK_GYRO_BIAS_Y", buf); + snprintf(buf, sizeof buf, "%.6f", bias[2]); + cfg_set("TOTK_GYRO_BIAS_Z", buf); + if (pad[0]) cfg_set("TOTK_GYRO_BIAS_PAD", pad); + // Saved now rather than at the menu's save tick, which may not run if the menu is closed first. + if (cfg_save()) + rt_log("motion: gyro bias calibrated from %d samples: %.5f %.5f %.5f rad/s", samples, bias[0], bias[1], bias[2]); + else + rt_log("motion: gyro bias %.5f %.5f %.5f rad/s could not be written to totk.ini; it applies to this run only", + bias[0], bias[1], bias[2]); +} + +// The one timeout decision, for the sample path and the menu's poll alike, so the two cannot drift apart. Called +// with m->lock held; the caller logs, with the lock released. +static int calib_check_timeout(MotionFusion *m, uint64_t now) { + if (!m->calibrating || now - m->calib_start_ns <= CALIB_TIMEOUT_NS) return 0; + m->calibrating = 0; + // A bias that is already in effect stays in effect, and stays reported as calibrated: the row describes the bias + // that is applied, and the failed attempt is in the log. A controller with no bias at all shows the failure. + m->calib_result = m->bias_valid ? 1 : -1; + return 1; +} + +// Copy a name into cap bytes without splitting a UTF-8 sequence; the same as input.c's copy_name, which this file +// cannot include from. +static void copy_name(char *dst, size_t cap, const char *src) { + if (!src) src = ""; + size_t n = strlen(src); + if (n >= cap) { + n = cap - 1; + while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; // back off to the start of the character + } + memcpy(dst, src, n); + dst[n] = 0; +} + +// The bias's own switch (gyro_calibration, live, default 1): off, the fusion takes the raw gyro, an attempt in +// progress is dropped, and the menu's calibrate entry does nothing. The saved keys stay untouched, so turning it +// back on restores the last calibration. +int motion_calibration_enabled(void) { + static int on = -1; + static unsigned seen; + unsigned v = cfg_version(); + if (on < 0 || v != seen) { + seen = v; + on = cfg_bool("TOTK_GYRO_CALIBRATION"); + } + return on; +} + +void motion_feed(MotionFusion *m, const double acc[3], const double gyro_raw[3], uint64_t now) { + double saved[3] = {0, 0, 0}; + char saved_pad[sizeof m->calib_pad] = ""; + int save = 0, samples = 0, first = 0, logged_timeout = 0; + uint64_t saved_start = 0; plat_mutex_lock(&m->lock); double dt = m->last_ns && now > m->last_ns ? (double)(now - m->last_ns) / 1e9 : 0.004; if (dt > 0.05) dt = 0.004; // a gap (no controller, sensors were off): no integration across it m->last_ns = now; + // Calibration: collect gyro samples for CALIB_SAMPLES frames. The user holds the controller still, so the + // average of the raw gyro (which reads the bias at rest) is the bias estimate. No motion threshold: the bias + // itself can exceed any absolute threshold, which would block collection forever. calib_check_timeout decides + // the deadline for both this path and the menu's poll, so the two cannot drift apart. With the calibration + // switch off, an attempt in progress is dropped and no new one starts; the bias stops applying below. + if (m->calibrating && !motion_calibration_enabled()) { + m->calibrating = 0; + m->calib_result = 0; + } + if (m->calibrating) { + if (calib_check_timeout(m, now)) { + logged_timeout = 1; + samples = m->calib_count; + saved_start = m->calib_start_ns; + } + if (m->calibrating) { + for (int i = 0; i < 3; i++) m->calib_sum[i] += gyro_raw[i]; + m->calib_count++; + first = m->calib_count == 1; + if (m->calib_count >= CALIB_SAMPLES) { + for (int i = 0; i < 3; i++) m->gyro_bias[i] = m->calib_sum[i] / m->calib_count; + m->calibrating = 0; + m->calib_result = 1; + m->bias_valid = 1; + m->restart = 1; + // Hand the result to save_bias below, which runs with the lock released. + memcpy(saved, m->gyro_bias, sizeof saved); + snprintf(saved_pad, sizeof saved_pad, "%s", m->calib_pad); + samples = m->calib_count; + save = 1; + } + } + } + // Subtract the bias from the gyro for integration and storage. + // The bias applies only while the switch is on: off, the fusion sees the raw gyro again. + const int calib_on = motion_calibration_enabled(); + double gyro[3] = {gyro_raw[0] - (calib_on ? m->gyro_bias[0] : 0), + gyro_raw[1] - (calib_on ? m->gyro_bias[1] : 0), + gyro_raw[2] - (calib_on ? m->gyro_bias[2] : 0)}; if (m->restart) { align_to(m->R, acc); memset(m->angle, 0, sizeof m->angle); @@ -128,7 +238,17 @@ void motion_feed(MotionFusion *m, const double acc[3], const double gyro[3], uin for (int j = 0; j < 3; j++) s->dir[i * 3 + j] = (float)m->R[j][i]; // direction i = controller axis i in the reference frame } m->produced++; + // The logs run with the lock released: the log back end can be slow, and the menu thread holds this lock in + // motion_calib_poll. plat_mutex_unlock(&m->lock); + if (first) + rt_log("motion: calibration started, first raw gyro: %.5f %.5f %.5f rad/s", gyro_raw[0], gyro_raw[1], + gyro_raw[2]); + if (logged_timeout) { + double secs = (now - saved_start) / 1e9; // captured under the lock; another attempt may have begun since + rt_log("motion: gyro calibration timed out after %.1f s (%d samples): sensor data not arriving", secs, samples); + } + if (save) save_bias(saved, saved_pad, samples); } int motion_read(MotionFusion *m, MotionState *out, int max, uint64_t now) { @@ -152,3 +272,73 @@ void motion_restart(MotionFusion *m) { memset(m->angle, 0, sizeof m->angle); plat_mutex_unlock(&m->lock); } + +void motion_calibrate_start(MotionFusion *m, const char *pad_name) { + plat_mutex_lock(&m->lock); + m->calibrating = 1; + m->calib_result = 0; + m->calib_count = 0; + m->calib_sum[0] = m->calib_sum[1] = m->calib_sum[2] = 0; + m->calib_start_ns = kern_now_ns(); + // The caller passes the same name it put into its own row label; copied again here so the fusion keeps the name + // the bias belongs to without relying on another buffer's lifetime. Same UTF-8-safe cut as input.c's copy_name: + // a name cut mid-sequence would reach totk.ini and the next run's comparisons. + copy_name(m->calib_pad, sizeof m->calib_pad, pad_name); + plat_mutex_unlock(&m->lock); +} + +// One axis of totk.ini; a value that is not a number leaves the axis at zero and is named in the log, so a +// hand-edited or damaged line does not silently read as a calibrated zero. +static double bias_value(const char *key) { + const char *v = cfg_get(key); + if (!v || !*v) return 0; + char *end; + double d = strtod(v, &end); + if (end == v || *end || !isfinite(d)) { + rt_log("motion: %s = \"%s\" is not a number; that axis starts uncalibrated", key, v); + return 0; + } + return d; +} + +void motion_bias_load(MotionFusion *m) { + double bias[3] = {bias_value("TOTK_GYRO_BIAS_X"), bias_value("TOTK_GYRO_BIAS_Y"), + bias_value("TOTK_GYRO_BIAS_Z")}; + plat_mutex_lock(&m->lock); + memcpy(m->gyro_bias, bias, sizeof bias); + m->bias_valid = 1; + m->calib_result = 1; // 1 means "a bias is in effect", whether measured or loaded + plat_mutex_unlock(&m->lock); +} + +void motion_bias_reset(MotionFusion *m) { + plat_mutex_lock(&m->lock); + m->gyro_bias[0] = m->gyro_bias[1] = m->gyro_bias[2] = 0; + // A calibration that was running belonged to the previous controller: its samples must not complete against this + // one, and its name must not label a bias measured from another controller. + m->calibrating = 0; + m->calib_count = 0; + m->calib_sum[0] = m->calib_sum[1] = m->calib_sum[2] = 0; + m->calib_pad[0] = 0; + m->calib_result = 0; + m->bias_valid = 0; + plat_mutex_unlock(&m->lock); +} + +// The timeout, for the case motion_feed is not called at all (the sensor thread stalled): without this the row would +// read "Calibrating..." forever, which is the failure the timeout exists to end. Returns the row's state: 0 none, +// 1 collecting, 2 calibrated, 3 the last attempt timed out. Logs with the lock released: the menu thread holds this +// lock briefly on every redraw, and the sensor thread waits for it. +int motion_calib_poll(MotionFusion *m, uint64_t now) { + plat_mutex_lock(&m->lock); + int timed_out = calib_check_timeout(m, now); + int st = m->calibrating ? 1 : m->calib_result > 0 ? 2 : m->calib_result < 0 ? 3 : 0; + uint64_t start = m->calib_start_ns; + int count = m->calib_count; + plat_mutex_unlock(&m->lock); + if (timed_out) { + rt_log("motion: gyro calibration timed out after %.1f s (%d samples): the sensor thread stopped feeding it", + (now - start) / 1e9, count); + } + return st; +} diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 44031fcd..00d346d3 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -928,6 +928,22 @@ static void tab_controls(float scale) { opt_choice("TOTK_PROMPTS", UI_MENU_PROMPTS, prompts, 3, UI_MENU_PROMPTS_DESC, note); bool ask = restart && opt_button("##restart", UI_MENU_RESTART, UI_MENU_RESTART_DESC); opt_toggle("TOTK_MOTION", UI_MENU_MOTION, UI_MENU_MOTION_DESC); + opt_toggle("TOTK_GYRO_CALIBRATION", UI_MENU_GYRO_CALIBRATION, UI_MENU_GYRO_CALIBRATION_DESC); + if (input_gyro_calibration_enabled()) { + // The row exists only while the switch is on: with the bias not applied there is nothing to calibrate and no + // state to show. + char pad[128] = ""; + int st = input_motion_calib_state(pad, sizeof pad); + char status[192] = ""; + if (st == 1) + snprintf(status, sizeof status, "%s", menu_text(UI_MENU_GYRO_CALIBRATE_WAIT)); + else if (st == 2) + snprintf(status, sizeof status, menu_text(UI_MENU_GYRO_CALIBRATED), pad); + else if (st == 3) + snprintf(status, sizeof status, "%s", menu_text(UI_MENU_GYRO_CALIBRATE_FAILED)); + if (opt_button("##gyro_calib", UI_MENU_GYRO_CALIBRATE, UI_MENU_GYRO_CALIBRATE_DESC, status)) + input_motion_calibrate(); + } opt_slider("TOTK_RUMBLE_STRENGTH", UI_MENU_RUMBLE_STRENGTH, 0, 300, UI_MENU_RUMBLE_STRENGTH_DESC, 10, "%.0f %%"); opt_slider("TOTK_RUMBLE_CURVE", UI_MENU_RUMBLE_CURVE, 0.2f, 1.5f, UI_MENU_RUMBLE_CURVE_DESC, 0.05f, "%.2f"); opt_toggle("TOTK_RUMBLE_TRIGGERS", UI_MENU_RUMBLE_TRIGGERS, UI_MENU_RUMBLE_TRIGGERS_DESC); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 54a7b690..0d8f2f2c 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -251,6 +251,14 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_CHANNELS_AUTO] = "Automatic", [UI_MENU_CHANNELS_STEREO] = "Stereo", [UI_MENU_CHANNELS_51] = "5.1 surround", [UI_MENU_MOTION] = "Motion controls", [UI_MENU_MOTION_DESC] = "Aiming with the controller's gyroscope.", + [UI_MENU_GYRO_CALIBRATION] = "Gyro bias calibration", + [UI_MENU_GYRO_CALIBRATION_DESC] = "Subtracts the controller's resting drift, measured with the calibrate row " + "below. The measured values stay saved when this is off.", + [UI_MENU_GYRO_CALIBRATE] = "Calibrate gyro", + [UI_MENU_GYRO_CALIBRATE_DESC] = "Estimate and remove the gyroscope's resting drift. Hold the controller still.", + [UI_MENU_GYRO_CALIBRATE_WAIT] = "Calibrating...", + [UI_MENU_GYRO_CALIBRATED] = "%s, calibrated", + [UI_MENU_GYRO_CALIBRATE_FAILED] = "Calibration failed", [UI_MENU_RUMBLE_STRENGTH] = "Vibration strength", [UI_MENU_RUMBLE_STRENGTH_DESC] = "How strong the controller vibrates.", [UI_MENU_RUMBLE_CURVE] = "Vibration curve", @@ -782,6 +790,14 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_CHANNELS_AUTO] = "Автоматически", [UI_MENU_CHANNELS_STEREO] = "Стерео", [UI_MENU_CHANNELS_51] = "Объёмный 5.1", [UI_MENU_MOTION] = "Управление движением", [UI_MENU_MOTION_DESC] = "Прицеливание гироскопом контроллера.", + [UI_MENU_GYRO_CALIBRATION] = "Калибровка смещения гироскопа", + [UI_MENU_GYRO_CALIBRATION_DESC] = "Убирает покоящееся смещение контроллера, измеренное строкой калибровки ниже. " + "Измеренные значения сохраняются и при выключенном состоянии.", + [UI_MENU_GYRO_CALIBRATE] = "Калибровка гироскопа", + [UI_MENU_GYRO_CALIBRATE_DESC] = "Оценить и убрать покоящееся смещение гироскопа. Держите контроллер неподвижно.", + [UI_MENU_GYRO_CALIBRATE_WAIT] = "Калибровка...", + [UI_MENU_GYRO_CALIBRATED] = "%s, откалиброван", + [UI_MENU_GYRO_CALIBRATE_FAILED] = "Калибровка не удалась", [UI_MENU_RUMBLE_STRENGTH] = "Сила вибрации", [UI_MENU_RUMBLE_STRENGTH_DESC] = "Насколько сильно вибрирует контроллер.", [UI_MENU_RUMBLE_CURVE] = "Кривая вибрации", @@ -1232,6 +1248,14 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_CHANNELS_AUTO] = "Automático", [UI_MENU_CHANNELS_STEREO] = "Estéreo", [UI_MENU_CHANNELS_51] = "Surround 5.1", [UI_MENU_MOTION] = "Controles de movimento", [UI_MENU_MOTION_DESC] = "Mirar com o giroscópio do controle.", + [UI_MENU_GYRO_CALIBRATION] = "Calibração do desvio do giroscópio", + [UI_MENU_GYRO_CALIBRATION_DESC] = "Remove o desvio de repouso do controle, medido pela linha de calibração " + "abaixo. Os valores medidos ficam salvos mesmo com isto desligado.", + [UI_MENU_GYRO_CALIBRATE] = "Calibrar giroscópio", + [UI_MENU_GYRO_CALIBRATE_DESC] = "Estimar e remover o desvio de repouso do giroscópio. Segure o controle parado.", + [UI_MENU_GYRO_CALIBRATE_WAIT] = "Calibrando...", + [UI_MENU_GYRO_CALIBRATED] = "%s, calibrado", + [UI_MENU_GYRO_CALIBRATE_FAILED] = "Falha na calibração", [UI_MENU_RUMBLE_STRENGTH] = "Intensidade da vibração", [UI_MENU_RUMBLE_STRENGTH_DESC] = "Quão forte o controle vibra.", [UI_MENU_RUMBLE_CURVE] = "Curva da vibração", @@ -1845,6 +1869,13 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_CHANNELS_AUTO] = "自动", [UI_MENU_CHANNELS_STEREO] = "立体声", [UI_MENU_CHANNELS_51] = "5.1 环绕声", [UI_MENU_MOTION] = "体感操作", [UI_MENU_MOTION_DESC] = "用手柄的陀螺仪瞄准。", + [UI_MENU_GYRO_CALIBRATION] = "陀螺仪偏置校准", + [UI_MENU_GYRO_CALIBRATION_DESC] = "减去下面校准行测得的手柄静止偏移。关闭时测量值仍然保留。", + [UI_MENU_GYRO_CALIBRATE] = "校准陀螺仪", + [UI_MENU_GYRO_CALIBRATE_DESC] = "估计并消除陀螺仪的静止偏移。请保持手柄静止。", + [UI_MENU_GYRO_CALIBRATE_WAIT] = "校准中...", + [UI_MENU_GYRO_CALIBRATED] = "%s,已校准", + [UI_MENU_GYRO_CALIBRATE_FAILED] = "校准失败", [UI_MENU_RUMBLE_STRENGTH] = "振动强度", [UI_MENU_RUMBLE_STRENGTH_DESC] = "手柄振动的强弱。", [UI_MENU_RUMBLE_CURVE] = "振动曲线", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 0c9ee4e6..7ebc2443 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -106,6 +106,9 @@ typedef enum { // Controls UI_MENU_MOTION, UI_MENU_MOTION_DESC, + UI_MENU_GYRO_CALIBRATION, UI_MENU_GYRO_CALIBRATION_DESC, + UI_MENU_GYRO_CALIBRATE, UI_MENU_GYRO_CALIBRATE_DESC, UI_MENU_GYRO_CALIBRATE_WAIT, + UI_MENU_GYRO_CALIBRATED, UI_MENU_GYRO_CALIBRATE_FAILED, UI_MENU_RUMBLE_STRENGTH, UI_MENU_RUMBLE_STRENGTH_DESC, UI_MENU_RUMBLE_CURVE, UI_MENU_RUMBLE_CURVE_DESC, UI_MENU_RUMBLE_TRIGGERS, UI_MENU_RUMBLE_TRIGGERS_DESC, diff --git a/totk.ini.example b/totk.ini.example index 255ecc0e..bfd7af13 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -343,6 +343,18 @@ [input] # Gamepad motion controls (0 = off) and vibration (0 = off). # motion = 1 +# Gyro bias calibration (1 = on): the Controls tab's "Calibrate gyro" row measures the controller's resting drift and +# subtracts it from the gyro readings, which removes the yaw drift. Off, the fusion takes the raw gyro, an attempt in +# progress is dropped and nothing is loaded at start; the four keys below stay in the file, so turning it back on +# restores the last calibration. +# gyro_calibration = 1 +# The measured bias and the controller it was measured from, written by the game when calibration finishes; they are +# not meant to be typed. The bias is loaded at the next start only when gyro_bias_pad names the controller that is +# connected, so calibrate each controller separately: another one starts with no bias until it is calibrated. +# gyro_bias_x = 0 +# gyro_bias_y = 0 +# gyro_bias_z = 0 +# gyro_bias_pad = Nintendo Switch Pro Controller # rumble = 1 # Rumble strength in percent (0 to 300; 0 = off) and the exponent on the amplitude (0.2 to 1.5; lower = weak pulses # stronger, 1 = linear). rumble_triggers = 1 also drives the impulse triggers of an Xbox pad.