ShutterController.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "ShutterController.h"
  2. #include <Arduino.h>
  3. /// <summary>
  4. /// Non-blocking delay that yields to WiFi stack regularly.
  5. /// For delays < 2 seconds, uses blocking delay directly.
  6. /// For longer delays, breaks into 2-second chunks and feeds the watchdog.
  7. /// </summary>
  8. void ShutterController::nonBlockingDelay(unsigned long ms)
  9. {
  10. if (ms < 1000)
  11. {
  12. delay(ms);
  13. return;
  14. }
  15. unsigned long startTime = millis();
  16. unsigned long remaining = 0;
  17. unsigned long delayTime = 0;
  18. while (millis() - startTime < ms)
  19. {
  20. remaining = ms - (millis() - startTime);
  21. delayTime = (remaining < 1000) ? remaining : 1000;
  22. delay(delayTime);
  23. ESP.wdtFeed();
  24. }
  25. }
  26. /// <summary>
  27. /// Constructs a ShutterController instance with specified GPIO pins.
  28. /// </summary>
  29. ShutterController::ShutterController(uint8_t focusPin, uint8_t shutterPin)
  30. : focusPin(focusPin), shutterPin(shutterPin), isFocusPressed(false)
  31. {
  32. }
  33. /// <summary>
  34. /// Performs a half press of the shutter to initiate focus.
  35. /// Sets the focus pin HIGH and marks focus as pressed.
  36. /// </summary>
  37. void ShutterController::halfPress()
  38. {
  39. digitalWrite(focusPin, HIGH);
  40. isFocusPressed = true;
  41. }
  42. /// <summary>
  43. /// Performs a full press of the shutter to take a photo.
  44. /// Automatically half-presses the shutter if not already pressed.
  45. /// Sets the shutter pin HIGH to simulate a full button press.
  46. /// </summary>
  47. void ShutterController::fullPress()
  48. {
  49. if (!isFocusPressed)
  50. {
  51. halfPress();
  52. nonBlockingDelay(1000);
  53. }
  54. digitalWrite(shutterPin, HIGH);
  55. }
  56. /// <summary>
  57. /// Presses the shutter fully, waits for the specified delay, then releases.
  58. /// Automatically returns to neutral position after the delay.
  59. /// </summary>
  60. /// <param name="ms">Delay in milliseconds between full press and release</param>
  61. void ShutterController::pressFullWithDelay(int ms)
  62. {
  63. fullPress();
  64. nonBlockingDelay(ms);
  65. unPress();
  66. }
  67. /// <summary>
  68. /// Releases the shutter and returns to neutral position.
  69. /// Sets both focus and shutter pins LOW and updates the focus state.
  70. /// </summary>
  71. void ShutterController::unPress()
  72. {
  73. digitalWrite(shutterPin, LOW);
  74. digitalWrite(focusPin, LOW);
  75. isFocusPressed = false;
  76. }
  77. /// <summary>
  78. /// Takes multiple photos in sequence with configurable timing.
  79. /// Ensures focus is locked before taking photos, then executes the sequence.
  80. /// </summary>
  81. /// <param name="ms">Duration of each full press in milliseconds</param>
  82. /// <param name="count">Number of photos to take</param>
  83. /// <param name="delayMs">Delay between consecutive shots in milliseconds</param>
  84. void ShutterController::multipleShoot(int ms, int count, int delayMs)
  85. {
  86. if (!isFocusPressed)
  87. {
  88. halfPress();
  89. nonBlockingDelay(1000);
  90. }
  91. while (count > 0)
  92. {
  93. fullPress();
  94. nonBlockingDelay(ms);
  95. digitalWrite(shutterPin, LOW);
  96. count--;
  97. if (count > 0)
  98. {
  99. nonBlockingDelay(delayMs);
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Returns the current focus press state.
  105. /// </summary>
  106. /// <returns>True if focus is currently pressed, false otherwise</returns>
  107. bool ShutterController::getFocusPressedState() const
  108. {
  109. return isFocusPressed;
  110. }