Module calibration

Source
Expand description

Opt-in checked calibration for normalized embedding-host samples.

This module has no device reader, hardware discovery, persistence or automatic browser adoption. Linux polling and the existing browser ABI remain unchanged. A custom host owns one CalibratedGamepad per slot and submits raw PadState samples through it instead of also calling super::gamepad_sample. Never feed the output of CalibrationProfile::apply back into the adapter. Buttons (including analog triggers and hats) are copied, never inferred from calibrated axes. Unselected axes keep their channel meanings.

Profiles have no hardware identity. Selection belongs to this adapter’s current connection only: disconnect or a changed host-supplied PadState::epoch clears it. A host replacing a device without an observed disconnect must call CalibratedGamepad::disconnect first, or change its supplied epoch. The platform journal owns its own epoch; the supplied epoch is not forwarded as an ABI or hardware ID. Use the adapter and its slot on one host thread, with no competing sample producer; dropping it does not submit a disconnect.

A headless embedding-host recipe using the real journal and PlayerInput:

use ferravex_core::session::GameSession;
use ferravex_render::input::{
    self, ActionKind, Binding, InputCode, PadState, PlayerInput,
    calibration::{AxisCalibration, CalibrationProfile, CalibratedGamepad, RadialStick},
};
let mut session = GameSession::try_new(64., 4, true)?;
let mut player = PlayerInput::transient("calibration-recipe");
player.action("move_x", "Move", ActionKind::Axis,
    vec![Binding::digital(InputCode::PadAxis(0))])?;
player.action("jump", "Jump", ActionKind::Button,
    vec![Binding::digital(InputCode::PadButton(0))])?;
// Calibration supplies the radial response. Avoid applying a second deadzone
// in PlayerInput; its default response otherwise still applies independently.
let mut settings = player.settings();
settings.pad_deadzone = 0.;
settings.pad_curve = 1.;
player.set_settings(settings)?;
let mut pad = CalibratedGamepad::new(0)?;
let mut raw = PadState { connected: true, ..PadState::default() };
raw.axes[0] = 0.125; // supplied normalized drift, not a learned hardware value
pad.submit(raw)?;
input::host_frame(0., 640, 480, || player.sample(true)); // connection baseline
pad.select_profile(CalibrationProfile::try_new(
    &[(0, AxisCalibration { min: -0.75, center: 0.125, max: 0.875 })],
    &[RadialStick { axes: [0, 1], deadzone: 0.125 }],
)?)?;
raw.axes[0] = 0.875;
raw.buttons[0] = 1.;
pad.submit(raw)?;
raw.buttons[0] = 0.;
pad.submit(raw)?; // quick tap within the same render frame
input::host_frame(0., 640, 480, || {
    player.sample(true);
    assert_eq!(session.advance(0.).steps, 0); // leave gameplay edges pending
});
input::host_frame(2. / 64., 640, 480, || {
    player.sample(true);
    assert_eq!(player.value("move_x"), 1.);
    let ticks = session.advance(2. / 64.);
    assert_eq!(ticks.steps, 2);
    for tick in 0..ticks.steps {
        // Consume only on an actual simulation tick; share this snapshot.
        let [jump] = player.take_tick(["jump"]);
        assert_eq!(jump.pressed, tick == 0);
        assert_eq!(jump.released, tick == 0);
        assert_eq!(jump.value, 0.);
    }
});
pad.reset(); // immediately resubmits the last raw sample with default axes
pad.disconnect(); // neutral journal state and no retained profile

Structs§

AxisCalibration
A signed axis maps min to -1, center to 0 and max to 1, piecewise linearly. Require finite -1 <= min < center < max <= 1. Author the values in the host’s normalized units, before any application deadzone. For a trigger, opt in only if this signed response is desired; its button value is unchanged.
CalibratedGamepad
Explicit embedding-host adapter for one slot. No automatic device association, global selection, hidden native polling, allocations or button synthesis. It retains one last-good raw sample for immediate profile changes/reset. Errors preserve that sample, selection and existing journal state. Like super::gamepad_sample, success means accepted submission, not consumption; normal journal capacity/focus rules and PlayerInput held suppression still apply.
CalibrationProfile
Immutable, checked, fixed-storage configuration. Default has no axis corrections or radial pairs. Configuration is caller-owned and not serialized.
RadialStick
An explicit pair, processed after per-axis calibration. Radius <= deadzone becomes zero; (deadzone, 1) maps linearly to (0, 1); radius >= 1 becomes a unit vector. Direction is preserved. Require finite 0 <= deadzone < 1 and two distinct in-range axes which occur in no other pair. Zero still bounds diagonals to the unit disk; omit the pair to preserve square fullscale.

Enums§

CalibrationError
Validation errors never partially update a profile, adapter or journal.

Constants§

CALIBRATION_AXES
Axis count in PadState. Profiles are not restricted to Linux’s six axes.
CALIBRATION_STICKS
At most eight disjoint two-axis sticks fit in a sample.
GAMEPAD_SLOTS
Number of slots accepted by the existing embedding boundary.