I will clean this mess but for now yay for the relative

This commit is contained in:
2025-04-10 23:27:22 +01:00
parent a44019a9a6
commit 4c57028874
9 changed files with 240 additions and 81 deletions
+138 -34
View File
@@ -8,11 +8,13 @@
#include "debug/debug.h"
#include "displaySetup.h"
#include "esp32-hal-psram.h"
#include "esp_system.h"
#include "ui.h"
#include <Arduino.h>
#include <Arduino_GFX_Library.h>
#include <cstdint>
#include <cstring>
#include <unistd.h>
#define UART1_TX 19
#define UART1_RX 18
@@ -69,6 +71,7 @@ void setup() {
// Initialize the debug serial object
Serial2.begin(115200, SERIAL_8N1, UART1_RX, UART1_TX);
Serial2.flush();
Serial2.println(F("=== ESP32 SimRacing DashDisplay ==="));
@@ -83,6 +86,7 @@ void setup() {
// relative.dims.y = 250;
relative.setup();
relative.drawBox();
ui->relative = &relative;
// Setup the rpmText box
rpmTextDecor->textSize = 7;
@@ -90,19 +94,20 @@ void setup() {
calculateHeight(rpmTextDecor->titleSize, rpmTextDecor->textSize, 1);
rpmText.dims.width = calculateWidth(rpmTextDecor->textSize, 5);
rpmText.horizontalCenter();
ui->rpmText = &rpmText;
ui->rpmText->drawBox();
ui->rpmText->Update("12345");
ui->digiTacho = &rpmText;
ui->digiTacho->drawBox();
ui->digiTacho->Update("12345");
// Setup the speedText box
speedTextDecor->textSize = 4;
speedText.dims.height =
calculateHeight(speedTextDecor->titleSize, speedTextDecor->textSize, 1);
calculateHeight(speedTextDecor->titleSize, speedTextDecor->textSize,
1);
speedText.dims.width = calculateWidth(speedTextDecor->textSize, 4);
speedText.placeRight(&rpmText);
ui->speedText = &speedText;
ui->speedText->drawBox();
ui->speedText->Update("253");
ui->digiSpeedo = &speedText;
ui->digiSpeedo->drawBox();
ui->digiSpeedo->Update("253");
// Setup the gear text box
gearTextDecor->textSize = 7;
@@ -111,9 +116,9 @@ void setup() {
gearText.dims.width = calculateWidth(gearTextDecor->textSize, 2);
gearText.horizontalCenter();
gearText.dims.y = rpmText.dims.height + 5;
ui->gearText = &gearText;
ui->gearText->drawBox();
ui->gearText->Update("3");
ui->digiGear = &gearText;
ui->digiGear->drawBox();
ui->digiGear->Update("3");
// Setup the best lap box
bestLapDecor->textSize = 3;
@@ -164,46 +169,145 @@ void setup() {
ui->fuelConsumption = &fuelConsumption;
ui->fuelConsumption->drawBox();
ui->fuelConsumption->Update("10.4 | 11.2");
// Connect to ESDI
// Serial2.println("Waiting for ESDI signal");
// uint8_t buffer[sizeof(DataReq)];
// size_t bufferIndex = 0;
// bool ready = false;
// while (!ready) {
// while (Serial.available() > 0) {
// buffer[bufferIndex] = Serial.read();
// bufferIndex++;
//
// if (bufferIndex >= sizeof(DataReq)) {
// // Copy the buffer into the struct
// DataReq packet;
// memcpy(&packet, buffer, sizeof(DataReq));
//
// // Reset the buffer index
// bufferIndex = 0;
//
// Serial2.print("Received msg from ESDI: ");
// Serial2.println(packet.type);
//
// if (packet.type == 3) {
// ready = true;
// break;
// }
// }
// }
// }
// const char *words[] = {"hel", "wor", "why", "is", "thi", "hap", "to",
// "me"};
//
// int wIndex = 0;
// while (1) {
// for (int row = 0; row < ROWS; row++) {
// for (int col = 0; col < COLUMNS; col++) {
// ui->relative->tableData[row * COLUMNS +
// col]->Update(words[wIndex++]); if (wIndex >= 8) {
// wIndex = 0;
// }
// }
// }
// }
Serial2.println("* Ready for loop");
}
uint64_t lastDataRead = 0;
uint8_t packetBuffer[sizeof(DataPacket)];
int bufferIndex = 0;
void debugDataPacket(DataPacket *p) {
Serial2.printf("\rSize: %zu\n", sizeof(struct DataPacket));
Serial2.printf("\rStartMarker: 0x%02x\n", p->StartMarker);
Serial2.printf("\rSpeed: %s\n", p->speed);
Serial2.printf("\rGear: %s\n", p->gear);
Serial2.printf("\rRPM: %s\n", p->rpm);
Serial2.printf("\rLapNumber: %s\n", p->LapNumber);
Serial2.printf("\rcurrLapTime: %s\n", p->currLapTime);
Serial2.printf("\rlastLapTime: %s\n", p->lastLapTime);
Serial2.printf("\rfuelEst: %s\n", p->FuelEst);
Serial2.printf("\rStandings:\n");
for (int k = 0; k < 5; k++) {
Serial2.printf("\r Lap: %s\n", p->standings[k].Lap);
Serial2.printf("\r DriverName: %s\n", p->standings[k].DriverName);
Serial2.printf("\r TimeBehind: %s\n", p->standings[k].TimeBehindString);
}
Serial2.printf("\rEndMarker: 0x%02x\n\n", p->EndMarker);
}
bool isReceiving = false;
void loop(void) {
// Read bytes one by one
// Request data here
DataReq req = {5};
Serial.write((uint8_t *)&req, sizeof(req));
Serial.flush();
// Receive data
while (Serial.available() > 0) {
packetBuffer[bufferIndex] = Serial.read();
bufferIndex++;
uint8_t byte = Serial.read();
if (!isReceiving) {
if (byte == 0x02) {
isReceiving = true;
bufferIndex = 0;
}
}
packetBuffer[bufferIndex++] = byte;
// Check if we have a complete packet
if (bufferIndex >= sizeof(DataPacket)) {
// Copy the buffer into the struct
Serial.flush();
bufferIndex = 0;
isReceiving = false;
DataPacket packet;
// SerializeDataPacket(&packet, packetBuffer);
memcpy(&packet, packetBuffer, sizeof(DataPacket));
// Reset the buffer index
bufferIndex = 0;
// Process the packet
// if (millis() - lastDataPrint > 50) {
// debugDataPacket(&packet);
// lastDataPrint = millis();
if (packet.EndMarker != 0x03) {
// Serial2.println(F("\r////////////// DATA IS MALFORMED
// //////////////"));
continue;
}
// for(int k = 0; k < sizeof(DataPacket); k++) {
// if (k % 8 == 0) {
// Serial2.printf("\r\n");
// }
// Serial2.printf(" 0x%02x", packetBuffer[k]);
// }
// Serial2.printf("\r\n");
// ui.speedometer->Update(packet.speed);
//
// ui.gear->Update(packet.gear);
// ui.numberTach->Update(packet.rpm);
// ui.barTach->Update(packet.rpm);
// Serial2.printf("%d / %d [%2d] || %d / %d [%2d]\n\n\r",
// ESP.getFreeHeap(),
// ESP.getHeapSize(),
// (int)(((float)(ESP.getHeapSize() - ESP.getFreeHeap()) /
// ESP.getHeapSize()) *
// 100),
// ESP.getFreePsram(), ESP.getPsramSize(),
// (int)(((float)(ESP.getPsramSize() - ESP.getFreePsram())
// /
// ESP.getPsramSize()) *
// 100));
// ui.relative->Update(packet.standings);
for (int k = 0; k < 15; k += 3) {
// ui.relative->tableData[k + 0].Update(packet.standings[k / 3].Lap);
// ui.relative->tableData[k + 1].Update(
// packet.standings[k / 3].DriverName);
// ui.relative->tableData[k + 2].Update(
// packet.standings[k / 3].TimeBehindString);
// debugDataPacket(&packet);
ui->digiSpeedo->Update(packet.speed);
ui->digiTacho->Update(packet.rpm);
ui->digiGear->Update(packet.gear);
ui->curLap->Update(packet.currLapTime);
ui->lastLap->Update(packet.lastLapTime);
for (int row = 0; row < ROWS; row++) {
ui->relative->tableData[row * COLUMNS + 0]->Update(
packet.standings[row].Lap);
ui->relative->tableData[row * COLUMNS + 1]->Update(
packet.standings[row].DriverName);
ui->relative->tableData[row * COLUMNS + 2]->Update(
packet.standings[row].TimeBehindString);
}
lastDataRead = millis();