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

Image ratelimiting / debouncing #20

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions memoryio/memoryio/TSAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ - (IBAction)aboutAction:(id)sender

- (IBAction)forceAction:(id)sender
{
[self takePhotoWithDelay:2.0f];
[self takeBlockedImage];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
Expand Down Expand Up @@ -251,6 +251,7 @@ - (void) messageReceived:(natural_t)messageType withArgument:(void *)messageArgu
case kIOMessageDeviceHasPoweredOn :
// mainly for when the display goesto sleep and wakes up
NSLog(@"messageReceived: got a kIOMessageDeviceHasPoweredOn - device powered on");
[self takeBlockedImage];
break;
case kIOMessageSystemWillSleep:
IOAllowPowerChange(root_port,(long)messageArgument);
Expand All @@ -261,7 +262,7 @@ - (void) messageReceived:(natural_t)messageType withArgument:(void *)messageArgu
case kIOMessageSystemHasPoweredOn:
// mainly for when the system goes to sleep and wakes up
NSLog(@"messageReceived: got a kIOMessageSystemHasPoweredOn - system powered on");
[self takePhotoWithDelay:2.0f];
[self takeBlockedImage];
break;
}
}
Expand Down Expand Up @@ -323,4 +324,42 @@ - (void) takePhotoWithDelay: (float) delay {

}

@end

// Block image taking
// F these new-school selectors and mutable associated directories. Let's do this 1997 javascript style..
// set a timer. If it tries to call again too fast, then drop the request. Sync setting a timer.
// Timer is a thread, so the sync will unlock and all is well.
BOOL imageBlocked = NO;
NSTimer *imageBlockedTimer;

//
-(void) takeBlockedImage
{
NSLog(@"takeBlockedImage: Trying to take lock...");
BOOL iWin = NO;
@synchronized(self){
if(!imageBlocked) {
imageBlocked = YES;
iWin = YES;
NSLog(@"unblockImage: blocking..");
imageBlockedTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(unblockImage) userInfo:nil repeats:NO];
} // end of if blocked
} // end of sync
if(iWin) {
NSLog(@"takeBlockedImage: I won");
[self takePhotoWithDelay:2.0f];
} else {
NSLog(@"takeBlockedImage: I lost");
} // end of if iWin


}

-(void) unblockImage
{
NSLog(@"unblockImage: unblocking..");
imageBlocked = NO;
}


@end