Struct pc_keyboard::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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    let mut decoder = Ps2Decoder::new();

    // If you get all 11 bits as one `u16`
    match decoder.add_word(0x0402) {
        Ok(byte) => println!("Word 0x0402 is byte 0x{:02x}", byte),
        Err(e) => println!("Word 0x0402 failed to decode: {:?}", e),
    }

    // If you get a bit at a time
    for bit in [
        false, true, false, false, false, false, false, false, false, false, true,
    ] {
        match decoder.add_bit(bit) {
            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
            Err(e) => println!("Failed to decode: {e:?}"),
        }
    }

    // Flipped a random bit, so we get a parity error
    for bit in [
        false, true, false, false, false, false, true, false, false, false, true,
    ] {
        match decoder.add_bit(bit) {
            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
            Err(e) => println!("Failed to decode: {e:?}"),
        }
    }
}
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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    let mut decoder = Ps2Decoder::new();

    // If you get all 11 bits as one `u16`
    match decoder.add_word(0x0402) {
        Ok(byte) => println!("Word 0x0402 is byte 0x{:02x}", byte),
        Err(e) => println!("Word 0x0402 failed to decode: {:?}", e),
    }

    // If you get a bit at a time
    for bit in [
        false, true, false, false, false, false, false, false, false, false, true,
    ] {
        match decoder.add_bit(bit) {
            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
            Err(e) => println!("Failed to decode: {e:?}"),
        }
    }

    // Flipped a random bit, so we get a parity error
    for bit in [
        false, true, false, false, false, false, true, false, false, false, true,
    ] {
        match decoder.add_bit(bit) {
            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
            Err(e) => println!("Failed to decode: {e:?}"),
        }
    }
}
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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    let mut decoder = Ps2Decoder::new();

    // If you get all 11 bits as one `u16`
    match decoder.add_word(0x0402) {
        Ok(byte) => println!("Word 0x0402 is byte 0x{:02x}", byte),
        Err(e) => println!("Word 0x0402 failed to decode: {:?}", e),
    }

    // If you get a bit at a time
    for bit in [
        false, true, false, false, false, false, false, false, false, false, true,
    ] {
        match decoder.add_bit(bit) {
            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
            Err(e) => println!("Failed to decode: {e:?}"),
        }
    }

    // Flipped a random bit, so we get a parity error
    for bit in [
        false, true, false, false, false, false, true, false, false, false, true,
    ] {
        match decoder.add_bit(bit) {
            Ok(None) => println!("Added {}, not enough bits yet!", bit as u8),
            Ok(Some(byte)) => println!("Added {}, got byte 0x{byte:02x}", bit as u8),
            Err(e) => println!("Failed to decode: {e:?}"),
        }
    }
}

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.