Added one more Joystick button

This commit is contained in:
Christoffer Martinsson 2023-08-19 13:12:06 +02:00
parent d8e2084eb9
commit 29db001fe3
3 changed files with 176 additions and 160 deletions

View File

@ -37,8 +37,19 @@ pub enum ElrsButton {
CH11OFF = 25,
CH12ON = 26,
CH12OFF = 27,
NoEventIndicated = 28,
XY1TU = 28,
XY1TR = 29,
XY1TD = 30,
XY1TL = 31,
XY1TRESET = 32,
XY2TU = 33,
XY2TR = 34,
XY2TD = 35,
XY2TL = 36,
XY2TRESET = 37,
NoEventIndicated = 38,
}
#[allow(dead_code)]
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
pub enum HidButton {
B1 = 0,
@ -61,46 +72,47 @@ pub enum HidButton {
B18 = 17,
B19 = 18,
B20 = 19,
FnL = 20,
FnR = 21,
ModeL = 22,
ModeR = 23,
Hat1U = 24,
Hat1R = 25,
Hat1D = 26,
Hat1L = 27,
Hat1B = 28,
Hat2U = 29,
Hat2R = 30,
Hat2D = 31,
Hat2L = 32,
Hat2B = 33,
Hat3U = 34,
Hat3R = 35,
Hat3D = 36,
Hat3L = 37,
Hat3B = 38,
Hat4U = 39,
Hat4R = 40,
Hat4D = 41,
Hat4L = 42,
Hat4B = 43,
NoEventIndicated = 44,
B21 = 20,
FnL = 21,
FnR = 22,
ModeL = 23,
ModeR = 24,
Hat1U = 25,
Hat1R = 26,
Hat1D = 27,
Hat1L = 28,
Hat1B = 29,
Hat2U = 30,
Hat2R = 31,
Hat2D = 32,
Hat2L = 33,
Hat2B = 34,
Hat3U = 35,
Hat3R = 36,
Hat3D = 37,
Hat3L = 38,
Hat3B = 39,
Hat4U = 40,
Hat4R = 41,
Hat4D = 42,
Hat4L = 43,
Hat4B = 44,
NoEventIndicated = 45,
}
#[warn(dead_code)]
// Button index map:
// --------------------------------------------------------------
// | 0 | 1 | | 3 | 4 | (2)
// --------------------------------------------------------------
// | | 5 | 6 | 7 | | 12 | 11 | 10 | |
// | |
// | | 8 | | 13 | |
// | | 9 | | 14 | |
// | X1/Y1 X2/Y2 |
// | | 16 | | 21 | |
// | | 17 | 15 | 18 || 22 | 20 | 23 | |
// | | 19 | | 24 | |
// --------------------------------------------------------------
// | 0 | 1 | | 2 | | 3 | 4 |
// ---------------------------------------------------------------
// | | 5 | 6 | 7 | | 12 | 11 | 10 | |
// | |
// | | 8 | | 13 | |
// | | 9 | | 14 | |
// | X1/Y1 X2/Y2 |
// | | 16 | | 21 | |
// | | 17 | 15 | 18 | | 22 | 20 | 23 | |
// | | 19 | | 24 | |
// ---------------------------------------------------------------
//
/// 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
@ -111,121 +123,121 @@ pub const HID_MAP: [[HidButton; NUMBER_OF_BUTTONS]; 4] = [
// Function layer 0
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B1, // 1
HidButton::NoEventIndicated, // 2 Not connected to any button
HidButton::FnR, // 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::Hat1B, // 15 button 21
HidButton::Hat1U, // 16
HidButton::Hat1R, // 17
HidButton::Hat1D, // 18
HidButton::Hat1L, // 19
HidButton::Hat2B, // 20 button 22
HidButton::Hat2U, // 21
HidButton::Hat2R, // 22
HidButton::Hat2D, // 23
HidButton::Hat2L, // 24
HidButton::FnL, // 0
HidButton::B1, // 1
HidButton::B21, // 2
HidButton::FnR, // 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::Hat1B, // 15 button 22
HidButton::Hat1U, // 16
HidButton::Hat1R, // 17
HidButton::Hat1D, // 18
HidButton::Hat1L, // 19
HidButton::Hat2B, // 20 button 23
HidButton::Hat2U, // 21
HidButton::Hat2R, // 22
HidButton::Hat2D, // 23
HidButton::Hat2L, // 24
],
[
// Function layer 1 (left Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B11, // 1
HidButton::NoEventIndicated, // 2 Not connected to any button
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::Hat3B, // 15 button 23
HidButton::Hat3U, // 16
HidButton::Hat3R, // 17
HidButton::Hat3D, // 18
HidButton::Hat3L, // 19
HidButton::Hat2B, // 20 button 22
HidButton::Hat2U, // 21
HidButton::Hat2R, // 22
HidButton::Hat2D, // 23
HidButton::Hat2L, // 24
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::Hat3B, // 15 button 24
HidButton::Hat3U, // 16
HidButton::Hat3R, // 17
HidButton::Hat3D, // 18
HidButton::Hat3L, // 19
HidButton::Hat2B, // 20 button 23
HidButton::Hat2U, // 21
HidButton::Hat2R, // 22
HidButton::Hat2D, // 23
HidButton::Hat2L, // 24
],
[
// Function layer 2 (right Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B1, // 1
HidButton::NoEventIndicated, // 2 Not connected to any button
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::Hat1B, // 15 button 21
HidButton::Hat1U, // 16
HidButton::Hat1R, // 17
HidButton::Hat1D, // 18
HidButton::Hat1L, // 19
HidButton::Hat4B, // 20 button 24
HidButton::Hat4U, // 21
HidButton::Hat4R, // 22
HidButton::Hat4D, // 23
HidButton::Hat4L, // 24
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::Hat1B, // 15 button 22
HidButton::Hat1U, // 16
HidButton::Hat1R, // 17
HidButton::Hat1D, // 18
HidButton::Hat1L, // 19
HidButton::Hat4B, // 20 button 25
HidButton::Hat4U, // 21
HidButton::Hat4R, // 22
HidButton::Hat4D, // 23
HidButton::Hat4L, // 24
],
[
// Function layer 3 (left + right Fn button pressed)
// HID Key // Button Index
// -----------------------------------------
HidButton::FnL, // 0
HidButton::B11, // 1
HidButton::NoEventIndicated, // 2 Not connected to any button
HidButton::FnR, // 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::Hat3B, // 15 button 23
HidButton::Hat3U, // 16
HidButton::Hat3R, // 17
HidButton::Hat3D, // 18
HidButton::Hat3L, // 19
HidButton::Hat4B, // 20 button 24
HidButton::Hat4U, // 21
HidButton::Hat4R, // 22
HidButton::Hat4D, // 23
HidButton::Hat4L, // 24
HidButton::FnL, // 0
HidButton::B11, // 1
HidButton::B21, // 2
HidButton::FnR, // 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::Hat3B, // 15 button 24
HidButton::Hat3U, // 16
HidButton::Hat3R, // 17
HidButton::Hat3D, // 18
HidButton::Hat3L, // 19
HidButton::Hat4B, // 20 button 25
HidButton::Hat4U, // 21
HidButton::Hat4R, // 22
HidButton::Hat4D, // 23
HidButton::Hat4L, // 24
],
];
@ -235,7 +247,7 @@ pub const ELRS_MAP: [ElrsButton; NUMBER_OF_BUTTONS] = [
// -----------------------------------------
ElrsButton::CH7OFF, // 0
ElrsButton::CH7ON, // 1
ElrsButton::NoEventIndicated, // 2 Not connected to any button
ElrsButton::NoEventIndicated, // 2
ElrsButton::CH8OFF, // 3
ElrsButton::CH8ON, // 4
ElrsButton::CH9ON, // 5
@ -248,14 +260,14 @@ pub const ELRS_MAP: [ElrsButton; NUMBER_OF_BUTTONS] = [
ElrsButton::CH6, // 12
ElrsButton::CH12ON, // 13
ElrsButton::CH12OFF, // 14
ElrsButton::NoEventIndicated, // 15
ElrsButton::NoEventIndicated, // 16
ElrsButton::NoEventIndicated, // 17
ElrsButton::NoEventIndicated, // 18
ElrsButton::NoEventIndicated, // 19
ElrsButton::NoEventIndicated, // 20
ElrsButton::NoEventIndicated, // 21
ElrsButton::NoEventIndicated, // 22
ElrsButton::NoEventIndicated, // 23
ElrsButton::NoEventIndicated, // 24
ElrsButton::XY1TRESET, // 15
ElrsButton::XY1TU, // 16
ElrsButton::XY1TR, // 17
ElrsButton::XY1TD, // 18
ElrsButton::XY1TL, // 19
ElrsButton::XY2TRESET, // 20
ElrsButton::XY2TU, // 21
ElrsButton::XY2TR, // 22
ElrsButton::XY2TD, // 23
ElrsButton::XY2TL, // 24
];

View File

@ -496,7 +496,7 @@ fn get_mode(pressed_keys: [bool; NUMBER_OF_BUTTONS]) -> u8 {
mode
}
/// Generate keyboard report based on pressed keys and Fn mode (0, 1 or 2)
/// Generate keyboard report based on pressed keys and Fn mode (0, 1, 2 or 3)
/// layout::MAP contains the keycodes for each key in each Fn mode
///
/// # Arguments
@ -591,10 +591,10 @@ 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) << 20
| ((hat_button2 as u32) << 21)
| ((hat_button3 as u32) << 22)
| ((hat_button4 as u32) << 23);
let mut buttons: u32 = (hat_button1 as u32) << 21
| ((hat_button2 as u32) << 22)
| ((hat_button3 as u32) << 23)
| ((hat_button4 as u32) << 24);
// Update button state for joystick button 1-20
for (index, key) in matrix_keys.iter_mut().enumerate() {
@ -602,7 +602,7 @@ fn get_joystick_report(
&& 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::B20 as usize
<= layout::HidButton::B21 as usize
{
buttons |= 1 << layout::HID_MAP[key.fn_mode as usize][index] as usize;
}

View File

@ -68,7 +68,7 @@ impl<T, E> Try for Result<T, E> {
}
// Based on example device from https://github.com/dlkj/usbd-human-interface-device/blob/main/src/device/joystick.rs
// Updated to 6pc 12bit axis, 24pc buttons and 4pc hat switches
// Updated to 6x 12bit axis, 25x buttons and 4x hat switches
#[rustfmt::skip]
pub const JOYSTICK_DESCRIPTOR: &[u8] = &[
0x05, 0x01, // Usage Page (Generic Desktop)
@ -90,12 +90,15 @@ pub const JOYSTICK_DESCRIPTOR: &[u8] = &[
0xc0, // End Collection
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (1)
0x29, 0x18, // Usage Maximum (24)
0x29, 0x19, // Usage Maximum (25)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x18, // Report Count (24)
0x95, 0x19, // Report Count (25)
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)
@ -133,7 +136,7 @@ pub struct Joystick<'a, B: UsbBus> {
impl<'a, B: UsbBus> Joystick<'a, B> {
pub fn write_report(&mut self, report: &JoystickReport) -> Result<(), UsbHidError> {
let mut data: [u8; 14] = [0; 14];
let mut data: [u8; 15] = [0; 15];
// Did not make the packed struct work, so doing it manually
// TODO: make this work with packed struct
@ -149,8 +152,9 @@ impl<'a, B: UsbBus> Joystick<'a, B> {
data[9] = report.buttons as u8;
data[10] = (report.buttons >> 8) as u8;
data[11] = (report.buttons >> 16) as u8;
data[12] = (report.hat1) | (report.hat2 << 4);
data[13] = (report.hat3) | (report.hat4 << 4);
data[12] = (report.buttons >> 24) as u8;
data[13] = (report.hat1) | (report.hat2 << 4);
data[14] = (report.hat3) | (report.hat4 << 4);
self.interface
.write_report(&data)