From 60309e3c7a867baf395e0780f98ab2cf4aec2a81 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Mon, 17 Jul 2023 16:32:47 +0200 Subject: [PATCH] Fixed GUI lock feature --- rp2040/src/main.rs | 50 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/rp2040/src/main.rs b/rp2040/src/main.rs index b6f9277..1df1cac 100644 --- a/rp2040/src/main.rs +++ b/rp2040/src/main.rs @@ -164,7 +164,7 @@ fn main() -> ! { // Create variables to track caps lock and fn mode let mut caps_lock_active: bool = false; let mut fn_mode: u8; - let mut gui_lock_active: bool = false; + let mut gui_lock_state: u8 = 0; let mut gui_lock_trigger_index: u8 = 0; // Initialize button matrix @@ -185,7 +185,7 @@ fn main() -> ! { loop { if status_led_count_down.wait().is_ok() { - update_status_led(&mut status_led, caps_lock_active, gui_lock_active); + update_status_led(&mut status_led, &caps_lock_active, &gui_lock_state); } if usb_hid_report_count_down.wait().is_ok() { @@ -194,7 +194,7 @@ fn main() -> ! { fn_mode = get_fn_mode(pressed_keys); if !caps_lock_active { - update_status_led(&mut status_led, caps_lock_active, gui_lock_active); + update_status_led(&mut status_led, &caps_lock_active, &gui_lock_state); } for (index, key) in pressed_keys.iter().enumerate() { buttons[index].pressed = *key; @@ -203,7 +203,7 @@ fn main() -> ! { let keyboard_report = get_keyboard_report( &mut buttons, fn_mode, - &mut gui_lock_active, + &mut gui_lock_state, &mut gui_lock_trigger_index, ); @@ -258,17 +258,17 @@ fn main() -> ! { /// * `caps_lock_active` - Is capslock active fn update_status_led( status_led: &mut Ws2812StatusLed, - caps_lock_active: bool, - gui_lock_active: bool, + caps_lock_active: &bool, + gui_lock_state: &u8, ) where P: PIOExt + FunctionConfig, I: PinId, Function

: ValidPinMode, SM: StateMachineIndex, { - if caps_lock_active { + if *caps_lock_active { status_led.update(StatusMode::Warning); - } else if gui_lock_active { + } else if *gui_lock_state != 0 { status_led.update(StatusMode::Activity); } else { status_led.update(StatusMode::Normal); @@ -307,7 +307,7 @@ fn get_fn_mode(pressed_keys: [bool; NUMBER_OF_KEYS]) -> u8 { fn get_keyboard_report( matrix_keys: &mut [KeyboardButton; NUMBER_OF_KEYS], fn_mode: u8, - gui_lock_active: &mut bool, + gui_lock_state: &mut u8, gui_lock_trigger_index: &mut u8, ) -> [Keyboard; NUMBER_OF_KEYS] { let mut keyboard_report: [Keyboard; NUMBER_OF_KEYS] = @@ -315,24 +315,27 @@ fn get_keyboard_report( // Filter report based on Fn mode and pressed keys for (index, key) in matrix_keys.iter_mut().enumerate() { - if key.pressed != key.previous_pressed && key.pressed { - key.fn_mode = fn_mode; - } - // Check if GUI lock button is pressed if key.pressed != key.previous_pressed && key.pressed - && index == layout::GUI_LOCK_BUTTON[0] as usize - && !(*gui_lock_active) + && index as u8 == layout::GUI_LOCK_BUTTON[0] && fn_mode == layout::GUI_LOCK_BUTTON[1] + && *gui_lock_state == 0 { - *gui_lock_active = true; - key.previous_pressed = key.pressed; - continue; + *gui_lock_state = 1; } + // Set fn mode for the pressed button + if key.pressed != key.previous_pressed && key.pressed { + key.fn_mode = fn_mode; + } key.previous_pressed = key.pressed; + // Skip key if defined as NoEventIndicated + if layout::MAP[key.fn_mode as usize][index] == Keyboard::NoEventIndicated { + continue; + } + /// Index of GUI key in keyboard report /// Index 36, 37, 38, 45, 46, 47 are not used by any other keys const GUI_REPORT_INDEX: usize = 47; @@ -340,15 +343,18 @@ fn get_keyboard_report( // If GUI lock is active, set LeftGUI key to pressed // when next button is pressed. Keep LeftGUI pressed // until next button is released - if *gui_lock_active && key.pressed { + if *gui_lock_state == 1 && key.pressed { *gui_lock_trigger_index = index as u8; + *gui_lock_state = 2; keyboard_report[GUI_REPORT_INDEX] = Keyboard::LeftGUI; - } else if *gui_lock_trigger_index as usize == index && key.pressed { + } else if *gui_lock_state == 2 && *gui_lock_trigger_index == index as u8 && key.pressed { keyboard_report[GUI_REPORT_INDEX] = Keyboard::LeftGUI; - } else if *gui_lock_trigger_index as usize == index && !key.pressed { - *gui_lock_active = false; + *gui_lock_state = 3; + } else if *gui_lock_state == 3 && *gui_lock_trigger_index == index as u8 && !key.pressed { + *gui_lock_state = 0; } + // Add defined HID key to the report if key.pressed { keyboard_report[index] = layout::MAP[key.fn_mode as usize][index]; }