Add an asynchronous print_keypresses task

This commit is contained in:
Philipp Oppermann
2020-03-27 15:56:39 +01:00
parent d63ddde756
commit d7b144364d
2 changed files with 24 additions and 3 deletions

View File

@@ -7,7 +7,7 @@
extern crate alloc; extern crate alloc;
use blog_os::println; 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 bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo; use core::panic::PanicInfo;
@@ -29,6 +29,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
let mut executor = SimpleExecutor::new(); let mut executor = SimpleExecutor::new();
executor.spawn(Task::new(example_task())); executor.spawn(Task::new(example_task()));
executor.spawn(Task::new(keyboard::print_keypresses()));
executor.run(); executor.run();
#[cfg(test)] #[cfg(test)]

View File

@@ -1,11 +1,15 @@
use crate::println; use crate::{print, println};
use conquer_once::spin::OnceCell; use conquer_once::spin::OnceCell;
use core::{ use core::{
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
}; };
use crossbeam_queue::ArrayQueue; 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 SCANCODE_QUEUE: OnceCell<ArrayQueue<u8>> = OnceCell::uninit();
static WAKER: AtomicWaker = AtomicWaker::new(); 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),
}
}
}
}
}