twitchyliquid64 / usbd-hid

MIT License
87 stars 37 forks source link

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

Open aspizu opened 9 months ago

aspizu commented 9 months ago

Add a better way to create a Joystick Report.

aspizu commented 9 months ago

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();
twitchyliquid64 commented 1 month ago

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