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
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)
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:?}"),
}
}
}
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)
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:?}"),
}
}
}
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)
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:?}"),
}
}
}