Dodany przez: klima, 22:08 10-07-2025

Nowy Pobierz
  1. // Struktura wyniku softstartu
  2. struct SoftUpdateResult {
  3.   int value;         // Zaktualizowana wartość
  4.   uint8_t state;     // 0 = osiągnięto, 1 = rośnie, 2 = maleje
  5.   float progressPercent; // Procent pozostały do osiągnięcia celu
  6. };
  7.  
  8. // Struktura stanu softstartu
  9. struct SoftUpdateState {
  10.   unsigned long lastUpdate; // Czas ostatniej aktualizacji (ms)
  11.   int currentValue;        // Aktualna wartość
  12.   bool active;             // Czy softstart jest aktywny
  13.   int initialDelta;        // Początkowa różnica między wartościami
  14. };
  15.  
  16. Pasztet::
  17.  
  18. SoftUpdateResult softUpdate(SoftUpdateState &state, int targetValue, int stepSize, float totalTimeSec, bool debug) {
  19.   SoftUpdateResult result = { state.currentValue, 0, 0.0 }; // Domyślnie: cel osiągnięty
  20.  
  21.   // Oblicz procent postępu
  22.   int delta = abs(targetValue - state.currentValue);
  23.   if (state.initialDelta == 0) {
  24.     result.progressPercent = 0.0; // Brak różnicy początkowej
  25.   } else {
  26.     result.progressPercent = (float)delta / state.initialDelta * 100.0; // Procent pozostały
  27.   }
  28.  
  29.   // Logowanie w trybie japierdole dlaczego to nie dziala ?!
  30.   Serial.printf("Debug: delta=%d, initialDelta=%d, progressPercent=%.1f%%\n", delta, state.initialDelta, result.progressPercent);
  31.  
  32.   // Jeśli wartości są równe, nie ma potrzeby aktualizacji
  33.   if (state.currentValue == targetValue) {
  34.     state.active = false;
  35.     result.state = 0;
  36.     result.progressPercent = 0.0;
  37.     return result;
  38.   }
  39.  
  40.   // Oblicz liczbę kroków i interwał czasowy
  41.   int totalSteps = (delta + stepSize - 1) / stepSize; // Zaokrąglenie w górę
  42.   unsigned long intervalMs = max(50UL, (unsigned long)((totalTimeSec * 1000.0) / totalSteps)); // Min 50ms
  43.  
  44.   unsigned long now = millis();
  45.   if (!state.active || now - state.lastUpdate >= intervalMs) {
  46.     state.lastUpdate = now;
  47.     state.active = true;
  48.  
  49.     if (state.currentValue < targetValue) {
  50.       state.currentValue += stepSize;
  51.       if (state.currentValue > targetValue) state.currentValue = targetValue;
  52.       result.state = 1; // Rośnie
  53.     } else if (state.currentValue > targetValue) {
  54.       state.currentValue -= stepSize;
  55.       if (state.currentValue < targetValue) state.currentValue = targetValue;
  56.       result.state = 2; // Maleje
  57.     }
  58.  
  59.     result.value = state.currentValue;
  60.     if (state.currentValue == targetValue) {
  61.       result.state = 0; // Osiągnięto
  62.       state.active = false;
  63.       result.progressPercent = 0.0; // Cel osiągnięty
  64.     } else {
  65.       // Zaktualizuj procent postępu
  66.       delta = abs(targetValue - state.currentValue);
  67.       if (state.initialDelta == 0) {
  68.         result.progressPercent = 0.0; // Zabezpieczenie
  69.       } else {
  70.         result.progressPercent = (float)delta / state.initialDelta * 100.0;
  71.       }
  72.     }
  73.  
  74.     // Logowanie generalne...
  75.     Serial.printf("SoftUpdate: Wartość=%d, Stan=%s, Pozostało=%.1f%%, Interwał=%lu ms\n",
  76.                   result.value,
  77.                   result.state == 0 ? "Osiągnięto" : (result.state == 1 ? "Rośnie" : "Maleje"),
  78.                   result.progressPercent,
  79.                   intervalMs);
  80.   }
  81.  
  82.   return result;
  83. }

Źródło:

Ostatnie wpisy

Linki

Funkcje