2 Commits

Author SHA1 Message Date
Priec
522236e20c removed redundant code 2025-11-23 23:12:32 +01:00
Priec
804dc66a4b sampling only in the middle 2025-11-23 22:16:36 +01:00

View File

@@ -116,25 +116,9 @@ pub fn decode_uart_samples(
let frame_bits = 1 + nbits + parity_bits + stop_bits_count; let frame_bits = 1 + nbits + parity_bits + stop_bits_count;
let frame_len = frame_bits * ovs; let frame_len = frame_bits * ovs;
// Majority vote over 3 samples centered at `i` // Get sample in the middle of the bit
let get_bit = |i: usize| -> u8 { let get_bit = |i: usize| -> u8 {
let mut votes = 0; if samples[i] != 0 { 1 } else { 0 }
// Check i-1, i, i+1. Saturating sub/add handles boundaries roughly.
if i > 0 && samples.get(i - 1).map_or(true, |&x| x != 0) {
votes += 1;
}
if samples.get(i).map_or(true, |&x| x != 0) {
votes += 1;
}
if samples.get(i + 1).map_or(true, |&x| x != 0) {
votes += 1;
}
if votes >= 2 {
1
} else {
0
}
}; };
// Decode while remaining samples for a full frame // Decode while remaining samples for a full frame
@@ -212,10 +196,7 @@ fn calculate_parity(data: u8, parity: Parity, data_bits: u8) -> u8 {
match parity { match parity {
Parity::None => 0, Parity::None => 0,
Parity::Even | Parity::Odd => { Parity::Even | Parity::Odd => {
// Mask to only count bits that are part of the data let ones = data.count_ones() & 1;
let mask: u8 = if data_bits == 8 { 0xFF } else { ((1u16 << data_bits) - 1) as u8 };
let ones = (data & mask).count_ones() & 1;
match parity { match parity {
Parity::Even => ones as u8, // If ones=1 (odd), emit 1 to make even Parity::Even => ones as u8, // If ones=1 (odd), emit 1 to make even
Parity::Odd => (ones ^ 1) as u8, // XOR - If ones=1 (odd), emit 0 to keep odd Parity::Odd => (ones ^ 1) as u8, // XOR - If ones=1 (odd), emit 0 to keep odd