Skip to content

Instantly share code, notes, and snippets.

@ofTheo
Created January 4, 2012 05:47
Show Gist options
  • Save ofTheo/1558683 to your computer and use it in GitHub Desktop.
Save ofTheo/1558683 to your computer and use it in GitHub Desktop.
example of auto sorting devices by serial no:
//---------------------------------------------------------------------------
bool ofxKinectContext::init() {
if(freenect_init(&kinectContext, NULL) < 0) {
ofLog(OF_LOG_ERROR, "ofxKinect: freenect_init failed");
return false;
}
//NEW:
freenect_device_attributes * devAttrib;
int numDevices = freenect_list_device_attributes(kinectContext, &devAttrib);
int id = 0;
devicesList.clear();
for(int i = 0; i < numDevices; i++){
indexPair dl;
dl.id = id;
dl.serial = devAttrib->camera_serial;
devicesList.push_back( dl );
id++;
devAttrib = devAttrib->next;
}
freenect_free_device_attributes(devAttrib);
cout << "numDevices is " << numDevices << endl;
cout << "order is: " << endl;
for(int i = 0; i < devicesList.size(); i++){
cout << "["<<devicesList[i].id<<"] " << devicesList[i].serial <<endl;
}
sort( devicesList.begin(), devicesList.end(), sortIndex);
cout << "order is now: " << endl;
for(int i = 0; i < devicesList.size(); i++){
cout << "["<<devicesList[i].id<<"] " << devicesList[i].serial <<endl;
}
ofLog(OF_LOG_VERBOSE, "ofxKinect: Context inited");
return true;
}
bool ofxKinectContext::open(ofxKinect& kinect, int id) {
if(numConnected() >= numTotal()) {
ofLog(OF_LOG_WARNING, "ofxKinect Cannot open any more devices");
return false;
}
//NEW:
if( id < devicesList.size() ){
id = devicesList[id].id;
}
.....
//NEW:
class indexPair{
public:
string serial;
int id;
};
/// \class ofxKinect
///
/// wrapper for the freenect context
///
/// do not use this directly
///
class ofxKinectContext {
public:
ofxKinectContext();
~ofxKinectContext();
/// \section Main
/// init the freenect context
bool init();
/// clear the freenect context
/// closes all currently connected devices
void clear();
/// is the context inited?
bool isInited();
/// open a kinect device
/// an id of -1 will open the first available
bool open(ofxKinect& kinect, int id=-1);
/// close a kinect device
void close(ofxKinect& kinect);
/// closes all currently connected kinects
void closeAll();
/// \section Util
/// get the total number of devices
int numTotal();
/// get the number of available devices (not connected)
int numAvailable();
/// get the number of currently connected devices
int numConnected();
/// get the device id of a kinect object
/// returns index or -1 if not connected
int getId(ofxKinect& kinect);
/// get the kinect object from a device pointer
/// returns NULL if not found
ofxKinect* getKinect(freenect_device* dev);
/// is the an id already connected?
bool isConnected(int id);
/// get the id of the next available device,
/// returns -1 if nothing found
int nextAvailableId();
/// get the raw pointer
freenect_context* getContext() {return kinectContext;}
private:
//NEW:
vector <indexPair> devicesList;
freenect_context * kinectContext; // kinect context handle
std::map<int,ofxKinect*> kinects; // the connected kinects
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment