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