-
Notifications
You must be signed in to change notification settings - Fork 0
Communication
In this section the usage of all communication related features will be explained.
PacketValue is a class with the aim of encapsulating the conversion procedure of a value such that is ready to be sent. It works the following way:

Where:
-
The cast_type is the type that needs to be transmitted or the value received from a sender.
-
The source is a double pointer thats used to store the reference of the lvalue that will store the data sent or received
-
The factor is a number that will multiply the rvalue contained in the source before casting it to the cast_type, this is used to maintain precision in floating point numbers while mainting the bit rate required to transmit the data at minimum. Similary when a new value is stored it is also divided by the factor
It contains 3 methods:
-
convert() which returns the converted value ready to be sent
-
load() which receives the new value received as a parameter, performs the inverse conversion and stores the converted value in the lvalue stored at source
-
size() which returns the size in bytes that the cast_type needs to be stored
A real use case might be the following:
double speed = 5.43;
double factor = 100;
using cast_type = uint16_t
uint16_t variable_to_be_sent;
PacketValue speed_packet_value = PacketValue<cast_type>(&speed,factor);
variable_to_be_sent = speed_packet_value.convert(); //Now variable_to_be_sent contains 543
uint16_t speed_received = 704;
speed_packet_value.load(speed_received); //Now speed contains 7.04
size_t size_of_variable = speed_packet_value.size() //returns sizeof(cast_type) which is 2 in this case
Packet is a class with the aim of automating the creation for a union-like buffer, a buffer with zero aligment between data of different types where each of the elements can be accesed individually while still being able to manipualate the buffer as a whole.
It can be seen as a special type of LinkedList of PacketValues.
Each of the Packets contains an id value ,by now is a two bytes length unsigned integer, which is used to identify the Packet from both ends of the communication protocol.
A packet is contructed the following way:

Packets have two main functionalities:
-
Being built: A packet can be built by calling the build() method over it, when a packet is built it performs convert() on all of its PacketValues and copies all of its converted values on a mallocated eigth bit unsigned integer buffer which is returned by the build() method.
This buffer is stored by the packet and can be obtained without building the packet again by accesing the instance variable buffer.
The memory in the buffer is in the same order as introduced in the PacketValues List
The average time complecity of building a Packet is Ɵ(n) where n is the number of PacketValues while the worst case scenario is O(2n) which only takes place the first time a Packet is built as it has to calculate how much space is needed to malloc for the buffer.
-
Being loaded: New data can be loaded to a Packet by handling as an argument an eight bit unsigned integer pointer to the save_data() method, this method will perform load() to each of the PacketValues with their corresponding type and postion in the buffer.
Additionally when a new Packet is constructed its save_data function is added to a static map called save_by_id contained in the Packet<> base class such that when a receiver receives a new payload for a packet it can load this new packet payload just by obtaining its id.
The id can be obtained from a payload by handling the payload buffer to the static get_id() method of the Packet<> base class
Each of the Packets has its own type beacause they operate with templates as it is the only way of storing multiple different type PacketValues, as PacketValues might also have different types between them. This means that storing multiple Packets inside most dataclasses won't work, tuples will work as each of the elements inside a tuple can be a different type.
