00001 #ifndef FREENECT_DRIVER_K8EEAIBB 00002 #define FREENECT_DRIVER_K8EEAIBB 00003 00004 #include <libfreenect/libfreenect.h> 00005 #include <freenect_camera/freenect_device.hpp> 00006 00007 namespace freenect_camera { 00008 00009 class FreenectDriver { 00010 00011 public: 00012 00013 static FreenectDriver& getInstance() { 00014 static FreenectDriver instance; 00015 return instance; 00016 } 00017 00018 void shutdown() { 00019 thread_running_ = false; 00020 freenect_thread_->join(); 00021 if (device_) 00022 device_->shutdown(); 00023 device_.reset(); 00024 freenect_shutdown(driver_); 00025 } 00026 00027 void updateDeviceList() { 00028 device_serials_.clear(); 00029 freenect_device_attributes* attr_list; 00030 freenect_device_attributes* item; 00031 freenect_list_device_attributes(driver_, &attr_list); 00032 for (item = attr_list; item != NULL; item = item->next) { 00033 device_serials_.push_back(std::string(item->camera_serial)); 00034 } 00035 freenect_free_device_attributes(attr_list); 00036 } 00037 00038 unsigned getNumberDevices() { 00039 return device_serials_.size(); 00040 } 00041 00043 unsigned getBus(unsigned device_idx) { 00044 return 0; 00045 } 00046 00048 unsigned getAddress(unsigned device_idx) { 00049 return 0; 00050 } 00051 00052 const char* getProductName(unsigned device_idx) { 00053 return PRODUCT_NAME.c_str(); 00054 } 00055 00056 unsigned getProductID(unsigned device_idx) { 00057 return PRODUCT_ID; 00058 } 00059 00060 const char* getVendorName(unsigned device_idx) { 00061 return VENDOR_NAME.c_str(); 00062 } 00063 00064 unsigned getVendorID(unsigned device_idx) { 00065 return VENDOR_ID; 00066 } 00067 00068 const char* getSerialNumber(unsigned device_idx) { 00069 if (device_idx < getNumberDevices()) 00070 return device_serials_[device_idx].c_str(); 00071 throw std::runtime_error("libfreenect: device idx out of range"); 00072 } 00073 00074 boost::shared_ptr<FreenectDevice> getDeviceByIndex(unsigned device_idx) { 00075 return getDeviceBySerialNumber(std::string(getSerialNumber(device_idx))); 00076 } 00077 00078 boost::shared_ptr<FreenectDevice> getDeviceBySerialNumber(std::string serial) { 00079 device_.reset(new FreenectDevice(driver_, serial)); 00080 // start freenect thread now that we have device 00081 thread_running_ = true; 00082 freenect_thread_.reset(new boost::thread(boost::bind(&FreenectDriver::process, this))); 00083 return device_; 00084 } 00085 00086 boost::shared_ptr<FreenectDevice> getDeviceByAddress(unsigned bus, unsigned address) { 00087 throw std::runtime_error("[ERROR] libfreenect does not support searching for device by bus/address"); 00088 } 00089 00090 void process() { 00091 while (thread_running_) { 00092 timeval t; 00093 t.tv_sec = 0; 00094 t.tv_usec = 10000; 00095 if (freenect_process_events_timeout(driver_, &t) < 0) 00096 throw std::runtime_error("freenect_process_events error"); 00097 if (device_) 00098 device_->executeChanges(); 00099 } 00100 } 00101 00102 void enableDebug() { 00103 freenect_set_log_level(driver_, FREENECT_LOG_SPEW); 00104 } 00105 00106 private: 00107 FreenectDriver() { 00108 freenect_init(&driver_, NULL); 00109 freenect_set_log_level(driver_, FREENECT_LOG_FATAL); // Prevent's printing stuff to the screen 00110 freenect_select_subdevices(driver_, (freenect_device_flags)(FREENECT_DEVICE_CAMERA)); 00111 thread_running_ = false; 00112 } 00113 00114 freenect_context* driver_; 00115 std::vector<std::string> device_serials_; 00116 boost::shared_ptr<boost::thread> freenect_thread_; 00117 boost::shared_ptr<FreenectDevice> device_; 00118 00119 bool thread_running_; 00120 }; 00121 00122 } 00123 00124 #endif /* end of include guard: FREENECT_DRIVER_K8EEAIBB */