DvG_StreamCommand
DvG_StreamCommand Class Reference

Class to manage listening to a stream, such as Serial or Wire, for incoming ASCII commands (or ASCII data packets in general) and act upon them. More...

#include <DvG_StreamCommand.h>

Public Member Functions

 DvG_StreamCommand (Stream &stream, char *buffer, uint16_t max_len)
 Construct a new DvG_StreamCommand object. More...
 
bool available ()
 Poll the stream for incoming characters and append them one-by-one to the command buffer buffer. This method should be called repeatedly. More...
 
char * getCommand ()
 Return the reference to the command buffer only when a complete command has been received. Otherwise, an empty C-string is returned. More...
 

Detailed Description

Class to manage listening to a stream, such as Serial or Wire, for incoming ASCII commands (or ASCII data packets in general) and act upon them.

We speak of a completely received 'command' once a linefeed ('\n', ASCII 10) character is received, or else when the number of incoming characters has exceeded the command buffer size. Carriage return ('\r', ASCII 13) characters are ignored from the stream.

The command buffer is supplied by the user and must be a fixed-size character array (C-string, i.e. '\0' terminated) to store incoming characters into. This keeps the memory usage low and unfragmented, instead of relying on memory hungry C++ strings.

Examples
StreamCommand.ino.

Constructor & Destructor Documentation

◆ DvG_StreamCommand()

DvG_StreamCommand::DvG_StreamCommand ( Stream &  stream,
char *  buffer,
uint16_t  max_len 
)

Construct a new DvG_StreamCommand object.

Parameters
streamReference to a stream to listen to, e.g. Serial, Wire, etc.
bufferReference to the command buffer: A fixed-size character array which will be managed by this class to hold a single incoming command. The buffer should be one character larger than the longest incoming command to allow for the C-string termination character '\0' to become appended.
max_lenArray size of buffer including '\0'. Do not exceed the maximum size of 2^^16 = 65536 characters.

Member Function Documentation

◆ available()

bool DvG_StreamCommand::available ( )

Poll the stream for incoming characters and append them one-by-one to the command buffer buffer. This method should be called repeatedly.

Returns
True when a complete command has been received and is ready to be returned by getCommand(), false otherwise.
Examples
StreamCommand.ino.

◆ getCommand()

char * DvG_StreamCommand::getCommand ( )

Return the reference to the command buffer only when a complete command has been received. Otherwise, an empty C-string is returned.

Returns
char*
Examples
StreamCommand.ino.

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