Esempio n. 1
0
        private Image RequestImages(ImageData imageData)
        {
            if (!_isRunning)
            {
                return(Image.Invalid);
            }

            LEAP_IMAGE_FRAME_DESCRIPTION imageSpecifier = new LEAP_IMAGE_FRAME_DESCRIPTION();

            imageSpecifier.frame_id   = imageData.frame_id;
            imageSpecifier.type       = imageData.type;
            imageSpecifier.pBuffer    = imageData.getPinnedHandle();
            imageSpecifier.buffer_len = (ulong)imageData.pixelBuffer.LongLength;
            LEAP_IMAGE_FRAME_REQUEST_TOKEN token;
            eLeapRS result = eLeapRS.eLeapRS_UnknownError;

            result = LeapC.RequestImages(_leapConnection, ref imageSpecifier, out token);

            if (result == eLeapRS.eLeapRS_Success)
            {
                imageData.isComplete = false;
                imageData.index      = token.requestID;
                Image futureImage = new Image(imageData);
                _pendingImageRequestList.Add(new ImageFuture(futureImage, imageData, LeapC.GetNow(), token));
                return(futureImage);
            }
            else
            {
                imageData.unPinHandle();
                reportAbnormalResults("LeapC Image Request call was ", result);
                return(Image.Invalid);
            }
        }
