More fixes from Andy

This commit is contained in:
Daniel 2023-11-22 10:45:15 +02:00
parent 0bfb757dff
commit 6fd2ca52db
4 changed files with 99 additions and 13 deletions

View file

@ -124,8 +124,31 @@ bool SerialDataLink::checkNewData(bool resetFlag) {
return currentStatus;
}
void SerialDataLink::muteACK(bool mute)
{
muteAcknowledgement = mute;
}
void SerialDataLink::run()
{
unsigned long currentTime = millis();
static DataLinkState oldstate;
// Check if state has not changed for a prolonged period
if (oldstate != currentState)
{
lastStateChangeTime = currentTime;
oldstate = currentState;
}
if ((currentTime - lastStateChangeTime) > stateChangeTimeout) {
// Reset the state to Idle and perform necessary cleanup
currentState = DataLinkState::Idle;
// Perform any additional cleanup or reinitialization here
// ...
lastStateChangeTime = currentTime; // Reset the last state change time
}
switch (currentState)
{
case DataLinkState::Idle:
@ -146,7 +169,12 @@ void SerialDataLink::run()
{
constructPacket(); // Construct a new packet if not currently transmitting
if (muteAcknowledgement)
{
needToACK = false;
needToNACK = false;
}
uint8_t ack;
// now it is known which acknoledge need sending since last Reception
if (needToACK)
@ -215,6 +243,15 @@ void SerialDataLink::run()
}
}
void SerialDataLink::updateState(DataLinkState newState)
{
if (currentState != newState)
{
currentState = newState;
lastStateChangeTime = millis();
}
}
bool SerialDataLink::shouldTransmit()
{
// Priority condition: Device with transmitID = 1 and receiveID = 0 has the highest priority

View file

@ -76,6 +76,7 @@ public:
void setHeaderChar(char header);
void setEOTChar(char eot);
void muteACK(bool mute);
private:
enum class DataLinkState
@ -115,6 +116,7 @@ private:
bool retransmitEnabled;
bool transmissionError = false;
bool readError = false;
bool muteAcknowledgement = false;
// Data arrays and update management
@ -130,6 +132,9 @@ private:
unsigned long ACK_TIMEOUT = 100;
unsigned long PACKET_TIMEOUT = 100; // Timeout in milliseconds
unsigned long lastStateChangeTime = 0;
unsigned long stateChangeTimeout = 200;
// Special characters for packet framing
char headerChar = '<';
char eotChar = '>';
@ -147,7 +152,8 @@ private:
void addToTxBuffer(uint8_t byte);
bool sendNextByte();
bool ackReceived();
bool ackTimeout();
bool ackTimeout();
void updateState(DataLinkState newState);
// Internal methods for reception
void read();