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
+13 -15
View File
@@ -29,9 +29,6 @@ pub struct Writer {
fg_color: Colour,
bg_color: Colour,
offset1: usize,
offset2: usize,
}
impl Default for Writer {
@@ -54,8 +51,6 @@ impl Writer {
text_col: 0,
fg_color: Colour::White,
bg_color: Colour::Black,
offset1: 16,
offset2: 0,
},
)
}
@@ -65,10 +60,10 @@ impl Writer {
}
/// This is sent when the user types a backspace.
const BACKSPACE: u16 = 8;
const BACKSPACE: u8 = 8;
pub fn write_glyph(&mut self, c: u16) {
if c == b'\n' as u16 {
pub fn write_glyph(&mut self, c: u8) {
if c == b'\n' {
self.newline();
return;
} else if c == Self::BACKSPACE {
@@ -104,11 +99,6 @@ impl Writer {
}
}
pub const fn set_offset(&mut self, offset1: usize, offset2: usize) {
self.offset1 = offset1;
self.offset2 = offset2;
}
pub const fn dimensions(&self) -> (u32, u32) {
(self.screen_width, self.screen_height)
}
@@ -133,14 +123,14 @@ impl Writer {
if self.text_col > 0 {
self.text_col -= 1;
// Blank out the previous char.
self.write_glyph(' ' as u16);
self.write_glyph(b' ');
self.text_col -= 1;
}
}
pub fn write_string(&mut self, s: &str) {
for c in s.chars() {
self.write_glyph(c as u16);
self.write_glyph(c as u8);
}
}
@@ -203,6 +193,14 @@ pub fn clear_screen() {
});
}
pub fn reset_cursor() {
interrupts::without_interrupts(|| {
let mut writer = WRITER.lock();
writer.text_line = 0;
writer.text_col = 0;
});
}
#[macro_export]
macro_rules! println_log {
() => ($crate::print_log!("\n"));
+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 {