Make receiver work

Switch from mutex to bin-semaphore (thread overarching) & Proper failure handling
This commit is contained in:
GHOSCHT 2023-03-30 17:53:27 +02:00
parent 5fe004ce30
commit 05adf10e45
No known key found for this signature in database
2 changed files with 32 additions and 16 deletions

View file

@ -16,7 +16,12 @@ public:
UBaseType_t priority, BaseType_t coreID) UBaseType_t priority, BaseType_t coreID)
: port{port}, messageQueue{messageQueue}, : port{port}, messageQueue{messageQueue},
serialReceiverTask{ serialReceiverTask{
exec, "receive serial data", stackDepth, this, priority, coreID} {} exec, "receive serial data", stackDepth, this, priority, coreID} {
for (size_t i = 0; i < bufferSize; ++i) {
message::Message &msg = buffer.pop();
msg.second.give();
}
}
private: private:
static void exec(void *receiverPtr) { static void exec(void *receiverPtr) {
@ -28,22 +33,32 @@ private:
receiver->magicNumberBuf.clear(); receiver->magicNumberBuf.clear();
receiver->magicNumberBuf.resize(receiver->magicNumber.length()); receiver->magicNumberBuf.resize(receiver->magicNumber.length());
size_t availableBytes = receiver->port.available(); size_t availableBytes = receiver->port.available();
if (availableBytes >= receiver->magicNumber.length() + 1) { if (receiver->port.available() > 0 && receiver->port.peek() != 'H') {
receiver->port.read();
receiver->port.println("flush");
} else if (availableBytes >= receiver->magicNumber.length() + 1) {
receiver->port.readBytes(receiver->magicNumberBuf.data(), receiver->port.readBytes(receiver->magicNumberBuf.data(),
receiver->magicNumber.length()); receiver->magicNumber.length());
receiver->port.println(receiver->magicNumberBuf == receiver->port.println("Received Magic Number:");
receiver->magicNumber); receiver->port.println(receiver->magicNumberBuf == receiver->magicNumber
? "correct"
: "incorrect");
uint8_t size; if (receiver->magicNumberBuf == receiver->magicNumber) {
receiver->port.readBytes(&size, sizeof(size)); uint8_t size;
if (receiver->port.available() >= size) { receiver->port.readBytes(&size, sizeof(size));
message::Message &msg = receiver->buffer.pop(); if (receiver->port.available() >= size) {
msg.second.lock(); message::Message &msg = receiver->buffer.pop();
msg.first.clear(); receiver->port.printf("Current semaphore count: %u\n",
msg.first.resize(size); msg.second.getCount());
receiver->port.readBytes(msg.first.data(), size); msg.second.take();
msg.second.unlock(); msg.first.clear();
receiver->messageQueue.push(&msg); msg.first.resize(size);
receiver->port.readBytes(msg.first.data(), size);
// msg.second.unlock();
receiver->port.println("Push message to queue");
receiver->messageQueue.push(&msg);
}
} }
} }
} }

View file

@ -1,12 +1,13 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include <FreeRTOS/Mutex.h> #include <FreeRTOS/BinarySemaphore.h>
#include <Messages/Protocol.h> #include <Messages/Protocol.h>
#include <etl/utility.h> #include <etl/utility.h>
#include <etl/vector.h> #include <etl/vector.h>
namespace message { namespace message {
/// @brief first: data, second: data ready to reuse
using Message = using Message =
etl::pair<etl::vector<uint8_t, message::protocol::MAX_PAYLOAD_LEN>, etl::pair<etl::vector<uint8_t, message::protocol::MAX_PAYLOAD_LEN>,
freertos::Mutex>; freertos::BinarySemaphore>;
} // namespace message } // namespace message