12 #include "Encoding/COBS.h" 13 #include "Encoding/SLIP.h" 27 template<
typename EncoderType, u
int8_t PacketMarker = 0,
size_t ReceiveBufferSize = 256>
54 _receiveBufferIndex(0),
56 _onPacketFunction(nullptr),
57 _onPacketFunctionWithSender(nullptr)
87 #if ARDUINO >= 100 && !defined(CORE_TEENSY) 97 void begin(
unsigned long speed,
size_t port) __attribute__ ((deprecated))
103 Serial1.begin(speed);
104 #if ARDUINO >= 100 && !defined(CORE_TEENSY) 112 Serial2.begin(speed);
113 #if ARDUINO >= 100 && !defined(CORE_TEENSY) 121 Serial3.begin(speed);
122 #if ARDUINO >= 100 && !defined(CORE_TEENSY) 136 void begin(Stream* stream) __attribute__ ((deprecated))
211 if (_stream ==
nullptr)
return;
213 while (_stream->available() > 0)
215 uint8_t data = _stream->read();
217 if (data == PacketMarker)
219 if (_onPacketFunction || _onPacketFunctionWithSender)
221 uint8_t _decodeBuffer[_receiveBufferIndex];
223 size_t numDecoded = EncoderType::decode(_receiveBuffer,
227 if (_onPacketFunction)
229 _onPacketFunction(_decodeBuffer, numDecoded);
231 else if (_onPacketFunctionWithSender)
233 _onPacketFunctionWithSender(
this, _decodeBuffer, numDecoded);
237 _receiveBufferIndex = 0;
238 _recieveBufferOverflow =
false;
242 if ((_receiveBufferIndex + 1) < ReceiveBufferSize)
244 _receiveBuffer[_receiveBufferIndex++] = data;
250 _recieveBufferOverflow =
true;
270 void send(
const uint8_t* buffer,
size_t size)
const 272 if(_stream ==
nullptr || buffer ==
nullptr || size == 0)
return;
274 uint8_t _encodeBuffer[EncoderType::getEncodedBufferSize(size)];
276 size_t numEncoded = EncoderType::encode(buffer,
280 _stream->write(_encodeBuffer, numEncoded);
281 _stream->write(PacketMarker);
303 _onPacketFunction = onPacketFunction;
304 _onPacketFunctionWithSender =
nullptr;
341 _onPacketFunction =
nullptr;
342 _onPacketFunctionWithSender = onPacketFunctionWithSender;
371 return _recieveBufferOverflow;
378 bool _recieveBufferOverflow =
false;
380 uint8_t _receiveBuffer[ReceiveBufferSize];
381 size_t _receiveBufferIndex = 0;
383 Stream* _stream =
nullptr;
void(* PacketHandlerFunctionWithSender)(const void *sender, const uint8_t *buffer, size_t size)
A typedef describing the packet handler method.
Definition: PacketSerial.h:50
void begin(unsigned long speed, size_t port) __attribute__((deprecated))
Deprecated. Use setStream() to configure a non-default port.
Definition: PacketSerial.h:97
void setPacketHandler(PacketHandlerFunctionWithSender onPacketFunctionWithSender)
Set the function that will receive decoded packets.
Definition: PacketSerial.h:339
const Stream * getStream() const
Get a pointer to the current stream.
Definition: PacketSerial.h:193
void(* PacketHandlerFunction)(const uint8_t *buffer, size_t size)
A typedef describing the packet handler method.
Definition: PacketSerial.h:39
bool overflow() const
Check to see if the receive buffer overflowed.
Definition: PacketSerial.h:369
void setPacketHandler(PacketHandlerFunction onPacketFunction)
Set the function that will receive decoded packets.
Definition: PacketSerial.h:301
Stream * getStream()
Get a pointer to the current stream.
Definition: PacketSerial.h:181
~PacketSerial_()
Destroy the PacketSerial_ device.
Definition: PacketSerial.h:62
void update()
The update function services the serial connection.
Definition: PacketSerial.h:209
void setStream(Stream *stream)
Attach PacketSerial to an existing Arduino Stream.
Definition: PacketSerial.h:169
PacketSerial_()
Construct a default PacketSerial_ device.
Definition: PacketSerial.h:53
void begin(unsigned long speed)
Begin a default serial connection with the given speed.
Definition: PacketSerial.h:84
void begin(Stream *stream) __attribute__((deprecated))
Deprecated. Use setStream() to configure a non-default port.
Definition: PacketSerial.h:136
A template class enabling packet-based Serial communication.
Definition: PacketSerial.h:28
void send(const uint8_t *buffer, size_t size) const
Set a packet of data.
Definition: PacketSerial.h:270