1313#include " Encoding/SLIP.h"
1414
1515
16- // / \brief A template class defining a packet-based Serial device .
16+ // / \brief A template class enabling packet-based Serial communication .
1717// /
1818// / Typically one of the typedefined versions are used, for example,
19- // / COBSPacketSerial or SLIPPacketSerial.
19+ // / ` COBSPacketSerial` or ` SLIPPacketSerial` .
2020// /
2121// / The template parameters allow the user to define their own packet encoder /
2222// / decoder, custom packet marker and receive buffer size.
@@ -32,17 +32,29 @@ class PacketSerial_
3232 // /
3333 // / The packet handler method usually has the form:
3434 // /
35- // / void onPacket (const uint8_t* buffer, size_t size)
35+ // / void onPacketReceived (const uint8_t* buffer, size_t size);
3636 // /
3737 // / where buffer is a pointer to the incoming buffer array, and size is the
3838 // / number of bytes in the incoming buffer.
3939 typedef void (*PacketHandlerFunction)(const uint8_t * buffer, size_t size);
4040
41+ // / \brief A typedef describing the packet handler method.
42+ // /
43+ // / The packet handler method usually has the form:
44+ // /
45+ // / void onPacketReceived(void* sender, const uint8_t* buffer, size_t size);
46+ // /
47+ // / where sender is a pointer to the PacketSerial_ instance that recieved
48+ // / the buffer, buffer is a pointer to the incoming buffer array, and size
49+ // / is the number of bytes in the incoming buffer.
50+ typedef void (*PacketHandlerFunctionWithSender)(const void * sender, const uint8_t * buffer, size_t size);
51+
4152 // / \brief Construct a default PacketSerial_ device.
4253 PacketSerial_ ():
4354 _receiveBufferIndex (0 ),
4455 _stream (nullptr ),
45- _onPacketFunction (nullptr )
56+ _onPacketFunction (nullptr ),
57+ _onPacketFunctionWithSender (nullptr )
4658 {
4759 }
4860
@@ -51,71 +63,98 @@ class PacketSerial_
5163 {
5264 }
5365
54- // / \brief Begin a serial connection with the given speed.
66+ // / \brief Begin a default serial connection with the given speed.
67+ // /
68+ // / The default Serial port `Serial` and default config `SERIAL_8N1` will be
69+ // / used. For example:
5570 // /
56- // / The default Serial Port `Serial` and default config `SERIAL_8N1` will be
57- // / used.
71+ // / PacketSerial myPacketSerial;
72+ // /
73+ // / void setup()
74+ // / {
75+ // / myPacketSerial.begin(9600);
76+ // / }
77+ // /
78+ // / This is a convenience method. For more complex Serial port
79+ // / configurations, use the `setStream()` function to set an arbitrary
80+ // / Arduino Stream.
5881 // /
5982 // / \param speed The serial data transmission speed in bits / second (baud).
6083 // / \sa https://www.arduino.cc/en/Serial/Begin
6184 void begin (unsigned long speed)
6285 {
63- begin (speed, SERIAL_8N1, 0 );
86+ Serial.begin (speed);
87+ setStream (&Serial);
6488 }
6589
66- // / \brief Deprecated begin () method .
90+ // / \brief Deprecated. Use setStream () to configure a non-default port .
6791 // / \param speed The serial data transmission speed in bits / second (baud).
6892 // / \param port The Serial port number (e.g. 0 is Serial, 1 is Serial1).
69- // / \deprecated Use the alternative begin() .
93+ // / \deprecated Use setStream() to configure a non-default port .
7094 void begin (unsigned long speed, size_t port) __attribute__ ((deprecated))
71- {
72- begin (port, SERIAL_8N1, port);
73- }
74-
75- // / \brief Begin a serial connection with the given speed, config and port.
76- // / \param speed The serial data transmission speed in bits / second (baud).
77- // / \param config The data, parity, and stop bits configuration.
78- // / \param port The Serial port number (e.g. 0 is Serial, 1 is Serial1).
79- // / \sa https://www.arduino.cc/en/Serial/Begin
80- void begin (unsigned long speed, uint8_t config, size_t port)
8195 {
8296 switch (port)
8397 {
8498 #if defined(UBRR1H)
8599 case 1 :
86- Serial1.begin (speed, config );
87- _stream = &Serial1;
100+ Serial1.begin (speed);
101+ setStream ( &Serial1) ;
88102 break ;
89103 #endif
90104 #if defined(UBRR2H)
91105 case 2 :
92- Serial2.begin (speed, config );
93- _stream = &Serial2;
106+ Serial2.begin (speed);
107+ setStream ( &Serial2) ;
94108 break ;
95109 #endif
96110 #if defined(UBRR3H)
97111 case 3 :
98- Serial3.begin (speed, config );
99- _stream = &Serial3;
112+ Serial3.begin (speed);
113+ setStream ( &Serial3) ;
100114 break ;
101115 #endif
102116 default :
103- Serial.begin (speed, config);
104- _stream = &Serial;
117+ begin (speed);
105118 }
106119 }
107120
108- // / \brief Begin a serial connection with the given Stream.
121+ // / \brief Deprecated. Use setStream() to configure a non-default port.
122+ // / \param stream A pointer to an Arduino `Stream`.
123+ // / \deprecated Use setStream() to configure a non-default port.
124+ void begin (Stream* stream) __attribute__ ((deprecated))
125+ {
126+ _stream = stream;
127+ }
128+
129+ // / \brief Attach PacketSerial to an existing Arduino `Stream`.
130+ // /
131+ // / This `Stream` could be a standard `Serial` `Stream` with a non-default
132+ // / configuration such as:
133+ // /
134+ // / PacketSerial myPacketSerial;
109135 // /
110- // / This Stream could be a standard Serial object e.g.:
136+ // / void setup()
137+ // / {
138+ // / Serial.begin(300, SERIAL_7N1);
139+ // / myPacketSerial.setStream(&Serial);
140+ // / }
111141 // /
112- // / myPacketSerial.begin(&Serial);
142+ // / Or it might be a `SoftwareSerial` `Stream` such as:
113143 // /
114- // / or can be any `Stream` object, including a network or other connection
115- // / that implements the `Stream` interface.
144+ // / PacketSerial myPacketSerial;
145+ // / SoftwareSerial mySoftwareSerial(10, 11);
146+ // /
147+ // / void setup()
148+ // / {
149+ // / mySoftwareSerial.begin(38400);
150+ // / myPacketSerial.setStream(&mySoftwareSerial);
151+ // / }
152+ // /
153+ // / Any class that implements the `Stream` interface should work, which
154+ // / includes some network objects.
116155 // /
117156 // / \param stream A pointer to an Arduino `Stream`.
118- void begin (Stream* stream)
157+ void setStream (Stream* stream)
119158 {
120159 _stream = stream;
121160 }
@@ -141,15 +180,22 @@ class PacketSerial_
141180
142181 if (data == PacketMarker)
143182 {
144- if (_onPacketFunction)
183+ if (_onPacketFunction || _onPacketFunctionWithSender )
145184 {
146185 uint8_t _decodeBuffer[_receiveBufferIndex];
147186
148187 size_t numDecoded = EncoderType::decode (_receiveBuffer,
149188 _receiveBufferIndex,
150189 _decodeBuffer);
151190
152- _onPacketFunction (_decodeBuffer, numDecoded);
191+ if (_onPacketFunction)
192+ {
193+ _onPacketFunction (_decodeBuffer, numDecoded);
194+ }
195+ else if (_onPacketFunctionWithSender)
196+ {
197+ _onPacketFunctionWithSender (this , _decodeBuffer, numDecoded);
198+ }
153199 }
154200
155201 _receiveBufferIndex = 0 ;
@@ -174,6 +220,12 @@ class PacketSerial_
174220 // / sending, it will send the specified `PacketMarker` defined in the
175221 // / template parameters.
176222 // /
223+ // / // Make an array.
224+ // / uint8_t myPacket[2] = { 255, 10 };
225+ // /
226+ // / // Send the array.
227+ // / myPacketSerial.send(myPacket, 2);
228+ // /
177229 // / \param buffer A pointer to a data buffer.
178230 // / \param size The number of bytes in the data buffer.
179231 void send (const uint8_t * buffer, size_t size) const
@@ -198,16 +250,57 @@ class PacketSerial_
198250 // /
199251 // / The packet handler method usually has the form:
200252 // /
201- // / void onPacket(const uint8_t* buffer, size_t size)
253+ // / void onPacketReceived(const uint8_t* buffer, size_t size);
254+ // /
255+ // / The packet handler would then be registered like this:
202256 // /
203- // / The packet handler would then be registered like:
257+ // / myPacketSerial.setPacketHandler(&onPacketReceived);
204258 // /
205- // / myPacketSerial.setPacketHandler(&onPacket);
259+ // / Setting a packet handler will remove all other packet handlers.
206260 // /
207261 // / \param onPacketFunction A pointer to the packet handler function.
208262 void setPacketHandler (PacketHandlerFunction onPacketFunction)
209263 {
210264 _onPacketFunction = onPacketFunction;
265+ _onPacketFunctionWithSender = nullptr ;
266+ }
267+
268+ // / \brief Set the function that will receive decoded packets.
269+ // /
270+ // / This function will be called when data is read from the serial stream
271+ // / connection and a packet is decoded. The decoded packet will be passed
272+ // / to the packet handler. The packet handler must have the form:
273+ // /
274+ // / The packet handler method usually has the form:
275+ // /
276+ // / void onPacketReceived(const void* sender, const uint8_t* buffer, size_t size);
277+ // /
278+ // / To determine the sender, compare the pointer to the known possible
279+ // / PacketSerial senders.
280+ // /
281+ // / void onPacketReceived(void* sender, const uint8_t* buffer, size_t size)
282+ // / {
283+ // / if (sender == &myPacketSerial)
284+ // / {
285+ // / // Do something with the packet from myPacketSerial.
286+ // / }
287+ // / else if (sender == &myOtherPacketSerial)
288+ // / {
289+ // / // Do something with the packet from myOtherPacketSerial.
290+ // / }
291+ // / }
292+ // /
293+ // / The packet handler would then be registered like this:
294+ // /
295+ // / myPacketSerial.setPacketHandler(&onPacketReceived);
296+ // /
297+ // / Setting a packet handler will remove all other packet handlers.
298+ // /
299+ // / \param onPacketFunctionWithSender A pointer to the packet handler function.
300+ void setPacketHandler (PacketHandlerFunctionWithSender onPacketFunctionWithSender)
301+ {
302+ _onPacketFunction = nullptr ;
303+ _onPacketFunctionWithSender = onPacketFunctionWithSender;
211304 }
212305
213306private:
@@ -220,7 +313,7 @@ class PacketSerial_
220313 Stream* _stream = nullptr ;
221314
222315 PacketHandlerFunction _onPacketFunction = nullptr ;
223-
316+ PacketHandlerFunctionWithSender _onPacketFunctionWithSender = nullptr ;
224317};
225318
226319
0 commit comments