TinyRobotics
Loading...
Searching...
No Matches
Public Member Functions | List of all members
MessageParser Class Reference

Parses binary TinyRobotics messages from a Stream and dispatches them by type. More...

#include <MessageParser.h>

Public Member Functions

bool parse (Stream &io, ParsedMessage &result)
 
bool parse (Stream &io, MessageHandler &handler)
 Parses a message from the stream and dispatches it to the given handler.
 
bool parse (uint8_t *data, size_t len, MessageHandler &handler)
 Parses a message from a raw buffer and dispatches it to the given handler.
 

Detailed Description

Parses binary TinyRobotics messages from a Stream and dispatches them by type.

MessageParser provides two main interfaces:

  1. parse(Stream&, ParsedMessage&):
    • Parses a message from the stream and fills a ParsedMessage union with the correct type.
    • The caller can inspect parsed.type and access the appropriate union member.
  2. parse(Stream&, MessageHandler&):
    • Parses a message from the stream and automatically dispatches it to the correct onMessage() overload of the provided MessageHandler instance.
    • This is convenient for direct message handling without manual type inspection.

The parser reads the message header (prefix, size, origin_id), then inspects the content field to determine the value type (float, Coordinate<float>, GPSCoordinate). It then reads the value and remaining fields accordingly.

Usage (manual type inspection):

if (parser.parse(stream, parsed)) {
switch (parsed.type) { ... }
}
Parses binary TinyRobotics messages from a Stream and dispatches them by type.
Definition: MessageParser.h:79
Holds a parsed TinyRobotics message of the correct type.
Definition: MessageParser.h:30
MessageValueType type
Indicates which union member is valid.
Definition: MessageParser.h:31

Usage (automatic dispatch):

parser.parse(stream, handler); // handler is a MessageHandler subclass

Member Function Documentation

◆ parse() [1/2]

bool parse ( Stream &  io,
MessageHandler handler 
)
inline

Parses a message from the stream and dispatches it to the given handler.

This method parses the message type and calls the appropriate onMessage overload on the provided MessageHandler instance.

Parameters
ioThe input stream to parse from.
handlerThe MessageHandler to dispatch the parsed message to.
Returns
true if a message was successfully parsed and dispatched, false otherwise.

◆ parse() [2/2]

bool parse ( uint8_t *  data,
size_t  len,
MessageHandler handler 
)
inline

Parses a message from a raw buffer and dispatches it to the given handler.

This overload wraps the buffer in a MemoryStream and calls the Stream-based parse.

Parameters
dataPointer to the message buffer.
lenLength of the buffer.
handlerThe MessageHandler to dispatch the parsed message to.
Returns
true if a message was successfully parsed and dispatched, false otherwise.

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