Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add a better way to create a Joystick Report. #61

Open
aspizu opened this issue Nov 24, 2023 · 5 comments
Open

Feature Request: Add a better way to create a Joystick Report. #61

aspizu opened this issue Nov 24, 2023 · 5 comments

Comments

@aspizu
Copy link

aspizu commented Nov 24, 2023

Add a better way to create a Joystick Report.

@aspizu
Copy link
Author

aspizu commented Nov 25, 2023

This is how you can create a Joystick Report with two analog sticks and 8 buttons.

/// JoystickReport describes a report and its companion descriptor that can be
/// used to send joystick movements and button presses to a host.
#[gen_hid_descriptor(
    (collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = JOYSTICK) = {
        (collection = APPLICATION, usage = POINTER) = {
            (usage = X,) = {
                #[item_settings data,variable,absolute] x=input;
            };
            (usage = Y,) = {
                #[item_settings data,variable,absolute] y=input;
            };
            (usage = 0x33,) = {
                #[item_settings data,variable,absolute] rx=input;
            };
            (usage = 0x34,) = {
                #[item_settings data,variable,absolute] ry=input;
            };
        };
        (usage_page = BUTTON, usage_min = BUTTON_1, usage_max = BUTTON_8) = {
            #[packed_bits 8] #[item_settings data,variable,absolute] buttons=input;
        }
    }
)]
#[allow(dead_code)]
pub struct JoystickReport {
    pub x: i8,
    pub y: i8,
    pub rx: i8,
    pub ry: i8,
    pub buttons: u8,
}

usage:

let report = JoystickReport {
    x: X,
    y: Y,
    rx: RX,
    ry: RY,
    buttons: BUTTON_1
        | BUTTON_2 << 1
        | BUTTON_3 << 2
        | BUTTON_4 << 3
        | BUTTON_5 << 4
        | BUTTON_6 << 5),
};
hid_writer.write_serialize(&report).await.unwrap();

@aspizu aspizu changed the title Feature Request: Create custom joystick. Feature Request: Add a better way to create a Joystick Report. Nov 25, 2023
@twitchyliquid64
Copy link
Owner

We already have KeyboardReport, but if you want to send a PR with the above to add in JoystickReport, im down for that!

I dont think it gets much simpler I'm afraid

@vic1707
Copy link

vic1707 commented Jan 5, 2025

Pretty new to this world, sorry if its a stupid question.

Is there a way to compose reports from other reports or using substructs?

Having the final report struct looking like this is really appealing

pub struct GamepadReport {
    pub left_joystick: Joystick,
    pub right_joystick: Joystick,
    /// (A, B, X, Y, L, R), one bit per button
    pub buttons: u8,
}

pub struct Joystick {
    /// Horizontal axis
    pub x: i8,
    /// Vertical axis
    pub y: i8,
}

@twitchyliquid64
Copy link
Owner

No worries at all Victor!

Talking in vague generalities: with HID you are usually talking to a computer that expects exactly some format for the HID data coming in. Unfortunately that means we usually have to follow what the spec suggested for things like joysticks. OS code implementing HID is trash, so its common that even stuff that should work doesn't on something like (cough) windows.

As for composing the report struct using usbd-hid: unfortunately the code does not support nested structs - you need to unravel all the fields into one struct.

@vic1707
Copy link

vic1707 commented Jan 10, 2025

Thanks for the explanation! no composition it is 👍
I would have never thought that windows could be problematic, Microsoft would never do that xD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants