Files
JoyD/ESP32/my_usb_project/rust_app/src/lib.rs

119 lines
3.0 KiB
Rust
Raw Normal View History

//! 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]
2026-04-08 14:07:37 +08:00
#![no_main]
use esp_backtrace as _;
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {
2026-04-01 11:39:33 +08:00
core::hint::spin_loop();
}
}
// HID 键码常量
const HID_KEY_A: u8 = 0x04;
const REPORT_ID_KEYBOARD: u8 = 1;
// FFI 函数声明
extern "C" {
fn rust_is_usb_mounted() -> bool;
fn rust_send_keyboard_report(report_id: u8, modifier: u8, keycode: *const u8);
fn rust_vTaskDelay(xTicksToDelay: u32);
2026-04-08 14:07:37 +08:00
fn rust_esp_log_i(tag: *const u8, format: *const u8);
}
/// 日志函数
fn log_info(message: &'static str) {
// 转换 Rust 静态字符串为 C 字符串
let tag = b"RustApp\0";
// 直接使用静态字符串的指针,确保它在整个程序生命周期内有效
// 注意Rust 的字符串字面量已经以 null 结尾
let msg_ptr = message.as_ptr();
unsafe {
rust_esp_log_i(tag.as_ptr(), msg_ptr);
}
}
#[no_mangle]
extern "C" fn rust_app_main() {
2026-04-01 13:57:22 +08:00
// 初始化代码
2026-04-08 14:07:37 +08:00
log_info("Rust app started");
2026-04-08 10:02:53 +08:00
loop {
2026-04-08 14:07:37 +08:00
log_info("Sending random key...");
2026-04-08 10:02:53 +08:00
send_random_key();
2026-04-08 14:07:37 +08:00
// 等待 5 秒 (5000ms / 10ms per tick = 500 ticks)
log_info("Waiting for 5 seconds...");
2026-04-08 10:02:53 +08:00
unsafe {
2026-04-08 14:07:37 +08:00
rust_vTaskDelay(500);
2026-04-08 10:02:53 +08:00
}
}
}
#[export_name = "rust_usb_callback"]
2026-04-01 11:39:33 +08:00
pub unsafe extern "C" fn usb_callback(_event: u32) {
}
2026-04-08 10:02:53 +08:00
/// 发送 A-Z 随机键的按键事件
fn send_random_key() {
// 等待 USB 设备挂载
let mut mounted = false;
for _ in 0..10 {
mounted = unsafe {
rust_is_usb_mounted()
};
if mounted {
2026-04-08 14:07:37 +08:00
log_info("USB mounted!");
break;
}
2026-04-08 14:07:37 +08:00
// 等待 100ms (100ms / 10ms per tick = 10 ticks)
log_info("USB not mounted yet");
unsafe {
2026-04-08 14:07:37 +08:00
rust_vTaskDelay(10);
}
}
if mounted {
2026-04-08 10:02:53 +08:00
// 生成随机的 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];
2026-04-08 14:07:37 +08:00
log_info("Sending key press event");
unsafe {
rust_send_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode.as_ptr());
}
2026-04-08 14:07:37 +08:00
// 等待 50ms (50ms / 10ms per tick = 5 ticks)
log_info("Waiting for 50ms...");
unsafe {
2026-04-08 14:07:37 +08:00
rust_vTaskDelay(5);
}
2026-04-08 10:02:53 +08:00
// 发送随机键释放事件
let keycode = [0, 0, 0, 0, 0, 0];
2026-04-08 14:07:37 +08:00
log_info("Sending key release event");
unsafe {
rust_send_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode.as_ptr());
}
2026-04-08 14:07:37 +08:00
} else {
log_info("USB not mounted after 10 attempts");
}
2026-04-08 10:02:53 +08:00
}
/// 获取随机数
fn get_random_u32() -> u32 {
// 使用简单的线性同余生成器
static mut SEED: u32 = 42;
unsafe {
SEED = SEED.wrapping_mul(1103515245).wrapping_add(12345);
SEED
}
2026-04-01 11:39:33 +08:00
}