AYAB Arduino Firmware 0.95
The goal of the AYAB project is to provide an alternative way to control the famous Brother KH-9xx range of knitting machines using a computer
PacketSerial_< EncoderType, PacketMarker, BufferSize > Class Template Reference

A template class enabling packet-based Serial communication. More...

#include <PacketSerial.h>

Collaboration diagram for PacketSerial_< EncoderType, PacketMarker, BufferSize >:
Collaboration graph

Public Types

typedef void(* PacketHandlerFunction) (const uint8_t *buffer, size_t size)
 A typedef describing the packet handler method.
 
typedef void(* PacketHandlerFunctionWithSender) (const void *sender, const uint8_t *buffer, size_t size)
 A typedef describing the packet handler method.
 

Public Member Functions

 PacketSerial_ ()
 Construct a default PacketSerial_ device.
 
 ~PacketSerial_ ()
 Destroy the PacketSerial_ device.
 
void begin (unsigned long speed)
 Begin a default serial connection with the given speed.
 
void begin (unsigned long speed, size_t port) __attribute__((deprecated))
 Deprecated. Use setStream() to configure a non-default port.
 
void begin (Stream *stream) __attribute__((deprecated))
 Deprecated. Use setStream() to configure a non-default port.
 
void setStream (Stream *stream)
 Attach PacketSerial to an existing Arduino Stream.
 
void update ()
 The update function services the serial connection.
 
void send (const uint8_t *buffer, size_t size) const
 Set a packet of data.
 
void setPacketHandler (PacketHandlerFunction onPacketFunction)
 Set the function that will receive decoded packets.
 
void setPacketHandler (PacketHandlerFunctionWithSender onPacketFunctionWithSender)
 Set the function that will receive decoded packets.
 

Private Member Functions

 PacketSerial_ (const PacketSerial_ &)
 
PacketSerial_operator= (const PacketSerial_ &)
 

Private Attributes

uint8_t _receiveBuffer [BufferSize]
 
size_t _receiveBufferIndex = 0
 
Stream * _stream = nullptr
 
PacketHandlerFunction _onPacketFunction = nullptr
 
PacketHandlerFunctionWithSender _onPacketFunctionWithSender = nullptr
 

Detailed Description

template<typename EncoderType, uint8_t PacketMarker = 0, size_t BufferSize = 256>
class PacketSerial_< EncoderType, PacketMarker, BufferSize >

A template class enabling packet-based Serial communication.

Typically one of the typedefined versions are used, for example, COBSPacketSerial or SLIPPacketSerial.

The template parameters allow the user to define their own packet encoder / decoder, custom packet marker and receive buffer size.

Template Parameters
EncoderTypeThe static packet encoder class name.
PacketMarkerThe byte value used to mark the packet boundary.
BufferSizeThe number of bytes allocated for the receive buffer.

Member Typedef Documentation

◆ PacketHandlerFunction

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
typedef void(* PacketSerial_< EncoderType, PacketMarker, BufferSize >::PacketHandlerFunction) (const uint8_t *buffer, size_t size)

A typedef describing the packet handler method.

The packet handler method usually has the form:

void onPacketReceived(const uint8_t* buffer, size_t size);

where buffer is a pointer to the incoming buffer array, and size is the number of bytes in the incoming buffer.

◆ PacketHandlerFunctionWithSender

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
typedef void(* PacketSerial_< EncoderType, PacketMarker, BufferSize >::PacketHandlerFunctionWithSender) (const void *sender, const uint8_t *buffer, size_t size)

A typedef describing the packet handler method.

The packet handler method usually has the form:

void onPacketReceived(void* sender, const uint8_t* buffer, size_t size);

where sender is a pointer to the PacketSerial_ instance that recieved the buffer, buffer is a pointer to the incoming buffer array, and size is the number of bytes in the incoming buffer.

Member Function Documentation

◆ begin() [1/3]

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::begin ( Stream *  stream)
inline

Deprecated. Use setStream() to configure a non-default port.

Parameters
streamA pointer to an Arduino Stream.
Deprecated:
Use setStream() to configure a non-default port.

◆ begin() [2/3]

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::begin ( unsigned long  speed)
inline

Begin a default serial connection with the given speed.

The default Serial port Serial and default config SERIAL_8N1 will be used. For example:

PacketSerial myPacketSerial;

