implemented stdin methods for reading a string (async) and reading keystrokes (sync + async). added a very basic shell on top of it for debugging

This commit is contained in:
2025-02-25 02:16:01 +00:00
parent 00d3a1de72
commit 27ee8226d8
7 changed files with 133 additions and 75 deletions
+27 -32
View File
@@ -24,9 +24,8 @@ pub static KEYBOARD: Lazy<Mutex<Keyboard<Uk105Key, ScancodeSet1>>> = Lazy::new(|
HandleControl::Ignore,
))
});
pub static KEYBOARD_HANDLER: Lazy<Mutex<KeyboardHandler>> =
Lazy::new(|| Mutex::new(KeyboardHandler::new()));
pub static SCANCODE_STREAM: Lazy<Mutex<ScancodeStream>> =
Lazy::new(|| Mutex::new(ScancodeStream::new()));
pub fn add_scancode(scancode: u8) {
if let Some(queue) = KBD_QUEUE.get() {
@@ -49,6 +48,10 @@ impl ScancodeStream {
KBD_QUEUE.call_once(|| ArrayQueue::new(5));
Self { _private: () }
}
pub fn try_next(&mut self) -> Option<u8> {
KBD_QUEUE.get().and_then(|queue| queue.pop())
}
}
impl Default for ScancodeStream {
@@ -77,42 +80,23 @@ impl Stream for ScancodeStream {
}
}
pub async fn print_keypresses() {
let mut scancodes = ScancodeStream::new();
let mut keyboard = Keyboard::new(
ScancodeSet1::new(),
layouts::Uk105Key,
HandleControl::Ignore,
);
while let Some(scancode) = scancodes.next().await {
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
if let Some(key) = keyboard.process_keyevent(key_event) {
match key {
DecodedKey::Unicode(character) => print!("{}", character),
DecodedKey::RawKey(key) => print!("{:?}", key),
}
pub async fn get_keystroke_async() -> KeyStroke {
loop {
if let Some(scancode) = SCANCODE_STREAM.lock().next().await {
if let Ok(keystroke) = KeyStroke::try_from(scancode) {
return keystroke;
}
}
}
}
pub struct KeyboardHandler {
_scancodes: ScancodeStream,
}
impl KeyboardHandler {
pub fn new() -> Self {
Self {
_scancodes: ScancodeStream::new(),
pub fn get_keystroke_optional() -> Option<KeyStroke> {
if let Some(scancode) = SCANCODE_STREAM.lock().try_next() {
if let Ok(keystroke) = KeyStroke::try_from(scancode) {
return Some(keystroke);
}
}
}
impl Default for KeyboardHandler {
fn default() -> Self {
Self::new()
}
None
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -185,6 +169,17 @@ impl TryFrom<u8> for KeyStroke {
}
}
impl TryInto<char> for KeyStroke {
type Error = ();
fn try_into(self) -> Result<char, Self::Error> {
match self {
Self::Char(c) => Ok(c),
_ => Err(()),
}
}
}
impl core::fmt::Display for KeyStroke {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {