Procházet zdrojové kódy

#fix wdt errors on photo with delay and multiple shoots

faqmoroz před 1 týdnem
rodič
revize
c941919e4c
2 změnil soubory, kde provedl 41 přidání a 5 odebrání
  1. 34 5
      ShutterController.cpp
  2. 7 0
      ShutterController.h

+ 34 - 5
ShutterController.cpp

@@ -1,6 +1,32 @@
 #include "ShutterController.h"
 #include <Arduino.h>
 
+/// <summary>
+/// Non-blocking delay that yields to WiFi stack regularly.
+/// For delays < 2 seconds, uses blocking delay directly.
+/// For longer delays, breaks into 2-second chunks and feeds the watchdog.
+/// </summary>
+void ShutterController::nonBlockingDelay(unsigned long ms)
+{
+    if (ms < 1000)
+    {
+        delay(ms);
+        return;
+    }
+    
+    unsigned long startTime = millis();
+    unsigned long remaining = 0;
+    unsigned long delayTime = 0;
+    
+    while (millis() - startTime < ms)
+    {
+        remaining = ms - (millis() - startTime);
+        delayTime = (remaining < 1000) ? remaining : 1000;
+        delay(delayTime);
+        ESP.wdtFeed();
+    }
+}
+
 /// <summary>
 /// Constructs a ShutterController instance with specified GPIO pins.
 /// </summary>
@@ -29,7 +55,7 @@ void ShutterController::fullPress()
     if (!isFocusPressed)
     {
         halfPress();
-        delay(1000);
+        nonBlockingDelay(1000);
     }
     
     digitalWrite(shutterPin, HIGH);
@@ -43,7 +69,7 @@ void ShutterController::fullPress()
 void ShutterController::pressFullWithDelay(int ms)
 {
     fullPress();
-    delay(ms);
+    nonBlockingDelay(ms);
     unPress();
 }
 
@@ -70,16 +96,19 @@ void ShutterController::multipleShoot(int ms, int count, int delayMs)
     if (!isFocusPressed)
     {
         halfPress();
-        delay(1000);
+        nonBlockingDelay(1000);
     }
     
     while (count > 0)
     {
         fullPress();
-        delay(ms);
+        nonBlockingDelay(ms);
         digitalWrite(shutterPin, LOW);
         count--;
-        delay(delayMs);
+        if (count > 0)
+        {
+            nonBlockingDelay(delayMs);
+        }
     }
 }
 

+ 7 - 0
ShutterController.h

@@ -61,6 +61,13 @@ private:
     /// <summary>Flag indicating whether focus is currently pressed</summary>
     bool isFocusPressed;
     
+    /// <summary>
+    /// Non-blocking delay that yields to WiFi stack regularly.
+    /// Breaks long delays into small chunks to prevent Soft WDT reset.
+    /// </summary>
+    /// <param name="ms">Delay duration in milliseconds</param>
+    void nonBlockingDelay(unsigned long ms);
+    
 public:
     /// <summary>
     /// Constructs a ShutterController instance.