freenect_driver.hpp
Go to the documentation of this file.
1 #ifndef FREENECT_DRIVER_K8EEAIBB
2 #define FREENECT_DRIVER_K8EEAIBB
3 
4 #include <libfreenect.h>
6 
7 namespace freenect_camera {
8 
9  class FreenectDriver {
10 
11  public:
12 
14  static FreenectDriver instance;
15  return instance;
16  }
17 
18  void shutdown() {
19  thread_running_ = false;
20  freenect_thread_->join();
21  if (device_)
22  device_->shutdown();
23  device_.reset();
24  freenect_shutdown(driver_);
25  }
26 
28  device_serials_.clear();
29  freenect_device_attributes* attr_list;
30  freenect_device_attributes* item;
31  freenect_list_device_attributes(driver_, &attr_list);
32  for (item = attr_list; item != NULL; item = item->next) {
33  device_serials_.push_back(std::string(item->camera_serial));
34  }
35  freenect_free_device_attributes(attr_list);
36  }
37 
38  unsigned getNumberDevices() {
39  return device_serials_.size();
40  }
41 
43  unsigned getBus(unsigned device_idx) {
44  return 0;
45  }
46 
48  unsigned getAddress(unsigned device_idx) {
49  return 0;
50  }
51 
52  const char* getProductName(unsigned device_idx) {
53  return PRODUCT_NAME.c_str();
54  }
55 
56  unsigned getProductID(unsigned device_idx) {
57  return PRODUCT_ID;
58  }
59 
60  const char* getVendorName(unsigned device_idx) {
61  return VENDOR_NAME.c_str();
62  }
63 
64  unsigned getVendorID(unsigned device_idx) {
65  return VENDOR_ID;
66  }
67 
68  const char* getSerialNumber(unsigned device_idx) {
69  if (device_idx < getNumberDevices())
70  return device_serials_[device_idx].c_str();
71  throw std::runtime_error("libfreenect: device idx out of range");
72  }
73 
75  return getDeviceBySerialNumber(std::string(getSerialNumber(device_idx)));
76  }
77 
79  device_.reset(new FreenectDevice(driver_, serial));
80  // start freenect thread now that we have device
81  thread_running_ = true;
82  freenect_thread_.reset(new boost::thread(boost::bind(&FreenectDriver::process, this)));
83  return device_;
84  }
85 
86  boost::shared_ptr<FreenectDevice> getDeviceByAddress(unsigned bus, unsigned address) {
87  throw std::runtime_error("[ERROR] libfreenect does not support searching for device by bus/address");
88  }
89 
90  void process() {
91  while (thread_running_) {
92  timeval t;
93  t.tv_sec = 0;
94  t.tv_usec = 10000;
95  if (freenect_process_events_timeout(driver_, &t) < 0)
96  throw std::runtime_error("freenect_process_events error");
97  if (device_)
98  device_->executeChanges();
99  }
100  }
101 
102  void enableDebug() {
103  freenect_set_log_level(driver_, FREENECT_LOG_SPEW);
104  }
105 
106  private:
108  freenect_init(&driver_, NULL);
109  freenect_set_log_level(driver_, FREENECT_LOG_FATAL); // Prevent's printing stuff to the screen
110  freenect_select_subdevices(driver_, (freenect_device_flags)(FREENECT_DEVICE_CAMERA));
111  thread_running_ = false;
112  }
113 
114  freenect_context* driver_;
115  std::vector<std::string> device_serials_;
118 
120  };
121 
122 }
123 
124 #endif /* end of include guard: FREENECT_DRIVER_K8EEAIBB */
unsigned getAddress(unsigned device_idx)
static FreenectDriver & getInstance()
boost::shared_ptr< FreenectDevice > getDeviceByIndex(unsigned device_idx)
static const std::string VENDOR_NAME
boost::shared_ptr< FreenectDevice > device_
boost::shared_ptr< FreenectDevice > getDeviceByAddress(unsigned bus, unsigned address)
static const unsigned VENDOR_ID
unsigned getBus(unsigned device_idx)
unsigned getVendorID(unsigned device_idx)
unsigned getProductID(unsigned device_idx)
std::vector< std::string > device_serials_
boost::shared_ptr< FreenectDevice > getDeviceBySerialNumber(std::string serial)
const char * getSerialNumber(unsigned device_idx)
const char * getVendorName(unsigned device_idx)
const char * getProductName(unsigned device_idx)
boost::shared_ptr< boost::thread > freenect_thread_
static const std::string PRODUCT_NAME
static const unsigned PRODUCT_ID


freenect_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu (original openni_camera driver)., Piyush Khandelwal (libfreenect port).
autogenerated on Fri Mar 20 2020 03:22:26