redesign of sent data
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
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 {
|
||||
async fn send_byte(byte: u8, tx: &Tx) {
|
||||
for i in (0..8).rev() {
|
||||
sender.send((byte >> i) & 1).await;
|
||||
tx.send((byte >> i) & 1).await;
|
||||
}
|
||||
}
|
||||
|
||||
for &bit in enkodovany {
|
||||
sender.send(bit).await;
|
||||
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
|
||||
pub async fn nrz(payload: &[u8], tx: &Tx) {
|
||||
// start
|
||||
send_byte(START, tx).await;
|
||||
|
||||
// len
|
||||
let len = payload.len() as u8;
|
||||
send_byte(len, tx).await;
|
||||
for &b in payload {
|
||||
send_byte(b, tx).await;
|
||||
}
|
||||
|
||||
for i in (0..8).rev() {
|
||||
sender.send((sync_hod >> i) & 1).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;
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user