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,26 +33,36 @@ 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");
if (receiver->magicNumberBuf == receiver->magicNumber) {
uint8_t size; uint8_t size;
receiver->port.readBytes(&size, sizeof(size)); receiver->port.readBytes(&size, sizeof(size));
if (receiver->port.available() >= size) { if (receiver->port.available() >= size) {
message::Message &msg = receiver->buffer.pop(); message::Message &msg = receiver->buffer.pop();
msg.second.lock(); receiver->port.printf("Current semaphore count: %u\n",
msg.second.getCount());
msg.second.take();
msg.first.clear(); msg.first.clear();
msg.first.resize(size); msg.first.resize(size);
receiver->port.readBytes(msg.first.data(), size); receiver->port.readBytes(msg.first.data(), size);
msg.second.unlock(); // msg.second.unlock();
receiver->port.println("Push message to queue");
receiver->messageQueue.push(&msg); receiver->messageQueue.push(&msg);
} }
} }
} }
} }
}
HardwareSerial &port; HardwareSerial &port;
freertos::Queue<message::Message *> &messageQueue; freertos::Queue<message::Message *> &messageQueue;

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