void setup()
{
    myPacketSerial.begin(9600);
}

This is a convenience method. For more complex Serial port configurations, use the setStream() function to set an arbitrary Arduino Stream.

Parameters
speedThe serial data transmission speed in bits / second (baud).
See also
https://www.arduino.cc/en/Serial/Begin

References PacketSerial_< EncoderType, PacketMarker, BufferSize >::setStream().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ begin() [3/3]

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::begin ( unsigned long  speed,
size_t  port 
)
inline

Deprecated. Use setStream() to configure a non-default port.

Parameters
speedThe serial data transmission speed in bits / second (baud).
portThe Serial port number (e.g. 0 is Serial, 1 is Serial1).
Deprecated:
Use setStream() to configure a non-default port.

References PacketSerial_< EncoderType, PacketMarker, BufferSize >::begin(), and PacketSerial_< EncoderType, PacketMarker, BufferSize >::setStream().

Here is the call graph for this function:

◆ send()

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::send ( const uint8_t *  buffer,
size_t  size 
) const
inline

Set a packet of data.

This function will encode and send an arbitrary packet of data. After sending, it will send the specified PacketMarker defined in the template parameters.

// Make an array.
uint8_t myPacket[2] = { 255, 10 };

// Send the array.
myPacketSerial.send(myPacket, 2);
Parameters
bufferA pointer to a data buffer.
sizeThe number of bytes in the data buffer.
Here is the caller graph for this function:

◆ setPacketHandler() [1/2]

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::setPacketHandler ( PacketHandlerFunction  onPacketFunction)
inline

Set the function that will receive decoded packets.

This function will be called when data is read from the serial stream connection and a packet is decoded. The decoded packet will be passed to the packet handler. The packet handler must have the form:

The packet handler method usually has the form:

void onPacketReceived(const uint8_t* buffer, size_t size);

The packet handler would then be registered like this:

myPacketSerial.setPacketHandler(&onPacketReceived);

Setting a packet handler will remove all other packet handlers.

Parameters
onPacketFunctionA pointer to the packet handler function.
Here is the caller graph for this function:

◆ setPacketHandler() [2/2]

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::setPacketHandler ( PacketHandlerFunctionWithSender  onPacketFunctionWithSender)
inline

Set the function that will receive decoded packets.

This function will be called when data is read from the serial stream connection and a packet is decoded. The decoded packet will be passed to the packet handler. The packet handler must have the form:

The packet handler method usually has the form:

void onPacketReceived(const void* sender, const uint8_t* buffer, size_t size);

To determine the sender, compare the pointer to the known possible PacketSerial senders.

void onPacketReceived(void* sender, const uint8_t* buffer, size_t size)
{
    if (sender == &myPacketSerial)
    {
        // Do something with the packet from myPacketSerial.
    }
    else if (sender == &myOtherPacketSerial)
    {
        // Do something with the packet from myOtherPacketSerial.
    }
}

The packet handler would then be registered like this:

myPacketSerial.setPacketHandler(&onPacketReceived);

Setting a packet handler will remove all other packet handlers.

Parameters
onPacketFunctionWithSenderA pointer to the packet handler function.

◆ setStream()

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::setStream ( Stream *  stream)
inline

Attach PacketSerial to an existing Arduino Stream.

This Stream could be a standard Serial Stream with a non-default configuration such as:

PacketSerial myPacketSerial;

void setup()
{
    Serial.begin(300, SERIAL_7N1);
    myPacketSerial.setStream(&Serial);
}

Or it might be a SoftwareSerial Stream such as:

PacketSerial myPacketSerial;
SoftwareSerial mySoftwareSerial(10, 11);

void setup()
{
    mySoftwareSerial.begin(38400);
    myPacketSerial.setStream(&mySoftwareSerial);
}

Any class that implements the Stream interface should work, which includes some network objects.

Parameters
streamA pointer to an Arduino Stream.
Here is the caller graph for this function:

◆ update()

template<typename EncoderType , uint8_t PacketMarker = 0, size_t BufferSize = 256>
void PacketSerial_< EncoderType, PacketMarker, BufferSize >::update ( )
inline

The update function services the serial connection.

This must be called often, ideally once per loop(), e.g.:

void loop()
{
    // Other program code.

    myPacketSerial.update();
}
Here is the caller graph for this function:

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