feat(input-server): add InputServer<D: InputDevice> with protocol, mock + UART adapters, and tests

This commit is contained in:
2026-10-18 03:08:36 +02:00
parent 5600ce0260
commit c9bd1d2c35
11 changed files with 346 additions and 12 deletions

View File

@@ -0,0 +1,107 @@
use server_framework::{Decode, Encode};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Key {
Up,
Down,
Left,
Right,
Enter,
Escape,
Char(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputEvent {
KeyDown(Key),
KeyUp(Key),
}
#[derive(Debug, PartialEq)]
pub enum InputRequest {
PollEvent,
WaitEvent,
}
#[derive(Debug, PartialEq)]
pub enum InputResponse {
Event(InputEvent),
NoEvent,
}
impl Encode for InputRequest {
fn encode(&self) -> [usize; 4] {
match self {
InputRequest::PollEvent => [0, 0, 0, 0],
InputRequest::WaitEvent => [1, 0, 0, 0],
}
}
}
impl Decode for InputRequest {
fn decode(words: [usize; 4]) -> Option<Self> {
match words[0] {
0 => Some(InputRequest::PollEvent),
1 => Some(InputRequest::WaitEvent),
_ => None,
}
}
}
fn encode_key(key: Key) -> (usize, usize) {
match key {
Key::Up => (0, 0),
Key::Down => (1, 0),
Key::Left => (2, 0),
Key::Right => (3, 0),
Key::Enter => (4, 0),
Key::Escape => (5, 0),
Key::Char(c) => (6, c as usize),
}
}
fn decode_key(kind: usize, data: usize) -> Option<Key> {
match kind {
0 => Some(Key::Up),
1 => Some(Key::Down),
2 => Some(Key::Left),
3 => Some(Key::Right),
4 => Some(Key::Enter),
5 => Some(Key::Escape),
6 => Some(Key::Char(data as u8)),
_ => None,
}
}
impl Encode for InputResponse {
fn encode(&self) -> [usize; 4] {
match self {
InputResponse::NoEvent => [0, 0, 0, 0],
InputResponse::Event(InputEvent::KeyDown(key)) => {
let (kind, data) = encode_key(*key);
[1, kind, data, 0]
}
InputResponse::Event(InputEvent::KeyUp(key)) => {
let (kind, data) = encode_key(*key);
[2, kind, data, 0]
}
}
}
}
impl Decode for InputResponse {
fn decode(words: [usize; 4]) -> Option<Self> {
match words[0] {
0 => Some(InputResponse::NoEvent),
1 => {
let key = decode_key(words[1], words[2])?;
Some(InputResponse::Event(InputEvent::KeyDown(key)))
}
2 => {
let key = decode_key(words[1], words[2])?;
Some(InputResponse::Event(InputEvent::KeyUp(key)))
}
_ => None,
}
}
}