Skip to content

Commit

Permalink
Add reset() and general clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerBroker2 committed Apr 20, 2021
1 parent 3458920 commit 6c3b8f6
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 86 deletions.
14 changes: 1 addition & 13 deletions examples/uart_rx_file/uart_rx_file.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ void setup()
Serial1.begin(115200);

myTransfer.begin(Serial1);

/*
Or, use the full constructor:
myTransfer.begin(Serial1, true, Serial, 50);
With the timeout parameter set to 50ms, a packet must be fully received and parsed within 50ms,
or it will be discarded.
The timeout value should depend on the baud rate and on the application.
Example back-of-the-envelope calculation:
115200bps = 14400Bps
One packet = 264B (max) should take max 264/11400 s = 0.02s = 20ms
to transfer. Include some time for parsing the packet (which depends on the frequency
of whatever task is calling transfer.available()) - and 50ms does not sound unreasonable.
}


Expand All @@ -44,4 +32,4 @@ void loop()
Serial.print((char)myTransfer.packet.rxBuff[i]);
Serial.println();
}
}
}
45 changes: 35 additions & 10 deletions src/I2CTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@


/*
void I2CTransfer::begin(TwoWire &_port, configST configs)
void I2CTransfer::begin(TwoWire &_port, configST& configs)
Description:
------------
* Advanced initializer for the I2CTransfer Class
Inputs:
-------
* const TwoWire &_port - I2C port to communicate over
* const configST configs - Struct that holds config
* const configST& configs - Struct that holds config
values for all possible initialization parameters
Return:
-------
* void
*/
void I2CTransfer::begin(TwoWire& _port, const configST configs)
void I2CTransfer::begin(TwoWire& _port, const configST& configs)
{
port = &_port;
port->onReceive(processData);
port->onReceive((void (*)(int))processData);
packet.begin(configs);
}


/*
void I2CTransfer::begin(TwoWire &_port, const bool _debug, Stream &_debugPort)
void I2CTransfer::begin(TwoWire &_port, const bool& _debug, Stream &_debugPort)
Description:
------------
* Simple initializer for the SerialTransfer Class
Inputs:
-------
* const TwoWire &_port - I2C port to communicate over
* const bool _debug - Whether or not to print error messages
* const bool& _debug - Whether or not to print error messages
* const Stream &_debugPort - Serial port to print error messages
Return:
-------
* void
*/
void I2CTransfer::begin(TwoWire& _port, const bool _debug, Stream& _debugPort)
void I2CTransfer::begin(TwoWire& _port, const bool& _debug, Stream& _debugPort)
{
port = &_port;
packet.begin(_debug, _debugPort);
Expand Down Expand Up @@ -77,19 +77,19 @@ uint8_t I2CTransfer::sendData(const uint16_t& messageLen, const uint8_t& packetI


/*
void I2CTransfer::processData(int numBytes)
void I2CTransfer::processData()
Description:
------------
* Parses incoming serial data automatically when an
I2C frame is received
Inputs:
-------
* int numBytes - Number of I2C bytes to read (ignored)
* void
Return:
-------
* void
*/
void I2CTransfer::processData(int numBytes)
void I2CTransfer::processData()
{
uint8_t recChar;
classToUse->bytesRead = 0;
Expand All @@ -101,7 +101,12 @@ void I2CTransfer::processData(int numBytes)
classToUse->status = classToUse->packet.status;

if (classToUse->status != CONTINUE)
{
if (classToUse->status < 0)
classToUse->reset();

break;
}
}
}

Expand All @@ -124,4 +129,24 @@ uint8_t I2CTransfer::currentPacketID()
}


/*
void I2CTransfer::reset()
Description:
------------
* Clears out the tx, and rx buffers, plus resets
the "bytes read" variable, finite state machine, etc
Inputs:
-------
* void
Return:
-------
* void
*/
void I2CTransfer::reset()
{
packet.reset();
status = packet.status;
}


I2CTransfer* I2CTransfer::classToUse = NULL;
7 changes: 4 additions & 3 deletions src/I2CTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class I2CTransfer
{
classToUse = this;
};
void begin(TwoWire& _port, const configST configs);
void begin(TwoWire& _port, const bool _debug = true, Stream& _debugPort = Serial);
void begin(TwoWire& _port, const configST& configs);
void begin(TwoWire& _port, const bool& _debug = true, Stream& _debugPort = Serial);
uint8_t sendData(const uint16_t& messageLen, const uint8_t& packetID = 0, const uint8_t& targetAddress = 0);
uint8_t currentPacketID();
void reset();


/*
Expand Down Expand Up @@ -106,5 +107,5 @@ class I2CTransfer
TwoWire* port;


static void processData(int numBytes);
static void processData();
};
104 changes: 68 additions & 36 deletions src/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,67 @@ PacketCRC crc;


/*
void Packet::begin(configST configs)
void Packet::begin(const configST& configs)
Description:
------------
* Advanced initializer for the Packet Class
Inputs:
-------
* const configST configs - Struct that holds config
* const configST& configs - Struct that holds config
values for all possible initialization parameters
Return:
-------
* void
*/
void Packet::begin(const configST configs)
void Packet::begin(const configST& configs)
{
debugPort = configs.debugPort;
debug = configs.debug;
callbacks = configs.callbacks;
callbacksLen = configs.callbacksLen;
timeout = configs.timeout;

}


