C
modify ui
YU CHIH CHANGc817e98f72e0
diff --git a/lib/GT7DerivedMetrics/GT7DerivedMetrics.cpp b/lib/GT7DerivedMetrics/GT7DerivedMetrics.cppindex 76bacd1..6571a18 100644--- a/lib/GT7DerivedMetrics/GT7DerivedMetrics.cpp+++ b/lib/GT7DerivedMetrics/GT7DerivedMetrics.cpp@@ -41,52 +41,302 @@ void GT7FuelEstimator::reset(){validSamples=0;}-void GT7FuelEstimator::update(float fuelLevel,int currentLap){- if(currentLap < 0 || fuelLevel < 0.0f) return;+void GT7FuelEstimator::update(float fuelLevel, int currentLap) {+ if (currentLap < 0 || fuelLevel < 0.0f) return;- if(lastLap < 0){- lastLap=currentLap;- lapStartFuel=fuelLevel;- lastFuel=fuelLevel;+ if (lastLap < 0) {+ lastLap = currentLap;+ lapStartFuel = fuelLevel;+ lastFuel = fuelLevel;return;}- if(currentLap < lastLap){+ if (currentLap < lastLap) {reset();- lastLap=currentLap;- lapStartFuel=fuelLevel;- lastFuel=fuelLevel;+ lastLap = currentLap;+ lapStartFuel = fuelLevel;+ lastFuel = fuelLevel;return;}- // Pit refuel during the same lap: restart the baseline.- if(fuelLevel > lastFuel + 0.5f){- lapStartFuel=fuelLevel;+ // 同一圈內進站加油,重新設定該圈的油量基準+ if (fuelLevel > lastFuel + 0.5f) {+ lapStartFuel = fuelLevel;}- if(currentLap > lastLap){- const float used=lapStartFuel-fuelLevel;- if(used > 0.01f){- if(validSamples == 0) avgFuelPerLap=used;- else avgFuelPerLap=avgFuelPerLap*0.7f+used*0.3f;+ if (currentLap > lastLap) {+ const float used = lapStartFuel - fuelLevel;++ // 略過起跑時 0 → 1 的假圈數切換+ // 也排除過小或明顯異常的油耗樣本+ const bool completedRealLap =+ lastLap >= 1 &&+ currentLap == lastLap + 1 &&+ used >= 0.2f &&+ used <= 50.0f;++ if (completedRealLap) {+ if (validSamples == 0) {+ avgFuelPerLap = used;+ } else {+ avgFuelPerLap =+ avgFuelPerLap * 0.7f ++ used * 0.3f;+ }+++validSamples;}- lastLap=currentLap;- lapStartFuel=fuelLevel;++ lastLap = currentLap;+ lapStartFuel = fuelLevel;+ }++ lastFuel = fuelLevel;++ remaining =+ (validSamples > 0 && avgFuelPerLap >= 0.2f)+ ? fuelLevel / avgFuelPerLap+ : -1.0f;+}++float GT7FuelEstimator::remainingLaps() const {+ return remaining;+}++float GT7DeltaTracker::distanceSquared(+ float x1, float y1, float z1,+ float x2, float y2, float z2)+{+ const float dx = x1 - x2;+ const float dy = y1 - y2;+ const float dz = z1 - z2;++ return dx * dx + dy * dy + dz * dz;+}++int GT7DeltaTracker::findNearestReference(+ float x, float y, float z)+{+ if(referenceCount <= 0){+ return -1;+ }++ int bestIndex = lastReferenceIndex;+ float bestDistance = INFINITY;++ // 正常情況只往目前位置附近搜尋,+ // 避免髮夾彎或賽道交叉時跳到錯誤路段。+ int start = lastReferenceIndex - 5;+ int end = lastReferenceIndex + 30;++ if(start < 0) start = 0;+ if(end >= referenceCount) end = referenceCount - 1;++ for(int i = start; i <= end; ++i){+ const float d = distanceSquared(+ x, y, z,+ reference[i].x,+ reference[i].y,+ reference[i].z+ );++ if(d < bestDistance){+ bestDistance = d;+ bestIndex = i;+ }+ }++ // 區域搜尋結果距離超過 50 公尺,+ // 可能是剛開始、封包中斷或索引跑掉,改做全域搜尋。+ constexpr float GLOBAL_SEARCH_DISTANCE_SQ = 50.0f * 50.0f;++ if(bestDistance > GLOBAL_SEARCH_DISTANCE_SQ){+ bestDistance = INFINITY;++ for(int i = 0; i < referenceCount; ++i){+ const float d = distanceSquared(+ x, y, z,+ reference[i].x,+ reference[i].y,+ reference[i].z+ );++ if(d < bestDistance){+ bestDistance = d;+ bestIndex = i;+ }+ }+ }++ return bestIndex;+}++void GT7DeltaTracker::reset()+{+ referenceCount = 0;+ currentCount = 0;+ sampleCounter = 0;++ lastReferenceIndex = 0;+ previousReferenceIndex = -1;++ bestLapTime = 0;++ currentDelta = 0.0f;+ projectedDelta = 0.0f;++ previousDelta = 0.0f;+ deltaSlope = 0.0f;++ deltaValid = false;+}++void GT7DeltaTracker::beginLap()+{+ currentCount = 0;+ sampleCounter = 0;++ lastReferenceIndex = 0;+ previousReferenceIndex = -1;++ currentDelta = 0.0f;+ projectedDelta = 0.0f;++ previousDelta = 0.0f;+ deltaSlope = 0.0f;++ deltaValid = false;+}++void GT7DeltaTracker::sample(+ float x,+ float y,+ float z,+ uint32_t lapTime)+{+ // 不需要每個 UDP 封包都存。+ ++sampleCounter;++ if(sampleCounter < SAMPLE_DIVIDER){+ return;+ }++ sampleCounter = 0;++ if(currentCount < MAX_POINTS){+ DeltaPoint &point = current[currentCount];++ point.x = x;+ point.y = y;+ point.z = z;+ point.lapTime = lapTime;++ ++currentCount;+ }++ // 第一圈還沒有參考資料,只負責記錄。+ if(referenceCount <= 1){+ deltaValid = false;+ return;+ }++ const int referenceIndex = findNearestReference(x, y, z);++ if(referenceIndex < 0){+ deltaValid = false;+ return;+ }++ lastReferenceIndex = referenceIndex;++ currentDelta =+ static_cast<float>(+ static_cast<int32_t>(lapTime) -+ static_cast<int32_t>(+ reference[referenceIndex].lapTime+ )+ ) / 1000.0f;++ deltaValid = true;++ /*+ * Delta P:+ *+ * 觀察 Delta 在參考圈索引上的變化速度,+ * 再把趨勢外推到終點。+ */+ if(previousReferenceIndex >= 0 &&+ referenceIndex > previousReferenceIndex)+ {+ const int indexDifference =+ referenceIndex - previousReferenceIndex;++ const float newSlope =+ (currentDelta - previousDelta) /+ static_cast<float>(indexDifference);++ // 平滑斜率,避免單一封包造成預測值劇烈跳動。+ deltaSlope =+ deltaSlope * 0.85f ++ newSlope * 0.15f;+ }++ const int remainingPoints =+ referenceCount - 1 - referenceIndex;++ projectedDelta =+ currentDelta ++ deltaSlope * static_cast<float>(remainingPoints);++ // 防止定位錯誤時出現非常離譜的數字。+ if(projectedDelta > 60.0f){+ projectedDelta = 60.0f;+ }else if(projectedDelta < -60.0f){+ projectedDelta = -60.0f;}- lastFuel=fuelLevel;- remaining=(avgFuelPerLap > 0.01f) ? fuelLevel/avgFuelPerLap : -1.0f;+ previousDelta = currentDelta;+ previousReferenceIndex = referenceIndex;}-float GT7FuelEstimator::remainingLaps() const{return remaining;}+void GT7DeltaTracker::endLap(uint32_t lapTime)+{+ // 圈無效、資料太少或時間不合理,不更新參考圈。+ if(lapTime == 0 || currentCount < 10){+ return;+ }++ const bool firstReference =+ referenceCount == 0;-void GT7DeltaTracker::reset(){currentDelta=0;projectedDelta=0;}-void GT7DeltaTracker::beginLap(){}-void GT7DeltaTracker::sample(float,float,float,uint32_t){}-void GT7DeltaTracker::endLap(uint32_t){}-float GT7DeltaTracker::delta() const{return currentDelta;}-float GT7DeltaTracker::deltaP() const{return projectedDelta;}+ const bool newBestLap =+ bestLapTime == 0 ||+ lapTime < bestLapTime;++ if(firstReference || newBestLap){+ referenceCount = currentCount;++ if(referenceCount > MAX_POINTS){+ referenceCount = MAX_POINTS;+ }++ for(int i = 0; i < referenceCount; ++i){+ reference[i] = current[i];+ }++ bestLapTime = lapTime;+ }+}++float GT7DeltaTracker::delta() const+{+ return deltaValid ? currentDelta : NAN;+}++float GT7DeltaTracker::deltaP() const+{+ return deltaValid ? projectedDelta : NAN;+}void GT7DerivedMetrics::reset(){fuel.reset();diff --git a/lib/GT7DerivedMetrics/GT7DerivedMetrics.h b/lib/GT7DerivedMetrics/GT7DerivedMetrics.hindex 440ada7..d251019 100644--- a/lib/GT7DerivedMetrics/GT7DerivedMetrics.h+++ b/lib/GT7DerivedMetrics/GT7DerivedMetrics.h@@ -48,15 +48,48 @@ class GT7DeltaTracker {public:void reset();void beginLap();- void sample(float x,float y,float z,uint32_t currentLapMs);- void endLap(uint32_t lapTimeMs);+ void sample(float x, float y, float z, uint32_t lapTime);+ void endLap(uint32_t lapTime);float delta() const;float deltaP() const;private:- float currentDelta = 0;- float projectedDelta = 0;+ struct DeltaPoint {+ float x;+ float y;+ float z;+ uint32_t lapTime;+ };++ static constexpr int MAX_POINTS = 1200;+ static constexpr int SAMPLE_DIVIDER = 5;++ DeltaPoint reference[MAX_POINTS];+ DeltaPoint current[MAX_POINTS];++ int referenceCount = 0;+ int currentCount = 0;+ int sampleCounter = 0;++ int lastReferenceIndex = 0;+ int previousReferenceIndex = -1;++ uint32_t bestLapTime = 0;++ float currentDelta = 0.0f;+ float projectedDelta = 0.0f;++ float previousDelta = 0.0f;+ float deltaSlope = 0.0f;++ bool deltaValid = false;++ int findNearestReference(float x, float y, float z);+ static float distanceSquared(+ float x1, float y1, float z1,+ float x2, float y2, float z2+ );};class GT7DerivedMetrics {diff --git a/src/SHCustomProtocol.h b/src/SHCustomProtocol.hindex 2263d3c..e3ee0a2 100644--- a/src/SHCustomProtocol.h+++ b/src/SHCustomProtocol.h@@ -105,8 +105,9 @@ private:bool hasReceivedGT7Packet = false;bool gt7CarOnTrack = false;- // GT7 衍生數據:ABS、剩餘油量圈數,以及之後的 Delta。+ // GT7 衍生數據:ABS 與剩餘油量圈數。GT7DerivedMetrics derivedMetrics;+#endifString formatLapTimeMs(int32_t milliseconds)@@ -133,6 +134,65 @@ private:return String(buffer);}+ String formatDeltaSeconds(float value)+ {+ if (isnan(value))+ {+ return "--";+ }++ char buffer[16];+ snprintf(buffer, sizeof(buffer), "%+.3f", value);+ return String(buffer);+ }++ String formatLastBestDifference(int32_t lastLapMs, int32_t bestLapMs)+ {+ if (lastLapMs <= 0 || bestLapMs <= 0)+ {+ return "--";+ }++ const float differenceSeconds =+ static_cast<float>(lastLapMs - bestLapMs) / 1000.0f;++ return formatDeltaSeconds(differenceSeconds);+ }++ int32_t parseLapTimeStringMs(String value)+ {+ value.trim();++ if (value.length() == 0 || value.indexOf('-') >= 0)+ {+ return -1;+ }++ const int colon = value.indexOf(':');+ const int dot = value.lastIndexOf('.');++ if (colon < 0 || dot < colon)+ {+ return -1;+ }++ const int32_t minutes = value.substring(0, colon).toInt();+ const int32_t seconds = value.substring(colon + 1, dot).toInt();+ String millisText = value.substring(dot + 1);++ while (millisText.length() < 3)+ {+ millisText += "0";+ }++ if (millisText.length() > 3)+ {+ millisText = millisText.substring(0, 3);+ }++ return minutes * 60000L + seconds * 1000L + millisText.toInt();+ }+// Connecting 畫面狀態bool connectingScreenActive = false;// int connectingDotIndex = 0;@@ -263,15 +323,17 @@ public:lastLapTime = formatLapTimeMs(data.lastLaptime);bestLapTime = formatLapTimeMs(data.bestLaptime);- // GT7DeltaTracker 目前只有介面骨架,尚未建立參考圈資料。- // 在完成真正的位置比對前維持 --,避免顯示假的 0.000。- sessionBestLiveDeltaSeconds = "--";- sessionBestLiveDeltaProgressSeconds = "--";-// 圈數const int currentLap = max(0, static_cast<int>(data.lapCount));const int totalLapCount = max(0, static_cast<int>(data.totalLaps));+ // 上一圈與最佳圈的圈速差。新最佳圈會顯示 +0.000。+ sessionBestLiveDeltaSeconds =+ formatLastBestDifference(data.lastLaptime, data.bestLaptime);++ // 此欄位不再顯示 Delta P,畫面改為油門/煞車圖條。+ sessionBestLiveDeltaProgressSeconds = "";+if (totalLapCount > 0){tyrePressureRearLeft = String(currentLap) + "/" + String(totalLapCount);@@ -384,6 +446,11 @@ public:sessionBestLiveDeltaProgressSeconds =FlowSerialReadStringUntil(';');+ // Delta 欄改為上一圈-最佳圈;仍讀取舊欄位以維持 Protocol 相容。+ sessionBestLiveDeltaSeconds = formatLastBestDifference(+ parseLapTimeStringMs(lastLapTime),+ parseLapTimeStringMs(bestLapTime));+// 10:預估剩餘油量圈數tyrePressureFrontLeft =FlowSerialReadStringUntil(';');@@ -841,13 +908,42 @@ public:// Third Column (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, 4, forceUpdate);- drawCell(SCREEN_WIDTH, ROW[2], sessionBestLiveDeltaProgressSeconds, "sessionBestLiveDeltaProgressSeconds", "Delta P", "right", sessionBestLiveDeltaProgressSeconds.indexOf('-') >= 0 ? TFT_GREEN : TFT_RED, 4, forceUpdate);+ // 右上:上一圈相對最佳圈的差值。+ uint16_t lastBestColor = TFT_WHITE;+ if (sessionBestLiveDeltaSeconds != "--")+ {+ lastBestColor = sessionBestLiveDeltaSeconds.startsWith("+") &&+ sessionBestLiveDeltaSeconds != "+0.000"+ ? TFT_RED+ : TFT_GREEN;+ }++ drawCell(+ SCREEN_WIDTH,+ ROW[1],+ sessionBestLiveDeltaSeconds,+ "lastBestDifference",+ "LAST-BEST",+ "right",+ lastBestColor,+ 4,+ forceUpdate);- // 油門、煞車、油量- const uint16_t throttleColor = isActiveValue(tcActive) ? TFT_RED : TFT_YELLOW;- const uint16_t brakeColor = isActiveValue(absActive) ? TFT_RED : TFT_BLUE;+ // 右中:油門與煞車改成上下兩個獨立格,整個格子本身就是 progress。+ drawPedalProgressCells(+ SCREEN_WIDTH - CELL_WIDTH * 2 + 1,+ ROW[2],+ CELL_WIDTH * 2 - 2,+ CELL_HEIGHT - 1,+ constrain(tcLevel.toInt(), 0, 100),+ constrain(absLevel.toInt(), 0, 100),+ forceUpdate);++ // ABS、TCS 狀態與油量+ const bool tcsOn = isActiveValue(tcActive);+ const bool absOn = isActiveValue(absActive);+ const uint16_t tcsColor = tcsOn ? TFT_YELLOW : TFT_DARKGREY;+ const uint16_t absColor = absOn ? TFT_RED : TFT_DARKGREY;float fuelPercentValue = fuelAlertActive.toFloat();// Fuel@@ -875,12 +971,28 @@ public:fuelStatusColor = TFT_RED;}- if (isTCCutNull == "False")- drawCell(COL[0], ROW[4], tcTcCut, "tcTcCut", "TC TC2", "center", throttleColor, 4, forceUpdate);- else- drawCell(COL[0], ROW[4], tcLevel, "tcLevel", "THR", "center", throttleColor, 4, forceUpdate);+ // 左下兩格:保留 ABS/TCS 標題,使用狀態圓燈取代大型 ON/OFF 文字。+ drawStatusIndicatorCell(+ COL[0],+ ROW[4],+ CELL_WIDTH,+ CELL_HEIGHT,+ "ABS",+ "absStatus",+ absOn,+ TFT_RED,+ forceUpdate);- drawCell(COL[1], ROW[4], absLevel, "absLevel", "BRK", "center", brakeColor, 4, forceUpdate);+ drawStatusIndicatorCell(+ COL[1],+ ROW[4],+ CELL_WIDTH,+ CELL_HEIGHT,+ "TCS",+ "tcsStatus",+ tcsOn,+ TFT_RED,+ forceUpdate);drawCell(COL[2], ROW[4], brakeBias, "brakeBias", "FUEL", "center", TFT_MAGENTA, 4, forceUpdate);// 剩餘圈數、位置、圈數、低油量警示@@ -1313,6 +1425,130 @@ public:prevColor[id] = color;}+ void drawStatusIndicatorCell(+ int32_t x,+ int32_t y,+ int32_t width,+ int32_t height,+ const String &label,+ const String &id,+ bool active,+ uint16_t activeColor,+ bool forceUpdate = false)+ {+ const String state = active ? "1" : "0";+ if (!forceUpdate && prevData[id] == state)+ {+ return;+ }++ tft.fillRect(x, y, width, height, TFT_BLACK);+ tft.drawRoundRect(x, y, width, height, 5, TFT_DARKGREY);++ tft.setTextPadding(0);+ tft.setTextDatum(TC_DATUM);+ tft.setTextColor(TFT_WHITE, TFT_BLACK);+ tft.drawString(label, x + width / 2, y + 3, 2);++ const int32_t radius = 7;+ const int32_t centerX = x + width / 2;+ const int32_t centerY = y + 31;+ const uint16_t lampColor = active ? activeColor : TFT_DARKGREY;++ tft.fillCircle(centerX, centerY, radius, lampColor);+ tft.drawCircle(centerX, centerY, radius + 1, active ? TFT_WHITE : TFT_DARKGREY);++ // Restore the default text state so later cells are not shifted.+ tft.setTextDatum(TL_DATUM);+ tft.setTextPadding(0);++ prevData[id] = state;+ }++ void drawPedalProgressCells(+ int32_t x,+ int32_t y,+ int32_t totalWidth,+ int32_t totalHeight,+ int throttlePercent,+ int brakePercent,+ bool forceUpdate = false)+ {+ throttlePercent = constrain(throttlePercent, 0, 100);+ brakePercent = constrain(brakePercent, 0, 100);++ // 上下兩格,而不是左右兩格。+ const int32_t gap = 2;+ const int32_t cellHeight = (totalHeight - gap) / 2;+ const int32_t throttleY = y;+ const int32_t brakeY = y + cellHeight + gap;++ auto updateOneCell = [&](+ int32_t cellY,+ int percent,+ const char *id,+ uint16_t color)+ {+ const bool firstDraw = prevData.count(id) == 0;+ const int oldPercent = firstDraw+ ? 0+ : constrain(prevData[id].toInt(), 0, 100);++ if (!forceUpdate && !firstDraw && oldPercent == percent)+ {+ return;+ }++ // Progress 幾乎填滿整個 cell,只留下 2 px 邊框。+ const int32_t inset = 2;+ const int32_t fillX = x + inset;+ const int32_t fillY = cellY + inset;+ const int32_t fillWidthMax = totalWidth - inset * 2;+ const int32_t fillHeight = cellHeight - inset * 2;+ const int32_t oldFill = fillWidthMax * oldPercent / 100;+ const int32_t newFill = fillWidthMax * percent / 100;++ // 第一次只建立格子;之後僅更新變動區段,避免閃爍。+ if (firstDraw || forceUpdate)+ {+ tft.fillRect(x, cellY, totalWidth, cellHeight, TFT_BLACK);+ tft.drawRoundRect(x, cellY, totalWidth, cellHeight, 5, TFT_DARKGREY);++ if (newFill > 0)+ {+ tft.fillRect(fillX, fillY, newFill, fillHeight, color);+ }+ }+ else if (newFill > oldFill)+ {+ // 增加時只補上新增的區段。+ tft.fillRect(+ fillX + oldFill,+ fillY,+ newFill - oldFill,+ fillHeight,+ color);+ }+ else if (newFill < oldFill)+ {+ // 減少時只擦掉多出的區段。+ tft.fillRect(+ fillX + newFill,+ fillY,+ oldFill - newFill,+ fillHeight,+ TFT_BLACK);+ }++ // 保險重畫邊框,避免填色蓋到外框。+ tft.drawRoundRect(x, cellY, totalWidth, cellHeight, 5, TFT_DARKGREY);+ prevData[id] = String(percent);+ };++ updateOneCell(throttleY, throttlePercent, "pedalThrottle", TFT_YELLOW);+ updateOneCell(brakeY, brakePercent, "pedalBrake", TFT_BLUE);+ }+void drawCell(int32_t x,int32_t y,@@ -1324,6 +1560,10 @@ public:uint8_t fontSize,bool forceUpdate = false){+ // Each normal cell sets its own text origin, independent of prior widgets.+ tft.setTextDatum(TL_DATUM);+ tft.setTextPadding(0);+const bool dataChanged =forceUpdate || prevData[id] != data;