Updated to 32 button descriptor

This commit is contained in:
Christoffer Martinsson 2024-07-08 23:36:14 +02:00
parent f16ecb30df
commit bf919854bb
3 changed files with 126 additions and 133 deletions

View File

@ -84,31 +84,37 @@ pub enum HidButton {
B19 = 18,
B20 = 19,
B21 = 20,
FnL = 21,
FnR = 22,
ModeL = 23,
ModeR = 24,
H1U = 25,
H1R = 26,
H1D = 27,
H1L = 28,
H1B = 29,
H2U = 30,
H2R = 31,
H2D = 32,
H2L = 33,
H2B = 34,
H3U = 35,
H3R = 36,
H3D = 37,
H3L = 38,
H3B = 39,
H4U = 40,
H4R = 41,
H4D = 42,
H4L = 43,
H4B = 44,
NoEventIndicated = 45,
B22 = 21,
B23 = 22,
B24 = 23,
B25 = 24,
B26 = 25,
B27 = 26,
B28 = 27,
Fn = 28,
ModeL = 29,
ModeR = 30,
H1U = 31,
H1R = 32,
H1D = 33,
H1L = 34,
H1B = 35,
H2U = 36,
H2R = 37,
H2D = 38,
H2L = 39,
H2B = 40,
H3U = 41,
H3R = 42,
H3D = 43,
H3L = 44,
H3B = 45,
H4U = 46,
H4R = 47,
H4D = 48,
H4L = 49,
H4B = 50,
NoEventIndicated = 51,
}
#[warn(dead_code)]
// Button index map:
@ -126,18 +132,18 @@ pub enum HidButton {
// ---------------------------------------------------------------
//
/// Button map to HID key (four function layers)
/// Please make sure to set FnL, FnR, ModeL and ModeR at the same position for all layers
/// Please make sure to set Fn, ModeL and ModeR at the same position for all layers
/// alt. only set these at function layer 0 and set NoEventIndicated in layer 1-3.
/// Hat button 1-4 = HID B21-B24.
/// Hat button 1-4 = HID B28-B32.
pub const HID_MAP: [[HidButton; NUMBER_OF_BUTTONS]; 4] = [
[
// Function layer 0
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::Fn, // 0
HidButton::B1, // 1
HidButton::B21, // 2
HidButton::FnR, // 3
HidButton::B27, // 2
HidButton::B21, // 3
HidButton::B6, // 4
HidButton::B2, // 5
HidButton::B3, // 6
@ -146,7 +152,7 @@ pub const HID_MAP: [[HidButton; NUMBER_OF_BUTTONS]; 4] = [
HidButton::B5, // 9
HidButton::B7, // 10
HidButton::B8, // 11
HidButton::ModeR, // 12
HidButton::B23, // 12
HidButton::B9, // 13
HidButton::B10, // 14
HidButton::H1B, // 15
@ -161,74 +167,14 @@ pub const HID_MAP: [[HidButton; NUMBER_OF_BUTTONS]; 4] = [
HidButton::H2L, // 24
],
[
// Function layer 1 (left Fn button pressed)
// Function layer 1 (Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B11, // 1
HidButton::B21, // 2
HidButton::FnR, // 3
HidButton::B6, // 4
HidButton::B12, // 5
HidButton::B13, // 6
HidButton::ModeL, // 7
HidButton::B14, // 8
HidButton::B15, // 9
HidButton::B7, // 10
HidButton::B8, // 11
HidButton::ModeR, // 12
HidButton::B9, // 13
HidButton::B10, // 14
HidButton::H3B, // 15
HidButton::H3U, // 16
HidButton::H3R, // 17
HidButton::H3D, // 18
HidButton::H3L, // 19
HidButton::H2B, // 20
HidButton::H2U, // 21
HidButton::H2R, // 22
HidButton::H2D, // 23
HidButton::H2L, // 24
],
[
// Function layer 2 (right Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B1, // 1
HidButton::B21, // 2
HidButton::FnR, // 3
HidButton::B16, // 4
HidButton::B2, // 5
HidButton::B3, // 6
HidButton::ModeL, // 7
HidButton::B4, // 8
HidButton::B5, // 9
HidButton::B17, // 10
HidButton::B18, // 11
HidButton::ModeR, // 12
HidButton::B19, // 13
HidButton::B20, // 14
HidButton::H1B, // 15
HidButton::H1U, // 16
HidButton::H1R, // 17
HidButton::H1D, // 18
HidButton::H1L, // 19
HidButton::H4B, // 20
HidButton::H4U, // 21
HidButton::H4R, // 22
HidButton::H4D, // 23
HidButton::H4L, // 24
],
[
// Function layer 3 (left + right Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B11, // 1
HidButton::B21, // 2
HidButton::FnR, // 3
HidButton::B16, // 4
HidButton::Fn, // 0
HidButton::B16, // 1
HidButton::B28, // 2
HidButton::B11, // 3
HidButton::B22, // 4
HidButton::B12, // 5
HidButton::B13, // 6
HidButton::ModeL, // 7
@ -236,7 +182,7 @@ pub const HID_MAP: [[HidButton; NUMBER_OF_BUTTONS]; 4] = [
HidButton::B15, // 9
HidButton::B17, // 10
HidButton::B18, // 11
HidButton::ModeR, // 12
HidButton::B23, // 12
HidButton::B19, // 13
HidButton::B20, // 14
HidButton::H3B, // 15
@ -250,6 +196,66 @@ pub const HID_MAP: [[HidButton; NUMBER_OF_BUTTONS]; 4] = [
HidButton::H4D, // 23
HidButton::H4L, // 24
],
[
// Function layer 2 (Mode R selected)
// HID Key // Button Index
// -----------------------------------------
HidButton::Fn, // 0
HidButton::H3U, // 1
HidButton::NoEventIndicated, // 2
HidButton::B21, // 3
HidButton::B6, // 4
HidButton::B2, // 5
HidButton::B3, // 6
HidButton::ModeL, // 7
HidButton::B4, // 8
HidButton::B5, // 9
HidButton::B7, // 10
HidButton::B8, // 11
HidButton::ModeR, // 12
HidButton::B9, // 13
HidButton::B10, // 14
HidButton::H1B, // 15
HidButton::H1U, // 16
HidButton::H1R, // 17
HidButton::H1D, // 18
HidButton::H1L, // 19
HidButton::H2B, // 20
HidButton::H2U, // 21
HidButton::H2R, // 22
HidButton::H2D, // 23
HidButton::H2L, // 24
],
[
// Function layer 3 (Mode R selected, Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::Fn, // 0
HidButton::H3D, // 1
HidButton::NoEventIndicated, // 2
HidButton::B11, // 3
HidButton::B16, // 4
HidButton::B12, // 5
HidButton::B13, // 6
HidButton::ModeL, // 7
HidButton::B14, // 8
HidButton::B15, // 9
HidButton::B17, // 10
HidButton::B18, // 11
HidButton::ModeR, // 12
HidButton::B19, // 13
HidButton::B20, // 14
HidButton::H3B, // 15
HidButton::H3U, // 16
HidButton::H3R, // 17
HidButton::H3D, // 18
HidButton::H3L, // 19
HidButton::H4B, // 20
HidButton::H4U, // 21
HidButton::H4R, // 22
HidButton::H4D, // 23
HidButton::H4L, // 24
],
];
// Button index map:

View File

@ -487,8 +487,7 @@ fn main() -> ! {
if !key.pressed {
key.fn_mode = mode & 0x0F;
} else if (usb_active
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::FnL
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::FnR
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::Fn
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::ModeL
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::ModeR
&& index != 2)
@ -513,8 +512,7 @@ fn main() -> ! {
for (index, key) in buttons.iter_mut().enumerate() {
if (usb_active
&& key.pressed != key.previous_pressed
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::FnL
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::FnR
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::Fn
&& layout::HID_MAP[key.fn_mode as usize][index] != layout::HidButton::ModeR)
|| (elrs_active
&& key.pressed != key.previous_pressed
@ -622,17 +620,13 @@ fn update_status_led<P, SM, I>(
fn get_mode(pressed_keys: [bool; NUMBER_OF_BUTTONS]) -> u8 {
// Check how many Fn keys are pressed
let mut mode: u8 = 0;
let mut fn_l_active: bool = false;
let mut fn_r_active: bool = false;
let mut fn_active: bool = false;
let mut alt_l_active: bool = false;
let mut alt_r_active: bool = false;
for (index, key) in pressed_keys.iter().enumerate() {
if *key && layout::HID_MAP[0][index] == layout::HidButton::FnL {
fn_l_active = true;
}
if *key && layout::HID_MAP[0][index] == layout::HidButton::FnR {
fn_r_active = true;
if *key && layout::HID_MAP[0][index] == layout::HidButton::Fn {
fn_active = true;
}
if *key && layout::HID_MAP[0][index] == layout::HidButton::ModeL {
alt_l_active = true;
@ -642,15 +636,15 @@ fn get_mode(pressed_keys: [bool; NUMBER_OF_BUTTONS]) -> u8 {
}
}
if fn_l_active && fn_r_active {
if alt_r_active && fn_active {
mode = 3;
} else if fn_r_active {
} else if alt_r_active {
mode = 2;
} else if fn_l_active {
} else if fn_active {
mode = 1;
}
// Set bit 4 and 5 if alt l/r is active
// Set bit 4 and 5 if mode l/r is active
if alt_l_active {
mode |= 0x10;
}
@ -677,7 +671,7 @@ fn get_joystick_report(
mode: &u8,
) -> JoystickReport {
let mut x: u16 = axis[GIMBAL_AXIS_RIGHT_X].value;
let mut y: u16 = axis[GIMBAL_AXIS_RIGHT_Y].value;
let mut y: u16 = AXIS_MAX - axis[GIMBAL_AXIS_RIGHT_Y].value;
let z: u16 = axis[GIMBAL_AXIS_LEFT_X].value;
let mut rx: u16 = AXIS_CENTER;
let mut ry: u16 = AXIS_CENTER;
@ -711,17 +705,13 @@ fn get_joystick_report(
// Right Alt mode active (bit 5)
// Right gimbal control third joystick axis when right Fn mode is active
if mode & 0x20 == 0x20
&& (axis[GIMBAL_AXIS_RIGHT_X].fn_mode == 2 || axis[GIMBAL_AXIS_RIGHT_X].fn_mode == 3)
{
if mode & 0x20 == 0x20 && (axis[GIMBAL_AXIS_RIGHT_X].fn_mode == 1) {
x = AXIS_CENTER;
rx = axis[GIMBAL_AXIS_RIGHT_X].value;
}
if mode & 0x20 == 0x20
&& (axis[GIMBAL_AXIS_RIGHT_Y].fn_mode == 2 || axis[GIMBAL_AXIS_RIGHT_Y].fn_mode == 3)
{
if mode & 0x20 == 0x20 && axis[GIMBAL_AXIS_RIGHT_Y].fn_mode == 1 {
y = AXIS_CENTER;
ry = axis[GIMBAL_AXIS_RIGHT_Y].value;
ry = AXIS_MAX - axis[GIMBAL_AXIS_RIGHT_Y].value;
}
// Generate array for all four hat switches with following structure:
@ -756,18 +746,18 @@ fn get_joystick_report(
let (hat4, hat_button4) = format_hat_value(hats[3]);
// Update button state for joystick button 21-24 according to hat button 1-4
let mut buttons: u32 = (hat_button1 as u32) << 21
| ((hat_button2 as u32) << 22)
| ((hat_button3 as u32) << 23)
| ((hat_button4 as u32) << 24);
let mut buttons: u32 = (hat_button1 as u32) << 28
| ((hat_button2 as u32) << 29)
| ((hat_button3 as u32) << 30)
| ((hat_button4 as u32) << 31);
// Update button state for joystick button 1-20
// Update button state for joystick button 1-28
for (index, key) in matrix_keys.iter_mut().enumerate() {
if key.pressed
&& layout::HID_MAP[key.fn_mode as usize][index] as usize
>= layout::HidButton::B1 as usize
&& layout::HID_MAP[key.fn_mode as usize][index] as usize
<= layout::HidButton::B21 as usize
<= layout::HidButton::B28 as usize
{
buttons |= 1 << layout::HID_MAP[key.fn_mode as usize][index] as usize;
}

View File

@ -90,15 +90,12 @@ pub const JOYSTICK_DESCRIPTOR: &[u8] = &[
0xc0, // End Collection
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (1)
0x29, 0x19, // Usage Maximum (25)
0x29, 0x20, // Usage Maximum (32)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x19, // Report Count (25)
0x95, 0x20, // Report Count (32)
0x81, 0x02, // Input (Data, Variable, Absolute)
0x75, 0x01, // Report Size (1) PADDING
0x95, 0x07, // Report Count (7) PADDING
0x81, 0x03, // Input (Const, Variable, Absolute) PADDING
0x15, 0x00, // Logical Minimum (0)
0x25, 0x07, // Logical Maximum (7)
0x35, 0x00, // Physical Minimum (0)
@ -123,7 +120,7 @@ pub struct JoystickReport {
pub rx: u16, // 12bit
pub ry: u16, // 12bit
pub rz: u16, // 12bit
pub buttons: u32, // 24bit
pub buttons: u32, // 32bit
pub hat1: u8, // 4bit
pub hat2: u8, // 4bit
pub hat3: u8, // 4bit