redesign of sent data

This commit is contained in:
Priec
2026-05-17 22:47:31 +02:00
parent 5399983dfb
commit 22b3b2307d
2 changed files with 46 additions and 35 deletions

View File

@@ -37,7 +37,7 @@ async fn main(spawner: Spawner) {
spawner.spawn(bit_send(tx, sender_receiver)).unwrap();
spawner.spawn(bit_receive_and_decode(rx)).unwrap();
spawner.spawn(bit_decode(receiver_reader)).unwrap();
// spawner.spawn(bit_decode(receiver_reader)).unwrap();
info!("starting echo");
let mut v: Vec<u8, 38, u8> = Vec::new();
@@ -51,7 +51,6 @@ async fn main(spawner: Spawner) {
info!("enkodovane, posielame do pipy...");
nrz(&v, &sender).await;
let _ = Timer::after(Duration::from_millis(1000));
embassy_time::block_for(Duration::from_millis(1000));
Timer::after(Duration::from_millis(1000)).await;
}
}

View File

@@ -10,58 +10,70 @@ use embassy_sync::{
use embassy_time::{Duration, Ticker};
use heapless::Vec;
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
const START: u8 = 0x7E;
const STOP: u8 = 0x81;
const BIT_PERIOD: Duration = Duration::from_millis(10);
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;
}
type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
fn crc8(data: &[u8]) -> u8 {
let mut crc: u8 = 0;
for &b in data {
crc ^= b;
for _ in 0..8 {
let mask = if (crc & 0x80) != 0 { 0x07 } else { 0 };
crc = (crc << 1) ^ mask;
}
}
crc
}
async fn send_byte(byte: u8, tx: &Tx) {
for i in (0..8).rev() {
tx.send((byte >> i) & 1).await;
}
}
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];
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
pub async fn nrz(payload: &[u8], tx: &Tx) {
// start
send_byte(START, tx).await;
for byte in sequence {
for i in (0..8).rev() {
sender.send((byte >> i) & 1).await;
}
// len
let len = payload.len() as u8;
send_byte(len, tx).await;
for &b in payload {
send_byte(b, tx).await;
}
for &bit in enkodovany {
sender.send(bit).await;
let mut buf: Vec<u8, 64> = Vec::new();
let _ = buf.push(len);
for &b in payload {
let _ = buf.push(b);
}
let crc = crc8(&buf);
send_byte(crc, tx).await;
for i in (0..8).rev() {
sender.send((sync_hod >> i) & 1).await;
let mut ones = len.count_ones() + crc.count_ones();
for &b in payload {
ones += b.count_ones();
}
tx.send((ones as u8) & 1).await;
send_byte(STOP, tx).await;
// idle
sender.send(1).await;
tx.send(1).await;
}
#[embassy_executor::task]
pub async fn bit_send(
mut pin: Output<'static>,
receiver: Receiver<'static, CriticalSectionRawMutex, u8, 64>,
rx: Receiver<'static, CriticalSectionRawMutex, u8, 64>,
) {
let mut ticker = Ticker::every(Duration::from_millis(10));
let mut ticker = Ticker::every(BIT_PERIOD);
loop {
let bit = receiver.receive().await;
let bit = rx.receive().await;
if bit == 1 {
pin.set_high();
} else {