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