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_send(tx, sender_receiver)).unwrap();
|
||||||
spawner.spawn(bit_receive_and_decode(rx)).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");
|
info!("starting echo");
|
||||||
let mut v: Vec<u8, 38, u8> = Vec::new();
|
let mut v: Vec<u8, 38, u8> = Vec::new();
|
||||||
@@ -51,7 +51,6 @@ async fn main(spawner: Spawner) {
|
|||||||
info!("enkodovane, posielame do pipy...");
|
info!("enkodovane, posielame do pipy...");
|
||||||
nrz(&v, &sender).await;
|
nrz(&v, &sender).await;
|
||||||
|
|
||||||
let _ = Timer::after(Duration::from_millis(1000));
|
Timer::after(Duration::from_millis(1000)).await;
|
||||||
embassy_time::block_for(Duration::from_millis(1000));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,58 +10,70 @@ use embassy_sync::{
|
|||||||
use embassy_time::{Duration, Ticker};
|
use embassy_time::{Duration, Ticker};
|
||||||
use heapless::Vec;
|
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>) {
|
type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
|
||||||
v.clear();
|
|
||||||
for byte in word.bytes() {
|
fn crc8(data: &[u8]) -> u8 {
|
||||||
for bit in 0..8 {
|
let mut crc: u8 = 0;
|
||||||
let bit_value = (byte >> (7 - bit)) & 1;
|
for &b in data {
|
||||||
if v.push(bit_value).is_err() {
|
crc ^= b;
|
||||||
info!("we panick boyz");
|
for _ in 0..8 {
|
||||||
return;
|
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(
|
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
|
||||||
enkodovany: &Vec<u8, 38, u8>,
|
pub async fn nrz(payload: &[u8], tx: &Tx) {
|
||||||
sender: &Sender<'_, CriticalSectionRawMutex, u8, 64>,
|
// start
|
||||||
) {
|
send_byte(START, tx).await;
|
||||||
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 {
|
// len
|
||||||
for i in (0..8).rev() {
|
let len = payload.len() as u8;
|
||||||
sender.send((byte >> i) & 1).await;
|
send_byte(len, tx).await;
|
||||||
}
|
for &b in payload {
|
||||||
|
send_byte(b, tx).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
for &bit in enkodovany {
|
let mut buf: Vec<u8, 64> = Vec::new();
|
||||||
sender.send(bit).await;
|
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() {
|
let mut ones = len.count_ones() + crc.count_ones();
|
||||||
sender.send((sync_hod >> i) & 1).await;
|
for &b in payload {
|
||||||
|
ones += b.count_ones();
|
||||||
}
|
}
|
||||||
|
tx.send((ones as u8) & 1).await;
|
||||||
|
|
||||||
|
send_byte(STOP, tx).await;
|
||||||
|
|
||||||
// idle
|
// idle
|
||||||
sender.send(1).await;
|
tx.send(1).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn bit_send(
|
pub async fn bit_send(
|
||||||
mut pin: Output<'static>,
|
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 {
|
loop {
|
||||||
let bit = receiver.receive().await;
|
let bit = rx.receive().await;
|
||||||
if bit == 1 {
|
if bit == 1 {
|
||||||
pin.set_high();
|
pin.set_high();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user