Skip to content

Commit

Permalink
Dev (#16)
Browse files Browse the repository at this point in the history
* Pass by reference for all params

* Update input params/doc

* Make txObj() and rxObj() more flexible

* Update library.properties
  • Loading branch information
PowerBroker2 authored Apr 12, 2020
1 parent 1b248cc commit 0fb7c94
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 69 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SerialTransfer
version=1.3.2
version=1.4.0
author=PowerBroker2 <[email protected]>
maintainer=PowerBroker2 <[email protected]>
sentence=Arduino library to transfer packetized data fast/reliably via UART/Serial
Expand Down
4 changes: 2 additions & 2 deletions src/ST_CRC.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SerialTransferCRC



SerialTransferCRC(uint8_t polynomial=0x9B, uint8_t crcLen=8)
SerialTransferCRC(const uint8_t &polynomial=0x9B, const uint8_t &crcLen=8)
{
poly = polynomial;
crcLen_ = crcLen;
Expand Down Expand Up @@ -53,7 +53,7 @@ class SerialTransferCRC
}
}

uint8_t calculate(uint8_t val)
uint8_t calculate(const uint8_t &val)
{
if (val < tableLen_)
return csTable[val];
Expand Down
68 changes: 40 additions & 28 deletions src/SerialTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
* Constructor for the SerialTransfer Class
Inputs:
-------
* Stream &_port - Pointer to Serial port to
communicate over
* Stream &_port - Serial port to communicate over
Return:
-------
* void
Expand All @@ -25,45 +24,58 @@ void SerialTransfer::begin(Stream &_port)


/*
bool SerialTransfer::sendData(uint8_t messageLen)
uint8_t SerialTransfer::sendData(const uint16_t &messageLen)
Description:
------------
* Send a specified number of bytes in packetized form
Inputs:
-------
* uint8_t messageLen - Number of values in txBuff to
send as the payload in the next packet
* const uint16_t &messageLen - Number of values in txBuff
to send as the payload in the next packet
Return:
-------
* bool - Whether or not messageLen was an acceptable
* uint8_t - Number of payload bytes included in packet
value
*/
bool SerialTransfer::sendData(uint8_t messageLen)
uint8_t SerialTransfer::sendData(const uint16_t &messageLen)
{
if (messageLen <= MAX_PACKET_SIZE)
if (messageLen > MAX_PACKET_SIZE)
{
calcOverhead(txBuff, messageLen);
stuffPacket(txBuff, messageLen);
uint8_t crcVal = crc.calculate(txBuff, messageLen);
calcOverhead(txBuff, MAX_PACKET_SIZE);
stuffPacket(txBuff, MAX_PACKET_SIZE);
uint8_t crcVal = crc.calculate(txBuff, MAX_PACKET_SIZE);

port->write(START_BYTE);
port->write(overheadByte);
port->write(messageLen);
port->write(txBuff, messageLen);
port->write(MAX_PACKET_SIZE);
port->write(txBuff, MAX_PACKET_SIZE);
port->write(crcVal);
port->write(STOP_BYTE);

return true;
return MAX_PACKET_SIZE;
}
else
{
calcOverhead(txBuff, (uint8_t)messageLen);
stuffPacket(txBuff, (uint8_t)messageLen);
uint8_t crcVal = crc.calculate(txBuff, (uint8_t)messageLen);

port->write(START_BYTE);
port->write(overheadByte);
port->write((uint8_t)messageLen);
port->write(txBuff, (uint8_t)messageLen);
port->write(crcVal);
port->write(STOP_BYTE);

return (uint8_t)messageLen;
}

return false;
}




/*
void SerialTransfer::calcOverhead(uint8_t arr[], uint8_t len)
void SerialTransfer::calcOverhead(uint8_t arr[], const uint8_t &len)
Description:
------------
* Calculates the COBS (Consistent Overhead Stuffing) Overhead
Expand All @@ -74,12 +86,12 @@ bool SerialTransfer::sendData(uint8_t messageLen)
-------
* uint8_t arr[] - Array of values the overhead is to be calculated
over
* uint8_t len - Number of elements in arr[]
* const uint8_t &len - Number of elements in arr[]
Return:
-------
* void
*/
void SerialTransfer::calcOverhead(uint8_t arr[], uint8_t len)
void SerialTransfer::calcOverhead(uint8_t arr[], const uint8_t &len)
{
overheadByte = 0xFF;

Expand All @@ -92,21 +104,21 @@ void SerialTransfer::calcOverhead(uint8_t arr[], uint8_t len)


/*
int16_t SerialTransfer::findLast(uint8_t arr[], uint8_t len)
int16_t SerialTransfer::findLast(uint8_t arr[], const uint8_t &len)
Description:
------------
* Finds last instance of the value START_BYTE within the given
packet array
Inputs:
-------
* uint8_t arr[] - Packet array
* uint8_t len - Number of elements in arr[]
* const uint8_t &len - Number of elements in arr[]
Return:
-------
* int16_t - Index of first instance of the value START_BYTE within the given
packet array
*/
int16_t SerialTransfer::findLast(uint8_t arr[], uint8_t len)
int16_t SerialTransfer::findLast(uint8_t arr[], const uint8_t &len)
{
for (uint8_t i = (len - 1); i != 0xFF; i--)
if (arr[i] == START_BYTE)
Expand All @@ -119,20 +131,20 @@ int16_t SerialTransfer::findLast(uint8_t arr[], uint8_t len)


/*
void SerialTransfer::stuffPacket(uint8_t arr[], uint8_t len)
void SerialTransfer::stuffPacket(uint8_t arr[], const uint8_t &len)
Description:
------------
* Enforces the COBS (Consistent Overhead Stuffing) ruleset across
all bytes in the packet against the value of START_BYTE
Inputs:
-------
* uint8_t arr[] - Array of values to stuff
* uint8_t len - Number of elements in arr[]
* const uint8_t &len - Number of elements in arr[]
Return:
-------
* void
*/
void SerialTransfer::stuffPacket(uint8_t arr[], uint8_t len)
void SerialTransfer::stuffPacket(uint8_t arr[], const uint8_t &len)
{
int16_t refByte = findLast(arr, len);

Expand All @@ -153,19 +165,19 @@ void SerialTransfer::stuffPacket(uint8_t arr[], uint8_t len)


/*
void SerialTransfer::unpackPacket(uint8_t arr[], uint8_t len)
void SerialTransfer::unpackPacket(uint8_t arr[], const uint8_t &len)
Description:
------------
* Unpacks all COBS-stuffed bytes within the array
Inputs:
-------
* uint8_t arr[] - Array of values to unpack
* uint8_t len - Number of elements in arr[]
* const uint8_t &len - Number of elements in arr[]
Return:
-------
* void
*/
void SerialTransfer::unpackPacket(uint8_t arr[], uint8_t len)
void SerialTransfer::unpackPacket(uint8_t arr[], const uint8_t &len)
{
uint8_t testIndex = recOverheadByte;
uint8_t delta = 0;
Expand Down
88 changes: 50 additions & 38 deletions src/SerialTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,87 +47,99 @@ class SerialTransfer


void begin(Stream &_port);
bool sendData(uint8_t messageLen);
uint8_t sendData(const uint16_t &messageLen);
uint8_t available();




/*
void SerialTransfer::txObj(T &val, uint8_t len, uint8_t index)
void SerialTransfer::txObj(const T &val, const uint16_t &len, const uint16_t &index=0)
Description:
------------
* Stuffs "len" number of bytes of an arbitrary object (byte, int,
float, double, struct, etc...) into the transmit buffer (txBuff)
starting at the index as specified by the argument "index"
Inputs:
-------
* T &val - Pointer to the object to be copied to the
* const T &val - Pointer to the object to be copied to the
transmit buffer (txBuff)
* uint8_t len - Number of bytes of the object "val" to transmit
* uint8_t index - Starting index of the object within the
* const uint16_t &len - Number of bytes of the object "val" to transmit
* const uint16_t &index - Starting index of the object within the
transmit buffer (txBuff)
Return:
-------
* bool - Whether or not the specified index is valid
* uint8_t - Number of bytes written to the transmit buffer (txBuff)
*/
template <typename T>
bool txObj(T &val, uint8_t len, uint8_t index=0)
uint8_t txObj(const T &val, const uint16_t &len, const uint16_t &index=0)
{
if (index < (MAX_PACKET_SIZE - len + 1))
{
uint8_t* ptr = (uint8_t*)&val;
uint8_t* ptr = (uint8_t*)&val;
uint16_t maxIndex;

for (byte i = index; i < (len + index); i++)
{
txBuff[i] = *ptr;
ptr++;
}
if ((len + index) > MAX_PACKET_SIZE)
maxIndex = MAX_PACKET_SIZE;
else
maxIndex = len + index;

return true;
for (uint16_t i = index; i < maxIndex; i++)
{
txBuff[i] = *ptr;
ptr++;
}

return false;
if ((len + index) > MAX_PACKET_SIZE)
return MAX_PACKET_SIZE - index;
else
return len;
}




/*
void SerialTransfer::rxObj(T &val, uint8_t len, uint8_t index)
void SerialTransfer::rxObj(const T &val, const uint16_t &len, const uint16_t &index=0)
Description:
------------
* Reads "len" number of bytes from the receive buffer (rxBuff)
starting at the index as specified by the argument "index"
into an arbitrary object (byte, int, float, double, struct, etc...)
Inputs:
-------
* T &val - Pointer to the object to be copied into from the
* const T &val - Pointer to the object to be copied into from the
receive buffer (rxBuff)
* uint8_t len - Number of bytes in the object "val" received
* uint8_t index - Starting index of the object within the
receive buffer (txBuff)
* const uint16_t &len - Number of bytes in the object "val" received
* const uint16_t &index - Starting index of the object within the
receive buffer (rxBuff)
Return:
-------
* bool - Whether or not the specified index is valid
* uint8_t - Number of bytes read from the receive buffer (rxBuff)
*/
template <typename T>
bool rxObj(T &val, uint8_t len, uint8_t index=0)
uint8_t rxObj(const T &val, const uint16_t &len, const uint16_t &index=0)
{
if (index < (MAX_PACKET_SIZE - len + 1))
{
uint8_t* ptr = (uint8_t*)&val;
uint8_t* ptr = (uint8_t*)&val;
uint16_t maxIndex;

for (byte i = index; i < (len + index); i++)
{
*ptr = rxBuff[i];
ptr++;
}
if ((len + index) > MAX_PACKET_SIZE)
maxIndex = MAX_PACKET_SIZE;
else
maxIndex = len + index;

return true;
for (uint16_t i = index; i < maxIndex; i++)
{
*ptr = rxBuff[i];
ptr++;
}

return false;
if ((len + index) > MAX_PACKET_SIZE)
return MAX_PACKET_SIZE - index;
else
return len;
}


Expand Down Expand Up @@ -155,8 +167,8 @@ class SerialTransfer



void calcOverhead(uint8_t arr[], uint8_t len);
int16_t findLast(uint8_t arr[], uint8_t len);
void stuffPacket(uint8_t arr[], uint8_t len);
void unpackPacket(uint8_t arr[], uint8_t len);
void calcOverhead(uint8_t arr[], const uint8_t &len);
int16_t findLast(uint8_t arr[], const uint8_t &len);
void stuffPacket(uint8_t arr[], const uint8_t &len);
void unpackPacket(uint8_t arr[], const uint8_t &len);
};

0 comments on commit 0fb7c94

Please sign in to comment.