pin level agnostic

This commit is contained in:
Priec
2026-05-28 07:47:54 +02:00
parent 6334c95e05
commit ddef3db5fb
5 changed files with 61 additions and 42 deletions

View File

@@ -2,7 +2,7 @@ use defmt::info;
use embassy_stm32::exti::ExtiInput;
use embassy_time::{Ticker, Timer};
use crate::Encoding;
use crate::{Encoding, bit_for_level, level_for_bit};
const START: u8 = 0x7E;
const STOP: u8 = 0x81;
@@ -32,13 +32,9 @@ async fn sample_byte(
let current_level = pin.is_high();
let bit = match encoding {
Encoding::Nrz => {
if pin.is_high() {
ticker.next().await;
1
} else {
ticker.next().await;
0
}
let bit = bit_for_level(pin.is_high());
ticker.next().await;
bit
}
Encoding::Nrzi => {
let bit = if current_level != *last_physical_level {
@@ -59,7 +55,11 @@ async fn sample_byte(
Timer::after(bit_time / 4).await;
if first_half && !second_half { 1 } else { 0 }
if first_half == level_for_bit(1) && second_half == level_for_bit(0) {
1
} else {
0
}
}
};
byte = (byte << 1) | bit;
@@ -82,19 +82,15 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
}
let mut ticker = Ticker::every(bit_time);
let mut last_level = true;
let mut last_level = level_for_bit(1);
let mut start_byte = 0u8;
for _ in 0..7 {
let bit = match encoding {
Encoding::Nrz => {
if pin.is_high() {
ticker.next().await;
1
} else {
ticker.next().await;
0
}
let bit = bit_for_level(pin.is_high());
ticker.next().await;
bit
}
Encoding::Nrzi => {
let current_level = pin.is_high();
@@ -109,7 +105,11 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
Timer::after(bit_time / 2).await;
let second_pol = pin.is_high();
Timer::after(bit_time / 4).await;
if first_pol && !second_pol { 1 } else { 0 }
if first_pol == level_for_bit(1) && second_pol == level_for_bit(0) {
1
} else {
0
}
}
};
start_byte = (start_byte << 1) | bit;
@@ -141,13 +141,9 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
// parita
let parity_recv = match encoding {
Encoding::Nrz => {
if pin.is_high() {
ticker.next().await;
1
} else {
ticker.next().await;
0
}
let bit = bit_for_level(pin.is_high());
ticker.next().await;
bit
}
Encoding::Nrzi => {
let current_p = pin.is_high();
@@ -162,7 +158,11 @@ pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encod
Timer::after(bit_time / 2).await;
let s = pin.is_high();
Timer::after(bit_time / 4).await;
if f && !s { 1 } else { 0 }
if f == level_for_bit(1) && s == level_for_bit(0) {
1
} else {
0
}
}
};