C
modified: src/SHCustomProtocol.h
1achy8f8e57fd3338
diff --git a/src/SHCustomProtocol.h b/src/SHCustomProtocol.hindex d30fa48..65d3540 100644--- a/src/SHCustomProtocol.h+++ b/src/SHCustomProtocol.h@@ -6,14 +6,13 @@// #include <LGFX_AUTODETECT.hpp> // Preparare la classe "LGFX"// #include <lgfx_user/LGFX_ESP8266_sample.hpp>#include <lgfx_user/LGFX_ESP32_esp32-2432s028.hpp>-// solo dashboard+// dashboard + free deck grafica#include <Arduino.h>#include <map>-#include <BleGamepad.h> // libreria bluetooh+#include <BleGamepad.h> // libreria bluetoothstatic LGFX tft;-static const int SCREEN_WIDTH = 320;static const int SCREEN_HEIGHT = 240;static const int X_CENTER = SCREEN_WIDTH / 2;@@ -22,11 +21,10 @@ static const int ROWS = 5;static const int COLS = 5;static const int CELL_WIDTH = SCREEN_WIDTH / COLS;static const int HALF_CELL_WIDTH = CELL_WIDTH / 2;-static const int CELL_HIGHT = SCREEN_HEIGHT / ROWS;-static const int HALF_CELL_HIGHT = CELL_HIGHT / 2;+static const int CELL_HEIGHT = SCREEN_HEIGHT / ROWS;+static const int HALF_CELL_HEIGHT = CELL_HEIGHT / 2;static const int COL[] = {0, CELL_WIDTH, CELL_WIDTH * 2, CELL_WIDTH * 3, CELL_WIDTH * 4, CELL_WIDTH * 6, CELL_WIDTH * 7};-static const int ROW[] = {0, CELL_HIGHT, CELL_HIGHT * 2, CELL_HIGHT * 3, CELL_HIGHT * 4, CELL_HIGHT * 6, CELL_HIGHT * 7};-+static const int ROW[] = {0, CELL_HEIGHT, CELL_HEIGHT * 2, CELL_HEIGHT * 3, CELL_HEIGHT * 4, CELL_HEIGHT * 6, CELL_HEIGHT * 7};std::map<String, String> prevData;std::map<String, int32_t> prevColor;@@ -36,8 +34,8 @@ std::map<String, int32_t> prevColor;BleGamepad bleGamepad("ESP32 Touch Gamepad", "YourCompany", 100);BleGamepadConfiguration bleGamepadConfig;-// configuro ble per gamepad-+int currentPage = 1; // Variabile per tenere traccia della pagina corrente+bool forceUpdate = false; // Variabile per forzare l'aggiornamento delle celleclass SHCustomProtocol{@@ -51,7 +49,7 @@ private:String speed = "0";String currentLapTime = "00:00.00";String lastLapTime = "00:00.00";- String bestLapTime = "00:00.00";+ String bestLapTime = "00.00.00";String sessionBestLiveDeltaSeconds = "0.000";String sessionBestLiveDeltaProgressSeconds = "0.00";String tyrePressureFrontLeft = "00.0";@@ -100,54 +98,10 @@ public:// Serial.begin(115200); //x debug// Serial.println("Test seriale avviato!"); //x debug-tft.init();tft.setRotation(1);tft.fillScreen(TFT_BLACK);--//Questa parte di 10 righe , dopo il commento ,del codice configura il comportamento del tuo gamepad Bluetooth emulato tramite l'ESP32. Ecco una spiegazione dettagliata di ogni riga:--//Dettagli delle Funzioni-//bleGamepadConfig.setAutoReport(false);-//Questa funzione disabilita la segnalazione automatica dello stato del gamepad.-//Significa che lo stato del gamepad non verrà inviato automaticamente ai dispositivi collegati. Dovrai chiamare manualmente bleGamepad.sendReport() per inviare lo stato del gamepad quando necessario.-//bleGamepadConfig.setAxesMax(32760);--//Questa funzione imposta il valore massimo per gli assi (X, Y, Z, ecc.) del gamepad.-//Anche se non stai usando gli assi nel tuo progetto, è buona pratica impostare un valore massimo sensato.-//bleGamepadConfig.setIncludeSlider1(false);--//Questa funzione specifica che il gamepad non includerà un asse slider1.-//Poiché stai utilizzando solo pulsanti touch, non hai bisogno di assi slider.-//bleGamepadConfig.setIncludeXAxis(false);--//Questa funzione specifica che il gamepad non includerà l'asse X.-//Non stai usando assi nel tuo progetto, quindi li escludi tutti.-//bleGamepadConfig.setIncludeYAxis(false);--//Questa funzione specifica che il gamepad non includerà l'asse Y.-//bleGamepadConfig.setIncludeZAxis(false);--//Questa funzione specifica che il gamepad non includerà l'asse Z.-//bleGamepadConfig.setIncludeRxAxis(false);--//Questa funzione specifica che il gamepad non includerà l'asse Rx (asse di rotazione X).-//bleGamepadConfig.setIncludeRyAxis(false);--//Questa funzione specifica che il gamepad non includerà l'asse Ry (asse di rotazione Y).-//bleGamepadConfig.setIncludeRzAxis(false);--//Questa funzione specifica che il gamepad non includerà l'asse Rz (asse di rotazione Z).-//bleGamepadConfig.setButtonCount(6);--//Questa funzione imposta il numero di pulsanti sul gamepad a 6.-//Questo corrisponde ai 6 pulsanti touch che vuoi configurare sul tuo display.-//bleGamepad.begin(&bleGamepadConfig);--//Questa funzione inizializza il gamepad Bluetooth con la configurazione specificata.-//Dopo questa chiamata, il gamepad è pronto per essere utilizzato e inviato ai dispositivi collegati.-bleGamepadConfig.setAutoReport(true); // in false non invia i comandi a windowsbleGamepadConfig.setAxesMax(32760);bleGamepadConfig.setIncludeSlider1(false);@@ -160,14 +114,8 @@ public:bleGamepadConfig.setButtonCount(6); // 6 touch buttonsbleGamepad.begin(&bleGamepadConfig);// Serial.println("Configurazione BleGamepad completata."); //x debug---}---// Called when new data is coming from computervoid read(){@@ -204,47 +152,86 @@ public:void loop(){readTouch(); // Leggi il touchscreen dello schermo++ static int lastPage = currentPage;+ if (currentPage != lastPage) {+ tft.fillScreen(TFT_BLACK);+ forceUpdate = true;+ lastPage = currentPage;+ }- drawRpmMeter(0, 0, SCREEN_WIDTH, HALF_CELL_HIGHT);+ if (currentPage == 1) {+ drawPage1(forceUpdate);+ } else {+ drawPage2();+ }++ forceUpdate = false;++ if (tft.getTouch(&touchX, &touchY)) {+ if (touchX < 50 && touchY < 50) {+ // Cambia pagina quando tocchi nell'angolo in alto a sinistra+ currentPage = (currentPage == 1) ? 2 : 1;+ }+ }+ }++ void idle() {}++ void drawPage1(bool forceUpdate = false) {+ drawRpmMeter(0, 0, SCREEN_WIDTH, HALF_CELL_HEIGHT);drawGear(COL[2], COL[1]);// First+Second Column (Lap times)- drawCell(COL[0], ROW[1], bestLapTime, "bestLapTime", "Best Lap", "left");- drawCell(COL[0], ROW[2], lastLapTime, "lastLapTime", "Last Lap", "left");- drawCell(COL[0], ROW[3], currentLapTime, "currenLapTime", "Current Lap", "left", lapInvalidated == "True" ? TFT_RED : TFT_WHITE);-+ drawCell(COL[0], ROW[1], bestLapTime, "bestLapTime", "Best Lap", "left", TFT_WHITE, 4, forceUpdate);+ drawCell(COL[0], ROW[2], lastLapTime, "lastLapTime", "Last Lap", "left", TFT_WHITE, 4, forceUpdate);+ drawCell(COL[0], ROW[3], currentLapTime, "currenLapTime", "Current Lap", "left", lapInvalidated == "True" ? TFT_RED : TFT_WHITE, 4, forceUpdate);// Third Column (speed)- drawCell(COL[2], ROW[3], speed, "speed", "Speed");+ drawCell(COL[2], ROW[3], speed, "speed", "Speed", "center", TFT_WHITE, 4, forceUpdate);// Fourth+Fifth Column (delta)- drawCell(SCREEN_WIDTH, ROW[1], sessionBestLiveDeltaSeconds, "sessionBestLiveDeltaSeconds", "Delta", "right", sessionBestLiveDeltaSeconds.indexOf('-') >= 0 ? TFT_GREEN : TFT_RED);- drawCell(SCREEN_WIDTH, ROW[2], sessionBestLiveDeltaProgressSeconds, "sessionBestLiveDeltaProgressSeconds", "Delta P", "right", sessionBestLiveDeltaProgressSeconds.indexOf('-') >= 0 ? TFT_GREEN : TFT_RED);-+ drawCell(SCREEN_WIDTH, ROW[1], sessionBestLiveDeltaSeconds, "sessionBestLiveDeltaSeconds", "Delta", "right", sessionBestLiveDeltaSeconds.indexOf('-') >= 0 ? TFT_GREEN : TFT_RED, 4, forceUpdate);+ drawCell(SCREEN_WIDTH, ROW[2], sessionBestLiveDeltaProgressSeconds, "sessionBestLiveDeltaProgressSeconds", "Delta P", "right", sessionBestLiveDeltaProgressSeconds.indexOf('-') >= 0 ? TFT_GREEN : TFT_RED, 4, forceUpdate);// (TC, ABS, BB)if (isTCCutNull == "False")- drawCell(COL[0], ROW[4], tcTcCut, "tcTcCut", "TC TC2", "center", TFT_YELLOW);+ drawCell(COL[0], ROW[4], tcTcCut, "tcTcCut", "TC TC2", "center", TFT_YELLOW, 4, forceUpdate);else- drawCell(COL[0], ROW[4], tcLevel, "tcLevel", "TC", "center", TFT_YELLOW);- drawCell(COL[1], ROW[4], absLevel, "absLevel", "ABS", "center", TFT_BLUE);- drawCell(COL[2], ROW[4], brakeBias, "brakeBias", "BB", "center", TFT_MAGENTA);+ drawCell(COL[0], ROW[4], tcLevel, "tcLevel", "TC", "center", TFT_YELLOW, 4, forceUpdate);+ drawCell(COL[1], ROW[4], absLevel, "absLevel", "ABS", "center", TFT_BLUE, 4, forceUpdate);+ drawCell(COL[2], ROW[4], brakeBias, "brakeBias", "BB", "center", TFT_MAGENTA, 4, forceUpdate);// (tyre pressure)- drawCell(COL[3], ROW[3], tyrePressureFrontLeft, "tyrePressureFrontLeft", "FL", "center", TFT_CYAN);- drawCell(COL[4], ROW[3], tyrePressureFrontRight, "tyrePressureFrontRight", "FR", "center", TFT_CYAN);- drawCell(COL[3], ROW[4], tyrePressureRearLeft, "tyrePressureRearLeft", "RL", "center", TFT_CYAN);- drawCell(COL[4], ROW[4], tyrePressureRearRight, "tyrePressureRearRight", "RR", "center", TFT_CYAN);+ drawCell(COL[3], ROW[3], tyrePressureFrontLeft, "tyrePressureFrontLeft", "FL", "center", TFT_CYAN, 4, forceUpdate);+ drawCell(COL[4], ROW[3], tyrePressureFrontRight, "tyrePressureFrontRight", "FR", "center", TFT_CYAN, 4, forceUpdate);+ drawCell(COL[3], ROW[4], tyrePressureRearLeft, "tyrePressureRearLeft", "RL", "center", TFT_CYAN, 4, forceUpdate);+ drawCell(COL[4], ROW[4], tyrePressureRearRight, "tyrePressureRearRight", "RR", "center", TFT_CYAN, 4, forceUpdate);}- // Called once between each byte read on arduino,- // THIS IS A CRITICAL PATH :- // AVOID ANY TIME CONSUMING ROUTINES !!!- // PREFER READ OR LOOP METHOS AS MUCH AS POSSIBLE- // AVOID ANY INTERRUPTS DISABLE (serial data would be lost!!!)- void idle()- {- }+void drawPage2()+{+ int buttonWidth = SCREEN_WIDTH / 3;+ int buttonHeight = SCREEN_HEIGHT / 2;+ String buttonLabels[6] = {"Button 1", "Button 2", "Button 3", "Button 4", "Button 5", "Button 6"};+ uint16_t buttonColors[6] = {TFT_RED, TFT_GREEN, TFT_BLUE, TFT_YELLOW, TFT_ORANGE, TFT_PURPLE};++ for (int i = 0; i < 6; i++) {+ int x = (i % 3) * buttonWidth;+ int y = (i / 3) * buttonHeight;+ drawButton(x, y, buttonWidth, buttonHeight, buttonLabels[i], buttonColors[i]);+ }+}++void drawButton(int x, int y, int width, int height, String label, uint16_t color)+{+ tft.fillRect(x, y, width, height, color);+ tft.drawRect(x, y, width, height, TFT_WHITE);+ tft.setTextColor(TFT_WHITE, color);+ tft.setTextDatum(MC_DATUM);+ tft.drawString(label, x + width / 2, y + height / 2);+ tft.setTextDatum(TL_DATUM);+}void drawGear(int32_t x, int32_t y){@@ -255,7 +242,7 @@ public:tft.setTextColor(TFT_YELLOW, TFT_BLACK);tft.setTextSize(8);tft.setTextDatum(MC_DATUM);- tft.setCursor(x + 12, y + HALF_CELL_HIGHT);+ tft.setCursor(x + 12, y + HALF_CELL_HEIGHT);tft.print(gear);tft.setTextSize(1);tft.setTextDatum(TL_DATUM);@@ -305,9 +292,7 @@ public:prev_rpmPercent = rpmPercent;}-// La funzione drawCell disegna una cella sullo schermo e aggiorna il suo contenuto solo se i dati o il colore sono cambiati.-- void drawCell(int32_t x, int32_t y, String data, String id, String name = "Data", String align = "center", int32_t color = TFT_WHITE, int fontSize = 4)+ void drawCell(int32_t x, int32_t y, String data, String id, String name = "Data", String align = "center", int32_t color = TFT_WHITE, int fontSize = 4, bool forceUpdate = false){const static int titleHeight = 19;const static int hPadding = 5;@@ -315,27 +300,27 @@ public:tft.setTextColor(color, TFT_BLACK);- const bool dataChanged = (prevData[id] != data); // variabilò per vedere cambiamento dati e cambiare valori sullo schermo- const bool colorChanged = (prevColor[id] != color); // variabilò per vedere cambiamento dati e disegnare sullo schermo+ bool dataChanged = (prevData[id] != data) || forceUpdate;+ bool colorChanged = (prevColor[id] != color) || forceUpdate;if (dataChanged) {if (align == "left"){- if (colorChanged) tft.drawRoundRect(x, y, CELL_WIDTH * 2 - 1, CELL_HIGHT - 2, 5, color); // Rectangle+ if (colorChanged) tft.drawRoundRect(x, y, CELL_WIDTH * 2 - 1, CELL_HEIGHT - 2, 5, color); // Rectangleif (colorChanged) tft.drawString(name, x + hPadding, y + vPadding, 2); // Titletft.drawString(data, x + hPadding, y + titleHeight, fontSize); // Data}else if (align == "right"){- if (colorChanged) tft.drawRoundRect(x - (CELL_WIDTH * 2), y, CELL_WIDTH * 2 - 1, CELL_HIGHT - 2, 5, color); // Rectangle+ if (colorChanged) tft.drawRoundRect(x - (CELL_WIDTH * 2), y, CELL_WIDTH * 2 - 1, CELL_HEIGHT - 2, 5, color); // Rectangleif (colorChanged) tft.drawRightString(name, x - hPadding, y + vPadding, 2); // Titletft.drawRightString(data, x - hPadding, y + titleHeight, fontSize); // Data}else // "center"{- if (colorChanged) tft.drawRoundRect(x, y, CELL_WIDTH - 2, CELL_HIGHT - 2, 5, color); // Rectangle+ if (colorChanged) tft.drawRoundRect(x, y, CELL_WIDTH - 2, CELL_HEIGHT - 2, 5, color); // Rectangleif (colorChanged) tft.drawCentreString(name, x + HALF_CELL_WIDTH, y + vPadding, 2); // Titletft.drawCentreString(data, x + HALF_CELL_WIDTH, y + titleHeight, fontSize); // Data}@@ -364,58 +349,39 @@ public:}- void readTouch()- {- if (tft.getTouch(&touchX, &touchY))--- {- // tft.fillCircle(touchX, touchY, 5, TFT_RED); // Visualizza le coordinate del tocco PER DEBUG- // Serial.print("Tocco rilevato alle coordinate: "); //x debug- // Serial.print(touchX); //x debug- // Serial.print(", "); //x debug- // Serial.println(touchY); //x debug---// Questa sezione del codice gestisce il rilevamento del tocco sullo schermo e mappa le coordinate del tocco a un pulsante-// virtuale, inviando quindi un comando tramite Bluetooth utilizzando il BleGamepad. Ecco una spiegazione dettagliata di ciò-// che fa ogni parte del codice:-- int buttonIndex = getTouchButtonIndex(touchX, touchY);+void readTouch()+{+ if (currentPage == 2) {+ if (tft.getTouch(&touchX, &touchY)) {+ int buttonIndex = getTouchButtonIndex(touchX, touchY);- // Serial.print("Indice pulsante: "); //x debug- // Serial.println(buttonIndex); //x debug+ if (buttonIndex != -1) {+ // Effetto di pressione+ int x = (buttonIndex % 3) * (SCREEN_WIDTH / 3);+ int y = (buttonIndex / 3) * (SCREEN_HEIGHT / 2);+ drawButton(x, y, SCREEN_WIDTH / 3, SCREEN_HEIGHT / 2, "Pressed", TFT_DARKGREY);- if (buttonIndex != -1)- {bleGamepad.press(buttonIndex + 1);-- // Serial.print("Pulsante "); //x debug- // Serial.print(buttonIndex + 1); //x debug- // Serial.println("premuto"); //x debug-delay(100); // debounce delaybleGamepad.release(buttonIndex + 1);- // Serial.print("Pulsante "); //x debug- // Serial.print(buttonIndex + 1); //x debug- // Serial.println(" rilasciato."); //x debug- }- }- }--// La funzione getTouchButtonIndex calcola l'indice di un pulsante touch in base alle coordinate di tocco x e y sullo schermo.-// In pratica, divide lo schermo in una griglia di pulsanti, quindi determina quale pulsante è stato toccato.-- int getTouchButtonIndex(int x, int y)- {- int buttonWidth = SCREEN_WIDTH / 3;- int buttonHeight = SCREEN_HEIGHT / 2;- int col = x / buttonWidth;- int row = y / buttonHeight;- return col + row * 3; // Restituisce l'indice del pulsante (0 a 5)+ // Ripristina il pulsante dopo la pressione+ String buttonLabels[6] = {"Button 1", "Button 2", "Button 3", "Button 4", "Button 5", "Button 6"};+ uint16_t buttonColors[6] = {TFT_RED, TFT_GREEN, TFT_BLUE, TFT_YELLOW, TFT_ORANGE, TFT_PURPLE};+ drawButton(x, y, SCREEN_WIDTH / 3, SCREEN_HEIGHT / 2, buttonLabels[buttonIndex], buttonColors[buttonIndex]);+ }+ }}+}+int getTouchButtonIndex(int x, int y)+{+ int buttonWidth = SCREEN_WIDTH / 3;+ int buttonHeight = SCREEN_HEIGHT / 2;+ int col = x / buttonWidth;+ int row = y / buttonHeight;+ return col + row * 3; // Restituisce l'indice del pulsante (0 a 5)+}};#endif\ No newline at end of file