example code of hk packets might look like

This commit is contained in:
Robin Müller 2022-06-14 19:16:38 +02:00
parent 8a3f53145b
commit 728796472d
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814

View File

@ -1,34 +1,43 @@
use core::ops::Deref; use postcard::{from_bytes, to_stdvec, to_vec};
use heapless::Vec;
use postcard::{from_bytes, to_vec};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use zerocopy::byteorder::{U16, I32};
use zerocopy::{FromBytes, AsBytes, Unaligned, NetworkEndian};
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] #[derive(AsBytes, FromBytes, Unaligned, Debug, Eq, PartialEq)]
struct RefStruct<'a> { #[repr(C, packed)]
bytes: &'a [u8], struct ZeroCopyTest {
str_s: &'a str, some_bool: u8,
some_u16: U16<NetworkEndian>,
some_i32: I32<NetworkEndian>,
some_float: [u8; 4]
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct PostcardTest {
some_bool: u8,
some_u16: u16,
some_i32: i32,
some_float: f32
} }
fn main() { fn main() {
let message = "hElLo"; let pc_test = PostcardTest {
let bytes = [0x01, 0x10, 0x02, 0x20]; some_bool: true as u8,
let output: Vec<u8, 11> = to_vec(&RefStruct { some_u16: 0x42,
bytes: &bytes, some_i32: -200,
str_s: message, some_float: 7.7 as f32
}) };
.unwrap();
assert_eq!( let out = to_stdvec(&pc_test).unwrap();
&[0x04, 0x01, 0x10, 0x02, 0x20, 0x05, b'h', b'E', b'l', b'L', b'o',], println!("{:#04x?}", out);
output.deref()
);
let out: RefStruct = from_bytes(output.deref()).unwrap(); let sample_hk = ZeroCopyTest {
assert_eq!( some_bool: true as u8,
out, some_u16: U16::from(0x42),
RefStruct { some_i32: I32::from(-200),
bytes: &bytes, some_float: (7.7 as f32).to_ne_bytes()
str_s: message, };
} let mut slice = [0; 11];
); sample_hk.write_to(slice.as_mut_slice());
println!("{:#04x?}", slice);
} }