got motor running

This commit is contained in:
Leonetienne
2025-12-14 18:57:28 +01:00
parent d339f5b297
commit bd906add53
14 changed files with 123 additions and 78 deletions
+11 -23
View File
@@ -2,8 +2,6 @@
#include "OperationTimeout.hpp"
#include "StateMachine.hpp"
constexpr uint8_t PACKET_MAX_BYTES_READ_AT_ONCE = 4;
UartHandler::UartHandler() noexcept
{
}
@@ -50,32 +48,22 @@ bool UartHandler::isOpen() const noexcept
void UartHandler::poll()
{
int bytesAvail = Serial.available();
size_t bytesAvail = Serial.available();
if (bytesAvail) {
// Available bytes fit within remaining buffer
if (bytesAvail <= PACKET_SIZE - inBufPos) {
// Consume at max one byte at a time to give consumers a chance to consume packets
size_t bytesRead = Serial.readBytes(inBuf + inBufPos, min(PACKET_MAX_BYTES_READ_AT_ONCE, bytesAvail));
inBufPos += bytesRead;
// Consume at max one byte at a time to give consumers a chance to consume packets
// ALso this simplifies recognizing packet ends
size_t bytesRead = Serial.readBytes(inBuf + inBufPos, 1);
if (!bytesRead) {
StateMachine::getInstance().raiseError("Unable to read available byte!");
return;
}
// Else, these bytes would overflow the buffer.
// Read as much as we can and go on. It may just be multiple packets queued.
else {
size_t bytesRead = Serial.readBytes(inBuf + inBufPos, min(PACKET_MAX_BYTES_READ_AT_ONCE, PACKET_SIZE - inBufPos));
inBufPos += bytesRead;
}
// Is the last byte read 0x0A? Then push the packet
if (inBufPos > 0 && inBuf[inBufPos - 1] == TERMINATOR_BYTE) {
inBufPos++;
// Is our buffer full? Push it.
if (inBufPos == PACKET_SIZE) {
pushPacket();
inBufPos = 0;
}
// Is the last byte NOT 0x0A and we are on the last possibly byte?
// Then something is wrong and we are declaring error.
if (inBufPos == PACKET_SIZE && inBuf[inBufPos - 1] != TERMINATOR_BYTE) {
StateMachine::getInstance().raiseError("UART packet did not contain the 0x0A endbyte! May it be too long?");
}
}
}