/*
void Packet::begin(const bool _debug, Stream &_debugPort)
void Packet::begin(const bool& _debug, Stream& _debugPort, const uint32_t& _timeout)
Description:
------------
* Simple initializer for the Packet Class
Inputs:
-------
* const Stream &_port - Serial port to communicate over
* const bool _debug - Whether or not to print error messages
* const Stream &_debugPort - Serial port to print error messages
* const bool& _debug - Whether or not to print error messages
* Stream &_debugPort - Serial port to print error messages
* const uint32_t& _timeout - Number of ms to wait before
declaring packet parsing timeout
Return:
-------
* void
*/
void Packet::begin(const bool _debug, Stream& _debugPort)
void Packet::begin(const bool& _debug, Stream& _debugPort, const uint32_t& _timeout)
{
debugPort = &_debugPort;
debug = _debug;
timeout = __UINT32_MAX__;
timeout = _timeout;
}

void Packet::begin(const bool _debug, Stream& _debugPort, uint32_t _timeout)
{
debugPort = &_debugPort;
debug = _debug;
timeout = _timeout;
}

/*
uint8_t Packet::constructPacket(const uint16_t &messageLen, const uint8_t packetID)
uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t& packetID)
Description:
------------
* Calculate, format, and insert the packet protocol metadata into the packet transmit
buffer
Inputs:
-------
* const uint16_t &messageLen - Number of values in txBuff
* const uint16_t& messageLen - Number of values in txBuff
to send as the payload in the next packet
* const uint8_t packetID - The packet 8-bit identifier
* const uint8_t& packetID - The packet 8-bit identifier
Return:
-------
* uint8_t - Number of payload bytes included in packet
*/
uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packetID)
uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t& packetID)
{
if (messageLen > MAX_PACKET_SIZE)
{
Expand Down Expand Up @@ -105,7 +99,7 @@ uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packet


/*
uint8_t Packet::parse(uint8_t recChar, bool valid)
uint8_t Packet::parse(const uint8_t& recChar, const bool& valid)
Description:
------------
* Parses incoming serial data, analyzes packet contents,
Expand All @@ -115,32 +109,42 @@ uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packet
"void Packet::begin(const configST configs)"
Inputs:
-------
* void
* const uint8_t& recChar - Next char to parse in the stream
* const bool& valid - Set if stream is "available()" and clear if not
Return:
-------
* uint8_t - Num bytes in RX buffer
*/

uint8_t Packet::parse(uint8_t recChar, bool valid)
uint8_t Packet::parse(const uint8_t& recChar, const bool& valid)
{
bool packet_fresh = packetStart==0 || millis()-packetStart<timeout;
if(!packet_fresh){ //packet is stale, start over.
debugPort->println("STALE PACKET");
bytesRead = 0;
state = find_start_byte;
packetStart=0;
return bytesRead;
bool packet_fresh = (packetStart == 0) || ((millis() - packetStart) < timeout);

if(!packet_fresh) //packet is stale, start over.
{
if (debug)
debugPort->println("ERROR: STALE PACKET");

bytesRead = 0;
state = find_start_byte;
status = STALE_PACKET_ERROR;
packetStart = 0;

return bytesRead;
}

if (valid)
{
switch (state)
{
case find_start_byte: /////////////////////////////////////////
{
if (recChar == START_BYTE){
state = find_id_byte;
packetStart=millis();
}
if (recChar == START_BYTE)
{
state = find_id_byte;
packetStart = millis();
}

break;
}

Expand Down Expand Up @@ -174,6 +178,7 @@ uint8_t Packet::parse(uint8_t recChar, bool valid)
if (debug)
debugPort->println("ERROR: PAYLOAD_ERROR");

reset();
return bytesRead;
}
break;
Expand Down Expand Up @@ -210,6 +215,7 @@ uint8_t Packet::parse(uint8_t recChar, bool valid)
if (debug)
debugPort->println("ERROR: CRC_ERROR");

reset();
return bytesRead;
}

Expand All @@ -222,7 +228,7 @@ uint8_t Packet::parse(uint8_t recChar, bool valid)

if (recChar == STOP_BYTE)
{
unpackPacket(rxBuff, bytesToRec);
unpackPacket(rxBuff);
bytesRead = bytesToRec;
status = NEW_DATA;

Expand All @@ -246,6 +252,7 @@ uint8_t Packet::parse(uint8_t recChar, bool valid)
if (debug)
debugPort->println("ERROR: STOP_BYTE_ERROR");

reset();
return bytesRead;
break;
}
Expand All @@ -258,6 +265,7 @@ uint8_t Packet::parse(uint8_t recChar, bool valid)
debugPort->println(state);
}

reset();
bytesRead = 0;
state = find_start_byte;
break;
Expand Down Expand Up @@ -397,7 +405,7 @@ void Packet::stuffPacket(uint8_t arr[], const uint8_t& len)
-------
* void
*/
void Packet::unpackPacket(uint8_t arr[], const uint8_t& len)
void Packet::unpackPacket(uint8_t arr[])
{
uint8_t testIndex = recOverheadByte;
uint8_t delta = 0;
Expand All @@ -413,3 +421,27 @@ void Packet::unpackPacket(uint8_t arr[], const uint8_t& len)
arr[testIndex] = START_BYTE;
}
}


/*
void Packet::reset()
Description:
------------
* Clears out the tx, and rx buffers, plus resets
the "bytes read" variable, finite state machine, etc
Inputs:
-------
* void
Return:
-------
* void
*/
void Packet::reset()
{
memset(txBuff, 0, sizeof(txBuff));
memset(rxBuff, 0, sizeof(rxBuff));

bytesRead = 0;
status = CONTINUE;
packetStart = millis();
}
Loading

0 comments on commit 6c3b8f6

Please sign in to comment.