Struct Ps2Decoder

Source
pub struct Ps2Decoder { /* private fields */ }
Expand description

Handles decoding of IBM PS/2 Keyboard (and IBM PC/AT Keyboard) bit-streams.

Implementations§

Source§

impl Ps2Decoder

Source

pub const fn new() -> Ps2Decoder

Build a new PS/2 protocol decoder.

Examples found in repository?
examples/decoder.rs (line 4)
3fn main() {
4    let mut decoder = Ps2Decoder::new();
5
6    // If you get all 11 bits as one `u16`
7    match decoder.add_word(0x0402) {
8        Ok(byte) => println!("Word 0x0402 is byte 0x{:02x}", byte),
9        Err(e) => println!("Word 0x0402 failed to decode: {:?}", e),
10    }
11
12    // If you get a bit at a time
13    for bit in [
14        false, true, false, false, false, false, false, false, false, false, true,
15    ] {
16        match decoder.add_bit(bit) {
17            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
18            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
19            Err(e) => println!("Failed to decode: {e:?}"),
20        }
21    }
22
23    // Flipped a random bit, so we get a parity error
24    for bit in [
25        false, true, false, false, false, false, true, false, false, false, true,
26    ] {
27        match decoder.add_bit(bit) {
28            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
29            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
30            Err(e) => println!("Failed to decode: {e:?}"),
31        }
32    }
33}
Source

pub fn clear(&mut self)

Clears the bit register.

Call this when there is a timeout reading data from the keyboard.

Source

pub fn add_bit(&mut self, bit: bool) -> Result<Option<u8>, Error>

Shift a bit into the register.

Until the last bit is added you get Ok(None) returned.

Examples found in repository?
examples/decoder.rs (line 16)
3fn main() {
4    let mut decoder = Ps2Decoder::new();
5
6    // If you get all 11 bits as one `u16`
7    match decoder.add_word(0x0402) {
8        Ok(byte) => println!("Word 0x0402 is byte 0x{:02x}", byte),
9        Err(e) => println!("Word 0x0402 failed to decode: {:?}", e),
10    }
11
12    // If you get a bit at a time
13    for bit in [
14        false, true, false, false, false, false, false, false, false, false, true,
15    ] {
16        match decoder.add_bit(bit) {
17            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
18            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
19            Err(e) => println!("Failed to decode: {e:?}"),
20        }
21    }
22
23    // Flipped a random bit, so we get a parity error
24    for bit in [
25        false, true, false, false, false, false, true, false, false, false, true,
26    ] {
27        match decoder.add_bit(bit) {
28            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
29            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
30            Err(e) => println!("Failed to decode: {e:?}"),
31        }
32    }
33}
Source

pub fn add_word(&self, word: u16) -> Result<u8, Error>

Process an entire 11-bit word.

Must be packed into the bottom 11-bits of the 16-bit value.

Examples found in repository?
examples/decoder.rs (line 7)
3fn main() {
4    let mut decoder = Ps2Decoder::new();
5
6    // If you get all 11 bits as one `u16`
7    match decoder.add_word(0x0402) {
8        Ok(byte) => println!("Word 0x0402 is byte 0x{:02x}", byte),
9        Err(e) => println!("Word 0x0402 failed to decode: {:?}", e),
10    }
11
12    // If you get a bit at a time
13    for bit in [
14        false, true, false, false, false, false, false, false, false, false, true,
15    ] {
16        match decoder.add_bit(bit) {
17            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
18            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
19            Err(e) => println!("Failed to decode: {e:?}"),
20        }
21    }
22
23    // Flipped a random bit, so we get a parity error
24    for bit in [
25        false, true, false, false, false, false, true, false, false, false, true,
26    ] {
27        match decoder.add_bit(bit) {
28            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
29            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
30            Err(e) => println!("Failed to decode: {e:?}"),
31        }
32    }
33}

Trait Implementations§

Source§

impl Debug for Ps2Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Ps2Decoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.