Replacing Serial2 calls with my logger

This commit is contained in:
2025-12-27 18:41:02 +00:00
parent 726b6fd4fd
commit c7250d51ae
2 changed files with 40 additions and 103 deletions
+15 -26
View File
@@ -1,5 +1,6 @@
#include "walkieTalkie.h"
#include "HardwareSerial.h"
#include "logger/logger.h"
#include "communication/commands.h"
#include "communication/communication.h"
#include <cstdint>
@@ -8,22 +9,18 @@
namespace WalkieTalkie {
static inline void WaitingSTXRoutine(byte *b) {
LOG_TRACE(F("First byte is: %d\n"), *b);
if (*b == STX) {
Serial2.println(F("First byte is STX"));
state = RecvCMD;
resetBuffer();
} else {
Serial2.print(F("First byte is not STX: "));
Serial2.println(*b);
}
}
}
static inline void RecvCMDRoutine(Command *command, byte *b) {
Serial2.println(F("Reading the command byte"));
LOG_TRACE(F("Reading the command byte\n"));
*command = Command(*b);
Serial2.print(" Command: ");
Serial2.println(*b);
LOG_TRACE(F(" Command: %d\n"), *b);
state = RecvLen;
@@ -31,23 +28,20 @@ namespace WalkieTalkie {
}
static inline void RecvLenRoutine(byte *b) {
Serial2.println(F("Reading the len"));
LOG_TRACE(F("Reading the len\n"));
buffer[bufferIndex++] = *b;
// we are currently using a int16_t for the len so: 2 bytes
if (bufferIndex > 1) {
len = buffer[0] | (buffer[1] << 8);
Serial2.print("Len: ");
Serial2.println(len);
LOG_TRACE(F("Len: %d\n"), len);
state = RecvPayload;
resetBuffer();
}
}
static inline void RecvPayloadRoutine(uint8_t *payload, byte *b) {
Serial2.print("Reading payload: ");
Serial2.print(bufferIndex);
Serial2.print(" - ");
Serial2.println(*b);
LOG_TRACE(F("Reading payload: %d %s %d\n"), bufferIndex, " - ", *b);
buffer[bufferIndex++] = *b;
if (bufferIndex == len) {
// put the buffer data somewhere
@@ -59,8 +53,7 @@ namespace WalkieTalkie {
static inline int RecvCRCRoutine(byte *b) {
crc = *b;
Serial2.print("Reading CRC:");
Serial2.println(crc);
LOG_TRACE(F("Reacing CRC: %d\n"), crc);
state = RecvETX;
resetBuffer();
@@ -69,19 +62,15 @@ namespace WalkieTalkie {
}
static inline size_t RecvETXRoutine(uint8_t *payload, byte *b) {
LOG_TRACE(F("READING ETX\n"));
state = WaitingSTX;
Serial2.println("Reading ETX");
uint8_t computedCRC = CRC8(payload, len);
if (*b != ETX) {
Serial2.print("Failure, last byte was: ");
Serial2.println(*b);
LOG_TRACE(F("Failure, last byte was: %d\n"), *b);
return -1;
} else if (crc != CRC8(payload, len)) {
Serial2.print("CRC don't match. Expected: ");
Serial2.print(crc);
Serial2.print(", Got: ");
Serial2.println(CRC8(payload, len));
} else if (crc != computedCRC) {
LOG_TRACE(F("CRC don't match. Expected: %d, got: %d\n"), crc, computedCRC);
return -2;
} else {
return len;
+25 -77
View File
@@ -16,6 +16,7 @@ to have more ways to run analytics
#include "communication/commands.h"
#include "communication/packets.h"
#include "communication/walkieTalkie.h"
#include "logger/logger.h"
#include "values.h"
#include "windowPool.h"
// #include "UIDrawing.h"
@@ -75,12 +76,11 @@ void setup() {
Serial.begin(115200);
// Initialize the debug serial object
Serial2.begin(115200, SERIAL_8N1, UART1_RX, UART1_TX);
Serial2.flush();
Logger::Initialize(UART1_RX, UART1_TX);
Serial2.print(F("\r=== ESP32 SimRacing DashDisplay ===\r\n"));
LOG_INFO(F("=== ESP32 SimRacing DashDisplay ===\r\n"));
LOG_INFO(F("* Initiating display\r\n"));
Serial2.print(F("* Initiating display\r\n"));
initialDisplaySetup(gfx);
// Only setup the main screen after initializing the Serial2
@@ -149,29 +149,29 @@ void setup() {
WindowPool::PrintInUse();
delay(500);
Serial2.println("* Ready for loop");
LOG_INFO(F("* Ready for loop"));
}
uint64_t lastDataRead = 0;
bool gotAck = false;
struct header {
uint8_t StartMarker;
Command Cmd;
uint8_t EndMarker;
};
// struct header {
// uint8_t StartMarker;
// Command Cmd;
// uint8_t EndMarker;
// };
void PrintHeader(header *h) {
Serial2.println("HEADER:");
Serial2.print(" StartMarker: ");
Serial2.println(h->StartMarker);
Serial2.print(" Command : ");
Serial2.println(CommandToStr(h->Cmd));
Serial2.print(" EndMarker : ");
Serial2.println(h->EndMarker);
Serial2.println();
}
// void PrintHeader(header *h) {
// Serial2.println("HEADER:");
// Serial2.print(" StartMarker: ");
// Serial2.println(h->StartMarker);
// Serial2.print(" Command : ");
// Serial2.println(CommandToStr(h->Cmd));
// Serial2.print(" EndMarker : ");
// Serial2.println(h->EndMarker);
// Serial2.println();
// }
struct UIWindowPacket {
uint8_t StartMarker;
@@ -194,17 +194,15 @@ void loop(void) {
if (Serial.available() > 0) {
int16_t resp = WalkieTalkie::RecvStream(&cmd, payload, 256);
if (resp < 0) {
Serial2.println(F("Failed to receive data from serial"));
LOG_WARN(F("Failed to receive data from serial"));
return;
} else if (resp == 0) {
Serial2.println(F("No data to receive"));
LOG_INFO(F("No data to receive"));
return;
}
Serial2.print("Command: ");
Serial2.println(CommandToStr(cmd));
Serial2.print("Response: ");
Serial2.println(resp);
LOG_INFO(F("Command: %s\n"), CommandToStr(cmd));
LOG_INFO(F("Response Payload Len: %d\n"), resp);
switch(cmd) {
case CmdRequestID:
@@ -213,57 +211,7 @@ void loop(void) {
WalkieTalkie::SendData(&papers);
break;
default:
Serial2.print(F("Command `"));
Serial2.print(CommandToStr(cmd));
Serial2.print(F("` has not been implemented yet."));
LOG_WARN(F("Command: `%s` has not been implemented yet."), CommandToStr(cmd));
}
}
// // Try to use a state machine for this instead? Would it be better or just more
// // verbose and difficult to read?
// if (Serial.available() >= 1) {
// // Receive the header
// header h = {0};
// int res = WalkieTalkie::RecvData(&h);
// if (res > 0) {
// PrintHeader(&h);
// } else {
// Serial2.println(F("Failed to read header"));
// return;
// }
//
// // Now we receive the body
// switch(h.Cmd) {
// case CmdRequestID:
// // We have no body to receive, we just return data
// break;
// case CmdCreateWindow:
// Serial2.println("Awaiting body...");
// // We have to receive a body, should have plenty of data
// //
// UIWindowPacket windowData;
// size_t bytes = WalkieTalkie::RecvData(&windowData);
// if (bytes <= 0) {
// Serial2.println("Malformed data");
// }
//
// Serial2.print("StartMarker: ");
// Serial2.println(windowData.StartMarker);
// Serial2.print("x0: ");
// Serial2.println(windowData.x0);
// Serial2.print("y0: ");
// Serial2.println(windowData.y0);
// Serial2.print("width: ");
// Serial2.println(windowData.width);
// Serial2.print("height: ");
// Serial2.println(windowData.height);
// // Serial2.print("title: ");
// // Serial2.println(windowData.title);
// Serial2.print("EndMarker: ");
// Serial2.println(windowData.EndMarker);
//
// Serial2.println("Received body... Continuing");
// break;
// }
// }
}