A new controller no longer inherits the previous one's gyro bias, and a calibration where the pad moved is rejected

The bias reset in sensors_ready only ran when sensor_pad was nonzero, but scan_pads zeroes it on every disconnect,
so a pad connected after another one kept the old pad's bias, and an attempt still collecting finished on the new
pad under the old pad's name. The bias was also loaded only once, at the first StartSixAxisSensor.

- input.c pad_bias_select runs for every pad the sensor code sees: it drops the old bias and attempt, then loads the
  saved bias when gyro_bias_pad names this pad. scan_pads drops them on disconnect. The bias loads with
  gyro_calibration off too, so turning the switch on mid-run applies it.
- The pad identity is the name plus an FNV-1a hash of SDL's serial when the pad reports one, so two pads of one
  model no longer share a bias; the hash keeps the Bluetooth address out of totk.ini and the log.
- An attempt skips 0.5 s of samples (the button press shakes the pad), then averages 1 s. It is rejected when the
  largest per-axis standard deviation exceeds 0.04 rad/s; a steady bias of any size passes. The row then says the
  controller moved (new UI_MENU_GYRO_CALIBRATE_MOVED in all four catalogs) and the bias in effect stays.
- The timeout counts 5 s without a sample, not 5 s from the start.
- Calibrate gyro starts the sensor thread when the game has not yet (title screen) instead of doing nothing.
- Row states are an enum (MOTION_CALIB_*); motion_calibrate_start takes the clock, and only the gamepad's fusion
  instance writes totk.ini, so --probe-services now checks a measured bias, a rejected attempt and the timeout.
- The duplicate copy_name in motion_fusion.c is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-15 09:32:05 +03:00
1 parent 0a792f22c1
commit 0efa904d90
9 files changed
+267 -182

No files matched your search

+15 -9
View File
@@ -114,15 +114,19 @@ 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.
// The Controls tab's Calibrate gyro row: start a bias measurement on the open gamepad (starts the sensor thread when
// the game has not yet). Callable from any thread.
void input_motion_calibrate(void);
// What the Calibrate gyro row shows (MOTION_CALIB_*): NONE no bias for this controller, COLLECTING, DONE a bias in
// effect (measured in this run or loaded from totk.ini), FAILED the last attempt got no samples and no bias is in
// effect, MOVED the last attempt was rejected because the controller moved. pad receives the name of the controller the
// bias belongs to (empty when none), copied so the caller does not read a buffer another thread may be rewriting.
enum { MOTION_CALIB_NONE, MOTION_CALIB_COLLECTING, MOTION_CALIB_DONE, MOTION_CALIB_FAILED, MOTION_CALIB_MOVED };
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.
// The fusion's own read of the same switch (motion_fusion.c); input.c reaches it for 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.
@@ -141,7 +145,9 @@ 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)
// Start a bias measurement at now_ns: samples while the controller rests, then the bias or a rejection (MOTION_CALIB_*).
// pad is the controller's identity, written to totk.ini with the bias.
void motion_calibrate_start(MotionFusion *m, const char *pad, uint64_t now_ns);
int motion_calib_poll(MotionFusion *m, uint64_t now_ns); // MOTION_CALIB_*; ends an attempt whose samples stopped
void motion_bias_load(MotionFusion *m); // take the bias from totk.ini (TOTK_GYRO_BIAS_*)
void motion_bias_reset(MotionFusion *m); // no bias and no attempt (another controller)