-2

So I am trying to create a LED strobe light and I have managed to make a on/off switch for the light. Here is my code:

@implementation ViewController
- (void) setTorchOn:(BOOL)isOn
{
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];

}

-(IBAction)changedSate:(id)sender {
UISwitch *switchValue = (UISwitch*)sender;

[self setTorchOn:[switchValue isOn]];

I was wondering if anyone could help me with this part.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82

2 Answers2

0

Just make a loop that continuously turns the torch on and off. The type of loops depends on how you wish this to be implemented.

G_Money
  • 318
  • 7
  • 22
0

I think you should use the NSTimer class to repeatedly toggle the torch. There are other ways, but just do not loop with a sleep() call.

// Have an NSTimer* timer and BOOL torchOn and volatile BOOL stopStrobe property in your class...

- (void) startFlashing{
self.timer = [[NSTimer alloc] initWithFireDate:[NSDate timeInvervalSinceNow: 0] interval:0.1 target:self selector:@selector(toggleTorch) userInfo:nil repeats:YES];
}

- (void) toggleTorch{
   if (stopStrobe){
      [self.timer invalidate];
   }
   torchOn = !torchOn
   [self setTorchOn:torchOn];
   }

// Set stopStrobe to YES elsewhere in your program when you want it to stop.

is probably what you're looking for.

UPDATE: I know this isn't what you originally asked, but I know it's often best to learn by example, so here is the full example of using this (untested):

@interface ViewController()
@property(nonatomic) BOOL torchOn;
@property(atomic) BOOL stopStrobe;
@end

@implementation ViewController
- (id) init{
self = [super init];
if (self){
self.torchOn = NO;
self.stopStrobe = NO;
}
}

- (void) setTorchOn:(BOOL)isOn
{
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}

- (void) toggleTorch{
if (stopStrobe){
[self.timer invalidate];
}
self.torchOn = !self.torchOn
[self setTorchOn:self.torchOn];
}

- (void) startFlashing{
self.timer = [[NSTimer alloc] initWithFireDate:[NSDate timeInvervalSinceNow: 0] interval:0.1 target:self selector:@selector(toggleTorch) userInfo:nil repeats:YES];
}

-(IBAction)changedSate:(id)sender {
UISwitch *switchValue = (UISwitch*)sender;
if ([switchValue isOn]{
self.stopStrobe = NO;
[self startFlashing];
}
else{
[self.stopStrobe = YES];
}
}

This will start the flashing whenever you turn the switch on and stop it once you turn the switch off.

sudo
  • 5,604
  • 5
  • 40
  • 78
  • Could you show me the full example with the completed code? – user3658255 May 21 '14 at 19:08
  • @user3658255 You should find some way for the user to make your program call startFlashing (to start) or self.stopStrobe = YES (to stop). I can give a full example once I get home, but that's a general Objective-C function structure question at that point. – sudo May 21 '14 at 21:40
  • @user3658255 yes that would be really helpful if you could give me a full example – user3658255 May 23 '14 at 18:05