diff --git a/src/data.cpp b/src/data.cpp index 20ca06a..deb5341 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,40 +1,84 @@ #include "data.h" #include -#include #include -void SerializeDataPacket(DataPacket *p, uint8_t *buffer) { - size_t offset = 0; +uint8_t packetBuffer[sizeof(DataPacket)] = {0}; +int bufferIndex = 0; +bool isReceiving = false; - memcpy(&p->StartMarker, buffer + offset, 1); - offset += 1; +void SendDataRequest() { + DataReq req = {5}; + Serial.write((uint8_t *)&req, sizeof(req)); + Serial.flush(); +} + +int RecvDataPacket(DataPacket *p) { + while (Serial.available() > 0) { + uint8_t byte = Serial.read(); + + if (!isReceiving) { + if (byte == 0x02) { + isReceiving = true; + bufferIndex = 0; + } + } + packetBuffer[bufferIndex++] = byte; + + if (bufferIndex >= sizeof(DataPacket)) { + Serial.flush(); + bufferIndex = 0; + isReceiving = false; + + memcpy(p, packetBuffer, sizeof(DataPacket)); + + if (p->EndMarker != 0x03) { + // Serial2.println(F("\r////////////// DATA IS MALFORMED + // //////////////")); + return 1; + } + + return 0; + // 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"); + + // 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)); + + // debugDataPacket(&packet); + } + } + return 1; +} + +void DebugDataPacket(DataPacket *p) { + Serial2.printf("\rSize: %zu\n", sizeof(struct DataPacket)); Serial2.printf("\rStartMarker: 0x%02x\n", p->StartMarker); - - memcpy(&p->speed, buffer + offset, speedLen); - offset += speedLen; Serial2.printf("\rSpeed: %s\n", p->speed); - - memcpy(&p->gear, buffer + offset, gearLen); - offset += gearLen; Serial2.printf("\rGear: %s\n", p->gear); - - memcpy(&p->rpm, buffer + offset, rpmLen); - offset += rpmLen; Serial2.printf("\rRPM: %s\n", p->rpm); - - memcpy(&p->LapNumber, buffer + offset, lapNumberLen); - offset += lapNumberLen; Serial2.printf("\rLapNumber: %s\n", p->LapNumber); - - memcpy(&p->currLapTime, buffer + offset, currLapTimeLen); - offset += currLapTimeLen; Serial2.printf("\rcurrLapTime: %s\n", p->currLapTime); - - memcpy(&p->lastLapTime, buffer + offset, lastLapTimeLen); - offset += lastLapTimeLen; Serial2.printf("\rlastLapTime: %s\n", p->lastLapTime); - - memcpy(&p->FuelEst, buffer + offset, FuelEstLen); - offset += FuelEstLen; - Serial2.printf("\rFuelEst: %s\n", p->FuelEst); + 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); } diff --git a/src/data.h b/src/data.h index 8c9a30d..11b505c 100644 --- a/src/data.h +++ b/src/data.h @@ -40,6 +40,8 @@ struct DataPacket { }; #pragma pack(pop) -void SerializeDataPacket(DataPacket *p, uint8_t *buffer); +void DebugDataPacket(DataPacket *p); +int RecvDataPacket(DataPacket *p); +void SendDataRequest(); #endif diff --git a/src/main.cpp b/src/main.cpp index 3334cd1..e997173 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,12 @@ +/* +ESP32-8048S043 based DashDisplay for simracing and stuff +by Eduardo Silva and others + +TODO: +- Add a better communication mechanism that allows the desktop interface +to have more ways to run analytics +*/ + #include "Arduino_GFX.h" #include "HardwareSerial.h" #include "UIDecorations.h" @@ -5,10 +14,8 @@ #include "UIString.h" #include "UITable.h" #include "data.h" -#include "debug/debug.h" #include "displaySetup.h" #include "esp32-hal-psram.h" -#include "esp_system.h" #include "ui.h" #include #include @@ -101,8 +108,7 @@ void setup() { // 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->digiSpeedo = &speedText; @@ -170,35 +176,6 @@ void setup() { 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"}; // @@ -218,100 +195,18 @@ void setup() { } 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) { // Request data here - DataReq req = {5}; - Serial.write((uint8_t *)&req, sizeof(req)); - Serial.flush(); + SendDataRequest(); // Receive data - while (Serial.available() > 0) { - uint8_t byte = Serial.read(); + DataPacket telemetryPacket; + int res = RecvDataPacket(&telemetryPacket); - if (!isReceiving) { - if (byte == 0x02) { - isReceiving = true; - bufferIndex = 0; - } - } - packetBuffer[bufferIndex++] = byte; + if (res == 0) { + ui->Update(&telemetryPacket); - if (bufferIndex >= sizeof(DataPacket)) { - Serial.flush(); - bufferIndex = 0; - isReceiving = false; - - DataPacket packet; - // SerializeDataPacket(&packet, packetBuffer); - memcpy(&packet, packetBuffer, sizeof(DataPacket)); - - 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"); - - // 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)); - - // 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(); - } + lastDataRead = millis(); } // If no data is received for 5 seconds, reset the display diff --git a/src/ui.cpp b/src/ui.cpp index 90ff83a..ff4bde3 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1,10 +1,23 @@ #include "ui.h" #include "UIDecorations.h" - +#include "data.h" DashUI::DashUI() {} -void DashUI::dashUISetup(Arduino_GFX *gfx) { - // rpmTextDecorations +void DashUI::Update(DataPacket *p) { + this->digiSpeedo->Update(p->speed); + this->digiTacho->Update(p->rpm); + this->digiGear->Update(p->gear); + this->curLap->Update(p->currLapTime); + this->lastLap->Update(p->lastLapTime); + this->fuelConsumption->Update(p->FuelEst); + + for (int row = 0; row < ROWS; row++) { + this->relative->tableData[row * COLUMNS + 0]->Update(p->standings[row].Lap); + this->relative->tableData[row * COLUMNS + 1]->Update( + p->standings[row].DriverName); + this->relative->tableData[row * COLUMNS + 2]->Update( + p->standings[row].TimeBehindString); + } } diff --git a/src/ui.h b/src/ui.h index 18f263d..ae431b6 100644 --- a/src/ui.h +++ b/src/ui.h @@ -4,6 +4,7 @@ #include "Arduino_GFX.h" #include "UIString.h" #include "UITable.h" +#include "data.h" struct DashUI { UIString *digiTacho; @@ -17,7 +18,7 @@ struct DashUI { UITable *relative; DashUI(); - void dashUISetup(Arduino_GFX *gfx); + void Update(DataPacket *p); }; #endif