mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 06:17:49 +00:00
Add an asynchronous print_keypresses task
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
extern crate alloc;
|
||||
|
||||
use blog_os::println;
|
||||
use blog_os::task::{simple_executor::SimpleExecutor, Task};
|
||||
use blog_os::task::{keyboard, simple_executor::SimpleExecutor, Task};
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
@@ -29,6 +29,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
|
||||
let mut executor = SimpleExecutor::new();
|
||||
executor.spawn(Task::new(example_task()));
|
||||
executor.spawn(Task::new(keyboard::print_keypresses()));
|
||||
executor.run();
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
use crate::println;
|
||||
use crate::{print, println};
|
||||
use conquer_once::spin::OnceCell;
|
||||
use core::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use futures_util::{stream::Stream, task::AtomicWaker};
|
||||
use futures_util::{
|
||||
stream::{Stream, StreamExt},
|
||||
task::AtomicWaker,
|
||||
};
|
||||
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
||||
|
||||
static SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
|
||||
static WAKER: AtomicWaker = AtomicWaker::new();
|
||||
@@ -61,3 +65,19 @@ impl Stream for ScancodeStream {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn print_keypresses() {
|
||||
let mut scancodes = ScancodeStream::new();
|
||||
let mut keyboard = Keyboard::new(layouts::Us104Key, ScancodeSet1, 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user