diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index 6bcaae4a5..2e674b96f 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -24,6 +24,8 @@ pub(crate) enum Opcode { IsRestricted = 8, /// Set-and-check of USB debug restriction DebugUsbOp = 9, + /// Set autotype rate + SetAutotypeRate = 10, /// Send a U2F message U2fTx = 128, diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index 977a421b0..f63389247 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -259,6 +259,13 @@ impl UsbHid { None => Err(xous::Error::UseBeforeInit), } } + /// Sets the autotype delay. Defaults to 30ms on boot, must be reset every time on reboot. + pub fn set_autotype_delay_ms(&self, rate: usize) { + send_message( + self.conn, + Message::new_scalar(Opcode::SetAutotypeRate.to_usize().unwrap(), rate, 0, 0, 0) + ).unwrap(); // just unwrap it. If the send fails, we want to see the panic at this spot! + } pub fn get_led_state(&self) -> Result { match send_message( self.conn, diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index d20789a71..22663be13 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -309,6 +309,7 @@ pub(crate) fn main_hw() -> ! { let mut lockstatus_force_update = true; // some state to track if we've been through a suspend/resume, to help out the status thread with its UX update after a restart-from-cold let mut was_suspend = true; + let mut autotype_delay_ms = 30; #[cfg(feature="minimal")] std::thread::spawn(move || { @@ -1026,11 +1027,11 @@ pub(crate) fn main_hw() -> ! { let keyboard = composite.interface::, _>(); keyboard.write_report(&codes).ok(); keyboard.tick().ok(); - tt.sleep_ms(30).ok(); + tt.sleep_ms(autotype_delay_ms).ok(); if auto_up { keyboard.write_report(&[]).ok(); // this is the key-up keyboard.tick().ok(); - tt.sleep_ms(30).ok(); + tt.sleep_ms(autotype_delay_ms).ok(); } xous::return_scalar(msg.sender, 0).unwrap(); } else { @@ -1074,6 +1075,15 @@ pub(crate) fn main_hw() -> ! { _ => {} // do nothing; don't fail, don't report any error. } } + Some(Opcode::SetAutotypeRate) => msg_scalar_unpack!(msg, rate, _, _, _, { + // limit rate to 0.5s delay. Even then, this will probably cause repeated characters because + // it also adjusts keydown delays + let checked_rate = if rate > 500 { + 500 + } else { rate }; + // there is no limit on the minimum rate. good luck if you set it to 0! + autotype_delay_ms = checked_rate; + }), Some(Opcode::SendString) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut usb_send = buffer.to_original::().unwrap(); @@ -1093,10 +1103,10 @@ pub(crate) fn main_hw() -> ! { let keyboard = composite.interface::, _>(); keyboard.write_report(&codes).ok(); keyboard.tick().ok(); - tt.sleep_ms(30).ok(); + tt.sleep_ms(autotype_delay_ms).ok(); keyboard.write_report(&[]).ok(); // this is the key-up keyboard.tick().ok(); - tt.sleep_ms(30).ok(); + tt.sleep_ms(autotype_delay_ms).ok(); sent += 1; } }