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
impl Ps2Decoder
Sourcepub const fn new() -> Ps2Decoder
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}Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the bit register.
Call this when there is a timeout reading data from the keyboard.
Sourcepub fn add_bit(&mut self, bit: bool) -> Result<Option<u8>, Error>
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}Sourcepub fn add_word(&self, word: u16) -> Result<u8, Error>
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}