| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #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>
- ShutterController::ShutterController(uint8_t focusPin, uint8_t shutterPin)
- : focusPin(focusPin), shutterPin(shutterPin), isFocusPressed(false)
- {
- }
- /// <summary>
- /// Performs a half press of the shutter to initiate focus.
- /// Sets the focus pin HIGH and marks focus as pressed.
- /// </summary>
- void ShutterController::halfPress()
- {
- digitalWrite(focusPin, HIGH);
- isFocusPressed = true;
- }
- /// <summary>
- /// Performs a full press of the shutter to take a photo.
- /// Automatically half-presses the shutter if not already pressed.
- /// Sets the shutter pin HIGH to simulate a full button press.
- /// </summary>
- void ShutterController::fullPress()
- {
- if (!isFocusPressed)
- {
- halfPress();
- nonBlockingDelay(1000);
- }
-
- digitalWrite(shutterPin, HIGH);
- }
- /// <summary>
- /// Presses the shutter fully, waits for the specified delay, then releases.
- /// Automatically returns to neutral position after the delay.
- /// </summary>
- /// <param name="ms">Delay in milliseconds between full press and release</param>
- void ShutterController::pressFullWithDelay(int ms)
- {
- fullPress();
- nonBlockingDelay(ms);
- unPress();
- }
- /// <summary>
- /// Releases the shutter and returns to neutral position.
- /// Sets both focus and shutter pins LOW and updates the focus state.
- /// </summary>
- void ShutterController::unPress()
- {
- digitalWrite(shutterPin, LOW);
- digitalWrite(focusPin, LOW);
- isFocusPressed = false;
- }
- /// <summary>
- /// Takes multiple photos in sequence with configurable timing.
- /// Ensures focus is locked before taking photos, then executes the sequence.
- /// </summary>
- /// <param name="ms">Duration of each full press in milliseconds</param>
- /// <param name="count">Number of photos to take</param>
- /// <param name="delayMs">Delay between consecutive shots in milliseconds</param>
- void ShutterController::multipleShoot(int ms, int count, int delayMs)
- {
- if (!isFocusPressed)
- {
- halfPress();
- nonBlockingDelay(1000);
- }
-
- while (count > 0)
- {
- fullPress();
- nonBlockingDelay(ms);
- digitalWrite(shutterPin, LOW);
- count--;
- if (count > 0)
- {
- nonBlockingDelay(delayMs);
- }
- }
- }
- /// <summary>
- /// Returns the current focus press state.
- /// </summary>
- /// <returns>True if focus is currently pressed, false otherwise</returns>
- bool ShutterController::getFocusPressedState() const
- {
- return isFocusPressed;
- }
|