idk
This commit is contained in:
FantasyPvP
2023-10-02 23:26:54 +01:00
parent bf9c9be88d
commit 410278b6e3
9 changed files with 165 additions and 6 deletions
+32
View File
@@ -70,6 +70,29 @@ impl KeyboardHandler {
}
}
pub fn try_keystroke(&mut self) -> Option<char> {
if let Some(scancode) = self.scancodes.try_next() {
if let Ok(Some(key_event)) = self.keyboard.add_byte(scancode) {
if let Some(key) = self.keyboard.process_keyevent(key_event) {
match key {
DecodedKey::Unicode(character) => {
if character == b'\x08' as char { // checks if the character is a backspace
interrupts::without_interrupts(|| {
RENDERER.lock().backspace(); // runs the backspace function of the vga buffer to remove the last character
});
return None;
} else {
return Some(character);
}
},
DecodedKey::RawKey(key) => { print!("{:?}", key) },
}
}
}
};
None
}
pub async fn get_string(&mut self) -> String {
let mut val = String::new();
@@ -116,6 +139,15 @@ impl ScanCodeStream {
.expect("ScanCodeStream::new has already been called once");
ScanCodeStream { _private: () }
}
pub fn try_next(&mut self) -> Option<u8> {
let queue = SCANCODE_QUEUE.try_get().expect("not initialised");
if let Ok(c) = queue.pop() {
return Some(c);
} else {
return None;
}
}
}
impl Stream for ScanCodeStream {
+5
View File
@@ -22,6 +22,11 @@ impl Stdin {
let chr = KEYBOARD.lock().get_keystroke().await;
chr
}
pub fn try_keystroke() -> Option<char> {
let chr = KEYBOARD.lock().try_keystroke();
chr
}
}
pub struct Screen {}
+1
View File
@@ -4,6 +4,7 @@ pub mod application;
pub mod tasks;
pub mod os;
pub mod frame;
pub mod time;
// this is where the standard library for the operating system will be defined
+28
View File
@@ -0,0 +1,28 @@
use core::time::Duration;
use embedded_time::{Clock, Timer};
use cmos_rtc::{ReadRTC, Time};
use crate::println;
use super::super::kernel::interrupts::GLOBALTIMER;
use x86_64::instructions::interrupts;
pub fn wait(seconds: i64) {
let mut start = 0;
interrupts::without_interrupts(||{
start = GLOBALTIMER.lock().val;
});
loop {
let mut new = 0;
interrupts::without_interrupts(||{
new = GLOBALTIMER.lock().val;
});
if new + seconds > start {
return
}
};
}
pub fn timer() {
interrupts::without_interrupts(||{
println!("{}", GLOBALTIMER.lock().val);
});
}
+1 -1
View File
@@ -6,5 +6,5 @@ pub mod shell;
//pub mod shellrewrite;
pub mod tasks;
mod gigachad_detector;
mod shellrewrite;
//mod shellrewrite;
mod snake;
+4
View File
@@ -132,6 +132,10 @@ async fn exec() -> Result<(), Error> {
"gigachad?" => {
let mut gigachad_detector = GigachadDetector::new();
gigachad_detector.run(args).await?;
}
"time" => {
use crate::std::time::timer;
timer();
}
"test_features" => {
use crate::std::random::Random;
+20 -5
View File
@@ -1,7 +1,8 @@
use alloc::string::String;
use alloc::{format, vec, vec::Vec, boxed::Box};
use async_trait::async_trait;
use crate::std::io::{Color, Screen};
use crate::std::io::{Color, Screen, Stdin};
use crate::std::time;
use crate::kernel::tasks::keyboard::KEYBOARD;
use crossbeam_queue::SegQueue;
use crate::kernel::render::{ColorCode, ScreenChar};
@@ -19,6 +20,7 @@ pub struct Game {
snake: SegQueue<Point>,
head: Point,
poi: Point,
mv: char,
score: u8
}
@@ -29,22 +31,34 @@ impl Application for Game {
snake: SegQueue::new(),
head: Point { x: 5, y: 5 },
poi: Point { x: 0, y: 0 },
mv: ' ',
score: 0
}
}
async fn run(&mut self, _: Vec<String>) -> Result<(), Error> {
Screen::application_mode();
let clone = self.clone_snake();
self.render(clone).map_err(|_| Error::ApplicationError(String::from("failed to render game screen")))?;
(0..=2).for_each(|x| {
(5..=7).for_each(|x| {
self.snake.push(Point { x, y: 5 });
});
self.head = Point { x: 2, y: 5 };
self.head = Point { x: 7, y: 5 };
self.new_poi();
'gameloop: loop {
let chr = KEYBOARD.lock().get_keystroke().await;
match chr {
time::wait(20);
// if let Some(c) = Stdin::try_keystroke() {
// self.mv = c;
// }
self.mv = Stdin::keystroke().await;
match self.mv {
'w' => self.head.y -= 1,
'a' => self.head.x -= 1,
's' => self.head.y += 1,
@@ -52,6 +66,7 @@ impl Application for Game {
'x' => break,
_ => continue,
}
self.snake.push(Point { x: self.head.x, y: self.head.y }); // new head added
if self.head == self.poi {