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

@@ -1,13 +1,13 @@
// src/send.rs
use embassy_stm32::gpio::Output;
use embassy_stm32::gpio::{Level, Output};
use embassy_sync::{
blocking_mutex::raw::CriticalSectionRawMutex,
channel::{Receiver, Sender},
};
use embassy_time::{Duration, Ticker};
use crate::Encoding;
use crate::{level_for_bit, Encoding};
const START: u8 = 0x7E;
const STOP: u8 = 0x81;
@@ -15,6 +15,10 @@ const BIT_PERIOD: Duration = Duration::from_millis(10);
pub type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
fn set_level(pin: &mut Output<'static>, is_high: bool) {
pin.set_level(if is_high { Level::High } else { Level::Low });
}
async fn send_byte(byte: u8, tx: &Tx) {
for i in (0..8).rev() {
tx.send((byte >> i) & 1).await;
@@ -71,20 +75,15 @@ pub async fn bit_send(
encoding: Encoding,
) {
let mut ticker = Ticker::every(BIT_PERIOD / 2);
let mut is_high = true;
pin.set_high();
let mut is_high = level_for_bit(1);
set_level(&mut pin, is_high);
loop {
let bit = rx.receive().await;
match encoding {
Encoding::Nrz => {
if bit == 1 {
is_high = true;
pin.set_high();
} else {
is_high = false;
pin.set_low();
}
is_high = level_for_bit(bit);
set_level(&mut pin, is_high);
ticker.next().await;
}
// toggle, ak sme v 1, inac sa nedeje nic
@@ -103,15 +102,15 @@ pub async fn bit_send(
Encoding::Manchester => {
if bit == 1 {
// high -> low
pin.set_high();
set_level(&mut pin, level_for_bit(1));
ticker.next().await;
pin.set_low();
set_level(&mut pin, level_for_bit(0));
ticker.next().await;
} else {
// low -> high
pin.set_low();
set_level(&mut pin, level_for_bit(0));
ticker.next().await;
pin.set_high();
set_level(&mut pin, level_for_bit(1));
ticker.next().await;
}
}