use crate::input_device::InputDevice; use crate::protocol::{InputEvent, Key}; const UART_BASE: usize = 0x10000000; const UART_LSR: usize = 5; pub struct UartInputDevice { base: usize, } impl UartInputDevice { pub fn new() -> Self { Self { base: UART_BASE } } fn read_byte(&self) -> Option { let lsr = unsafe { core::ptr::read_volatile((self.base + UART_LSR) as *const u8) }; if lsr & 1 != 0 { Some(unsafe { core::ptr::read_volatile(self.base as *const u8) }) } else { None } } fn byte_to_key(byte: u8) -> Key { match byte { b'w' | b'W' => Key::Up, b's' | b'S' => Key::Down, b'a' | b'A' => Key::Left, b'd' | b'D' => Key::Right, b'\r' | b'\n' => Key::Enter, 0x1B => Key::Escape, c => Key::Char(c), } } } impl InputDevice for UartInputDevice { fn poll(&mut self) -> Option { let byte = self.read_byte()?; Some(InputEvent::KeyDown(Self::byte_to_key(byte))) } fn has_event(&self) -> bool { let lsr = unsafe { core::ptr::read_volatile((self.base + UART_LSR) as *const u8) }; lsr & 1 != 0 } }