[−][src]Struct commune::Frame
Represents a packet of information.
Data layout
|TX|RX|LL|__|DDD...|
TX
(u16): Device (Transmitting) ID. Because we use the same RX and TX address for all our devices, we need to embed this ID in theFrame
.RX
(u16): Controller (Receiving) ID. Usually 0x0, which is the default controller ID but If we want to communicate directly to another ID we can specify that here.LL
(u16): = Packet length. Should be zero instead of in an init message. This can go up to theBUFFER_SIZE * PACKET_SIZE - 1
in length, anything over will be ignored.__
(u16): 2 bytes = Reserved for futureD...
(24 bytes) = Serialized data
Sending Frames in multiple parts
Start all messages (including single and multipart messages) with an LL
value greater than 0, where the value is the number of Frames required to
send the message. All subsequent messages have LL == 0x0
until entire
buffer has been sent
Implementations
impl Frame
[src]
pub fn new(tx_id: u16) -> Self
[src]
Creates a Frame with a specified transmitting ID. Panics if the transmitting ID value is invalid or the size of the buffer is smaller than 2.
Arguments
tx_id
- The transmitting ID. To create a Frame with completely zeroed data, useFrame::default()
.
Examples
// Creates a new Frame with the transmitting ID of 1 let frame = Frame::new(0x01);
pub fn extract_at(&self, index: usize) -> Result<u16, Error>
[src]
Extracts a u16 value at an index from the Frame. Returns a result containing the extracted value, or an error.
Arguments
index
- The position at which the value and the value one position to the right will be read.
Examples
// Assume we already have the value 12 at index 2 in a variable named `frame`. let value = frame.extract_at(2).unwrap(); assert_eq!(value, 12);
pub fn insert_at(&mut self, index: usize, value: u16) -> Result<(), Error>
[src]
Inserts a u16 value at an index in the Frame
. Returns a unit value on
success.
Arguments
index
- The position at which the value and the value one position to the right will be writtenvalue
- the u16 value to write to the position of our index
Examples
// Creates a frame with the value 12 at index 2 let mut frame = Frame::default(); frame.insert_at(2, 12); // Gets the value from our frame slice let value = u16::from_be_bytes(frame.0[2..4].try_into().unwrap()); assert_eq!(value, 12);
pub fn tx_id(&self) -> u16
[src]
Gets the transmitting ID
Examples
// Creates a Frame with the embedded TX ID of 2 let frame = Frame::new(2); // Gets the TX ID let id = frame.tx_id(); assert_eq!(id, 2);
pub fn set_tx_id(self, tx_id: u16) -> Self
[src]
Sets the transmitting ID
Arguments
tx_id
- the transmitting ID to set
Examples
// Creates a default blank Frame let mut frame = Frame::default(); // Sets the TX ID let frame = frame.set_tx_id(5); assert_eq!(frame.tx_id(), 5);
pub fn rx_id(&self) -> u16
[src]
Gets the receiving ID
Examples
// Creates a Frame with the RX ID of 420 let frame = Frame::default().set_rx_id(420); // Gets the RX ID let id = frame.rx_id(); assert_eq!(id, 420);
pub fn set_rx_id(self, rx_id: u16) -> Self
[src]
Sets the receiving ID
Arguments
rx_id
- the receiving ID to set
Examples
// Creates a default blank Frame let mut frame = Frame::default(); // Sets the RX ID to 420 frame = frame.set_tx_id(420); assert_eq!(frame.tx_id(), 420);
pub fn data(&self) -> [u8; 24]
[src]
Gets non-meta data
Examples
// Creates a Frame manually, with all zeros in the metadata, and all ones in the regular data let frame = Frame([0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]); // Gets all data. Notice the zeros in the metadata are stripped. let data = frame.data(); assert_eq!(data, [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]);
pub fn set_data(self, data: &[u8]) -> Self
[src]
Sets non-meta data
Examples
// Creates a default blank Frame. let mut frame = Frame::default(); let data = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; frame = frame.set_data(&data); assert_eq!(frame.data(), data);
pub fn multipart(&self) -> MultipartStatus
[src]
Gets multipart information. Returns a MultipartStatus.
Examples
// Creates a Frame with a multipart status let frame = Frame::default().set_multipart(MultipartStatus::Start(3)); let multipart = frame.multipart(); assert_eq!(multipart, MultipartStatus::Start(3));
pub fn set_multipart(self, status: MultipartStatus) -> Self
[src]
Sets the multipart status of a message. Transmissions must always begin
with a Start message. This has the effect of making it "one-indexed"
instead of zero, as 0x00 is the code for continue, whereas anything
greater than is a start message. MultipartStatus::Start(0)
is invalid.
Send single messages with MultipartStatus::Start(1)
.
Examples
// Creates a default blank Frame let mut frame = Frame::default(); frame = frame.set_multipart(MultipartStatus::Start(5)); assert_eq!(frame.multipart(), MultipartStatus::Start(5))
Trait Implementations
impl Debug for Frame
[src]
impl Default for Frame
[src]
impl Eq for Frame
[src]
impl PartialEq<Frame> for Frame
[src]
impl StructuralEq for Frame
[src]
impl StructuralPartialEq for Frame
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,