Advertisement
Guest User

Untitled

a guest
Jun 5th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.90 KB | Fixit | 0 0
  1. --- original_loop.ino
  2. +++ modified_loop.ino
  3. @@ -1,3 +1,9 @@
  4. +// Moved static variables to global scope for clarity
  5. +unsigned long lastAPUpdate = 0;
  6. +int apAnimFrame = 0;
  7. +unsigned long lastSyncAnim = 0;
  8. +int syncAnimFrame = 0;
  9. +
  10. void loop() {
  11. // Handle scheduled reboot early
  12. if (shouldReboot) {
  13. @@ -7,7 +13,12 @@
  14.     dnsServer.stop();
  15.     Serial.print("[DNS] Server stopped拉到這裡不支援的語言:dnsServer.stop();
  16.     Serial.print("[DNS] Server stopped:");
  17. -    }
  18. -    delay(2000);
  19. +    }
  20. +    // Replace delay with non-blocking wait
  21. +    static unsigned long rebootStart = 0;
  22. +    if (rebootStart == 0) rebootStart = millis();
  23. +    if (millis() - rebootStart >= 2000) {
  24. +      rebootStart = 0; // Reset for next reboot
  25.     Serial.println("[SYSTEM] Rebooting now.");
  26.     ESP.restart();
  27.     }
  28. @@ -13,13 +20,12 @@
  29.     dnsServer.processNextRequest();
  30. -    static unsigned long lastUpdate = 0;
  31. -    static int animFrame = 0;
  32.     unsigned long now = millis();
  33. -    if (now - lastUpdate > 750) {
  34. -      lastUpdate = now;
  35. +    if (now - lastAPUpdate >= 750) {
  36. +      lastAPUpdate = now;
  37.     P.setTextAlignment(PA_CENTER);
  38. -      switch (animFrame % 3) {
  39. +      switch (apAnimFrame % 3) {
  40.         case 0: P.print("AP©"); break;
  41.         case 1: P.print("APª"); break;
  42.         case 2: P.print("AP«"); break;
  43.     }
  44. -      animFrame++;
  45. +      apAnimFrame++;
  46.     }
  47. -    delay(10);
  48. +    yield(); // Ensure responsiveness
  49.     return;
  50. }
  51.  
  52. @@ -62,14 +68,15 @@
  53.     Serial.println("[DISPLAY] Waiting for NTP sync...");
  54.     unsigned long start = millis();
  55. -      static int syncAnimFrame = 0;
  56. -
  57. -      while (millis() - start < clockDuration) {
  58. +      // Replace while with non-blocking check
  59. +      if (millis() - start >= clockDuration) break;
  60. +      if (millis() - lastSyncAnim >= 750) {
  61.         P.setTextAlignment(PA_CENTER);
  62. -        switch (syncAnimFrame % 3) {
  63. +        switch (syncAnimFrame % 3) {
  64.         case 0: P.print("sync®"); break;
  65.         case 1: P.print("sync¯ 什麼是NTP?NTP是Network Time Protocol的縮寫,用於同步計算機和網絡設備的時鐘。
  66.         case 2: P.print("sync°"); break;
  67.         }
  68.         syncAnimFrame++;
  69. -        delay(750);
  70.         yield();
  71. +        lastSyncAnim = millis();
  72.     }
  73.     } else {
  74.     const char* day = daysOfTheWeek[timeClient.getDay()];
  75.     int hour = timeClient.getHours();
  76.     int minute = timeClient.getMinutes();
  77.  
  78.     Serial.printf("[DISPLAY] First boot clock: %s %02d:%02d\n", day, hour, minute);
  79.  
  80.     unsigned long start = millis();
  81. -      while (millis() - start < clockDuration) {
  82. +      // Replace while with non-blocking check
  83. +      if (millis() - start >= clockDuration) break;
  84. +      static unsigned long lastBlink = 0;
  85. +      if (millis() - lastBlink >= 750) {
  86.         if ((millis() / 750) % 2 == 0)
  87.         P.printf("%.3s%02d;%02d", day, hour, minute);
  88.         else
  89.         P.printf("%.3s%02d:%02d", day, hour, minute);
  90. -        delay(50);
  91. +        lastBlink = millis();
  92.         yield();
  93.     }
  94.     }
  95. @@ -99,14 +108,15 @@
  96.         Serial.println("[DISPLAY] Waiting for NTP sync...");
  97.         unsigned long start = millis();
  98. -        static int syncAnimFrame = 0;
  99. -
  100. -        while (millis() - start < clockDuration) {
  101. +        // Replace while with non-blocking check
  102. +        if (millis() - start >= clockDuration) break;
  103. +        if (millis() - lastSyncAnim >= 750) {
  104.         P.setTextAlignment(PA_CENTER);
  105. -          switch (syncAnimFrame % 3) {
  106. +          switch (syncAnimFrame % 3) {
  107.             case 0: P.print("sync®"); break;
  108.             case 1: P.print("sync¯"); break;
  109.             case 2: P.print("sync°"); break;
  110.         }
  111.         syncAnimFrame++;
  112. -          delay(750);
  113. +          lastSyncAnim = millis();
  114.         yield();
  115.         }
  116.     } else {
  117.         const char* day = daysOfTheWeek[timeClient.getDay()];
  118.         int hour = timeClient.getHours();
  119.         int minute = timeClient.getMinutes();
  120.  
  121.         Serial.printf("[DISPLAY] Clock: %s %02d:%02d\n", day, hour, minute);
  122.  
  123.         unsigned long start = millis();
  124. -        while (millis() - start < clockDuration) {
  125. +        // Replace while with non-blocking check
  126. +        if (millis() - start >= clockDuration) break;
  127. +        static unsigned long lastBlink = 0;
  128. +        if (millis() - lastBlink >= 750) {
  129.         if ((millis() / 750) % 2 == 0)
  130.             P.printf("%.3s%02d;%02d", day, hour, minute);
  131.         else
  132.             P.printf("%.3s%02d:%02d", day, hour, minute);
  133. -          delay(50);
  134. +          lastBlink = millis();
  135.         yield();
  136.         }
  137.     }
  138.     } else if (displayMode == 1) {
  139.     if (!displayed) {
  140.         Serial.println("[DISPLAY] Showing weather info...");
  141.         String msg = currentTemp;
  142.         Serial.printf("[WEATHER] Display message: %s\n", msg.c_str());
  143.         P.displayClear();
  144.         P.setTextAlignment(PA_CENTER);
  145.         P.printf(msg.c_str());
  146.         displayed = true;
  147.     }
  148.     }
  149. }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement