Guest User

Untitled

a guest
May 23rd, 2013
181
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <pthread.h>
  4. #include <libfreenect.h>
  5. #include <sys/time.h>
  6.  
  7. using namespace std;
  8.  
  9. void *depthThreadFunction(void *args);
  10. void depthCallback(freenect_device *freenectDevice, void *depthAsVoid, uint32_t timestamp);
  11.  
  12. freenect_context *freenectContext;
  13. freenect_device *freenectDevice;
  14. pthread_mutex_t depthMutex = PTHREAD_MUTEX_INITIALIZER;
  15. void *depthThreadFunction();
  16. bool endDepthThread;
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.     pthread_t depthThread;
  21.    
  22.     if (freenect_init(&freenectContext, NULL) < 0)
  23.     {
  24.         cout << "freenect_init() failed" << endl;
  25.        
  26.         return 1;
  27.     }
  28.    
  29.     freenect_set_log_level(freenectContext, FREENECT_LOG_DEBUG);
  30.     freenect_select_subdevices(freenectContext, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
  31.    
  32.     int deviceCount = freenect_num_devices(freenectContext);
  33.     cout << "Number of devices found: " << deviceCount << endl;
  34.    
  35.     int userDeviceNumber = 0;
  36.     if (argc > 1)
  37.         userDeviceNumber = atoi(argv[1]);
  38.    
  39.     if (deviceCount < 1)
  40.     {
  41.         freenect_shutdown(freenectContext);
  42.        
  43.         return 1;
  44.     }
  45.    
  46.     if (freenect_open_device(freenectContext, &freenectDevice, userDeviceNumber) < 0)
  47.     {
  48.         cout << "Could not open device" << endl;
  49.         freenect_shutdown(freenectContext);
  50.        
  51.         return 1;
  52.     }
  53.    
  54.     freenect_set_depth_callback(freenectDevice, depthCallback);
  55.     freenect_set_depth_mode(freenectDevice, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM));
  56.     freenect_start_depth(freenectDevice);
  57.    
  58.     endDepthThread = false;
  59.     pthread_create(&depthThread, 0, &depthThreadFunction, 0);
  60.    
  61.     pthread_join(depthThread, 0);
  62.    
  63.     freenect_stop_depth(freenectDevice);
  64.     freenect_shutdown(freenectContext);
  65.    
  66.     return 0;
  67. }
  68.  
  69. void *depthThreadFunction(void *args)
  70. {
  71.     while (endDepthThread == false && freenect_process_events(freenectContext) >= 0)
  72.     {
  73.     }
  74.    
  75.     return 0;
  76. }
  77.  
  78. void depthCallback(freenect_device *freenectDevice, void *depthAsVoid, uint32_t timestamp)
  79. {
  80.     pthread_mutex_lock(&depthMutex);
  81.    
  82.     short *depthData = (short*)depthAsVoid;
  83.  
  84.     pthread_mutex_unlock(&depthMutex);
  85. }
RAW Paste Data