![]() |
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
|
A template class enabling packet-based Serial communication. More...
#include <PacketSerial.h>

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 |
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.
| EncoderType | The static packet encoder class name. |
| PacketMarker | The byte value used to mark the packet boundary. |
| BufferSize | The number of bytes allocated for the receive buffer. |
| 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.
| 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.
|
inline |
Deprecated. Use setStream() to configure a non-default port.
| stream | A pointer to an Arduino Stream. |
|
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.
| speed | The serial data transmission speed in bits / second (baud). |
References PacketSerial_< EncoderType, PacketMarker, BufferSize >::setStream().


|
inline |
Deprecated. Use setStream() to configure a non-default port.
| speed | The serial data transmission speed in bits / second (baud). |
| port | The Serial port number (e.g. 0 is Serial, 1 is Serial1). |
References PacketSerial_< EncoderType, PacketMarker, BufferSize >::begin(), and PacketSerial_< EncoderType, PacketMarker, BufferSize >::setStream().

|
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);
| buffer | A pointer to a data buffer. |
| size | The number of bytes in the data buffer. |

|
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.
| onPacketFunction | A pointer to the packet handler function. |

|
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.
| onPacketFunctionWithSender | A pointer to the packet handler function. |
|
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.
| stream | A pointer to an Arduino Stream. |

|
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();
}