Esempio n. 2
0
        public bool SetPaused(bool pause)
        {
            ulong prior_state = 0;
            ulong set_flags   = 0;
            ulong clear_flags = 0;

            if (pause)
            {
                set_flags = (ulong)eLeapDeviceFlag.eLeapDeviceFlag_Stream;
            }
            else
            {
                clear_flags = (ulong)eLeapDeviceFlag.eLeapDeviceFlag_Stream;
            }

            eLeapRS result = eLeapRS.eLeapRS_UnknownError;

            if (IsValid)
            {
                result = LeapC.SetDeviceFlags(_hDevice, set_flags, clear_flags, out prior_state);
            }
            if (result == eLeapRS.eLeapRS_Success)
            {
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        public void Start()
        {
            if (_isRunning)
            {
                return;
            }

            if (_leapConnection == IntPtr.Zero)
            {
                eLeapRS result = LeapC.CreateConnection(out _leapConnection);
                if (result != eLeapRS.eLeapRS_Success || _leapConnection == IntPtr.Zero)
                {
                    reportAbnormalResults("LeapC CreateConnection call was ", result);
                    return;
                }
                result = LeapC.OpenConnection(_leapConnection);
                if (result != eLeapRS.eLeapRS_Success)
                {
                    reportAbnormalResults("LeapC OpenConnection call was ", result);
                    return;
                }
            }

            _isRunning = true;

            _polster              = new Thread(new ThreadStart(this.processMessages));
            _polster.Name         = "LeapC Worker";
            _polster.IsBackground = true;
            _polster.Start();
        }
 public uint GetConfigValue(string config_key) {
   uint requestId = 0;
   eLeapRS result = LeapC.RequestConfigValue(_leapConnection, config_key, out requestId);
   reportAbnormalResults("LeapC RequestConfigValue call was ", result);
   _configRequests[requestId] = config_key;
   return requestId;
 }
Esempio n. 5
0
        public void SetAndClearPolicy(Controller.PolicyFlag set, Controller.PolicyFlag clear)
        {
            UInt64  setFlags   = (ulong)FlagForPolicy(set);
            UInt64  clearFlags = (ulong)FlagForPolicy(clear);
            eLeapRS result     = LeapC.SetPolicyFlags(_leapConnection, setFlags, clearFlags);

            reportAbnormalResults("LeapC SetAndClearPolicy call was ", result);
        }
Esempio n. 6
0
        public UInt64 GetInterpolatedFrameSize(Int64 time)
        {
            UInt64  size   = 0;
            eLeapRS result = LeapC.GetFrameSize(_leapConnection, time, out size);

            reportAbnormalResults("LeapC get interpolated frame call was ", result);
            return(size);
        }
Esempio n. 7
0
        /**
         * Creates a new Clock Correlation object for maintaining a latency-adjusted relationship between the
         * Leap Motion system clock and an external clock.
         *
         * @since 3.x.y
         */
        public ClockCorrelator()
        {
            eLeapRS result = LeapC.CreateClockRebaser(out _rebaserHandle);

            if (result != eLeapRS.eLeapRS_Success)
            {
                throw new Exception(result.ToString());
            }
        }
    public void SetPolicy(Controller.PolicyFlag policy) {
      UInt64 setFlags = (ulong)flagForPolicy(policy);
      _requestedPolicies = _requestedPolicies | setFlags;
      setFlags = _requestedPolicies;
      UInt64 clearFlags = ~_requestedPolicies; //inverse of desired policies

      eLeapRS result = LeapC.SetPolicyFlags(_leapConnection, setFlags, clearFlags);
      reportAbnormalResults("LeapC SetPolicyFlags call was ", result);
    }
Esempio n. 9
0
        public void ClearPolicy(Controller.PolicyFlag policy)
        {
            UInt64 clearFlags = (ulong)flagForPolicy(policy);

            _requestedPolicies = _requestedPolicies & ~clearFlags;
            eLeapRS result = LeapC.SetPolicyFlags(_leapConnection, _requestedPolicies, ~_requestedPolicies);

            reportAbnormalResults("LeapC SetPolicyFlags call was ", result);
        }
Esempio n. 10
0
        public void GetPointMapping(ref LEAP_POINT_MAPPING pm)
        {
            UInt64 size   = 0;
            IntPtr buffer = IntPtr.Zero;

            while (true)
            {
                eLeapRS result = LeapC.GetPointMapping(_leapConnection, buffer, ref size);
                if (result == eLeapRS.eLeapRS_InsufficientBuffer)
                {
                    if (buffer != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(buffer);
                    }
                    buffer = Marshal.AllocHGlobal((Int32)size);
                    continue;
                }
                reportAbnormalResults("LeapC get point mapping call was ", result);
                if (result != eLeapRS.eLeapRS_Success)
                {
                    pm.nPoints = 0;
                    pm.points  = null;
                    pm.ids     = null;
                    return;
                }
                break;
            }
            LEAP_POINT_MAPPING_INTERNAL pmi;

            StructMarshal <LEAP_POINT_MAPPING_INTERNAL> .PtrToStruct(buffer, out pmi);

            Int32 nPoints = (Int32)pmi.nPoints;

            pm.frame_id  = pmi.frame_id;
            pm.timestamp = pmi.timestamp;
            pm.nPoints   = pmi.nPoints;
            pm.points    = new LEAP_VECTOR[nPoints];
            pm.ids       = new UInt32[nPoints];

            float[] points = new float[3 * nPoints];
            Int32[] ids    = new Int32[nPoints];
            Marshal.Copy(pmi.points, points, 0, 3 * nPoints);
            Marshal.Copy(pmi.ids, ids, 0, nPoints);

            int j = 0;

            for (int i = 0; i < nPoints; i++)
            {
                pm.points[i].x = points[j++];
                pm.points[i].y = points[j++];
                pm.points[i].z = points[j++];
                pm.ids[i]      = unchecked ((UInt32)ids[i]);
            }
            Marshal.FreeHGlobal(buffer);
        }
 public void GetInterpolatedFrameFromTime(Frame toFill, Int64 time, Int64 sourceTime) {
   UInt64 size = GetInterpolatedFrameSize(time);
   IntPtr trackingBuffer = Marshal.AllocHGlobal((Int32)size);
   eLeapRS result = LeapC.InterpolateFrameFromTime(_leapConnection, time, sourceTime, trackingBuffer, size);
   reportAbnormalResults("LeapC get interpolated frame from time call was ", result);
   if (result == eLeapRS.eLeapRS_Success) {
     LEAP_TRACKING_EVENT tracking_evt;
     StructMarshal<LEAP_TRACKING_EVENT>.PtrToStruct(trackingBuffer, out tracking_evt);
     toFill.CopyFrom(ref tracking_evt);
   }
   Marshal.FreeHGlobal(trackingBuffer);
 }
Esempio n. 12
0
        public uint GetConfigValue(string config_key)
        {
            uint requestId = 0;

            if (_leapConnection != IntPtr.Zero) //Test IsServiceConnected (when it works)
            {
                eLeapRS result = LeapC.RequestConfigValue(_leapConnection, config_key, out requestId);
                reportAbnormalResults("LeapC RequestConfigValue call was ", result);
                _configRequests [requestId] = config_key;
            }
            return(requestId);
        }
Esempio n. 13
0
        public void GetInterpolatedLeftRightTransform(Int64 time,
                                                      Int64 sourceTime,
                                                      Int64 leftId,
                                                      Int64 rightId,
                                                      out LeapTransform leftTransform,
                                                      out LeapTransform rightTransform)
        {
            leftTransform  = LeapTransform.Identity;
            rightTransform = LeapTransform.Identity;

            UInt64  size           = GetInterpolatedFrameSize(time);
            IntPtr  trackingBuffer = Marshal.AllocHGlobal((Int32)size);
            eLeapRS result         = LeapC.InterpolateFrameFromTime(_leapConnection, time, sourceTime, trackingBuffer, size);

            reportAbnormalResults("LeapC get interpolated frame from time call was ", result);

            if (result == eLeapRS.eLeapRS_Success)
            {
                LEAP_TRACKING_EVENT tracking_evt;
                StructMarshal <LEAP_TRACKING_EVENT> .PtrToStruct(trackingBuffer, out tracking_evt);

                int             id;
                LEAP_VECTOR     position;
                LEAP_QUATERNION orientation;

                long handPtr = tracking_evt.pHands.ToInt64();
                long idPtr   = handPtr + _handIdOffset;
                long posPtr  = handPtr + _handPositionOffset;
                long rotPtr  = handPtr + _handOrientationOffset;
                int  stride  = StructMarshal <LEAP_HAND> .Size;

                for (uint i = tracking_evt.nHands; i-- != 0; idPtr += stride, posPtr += stride, rotPtr += stride)
                {
                    id = Marshal.ReadInt32(new IntPtr(idPtr));
                    StructMarshal <LEAP_VECTOR> .PtrToStruct(new IntPtr(posPtr), out position);

                    StructMarshal <LEAP_QUATERNION> .PtrToStruct(new IntPtr(rotPtr), out orientation);

                    LeapTransform transform = new LeapTransform(position.ToLeapVector(), orientation.ToLeapQuaternion());
                    if (id == leftId)
                    {
                        leftTransform = transform;
                    }
                    else if (id == rightId)
                    {
                        rightTransform = transform;
                    }
                }
            }

            Marshal.FreeHGlobal(trackingBuffer);
        }
Esempio n. 14
0
        private static eLeapRS SaveConfigWithRefType(IntPtr hConnection, string key, LEAP_VARIANT_REF_TYPE valueStruct, out UInt32 requestId)
        {
            IntPtr  configValue = Marshal.AllocHGlobal(Marshal.SizeOf(valueStruct));
            eLeapRS callResult  = eLeapRS.eLeapRS_UnknownError;

            try {
                Marshal.StructureToPtr(valueStruct, configValue, false);
                callResult = SaveConfigValue(hConnection, key, configValue, out requestId);
            } finally {
                Marshal.FreeHGlobal(configValue);
            }
            return(callResult);
        }
 private eLeapRS _lastResult; //Used to avoid repeating the same log message, ie. for events like time out
 private void reportAbnormalResults(string context, eLeapRS result) {
   if (result != eLeapRS.eLeapRS_Success &&
      result != _lastResult) {
     string msg = context + " " + result;
     if (LeapLogEvent != null) {
       LeapLogEvent.DispatchOnContext(this, EventContext,
         new LogEventArgs(MessageSeverity.MESSAGE_CRITICAL,
             LeapC.GetNow(),
             msg));
     }
   }
   _lastResult = result;
 }
Esempio n. 16
0
        public uint GetConfigValue(string config_key)
        {
            lock (_connLocker) {
                uint requestId = 0;
                if (_leapConnection == IntPtr.Zero)
                {
                    return(requestId);
                }

                eLeapRS result = LeapC.RequestConfigValue(_leapConnection, config_key, out requestId);
                reportAbnormalResults("LeapC RequestConfigValue call was ", result);
                _configRequests [requestId] = config_key;
                return(requestId);
            }
        }
Esempio n. 17
0
        public void ClearPolicy(Controller.PolicyFlag policy)
        {
            lock (_connLocker) {
                if (_leapConnection == IntPtr.Zero)
                {
                    return;
                }

                UInt64 clearFlags = (ulong)flagForPolicy(policy);
                _requestedPolicies = _requestedPolicies & ~clearFlags;
                eLeapRS result = eLeapRS.eLeapRS_UnknownError;
                result = LeapC.SetPolicyFlags(_leapConnection, 0, clearFlags);
                reportAbnormalResults("LeapC SetPolicyFlags call was ", result);
            }
        }
Esempio n. 18
0
        private void InitClockRebaser()
        {
            _stopwatch.Start();
            eLeapRS result = LeapC.CreateClockRebaser(out _clockRebaser);

            if (result != eLeapRS.eLeapRS_Success)
            {
                Debug.LogError("Failed to create clock rebaser");
            }

            if (_clockRebaser == IntPtr.Zero)
            {
                Debug.LogError("Clock rebaser is null");
            }
        }
Esempio n. 19
0
        public void GetInterpolatedFrameFromTime(Frame toFill, long time, long sourceTime)
        {
            ulong   interpolatedFrameSize = this.GetInterpolatedFrameSize(time);
            IntPtr  pEvent = Marshal.AllocHGlobal((int)interpolatedFrameSize);
            eLeapRS result = LeapC.InterpolateFrameFromTime(this._leapConnection, time, sourceTime, pEvent, interpolatedFrameSize);

            this.reportAbnormalResults("LeapC get interpolated frame from time call was ", result);
            if (result == eLeapRS.eLeapRS_Success)
            {
                LEAP_TRACKING_EVENT leap_tracking_event;
                StructMarshal <LEAP_TRACKING_EVENT> .PtrToStruct(pEvent, out leap_tracking_event);

                toFill.CopyFrom(ref leap_tracking_event);
            }
            Marshal.FreeHGlobal(pEvent);
        }
Esempio n. 20
0
 public void Start()
 {
     if (!_isRunning)
     {
         if (_leapConnection == IntPtr.Zero)
         {
             eLeapRS result = LeapC.CreateConnection(out _leapConnection);
             reportAbnormalResults("LeapC CreateConnection call was ", result);
             result = LeapC.OpenConnection(_leapConnection);
             reportAbnormalResults("LeapC OpenConnection call was ", result);
         }
         _isRunning            = true;
         _polster              = new Thread(new ThreadStart(this.processMessages));
         _polster.IsBackground = true;
         _polster.Start();
     }
 }
Esempio n. 21
0
        public void GetInterpolatedLeftRightTransform(long time, long sourceTime, long leftId, long rightId, out LeapTransform leftTransform, out LeapTransform rightTransform)
        {
            leftTransform  = LeapTransform.Identity;
            rightTransform = LeapTransform.Identity;
            ulong   interpolatedFrameSize = this.GetInterpolatedFrameSize(time);
            IntPtr  pEvent = Marshal.AllocHGlobal((int)interpolatedFrameSize);
            eLeapRS result = LeapC.InterpolateFrameFromTime(this._leapConnection, time, sourceTime, pEvent, interpolatedFrameSize);

            this.reportAbnormalResults("LeapC get interpolated frame from time call was ", result);
            if (result == eLeapRS.eLeapRS_Success)
            {
                LEAP_TRACKING_EVENT leap_tracking_event;
                StructMarshal <LEAP_TRACKING_EVENT> .PtrToStruct(pEvent, out leap_tracking_event);

                int  num3   = leap_tracking_event.pHands.ToInt32();
                int  num4   = num3 + _handIdOffset;
                int  num5   = num3 + _handPositionOffset;
                int  num6   = num3 + _handOrientationOffset;
                int  size   = StructMarshal <LEAP_HAND> .Size;
                uint nHands = leap_tracking_event.nHands;
                while (nHands-- != 0)
                {
                    LEAP_VECTOR     leap_vector;
                    LEAP_QUATERNION leap_quaternion;
                    int             num2 = Marshal.ReadInt32(new IntPtr(num4));
                    StructMarshal <LEAP_VECTOR> .PtrToStruct(new IntPtr(num5), out leap_vector);

                    StructMarshal <LEAP_QUATERNION> .PtrToStruct(new IntPtr(num6), out leap_quaternion);

                    LeapTransform transform = new LeapTransform(leap_vector.ToLeapVector(), leap_quaternion.ToLeapQuaternion());
                    if (num2 == leftId)
                    {
                        leftTransform = transform;
                    }
                    else if (num2 == rightId)
                    {
                        rightTransform = transform;
                    }
                    num4 += size;
                    num5 += size;
                    num6 += size;
                }
            }
            Marshal.FreeHGlobal(pEvent);
        }
        public Frame GetInterpolatedFrame(Int64 time)
        {
            UInt64  size           = GetInterpolatedFrameSize(time);
            IntPtr  trackingBuffer = Marshal.AllocHGlobal((Int32)size);
            eLeapRS result         = LeapC.InterpolateFrame(_leapConnection, time, trackingBuffer, size);

            reportAbnormalResults("LeapC get interpolated frame call was ", result);
            Frame frame = null;

            if (result == eLeapRS.eLeapRS_Success)
            {
                LEAP_TRACKING_EVENT tracking_evt = StructMarshal <LEAP_TRACKING_EVENT> .PtrToStruct(trackingBuffer);

                frame = frameFactory.makeFrame(ref tracking_evt);
            }
            Marshal.FreeHGlobal(trackingBuffer);
            return(frame);
        }
Esempio n. 23
0
        public void SetPolicy(Controller.PolicyFlag policy)
        {
            lock (_connLocker) {
                if (_leapConnection == IntPtr.Zero)
                {
                    return;
                }

                UInt64 setFlags = (ulong)flagForPolicy(policy);
                _requestedPolicies = _requestedPolicies | setFlags;
                setFlags           = _requestedPolicies;
                UInt64 clearFlags = ~_requestedPolicies; //inverse of desired policies

                eLeapRS result = eLeapRS.eLeapRS_UnknownError;
                result = LeapC.SetPolicyFlags(_leapConnection, setFlags, clearFlags);
                reportAbnormalResults("LeapC SetPolicyFlags call was ", result);
            }
        }
Esempio n. 24
0
        public bool SetPaused(bool pause)
        {
            ulong prior_state = 0;
            ulong set_flags   = 0;
            ulong clear_flags = 0;

            if (pause)
            {
                set_flags = (ulong)eLeapDeviceFlag.eLeapDeviceFlag_Stream;
            }
            else
            {
                clear_flags = (ulong)eLeapDeviceFlag.eLeapDeviceFlag_Stream;
            }

            eLeapRS result = LeapC.SetDeviceFlags(Handle, set_flags, clear_flags, out prior_state);

            return(result == eLeapRS.eLeapRS_Success);
        }
Esempio n. 25
0
        private void handleDevice(ref LEAP_DEVICE_EVENT deviceMsg)
        {
            IntPtr deviceHandle = deviceMsg.device.handle;

            if (deviceHandle != IntPtr.Zero)
            {
                IntPtr           device;
                eLeapRS          result        = LeapC.OpenDevice(deviceMsg.device, out device);
                LEAP_DEVICE_INFO deviceInfo    = new LEAP_DEVICE_INFO();
                uint             defaultLength = 14;
                deviceInfo.serial_length = defaultLength;
                deviceInfo.serial        = Marshal.AllocCoTaskMem((int)defaultLength);
                deviceInfo.size          = (uint)Marshal.SizeOf(deviceInfo);
                result = LeapC.GetDeviceInfo(device, out deviceInfo);
                if (result == eLeapRS.eLeapRS_InsufficientBuffer)
                {
                    Marshal.FreeCoTaskMem(deviceInfo.serial);
                    deviceInfo.serial = Marshal.AllocCoTaskMem((int)deviceInfo.serial_length);
                    deviceInfo.size   = (uint)Marshal.SizeOf(deviceInfo);
                    result            = LeapC.GetDeviceInfo(deviceHandle, out deviceInfo);
                }

                if (result == eLeapRS.eLeapRS_Success)
                {
                    Device apiDevice = new Device(deviceHandle,
                                                  deviceInfo.h_fov,           //radians
                                                  deviceInfo.v_fov,           //radians
                                                  deviceInfo.range / 1000,    //to mm
                                                  deviceInfo.baseline / 1000, //to mm
                                                  (deviceInfo.caps == (UInt32)eLeapDeviceCaps.eLeapDeviceCaps_Embedded),
                                                  (deviceInfo.status == (UInt32)eLeapDeviceStatus.eLeapDeviceStatus_Streaming),
                                                  Marshal.PtrToStringAnsi(deviceInfo.serial));
                    Marshal.FreeCoTaskMem(deviceInfo.serial);
                    _devices.AddOrUpdate(apiDevice);
                    this.LeapDevice.Dispatch(this, new DeviceEventArgs(apiDevice));
                }
            }
        }
Esempio n. 26
0
        public bool SetPaused(bool pause)
        {
            eLeapRS result = LeapC.LeapSetPause(Handle, pause);

            return(result == eLeapRS.eLeapRS_Success);
        }
        public void GetNearestFrameTimes(Int64 time, out Int64 before, out Int64 after)
        {
            eLeapRS result = LeapC.GetNearestFrames(_leapConnection, time, out before, out after);

            reportAbnormalResults("LeapC get nearest frame timestamp call was ", result);
        }
Esempio n. 28
0
        public void GetInterpolatedHeadPose(ref LEAP_HEAD_POSE_EVENT toFill, Int64 time)
        {
            eLeapRS result = LeapC.InterpolateHeadPose(_leapConnection, time, ref toFill);

            reportAbnormalResults("LeapC get interpolated head pose call was ", result);
        }
Esempio n. 29
0
        protected virtual void Update()
        {
            if (_workerThreadProfiling)
            {
                LeapProfiling.Update();
            }

            if (!checkConnectionIntegrity())
            {
                return;
            }

#if UNITY_EDITOR
            if (UnityEditor.EditorApplication.isCompiling)
            {
                UnityEditor.EditorApplication.isPlaying = false;
                Debug.LogWarning("Unity hot reloading not currently supported. Stopping Editor Playback.");
                return;
            }
#endif

            _fixedOffset.Update(Time.time - Time.fixedTime, Time.deltaTime);

            if (_frameOptimization == FrameOptimizationMode.ReusePhysicsForUpdate)
            {
                DispatchUpdateFrameEvent(_transformedFixedFrame);
                return;
            }

#if SVR
            if (_clockRebaser != IntPtr.Zero)
            {
                eLeapRS result = LeapC.UpdateRebase(_clockRebaser, _stopwatch.ElapsedMilliseconds, LeapC.GetNow());
                if (result != eLeapRS.eLeapRS_Success)
                {
                    Debug.LogWarning("UpdateRebase call failed");
                }
            }
#endif

            if (_useInterpolation)
            {
#if !UNITY_ANDROID || UNITY_EDITOR
                _smoothedTrackingLatency.value = Mathf.Min(_smoothedTrackingLatency.value, 30000f);
                _smoothedTrackingLatency.Update((float)(_leapController.Now() - _leapController.FrameTimestamp()), Time.deltaTime);
#endif
                long timestamp = CalculateInterpolationTime() + (ExtrapolationAmount * 1000);
                _unityToLeapOffset = timestamp - (long)(Time.time * S_TO_NS);

                _leapController.GetInterpolatedFrameFromTime(_untransformedUpdateFrame, timestamp, CalculateInterpolationTime() - (BounceAmount * 1000));
            }
            else
            {
                _leapController.Frame(_untransformedUpdateFrame);
            }

            if (_untransformedUpdateFrame != null)
            {
                transformFrame(_untransformedUpdateFrame, _transformedUpdateFrame);

                DispatchUpdateFrameEvent(_transformedUpdateFrame);
            }
        }
Esempio n. 30
0
        public void TelemetryProfiling(ref LEAP_TELEMETRY_DATA telemetryData)
        {
            eLeapRS result = LeapC.LeapTelemetryProfiling(_leapConnection, ref telemetryData);

            reportAbnormalResults("LeapC TelemetryProfiling call was ", result);
        }