AForge.Video.Kinect.KinectNative.freenect_open_device C# (CSharp) Method

freenect_open_device() private method

private freenect_open_device ( IntPtr context, IntPtr &device, int index ) : int
context System.IntPtr
device System.IntPtr
index int
return int
        public static extern int freenect_open_device( IntPtr context, ref IntPtr device, int index );

Usage Example

Example #1
0
        /// <summary>
        /// Get initialized instance of the Kinect device.
        /// </summary>
        ///
        /// <param name="deviceID">ID of the Kinect device to get instance of, [0, <see cref="DeviceCount"/>),</param>
        ///
        /// <returns>Returns initialized Kinect device. Use <see cref="Dispose()"/> method
        /// when the device is no longer required.</returns>
        ///
        /// <exception cref="ArgumentException">There is no Kinect device with specified ID connected to the system.</exception>
        /// <exception cref="ConnectionFailedException">Failed connecting to the Kinect device specified ID.</exception>
        ///
        public static Kinect GetDevice(int deviceID)
        {
            if ((deviceID < 0) || (deviceID >= DeviceCount))
            {
                throw new ArgumentException("There is no Kinect device with specified ID connected to the system.");
            }

            bool   needToStartStatusThread = false;
            Kinect kinect = null;

            lock ( openDevices )
            {
                needToStartStatusThread = (openDevices.Count == 0);

                // check if the device is open already
                if (!openDevices.ContainsKey(deviceID))
                {
                    IntPtr devicePointer = IntPtr.Zero;

                    // connect to Kinect device witht the specified ID
                    if (KinectNative.freenect_open_device(KinectNative.Context, ref devicePointer, deviceID) != 0)
                    {
                        throw new ConnectionFailedException("Failed connecting to the Kinect device with ID: " + deviceID);
                    }

                    openDevices.Add(deviceID, new DeviceContext(devicePointer));
                }

                openDevices[deviceID].ReferenceCounter++;
                kinect = new Kinect(openDevices[deviceID].Device, deviceID);
            }

            if (needToStartStatusThread)
            {
                StartStatusThread( );
            }

            return(kinect);
        }