Data Link Serial Protocol
Loading...
Searching...
No Matches
proto::Bicoder< NMaxMessage > Struct Template Reference

#include <DataLinkSerialProtocol.h>

Public Member Functions

bool decodeByte (const uint8_t data)
 
bool decodeMessage (const uint8_t *data, uint8_t size)
 
bool encodeMessage (const uint8_t *data, uint8_t size)
 
void reset ()
 
bool isCompleted () const
 
uint8_t size () const
 
const uint8_t * buff () const
 

Static Public Attributes

static constexpr uint8_t maxEncodedSize = 2U * NMaxMessage + 2U
 

Detailed Description

template<uint8_t NMaxMessage = 10>
struct proto::Bicoder< NMaxMessage >

The main (and only) class containing the whole functionality. This class holds both the encoder and decoder. It stores the encoded or decoded message in the internal buffer. A typical workflow looks like this:

Bicoder<5> bicoder; // Create an instance
if(bicoder.encodeMessage(data, length)) // encode
{
uint8_t* pBuffer = bicoder.buff(); // pointer to the buffer
for(uint8_t i = 0; i < bicoder.size(); ++i)
{
print(pBuffer[i]); // do something with the encoded message
}
}
if(bicoder.decodeMessage(data, length)) // decode
{
// similar to the code above
}
Definition DataLinkSerialProtocol.h:76
uint8_t size() const
Definition DataLinkSerialProtocol.h:172
bool decodeMessage(const uint8_t *data, uint8_t size)
Definition DataLinkSerialProtocol.h:234
bool encodeMessage(const uint8_t *data, uint8_t size)
Definition DataLinkSerialProtocol.h:266
const uint8_t * buff() const
Definition DataLinkSerialProtocol.h:179
Note
The buffer is shared between the encoder and decoder so don't try to encode a message while you haven't finished decoding, and vice versa. It doesn't mean you have to create 2 instances of this class: one for the encoder and one for the decoder (though you can if you want). It just means once you've started, f.e. the encoding process any data in the buffer (if any) is lost, and vice versa. Once you've read the encoded (decoded) message and you don't need it anymore you can start a new operation.
Template Parameters
NMaxMessageThe maximum length of a (raw) message, i.e. the maximum number of bytes that the data may consist of.

Member Function Documentation

◆ buff()

template<uint8_t NMaxMessage = 10>
const uint8_t * proto::Bicoder< NMaxMessage >::buff ( ) const
inline

Use this member-function in conjunction with the size member-function to read the encoded/decoded message.

See also
size()

◆ decodeByte()

template<uint8_t N>
bool proto::Bicoder< N >::decodeByte ( const uint8_t data)

Decode a single byte. An auxiliary function. Used by the decodeMessage member-function.

Parameters
datathe byte to decode
Returns
true if succeedded, false - otherwise
Important
Before this function is called on the first byte of the message it is necessary to call the reset() member-function first:
Bicoder<5> bicoder;
...
bicoder.reset(); // call this first before decoding a message byte by byte
for(uint8_t i = 0; i < messageLength; ++i)
{
bicoder.decodeByte(message[i]);
}
void reset()
Definition DataLinkSerialProtocol.h:220
bool decodeByte(const uint8_t data)
Definition DataLinkSerialProtocol.h:228
See also
isCompleted()

◆ decodeMessage()

template<uint8_t N>
bool proto::Bicoder< N >::decodeMessage ( const uint8_t * data,
uint8_t size )

Decode an encoded message.

Note
This function overwrites the internal buffer, so be sure you don't need its content anymore before call this member-function.
This member-function calls the reset() member-function.
Parameters
datathe encoded message
sizethe length of the encoded message (the number of bytes in data)
Returns
true if succeedded, false - otherwise
See also
size(), buff()

◆ encodeMessage()

template<uint8_t N>
bool proto::Bicoder< N >::encodeMessage ( const uint8_t * data,
uint8_t size )

Encode a raw message.

Note
This function rewrites the internal buffer, so be sure you don't need its content anymore before call this member-function.
This member-function calls the reset() member-function.
Parameters
datathe raw message
sizethe length of the message (the number of bytes in data)
Returns
true if succeedded, false - otherwise
See also
size(), buff()

◆ isCompleted()

template<uint8_t NMaxMessage = 10>
bool proto::Bicoder< NMaxMessage >::isCompleted ( ) const
inline

Indicates whether a frame has been received (decoded) completely. Call it to check if the buffer contains a complete frame, i.e. the message has been decoded. Usually used in conjunction with the decodeByte() member function.

Returns
true if the internal buffer contains a complete frame and can be read using the size and buff member functions. false - otherwise.
See also
decodeByte()

◆ reset()

template<uint8_t N>
void proto::Bicoder< N >::reset ( )

Resets the internal state of the instance. After this member function has been called the state is the following:

  • the isCompleted member function will return false
  • the decoder is in the waitHeader state awaiting for the start of the frame
  • the current index of the internal buffer is set to 0
Note
It doesn't clear the buffer - just sets the current index to 0

◆ size()

template<uint8_t NMaxMessage = 10>
uint8_t proto::Bicoder< NMaxMessage >::size ( ) const
inline

Returns the size of the internal buffer. Use this member-function in conjunction with the buff member-function to read the encoded/decoded message.

See also
buff()

Member Data Documentation

◆ maxEncodedSize

template<uint8_t NMaxMessage = 10>
uint8_t proto::Bicoder< NMaxMessage >::maxEncodedSize = 2U * NMaxMessage + 2U
staticconstexpr

Maximum possible length of the encoded message. This is the capacity of the internal buffer.


The documentation for this struct was generated from the following file: