void SensorManager::UpdateSensorOnList( _In_z_ const WCHAR* wcPortID, _In_ HRESULT hrStatus ) { // set the lock on the list AutoLock sensorLock( m_csSensorLock ); AutoLock handleLock( m_csHandleLock ); // find the sensor id on the list auto iter = m_kinectSensors.find( wcPortID ); if( iter == m_kinectSensors.end() ) { // we didn't find this port id of a sensor // look for a temporary sensor that we can assign this to // select the first one on the list iter = m_kinectSensors.begin(); if( m_kinectSensors.size() == 1 && !iter->first.compare( this->m_wcTempPortID ) ) { // hold on to the instance of the sensor // have to re-add it to the list with the correct PortID auto pSensor = iter->second; assert( nullptr != pSensor ); // has a handle been issued for the temporary sensor std::wstring wsPortID( pSensor->GetPortID() ); for( auto handleIter = m_handleMap.begin(); handleIter != m_handleMap.end(); ++handleIter ) { if( 0 == wsPortID.compare( handleIter->second ) ) { // update the portID for the handle handleIter->second = wcPortID; break; } } // now remove it from the sensor map // set the lock/wait to modify the map of sensors m_kinectSensors.erase( iter ); // add it with the correct id m_kinectSensors.insert( std::make_pair(wcPortID, pSensor) ); } else { // wasn't a sensor available based on the wcPortID // so we can create one since the id seems valid AddSensorToList( wcPortID ); } // should be there now since we can account to all id's // on the system iter = m_kinectSensors.find( wcPortID ); } // notify the wrapper of the change iter->second->NuiStatusNotification( iter->first.c_str(), hrStatus ); }
// called once on initialization void SensorManager::CreateListOfAvailableSensors() { // gets the count of sensors from Kinect for Windows API int iCount = 0; HRESULT hr = NuiGetSensorCount( &iCount ); if( FAILED(hr) ) { return; } // if there are no sensors, create a temporary one if( 0 == iCount ) { // when the first sensor is connected, it will be added // with the correct id if( m_kinectSensors.size() == 0 ) { AddSensorToList( m_wcTempPortID ); } } // continue with the known sensors to the list for (int i = 0; i < iCount; ++i) { INuiSensor* pNui = nullptr; ComSmartPtr<INuiSensor> pNuiPtr; if( SUCCEEDED( NuiCreateSensorByIndex(i, &pNui) ) ) { pNuiPtr.Attach(pNui); // Setting up smart pointer for sensor. const WCHAR* wcPortID = pNuiPtr->NuiDeviceConnectionId(); // contains the connections string for the device // plugged into a specific USB port. // removing the sensor and using a different port will have // a different ID. AddSensorToList( wcPortID ); } } }
// gets an instance of the sensor for a particular port string that is // found by enumerating or one that is already known std::shared_ptr<KinectSensor> SensorManager::GetSensor( _In_z_ const WCHAR* wcPortID ) { if( nullptr == wcPortID ) { return nullptr; } auto iter = m_kinectSensors.find( wcPortID ); if( iter == m_kinectSensors.end() ) { // if the id has not been found, add it to our list to track if( AddSensorToList(wcPortID) ) { iter = m_kinectSensors.find( wcPortID ); } else { return nullptr; } } return iter->second; }