Created
October 20, 2013 20:43
-
-
Save nevyn/7075056 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TCKinectDepthController.h | |
// Ocunect2 | |
// | |
// Created by Joachim Bengtsson on 2013-10-20. | |
// Copyright (c) 2013 nevyn. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
static const int TCKinectVideoWidth = 640, TCKinectVideoHeight = 480, TCKinectVideoDepth = 3; | |
@interface TCKinectController : NSObject | |
@property(nonatomic,readonly) NSArray *depthValues; | |
@property(nonatomic,readonly) NSData *colorData; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TCKinectDepthController.m | |
// Ocunect2 | |
// | |
// Created by Joachim Bengtsson on 2013-10-20. | |
// Copyright (c) 2013 nevyn. All rights reserved. | |
// | |
#import "TCKinectController.h" | |
#import <libfreenect.h> | |
@interface TCKinectController () | |
{ | |
freenect_context *_freenect; | |
freenect_device *_device; | |
uint8_t *_depthBuffer; | |
uint8_t *_rgbBuffer; | |
volatile BOOL _die; | |
volatile BOOL _dead; | |
} | |
@property(nonatomic,readwrite) NSArray *depthValues; | |
@property(nonatomic,readwrite) NSData *colorData; | |
@end | |
@implementation TCKinectController | |
- (id)init | |
{ | |
if(!(self = [super init])) | |
return nil; | |
if (freenect_init(&_freenect, NULL) < 0) { | |
NSLog(@"freenect_init() failed"); | |
_freenect = NULL; | |
return nil; | |
} | |
freenect_select_subdevices(_freenect, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA)); | |
int nr_devices = freenect_num_devices (_freenect); | |
if(nr_devices == 0) { | |
NSLog(@"No Kinects found"); | |
freenect_shutdown(_freenect); | |
_freenect = NULL; | |
return nil; | |
} | |
if (freenect_open_device(_freenect, &_device, 0) < 0) { | |
NSLog(@"Could not open freenect device"); | |
freenect_shutdown(_freenect); | |
_freenect = NULL; | |
return nil; | |
} | |
[self performSelectorInBackground:@selector(_main) withObject:nil]; | |
return self; | |
} | |
void depth_cb(freenect_device *dev, void *v_depth, uint32_t timestamp) | |
{ | |
TCKinectController *self = (__bridge id)freenect_get_user(dev); | |
uint16_t *depth = (uint16_t*)v_depth; | |
NSMutableArray *depthValues = [[NSMutableArray alloc] initWithCapacity:TCKinectVideoWidth*TCKinectVideoHeight]; | |
for(int y = 0; y < TCKinectVideoHeight; y++) { | |
for(int x = 0; x < TCKinectVideoWidth; x++) { | |
int16_t value = depth[y*TCKinectVideoWidth + x]; | |
[depthValues addObject:@(value)]; | |
} | |
} | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
self.depthValues = depthValues; | |
}); | |
} | |
void rgb_cb(freenect_device *dev, void *rgb, uint32_t timestamp) | |
{ | |
TCKinectController *self = (__bridge id)freenect_get_user(dev); | |
NSData *colorData = [NSData dataWithBytes:rgb length:TCKinectVideoWidth*TCKinectVideoHeight*TCKinectVideoDepth]; | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
self.colorData = colorData; | |
}); | |
} | |
// On its own thread | |
- (void)_main | |
{ | |
_depthBuffer = (uint8_t*)malloc(TCKinectVideoWidth*TCKinectVideoHeight*TCKinectVideoDepth); | |
_rgbBuffer = (uint8_t*)malloc(TCKinectVideoWidth*TCKinectVideoHeight*TCKinectVideoDepth); | |
freenect_set_user(_device, (__bridge void*)self); | |
freenect_set_depth_callback(_device, depth_cb); | |
freenect_set_video_callback(_device, rgb_cb); | |
freenect_set_video_mode(_device, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB)); | |
freenect_set_depth_mode(_device, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_REGISTERED)); | |
freenect_set_depth_buffer(_device, _depthBuffer); | |
freenect_set_video_buffer(_device, _rgbBuffer); | |
freenect_start_depth(_device); | |
freenect_start_video(_device); | |
while (!_die && freenect_process_events(_freenect) >= 0) { | |
} | |
printf("\nshutting down streams...\n"); | |
freenect_stop_depth(_device); | |
freenect_stop_video(_device); | |
freenect_close_device(_device); | |
freenect_shutdown(_freenect); | |
free(_depthBuffer); | |
free(_rgbBuffer); | |
} | |
- (void)dealloc | |
{ | |
if(_freenect) { | |
_die = YES; | |
while(!_dead) {} | |
freenect_shutdown(_freenect); | |
_freenect = NULL; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment