Compare commits
2 Commits
9451bc5ae9
...
522236e20c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
522236e20c | ||
|
|
804dc66a4b |
@@ -116,25 +116,9 @@ pub fn decode_uart_samples(
|
||||
let frame_bits = 1 + nbits + parity_bits + stop_bits_count;
|
||||
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 mut votes = 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
|
||||
}
|
||||
if samples[i] != 0 { 1 } else { 0 }
|
||||
};
|
||||
|
||||
// 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 {
|
||||
Parity::None => 0,
|
||||
Parity::Even | Parity::Odd => {
|
||||
// Mask to only count bits that are part of the data
|
||||
let mask: u8 = if data_bits == 8 { 0xFF } else { ((1u16 << data_bits) - 1) as u8 };
|
||||
|
||||
let ones = (data & mask).count_ones() & 1;
|
||||
let ones = data.count_ones() & 1;
|
||||
match parity {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user