@@ -54,7 +54,8 @@ class PacketSerial_
5454 _receiveBufferIndex (0 ),
5555 _stream (nullptr ),
5656 _onPacketFunction (nullptr ),
57- _onPacketFunctionWithSender (nullptr )
57+ _onPacketFunctionWithSender (nullptr ),
58+ _senderPtr (nullptr )
5859 {
5960 }
6061
@@ -224,18 +225,23 @@ class PacketSerial_
224225 _receiveBufferIndex,
225226 _decodeBuffer);
226227
228+ // clear the index here so that the callback function can call update() if needed and receive more data
229+ _receiveBufferIndex = 0 ;
230+ _recieveBufferOverflow = false ;
231+
227232 if (_onPacketFunction)
228233 {
229234 _onPacketFunction (_decodeBuffer, numDecoded);
230235 }
231236 else if (_onPacketFunctionWithSender)
232237 {
233- _onPacketFunctionWithSender (this , _decodeBuffer, numDecoded);
238+ _onPacketFunctionWithSender (_senderPtr , _decodeBuffer, numDecoded);
234239 }
235- }
236240
237- _receiveBufferIndex = 0 ;
238- _recieveBufferOverflow = false ;
241+ } else {
242+ _receiveBufferIndex = 0 ;
243+ _recieveBufferOverflow = false ;
244+ }
239245 }
240246 else
241247 {
@@ -302,6 +308,7 @@ class PacketSerial_
302308 {
303309 _onPacketFunction = onPacketFunction;
304310 _onPacketFunctionWithSender = nullptr ;
311+ _senderPtr = nullptr ;
305312 }
306313
307314 // / \brief Set the function that will receive decoded packets.
@@ -336,10 +343,12 @@ class PacketSerial_
336343 // / Setting a packet handler will remove all other packet handlers.
337344 // /
338345 // / \param onPacketFunctionWithSender A pointer to the packet handler function.
339- void setPacketHandler (PacketHandlerFunctionWithSender onPacketFunctionWithSender)
346+ void setPacketHandler (PacketHandlerFunctionWithSender onPacketFunctionWithSender, void * senderPtr = NULL )
340347 {
341348 _onPacketFunction = nullptr ;
342349 _onPacketFunctionWithSender = onPacketFunctionWithSender;
350+ _senderPtr = senderPtr;
351+ if (!senderPtr) _senderPtr = this ;
343352 }
344353
345354 // / \brief Check to see if the receive buffer overflowed.
@@ -384,6 +393,7 @@ class PacketSerial_
384393
385394 PacketHandlerFunction _onPacketFunction = nullptr ;
386395 PacketHandlerFunctionWithSender _onPacketFunctionWithSender = nullptr ;
396+ void * _senderPtr = nullptr ;
387397};
388398
389399
0 commit comments