split the codebase
This commit is contained in:
73
2sem_sem2/src/send.rs
Normal file
73
2sem_sem2/src/send.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// src/send.rs
|
||||
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use defmt::info;
|
||||
use embassy_stm32::gpio::Output;
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::CriticalSectionRawMutex,
|
||||
channel::{Receiver, Sender},
|
||||
};
|
||||
use embassy_time::{Duration, Ticker};
|
||||
use heapless::Vec;
|
||||
|
||||
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||
|
||||
pub fn encode(word: &str, v: &mut Vec<u8, 38, u8>) {
|
||||
v.clear();
|
||||
for byte in word.bytes() {
|
||||
for bit in 0..8 {
|
||||
let bit_value = (byte >> (7 - bit)) & 1;
|
||||
if v.push(bit_value).is_err() {
|
||||
info!("we panick boyz");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn nrz(
|
||||
enkodovany: &Vec<u8, 38, u8>,
|
||||
sender: &Sender<'_, CriticalSectionRawMutex, u8, 64>,
|
||||
) {
|
||||
if !SEND_ALLOWED.load(Ordering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
let sync_hod: u8 = 0xAA;
|
||||
let start_bits: u8 = 0xAB;
|
||||
let sequence = [sync_hod, start_bits];
|
||||
|
||||
for byte in sequence {
|
||||
for i in (0..8).rev() {
|
||||
sender.send((byte >> i) & 1).await;
|
||||
}
|
||||
}
|
||||
|
||||
for &bit in enkodovany {
|
||||
sender.send(bit).await;
|
||||
}
|
||||
|
||||
for i in (0..8).rev() {
|
||||
sender.send((sync_hod >> i) & 1).await;
|
||||
}
|
||||
|
||||
// idle
|
||||
sender.send(1).await;
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn bit_send(
|
||||
mut pin: Output<'static>,
|
||||
receiver: Receiver<'static, CriticalSectionRawMutex, u8, 64>,
|
||||
) {
|
||||
let mut ticker = Ticker::every(Duration::from_millis(10));
|
||||
loop {
|
||||
let bit = receiver.receive().await;
|
||||
if bit == 1 {
|
||||
pin.set_high();
|
||||
} else {
|
||||
pin.set_low();
|
||||
}
|
||||
// bitrate
|
||||
ticker.next().await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user