Skip to content

Commit 2e6da54

Browse files
committed
made timeout a constructor parameter
1 parent ea5ef95 commit 2e6da54

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/Packet.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ void Packet::begin(const configST configs)
2323
debug = configs.debug;
2424
callbacks = configs.callbacks;
2525
callbacksLen = configs.callbacksLen;
26+
timeout = configs.timeout;
2627

27-
// need to give the debug port a kick to get things working for some strange reason...
28-
debugPort->println();
2928
}
3029

3130

@@ -47,8 +46,15 @@ void Packet::begin(const bool _debug, Stream& _debugPort)
4746
{
4847
debugPort = &_debugPort;
4948
debug = _debug;
49+
timeout = __UINT32_MAX__;
5050
}
5151

52+
void Packet::begin(const bool _debug, Stream& _debugPort, uint32_t _timeout)
53+
{
54+
debugPort = &_debugPort;
55+
debug = _debug;
56+
timeout = _timeout;
57+
}
5258

5359
/*
5460
uint8_t Packet::constructPacket(const uint16_t &messageLen, const uint8_t packetID)
@@ -114,10 +120,10 @@ uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packet
114120
-------
115121
* uint8_t - Num bytes in RX buffer
116122
*/
117-
#define PACKET_TIMEOUT 1000;
123+
118124
uint8_t Packet::parse(uint8_t recChar, bool valid)
119125
{
120-
bool packet_fresh = packetStart==0 || millis()-packetStart<PACKET_TIMEOUT;
126+
bool packet_fresh = packetStart==0 || millis()-packetStart<timeout;
121127
if(!packet_fresh){
122128

123129
bytesRead = 0;

src/Packet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct configST
4141
bool debug = true;
4242
const functionPtr* callbacks = NULL;
4343
uint8_t callbacksLen = 0;
44+
uint32_t timeout = __UINT32_MAX__;
4445
};
4546

4647

@@ -58,6 +59,7 @@ class Packet
5859

5960
void begin(const configST configs);
6061
void begin(const bool _debug = true, Stream& _debugPort = Serial);
62+
void begin(const bool _debug, Stream& _debugPort, uint32_t _timeout);
6163
uint8_t constructPacket(const uint16_t& messageLen, const uint8_t packetID = 0);
6264
uint8_t parse(uint8_t recChar, bool valid = true);
6365
uint8_t currentPacketID();
@@ -172,7 +174,7 @@ class Packet
172174
uint8_t overheadByte = 0;
173175
uint8_t recOverheadByte = 0;
174176
uint32_t packetStart = 0;
175-
177+
uint32_t timeout;
176178
void calcOverhead(uint8_t arr[], const uint8_t& len);
177179
int16_t findLast(uint8_t arr[], const uint8_t& len);
178180
void stuffPacket(uint8_t arr[], const uint8_t& len);

src/SerialTransfer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ void SerialTransfer::begin(Stream& _port, const bool _debug, Stream& _debugPort)
4242
packet.begin(_debug, _debugPort);
4343
}
4444

45+
void SerialTransfer::begin(Stream& _port, const bool _debug, Stream& _debugPort, uint32_t _timeout)
46+
{
47+
port = &_port;
48+
timeout = _timeout;
49+
packet.begin(_debug, _debugPort, _timeout);
50+
}
51+
4552

4653
/*
4754
uint8_t SerialTransfer::sendData(const uint16_t &messageLen, const uint8_t packetID)

src/SerialTransfer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SerialTransfer
1313

1414
void begin(Stream& _port, const configST configs);
1515
void begin(Stream& _port, const bool _debug = true, Stream& _debugPort = Serial);
16+
void begin(Stream& _port, const bool _debug, Stream& _debugPort, uint32_t _timeout);
1617
uint8_t sendData(const uint16_t& messageLen, const uint8_t packetID = 0);
1718
uint8_t available();
1819
bool tick();
@@ -97,4 +98,5 @@ class SerialTransfer
9798

9899
private: // <<---------------------------------------//private
99100
Stream* port;
101+
uint32_t timeout;
100102
};

0 commit comments

Comments
 (0)