//! Rust application for ESP32-S3 //! //! This module provides the main Rust application logic //! that interacts with the USB HID interface implemented in C/TinyUSB. #![no_std] #![no_main] use esp_backtrace as _; #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { loop { core::hint::spin_loop(); } } // HID 键码常量 const HID_KEY_A: u8 = 0x04; const REPORT_ID_KEYBOARD: u8 = 1; const REPORT_ID_MOUSE: u8 = 2; // FFI 函数声明 extern "C" { fn rust_is_usb_mounted() -> bool; fn rust_send_keyboard_report(report_id: u8, modifier: u8, keycode: *const u8); fn rust_send_mouse_report(report_id: u8, buttons: u8, x: i8, y: i8, wheel: i8, pan: i8); fn rust_vTaskDelay(xTicksToDelay: u32); fn rust_esp_log_i(tag: *const u8, format: *const u8); fn esp_task_wdt_reset(); } /// 日志函数 fn log_info(message: &'static str) { let tag = b"RustApp\0"; unsafe { rust_esp_log_i(tag.as_ptr(), message.as_ptr()); } } #[no_mangle] extern "C" fn rust_app_main() -> !{ // 初始化代码 - 添加延迟确保系统稳定 unsafe { rust_vTaskDelay(100); // 等待 1 秒让系统稳定 } log_info("========================\0"); log_info("Rust app started\0"); log_info("========================\0"); // 等待 USB 挂载成功 wait_usb_mounted(); let mut tick_count = 0; loop { // 每 5 秒 (500 ticks @ 10ms/tick) 按下一个随机键 if tick_count % 500 == 0 { log_info("Sending random key...\0"); send_random_key(); } // 每 6 秒 (600 ticks @ 10ms/tick) 随机移动一下鼠标 if tick_count % 600 == 0 { log_info("Sending random mouse move...\0"); send_random_mouse_move(); } // 喂看门狗 unsafe { esp_task_wdt_reset(); } // 等待 1 tick (10ms with default 100Hz config) unsafe { rust_vTaskDelay(1); } tick_count += 1; } } /// 等待 USB 挂载成功 fn wait_usb_mounted() { log_info("Waiting for USB to mount...\0"); log_info("Please connect USB OTG cable to host\0"); let mut retry_count = 0; loop { let mounted = unsafe { rust_is_usb_mounted() }; if mounted { log_info("USB mounted successfully!\0"); break; } retry_count += 1; if retry_count % 50 == 0 { // 每 5 秒输出一次 log_info("Still waiting for USB connection...\0"); } // 喂看门狗,防止复位 unsafe { esp_task_wdt_reset(); } // 等待 100ms (10 ticks @ 10ms/tick) unsafe { rust_vTaskDelay(10); } } } /// 发送 A-Z 随机键的按键事件 fn send_random_key() { // 检查 USB 是否仍然挂载 let mounted = unsafe { rust_is_usb_mounted() }; if !mounted { log_info("USB not mounted, skipping key press\0"); return; } // 生成随机的 A-Z 键码 let random_value = get_random_u32() % 26; let random_key = HID_KEY_A + (random_value as u8); // 发送随机键按下事件 let keycode = [random_key, 0, 0, 0, 0, 0]; log_info("Sending key press event\0"); unsafe { rust_send_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode.as_ptr()); } // 等待 50ms (5 ticks @ 10ms/tick) log_info("Waiting 50ms...\0"); unsafe { rust_vTaskDelay(5); } // 再次检查 USB 是否仍然挂载 let mounted = unsafe { rust_is_usb_mounted() }; if !mounted { log_info("USB not mounted, skipping key release\0"); return; } // 发送随机键释放事件 let keycode = [0, 0, 0, 0, 0, 0]; log_info("Sending key release event\0"); unsafe { rust_send_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode.as_ptr()); } } /// 发送随机鼠标移动事件 fn send_random_mouse_move() { // 生成随机的鼠标移动距离 (-10 到 10 之间) let random_x = (get_random_u32() % 21) as i8 - 10; let random_y = (get_random_u32() % 21) as i8 - 10; // 发送鼠标移动事件 log_info("Sending mouse move event\0"); unsafe { rust_send_mouse_report(REPORT_ID_MOUSE, 0, random_x, random_y, 0, 0); } } /// 获取随机数 fn get_random_u32() -> u32 { // 使用简单的线性同余生成器 static mut SEED: u32 = 42; unsafe { SEED = SEED.wrapping_mul(1103515245).wrapping_add(12345); SEED } }