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 "walkieTalkie.h"
#include "HardwareSerial.h" #include "HardwareSerial.h"
#include "logger/logger.h"
#include "communication/commands.h" #include "communication/commands.h"
#include "communication/communication.h" #include "communication/communication.h"
#include <cstdint> #include <cstdint>
@@ -8,22 +9,18 @@
namespace WalkieTalkie { namespace WalkieTalkie {
static inline void WaitingSTXRoutine(byte *b) { static inline void WaitingSTXRoutine(byte *b) {
LOG_TRACE(F("First byte is: %d\n"), *b);
if (*b == STX) { if (*b == STX) {
Serial2.println(F("First byte is STX"));
state = RecvCMD; state = RecvCMD;
resetBuffer(); resetBuffer();
} else { }
Serial2.print(F("First byte is not STX: "));
Serial2.println(*b);
}
} }
static inline void RecvCMDRoutine(Command *command, byte *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); *command = Command(*b);
Serial2.print(" Command: "); LOG_TRACE(F(" Command: %d\n"), *b);
Serial2.println(*b);
state = RecvLen; state = RecvLen;
@@ -31,23 +28,20 @@ namespace WalkieTalkie {
} }
static inline void RecvLenRoutine(byte *b) { static inline void RecvLenRoutine(byte *b) {
Serial2.println(F("Reading the len")); LOG_TRACE(F("Reading the len\n"));
buffer[bufferIndex++] = *b; buffer[bufferIndex++] = *b;
// we are currently using a int16_t for the len so: 2 bytes // we are currently using a int16_t for the len so: 2 bytes
if (bufferIndex > 1) { if (bufferIndex > 1) {
len = buffer[0] | (buffer[1] << 8); len = buffer[0] | (buffer[1] << 8);
Serial2.print("Len: "); LOG_TRACE(F("Len: %d\n"), len);
Serial2.println(len);
state = RecvPayload; state = RecvPayload;
resetBuffer(); resetBuffer();
} }
} }
static inline void RecvPayloadRoutine(uint8_t *payload, byte *b) { static inline void RecvPayloadRoutine(uint8_t *payload, byte *b) {
Serial2.print("Reading payload: "); LOG_TRACE(F("Reading payload: %d %s %d\n"), bufferIndex, " - ", *b);
Serial2.print(bufferIndex);
Serial2.print(" - ");
Serial2.println(*b);
buffer[bufferIndex++] = *b; buffer[bufferIndex++] = *b;
if (bufferIndex == len) { if (bufferIndex == len) {
// put the buffer data somewhere // put the buffer data somewhere
@@ -59,8 +53,7 @@ namespace WalkieTalkie {
static inline int RecvCRCRoutine(byte *b) { static inline int RecvCRCRoutine(byte *b) {
crc = *b; crc = *b;
Serial2.print("Reading CRC:"); LOG_TRACE(F("Reacing CRC: %d\n"), crc);
Serial2.println(crc);
state = RecvETX; state = RecvETX;
resetBuffer(); resetBuffer();
@@ -69,19 +62,15 @@ namespace WalkieTalkie {
} }
static inline size_t RecvETXRoutine(uint8_t *payload, byte *b) { static inline size_t RecvETXRoutine(uint8_t *payload, byte *b) {
LOG_TRACE(F("READING ETX\n"));
state = WaitingSTX; state = WaitingSTX;
Serial2.println("Reading ETX"); uint8_t computedCRC = CRC8(payload, len);
if (*b != ETX) { if (*b != ETX) {
Serial2.print("Failure, last byte was: "); LOG_TRACE(F("Failure, last byte was: %d\n"), *b);
Serial2.println(*b);
return -1; return -1;
} else if (crc != CRC8(payload, len)) { } else if (crc != computedCRC) {
Serial2.print("CRC don't match. Expected: "); LOG_TRACE(F("CRC don't match. Expected: %d, got: %d\n"), crc, computedCRC);
Serial2.print(crc);
Serial2.print(", Got: ");
Serial2.println(CRC8(payload, len));
return -2; return -2;
} else { } else {
return len; return len;
+25 -77
View File
@@ -16,6 +16,7 @@ to have more ways to run analytics
#include "communication/commands.h" #include "communication/commands.h"
#include "communication/packets.h" #include "communication/packets.h"
#include "communication/walkieTalkie.h" #include "communication/walkieTalkie.h"
#include "logger/logger.h"
#include "values.h" #include "values.h"
#include "windowPool.h" #include "windowPool.h"
// #include "UIDrawing.h" // #include "UIDrawing.h"
@@ -75,12 +76,11 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
// Initialize the debug serial object // Initialize the debug serial object
Serial2.begin(115200, SERIAL_8N1, UART1_RX, UART1_TX); Logger::Initialize(UART1_RX, UART1_TX);
Serial2.flush();
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); initialDisplaySetup(gfx);
// Only setup the main screen after initializing the Serial2 // Only setup the main screen after initializing the Serial2
@@ -149,29 +149,29 @@ void setup() {
WindowPool::PrintInUse(); WindowPool::PrintInUse();
delay(500); delay(500);
Serial2.println("* Ready for loop"); LOG_INFO(F("* Ready for loop"));
} }
uint64_t lastDataRead = 0; uint64_t lastDataRead = 0;
bool gotAck = false; bool gotAck = false;
struct header { // struct header {
uint8_t StartMarker; // uint8_t StartMarker;
Command Cmd; // Command Cmd;
uint8_t EndMarker; // uint8_t EndMarker;
}; // };
void PrintHeader(header *h) { // void PrintHeader(header *h) {
Serial2.println("HEADER:"); // Serial2.println("HEADER:");
Serial2.print(" StartMarker: "); // Serial2.print(" StartMarker: ");
Serial2.println(h->StartMarker); // Serial2.println(h->StartMarker);
Serial2.print(" Command : "); // Serial2.print(" Command : ");
Serial2.println(CommandToStr(h->Cmd)); // Serial2.println(CommandToStr(h->Cmd));
Serial2.print(" EndMarker : "); // Serial2.print(" EndMarker : ");
Serial2.println(h->EndMarker); // Serial2.println(h->EndMarker);
Serial2.println(); // Serial2.println();
} // }
struct UIWindowPacket { struct UIWindowPacket {
uint8_t StartMarker; uint8_t StartMarker;
@@ -194,17 +194,15 @@ void loop(void) {
if (Serial.available() > 0) { if (Serial.available() > 0) {
int16_t resp = WalkieTalkie::RecvStream(&cmd, payload, 256); int16_t resp = WalkieTalkie::RecvStream(&cmd, payload, 256);
if (resp < 0) { if (resp < 0) {
Serial2.println(F("Failed to receive data from serial")); LOG_WARN(F("Failed to receive data from serial"));
return; return;
} else if (resp == 0) { } else if (resp == 0) {
Serial2.println(F("No data to receive")); LOG_INFO(F("No data to receive"));
return; return;
} }
Serial2.print("Command: "); LOG_INFO(F("Command: %s\n"), CommandToStr(cmd));
Serial2.println(CommandToStr(cmd)); LOG_INFO(F("Response Payload Len: %d\n"), resp);
Serial2.print("Response: ");
Serial2.println(resp);
switch(cmd) { switch(cmd) {
case CmdRequestID: case CmdRequestID:
@@ -213,57 +211,7 @@ void loop(void) {
WalkieTalkie::SendData(&papers); WalkieTalkie::SendData(&papers);
break; break;
default: default:
Serial2.print(F("Command `")); LOG_WARN(F("Command: `%s` has not been implemented yet."), CommandToStr(cmd));
Serial2.print(CommandToStr(cmd));
Serial2.print(F("` has not been implemented yet."));
} }
} }
// // 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;
// }
// }
} }