Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔦 Add torch (flashlight) control #7

Open
superturboryan opened this issue Nov 18, 2024 · 0 comments · May be fixed by #8
Open

🔦 Add torch (flashlight) control #7

superturboryan opened this issue Nov 18, 2024 · 0 comments · May be fixed by #8
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Milestone

Comments

@superturboryan
Copy link
Owner

Overview

Let's add a way to control the torch on the device.

This could go under a new namespace Control.Flashlight 💡

Tactics

Thanks ChatGPT 🤖

Check flashlight availability

import AVFoundation

if let device = AVCaptureDevice.default(for: .video), device.hasTorch {
    print("Torch is available")
} else {
    print("Torch is not available")
}

Turn the Flashlight on or off

func toggleFlashlight(on: Bool) {
    guard let device = AVCaptureDevice.default(for: .video), device.hasTorch else {
        print("Torch is not available")
        return
    }
    
    do {
        try device.lockForConfiguration()
        if on {
            try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel)
        } else {
            device.torchMode = .off
        }
        device.unlockForConfiguration()
    } catch {
        print("Error accessing the torch: \(error.localizedDescription)")
    }
}

Testing

  • Unit tests? 🧪🧪
@superturboryan superturboryan added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Nov 18, 2024
@superturboryan superturboryan linked a pull request Nov 24, 2024 that will close this issue
@superturboryan superturboryan added this to the 0.0.3 milestone Nov 24, 2024
@superturboryan superturboryan linked a pull request Nov 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant