-
Notifications
You must be signed in to change notification settings - Fork 40
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
Comments
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(); |
We already have I dont think it gets much simpler I'm afraid |
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,
} |
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. |
Thanks for the explanation! no composition it is 👍 |
Add a better way to create a Joystick Report.
The text was updated successfully, but these errors were encountered: