3

i saw the post about using the iPhone 4/4s flash led as a torch. I found it very useful, i tried to use it for quickly turn on/off the led so to use it as a morse transmitter, but it doesn't work. It is too slow for this use, following the code i use:

-(void)toggleTorch
{
    AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // check if the device has the torch
    if([_device hasTorch] && [_device hasFlash])
    {
        if (_device.torchMode == AVCaptureTorchModeOff) 
        {            
            // we want to turn the torch on
            AVCaptureDeviceInput *_flashInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
            AVCaptureVideoDataOutput *_output = [[AVCaptureVideoDataOutput alloc] init];

            AVCaptureSession *_session = [[AVCaptureSession alloc] init];
            [_session beginConfiguration];
            [_device lockForConfiguration:nil];

             [_session addInput:_flashInput];
             [_session addOutput:_output];

            [_device unlockForConfiguration];

            //[_output release];
            [_session commitConfiguration];
            [_session startRunning];

            [self setTorchSession:_session];
        }
        else 
        {            
            [self.torchSession stopRunning];
        }
    }
}

// turn the torch on/off
-(IBAction)toggleTorch:(id)sender
{
    AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // check if the device has the torch
    if([_device hasTorch] && [_device hasFlash])
    {
        if (_device.torchMode == AVCaptureTorchModeOff) 
        {
            [self switchTorchON];
        }
        else 
        {
            [self switchTorchOFF];
        }
    }
}

-(void)switchTorchON
{
    [NSThread detachNewThreadSelector:@selector(changeSwitchImage) 
                             toTarget:self 
                           withObject:nil];

    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        [device lockForConfiguration:nil];

        [device setTorchMode:AVCaptureTorchModeOn];
        [device setFlashMode:AVCaptureFlashModeOn];

        [device unlockForConfiguration];

    }
}
-(void)switchTorchOFF
{
    [NSThread detachNewThreadSelector:@selector(changeSwitchImage) 
                             toTarget:self 
                           withObject:nil];
    // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        [device lockForConfiguration:nil];

        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];

        [device unlockForConfiguration];

    }   
}

-(IBAction)toggleSOS:(id)sender
{
    // morse SOS: ...---...
    [self switchTorchON];
    [self switchTorchOFF];

    [self switchTorchON];
    [self switchTorchOFF];

    [self switchTorchON];
    [self switchTorchOFF];

}

when i push on the sos button i see just a flash. Anyone may help me?

cursao
  • 53
  • 3
  • 2
    What post are you referring to? – BoltClock Jan 22 '12 at 16:46
  • possible duplicate of [How can I make the iPhone 4 LED light fire instantly?](http://stackoverflow.com/questions/3983032/how-can-i-make-the-iphone-4-led-light-fire-instantly) – hotpaw2 Jan 22 '12 at 19:08

2 Answers2

4

The method you are using is indeed very slow. Use the method found in this Stackoverflow link instead.

The actual on/off code is:

[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];

But make sure myDevice is initialized (see the link)

Implement it with NStimer to create a flash length of however long you want.

EDIT:

Sorry, I assumed that you were trying to gather the morse code input and then instructing the flash to toggle through NSTimer.

Try using NSTimer or sleep the thread to increase the time in between the intervals of the flashing. It might be executing too fast for the actual flash component to handle. Try this on the SOS method for easiness of testing.

Community
  • 1
  • 1
dgund
  • 3,459
  • 4
  • 39
  • 64
  • maybe there's something i misunderstood, but if you look at my switchTorchOn method it's the same you wrote, except for some check. I've to change -(void)toggleTorch??? – cursao Jan 24 '12 at 13:12
2

I tried with NSTimer, but it didn't work.
Finally I solved using performSelector:withObject:afterDelay:

oers
  • 18,436
  • 13
  • 66
  • 75
cursao
  • 53
  • 3