Discard repeated messages

discard-repeated-messages
Guðni Már Gilbert 2022-10-08 18:19:26 +00:00
parent a8df6732fe
commit b143247caf
6 changed files with 31 additions and 0 deletions

View File

@ -227,6 +227,8 @@ bool Application::CheckMsgs() {
while (modules::serial::Available()) {
switch (protocol.DecodeRequest(modules::serial::ConsumeByte())) {
case mpd::MessageCompleted:
// Discard any unread data
modules::serial::Reset();
// process the input message
return true;
break;

View File

@ -14,6 +14,16 @@ uint8_t USART::Read() {
return c;
}
void USART::ResetReceiver()
{
// Disable/Enable Receiver to discard pending bytes
husart->UCSRxB &= ~(1 << 4);
husart->UCSRxB |= (1 << 4);
// Reset circular buffer, head = tail
rx_buf.reset();
}
void USART::Write(uint8_t c) {
_written = true;
// If the buffer and the data register is empty, just write the byte

View File

@ -31,6 +31,11 @@ public:
return ((index_t)(head - tail) % (size * 2)) == size;
}
/// Reset the circular buffer to empty
inline void reset() {
head = tail;
}
/// Advance the head index of the buffer.
/// No checks are performed. full() needs to be queried beforehand.
inline void push() {
@ -101,6 +106,11 @@ public:
return index.full();
}
/// Reset the circular buffer to empty
inline void reset() {
index.reset();
}
/// Insert an element into the buffer.
/// Checks for empty spot for the element and does not change the buffer content
/// in case the buffer is full.

View File

@ -44,6 +44,9 @@ public:
/// @returns current character from the UART and extracts it from the read buffer
uint8_t Read();
/// Discard all pending data from the receiver
void ResetReceiver();
/// @param c character to be pushed into the TX buffer (to be sent)
void Write(uint8_t c);
/// @param str pointer to a string in RAM to be pushed byte-by-byte into the TX buffer (to be sent)

View File

@ -32,6 +32,10 @@ uint8_t ConsumeByte() {
return hu::usart1.Read();
}
void Reset() {
hu::usart1.ResetReceiver();
}
} // namespace serial
} // namespace modules

View File

@ -12,6 +12,8 @@ bool Available();
uint8_t ConsumeByte();
void Reset();
} // namespace serial
} // namespace modules