neser 0.1.1

NESER - NES Emulator in Rust - is a NES emulator written in Rust. It aims to be a high-quality, hardware-accurate emulator that is also easy to use and extend. It supports a wide range of NES games and features, including various mappers, audio processing, and input handling. NESER is designed to be modular and extensible, allowing developers to easily add new features or support for additional hardware. It can be run using one of two frontends: a native desktop application using SDL2, or a web application using WebAssembly. The desktop application provides a high-performance, feature-rich experience with support for various input devices and display options, while the web application allows users to play NES games directly in their browsers without needing to install any software in a BYOR manner (Bring Your Own Roms).
Documentation
// Default analog stick axis threshold used to interpret directional input.
// Callers can override this by passing a custom `axisThreshold` value.
const DEFAULT_AXIS_THRESHOLD = 0.5;

export function mapStandardGamepadState(gamepad, axisThreshold = DEFAULT_AXIS_THRESHOLD) {
    const buttons = gamepad?.buttons ?? [];
    const axes = gamepad?.axes ?? [];

    const up = Boolean(buttons[12]?.pressed) || axes[1] < -axisThreshold;
    const down = Boolean(buttons[13]?.pressed) || axes[1] > axisThreshold;
    const left = Boolean(buttons[14]?.pressed) || axes[0] < -axisThreshold;
    const right = Boolean(buttons[15]?.pressed) || axes[0] > axisThreshold;

    return {
        a: Boolean(buttons[0]?.pressed),
        b: Boolean(buttons[1]?.pressed),
        select: Boolean(buttons[8]?.pressed),
        start: Boolean(buttons[9]?.pressed),
        up,
        down,
        left,
        right
    };
}

export function selectPrimaryGamepad(gamepads) {
    if (!gamepads) return null;
    for (const gamepad of gamepads) {
        if (gamepad && gamepad.connected) {
            return gamepad;
        }
    }
    return null;
}

/**
 * Select up to two connected gamepads from the gamepads array.
 * 
 * @param {Gamepad[]} gamepads - Array of gamepads (may contain null/undefined)
 * @returns {Gamepad[]} Array of connected gamepads (0-2 elements)
 */
export function selectGamepads(gamepads) {
    if (!gamepads) return [];
    
    const connected = [];
    for (const gamepad of gamepads) {
        if (gamepad && gamepad.connected) {
            connected.push(gamepad);
            if (connected.length >= 2) {
                break;
            }
        }
    }
    return connected;
}