mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-06 03:50:13 +02:00
Latest fixes from Andy
This commit is contained in:
parent
6fd2ca52db
commit
744db66f49
10 changed files with 97 additions and 45 deletions
|
@ -49,9 +49,12 @@ union Convert
|
|||
};
|
||||
}convert;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
#define SET_PA6() (GPIOA->BSRR = GPIO_BSRR_BS6)
|
||||
#define CLEAR_PA6() (GPIOA->BSRR = GPIO_BSRR_BR6)
|
||||
// Macro to toggle PA6
|
||||
#define TOGGLE_PA6() (GPIOA->ODR ^= GPIO_ODR_ODR6)
|
||||
*/
|
||||
|
||||
// Constructor
|
||||
SerialDataLink::SerialDataLink(Stream &serial, uint8_t transmitID, uint8_t receiveID, uint8_t maxIndexTX, uint8_t maxIndexRX, bool enableRetransmit)
|
||||
|
@ -167,10 +170,9 @@ void SerialDataLink::run()
|
|||
}
|
||||
else
|
||||
{
|
||||
|
||||
constructPacket(); // Construct a new packet if not currently transmitting
|
||||
|
||||
if (muteAcknowledgement)
|
||||
|
||||
if (muteAcknowledgement && (needToACK || needToNACK))
|
||||
{
|
||||
needToACK = false;
|
||||
needToNACK = false;
|
||||
|
@ -269,13 +271,11 @@ void SerialDataLink::constructPacket()
|
|||
{
|
||||
lastTransmissionTime = millis();
|
||||
txBufferIndex = 0; // Reset the TX buffer index
|
||||
|
||||
addToTxBuffer(headerChar);
|
||||
addToTxBuffer(transmitID);
|
||||
addToTxBuffer(0); // EOT position - place holder
|
||||
unsigned long currentTime = millis();
|
||||
int count = txBufferIndex;
|
||||
|
||||
for (uint8_t i = 0; i < maxIndexTX; i++)
|
||||
{
|
||||
if (dataUpdated[i] || (currentTime - lastSent[i] >= updateInterval))
|
||||
|
@ -289,13 +289,13 @@ void SerialDataLink::constructPacket()
|
|||
lastSent[i] = currentTime; // Update the last sent time for this index
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (count == txBufferIndex)
|
||||
{
|
||||
// No data was added to the buffer, so no need to send a packet
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
addToTxBuffer(eotChar);
|
||||
//----- assign EOT position
|
||||
txBuffer[2] = txBufferIndex - 1;
|
||||
|
@ -352,6 +352,7 @@ bool SerialDataLink::ackReceived()
|
|||
if (nextByte == headerChar)
|
||||
{
|
||||
requestToSend = true;
|
||||
transmissionError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -372,12 +373,11 @@ bool SerialDataLink::ackReceived()
|
|||
requestToSend = true;
|
||||
case NACK_CODE:
|
||||
transmissionError = true;
|
||||
return false;
|
||||
return true;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false; // No ACK, NACK, or new packet received
|
||||
|
@ -386,7 +386,9 @@ bool SerialDataLink::ackReceived()
|
|||
bool SerialDataLink::ackTimeout()
|
||||
{
|
||||
// Check if the current time has exceeded the last transmission time by the ACK timeout period
|
||||
if (millis() - lastTransmissionTime > ACK_TIMEOUT) {
|
||||
|
||||
if (millis() - lastTransmissionTime > ACK_TIMEOUT)
|
||||
{
|
||||
return true; // Timeout occurred
|
||||
}
|
||||
return false; // No timeout
|
||||
|
@ -397,9 +399,10 @@ bool SerialDataLink::ackTimeout()
|
|||
void SerialDataLink::read()
|
||||
{
|
||||
if (maxIndexRX < 1) return;
|
||||
if (serial.available())
|
||||
int count = 0;
|
||||
while (serial.available() && count < 10)
|
||||
{
|
||||
//Serial.print(".");
|
||||
count++;
|
||||
if (millis() - lastHeaderTime > PACKET_TIMEOUT && rxBufferIndex > 0)
|
||||
{
|
||||
// Timeout occurred, reset buffer and pointer
|
||||
|
|
|
@ -50,6 +50,8 @@
|
|||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
|
||||
class SerialDataLink {
|
||||
public:
|
||||
// Constructor
|
||||
|
@ -128,12 +130,12 @@ private:
|
|||
bool dataUpdated[dataArraySizeTX];
|
||||
unsigned long lastSent[dataArraySizeTX];
|
||||
|
||||
unsigned long updateInterval = 500;
|
||||
unsigned long ACK_TIMEOUT = 100;
|
||||
unsigned long PACKET_TIMEOUT = 100; // Timeout in milliseconds
|
||||
unsigned long updateInterval = 1000;
|
||||
unsigned long ACK_TIMEOUT = 200;
|
||||
unsigned long PACKET_TIMEOUT = 200; // Timeout in milliseconds
|
||||
|
||||
unsigned long lastStateChangeTime = 0;
|
||||
unsigned long stateChangeTimeout = 200;
|
||||
unsigned long stateChangeTimeout = 300;
|
||||
|
||||
// Special characters for packet framing
|
||||
char headerChar = '<';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue