Exemple #1
0
        private void handleConfigResponse(ref LEAP_CONNECTION_MESSAGE configMsg)
        {
            LEAP_CONFIG_RESPONSE_EVENT config_response_evt = LeapC.PtrToStruct <LEAP_CONFIG_RESPONSE_EVENT>(configMsg.eventStructPtr);
            string config_key = "";

            _configRequests.TryGetValue(config_response_evt.requestId, out config_key);
            if (config_key != null)
            {
                _configRequests.Remove(config_response_evt.requestId);
            }

            Config.ValueType dataType;
            object           value;
            uint             requestId = config_response_evt.requestId;

            if (config_response_evt.value.type != eLeapValueType.eLeapValueType_String)
            {
                switch (config_response_evt.value.type)
                {
                case eLeapValueType.eLeapValueType_Boolean:
                    dataType = Config.ValueType.TYPE_BOOLEAN;
                    value    = config_response_evt.value.boolValue;
                    break;

                case eLeapValueType.eLeapValueType_Int32:
                    dataType = Config.ValueType.TYPE_INT32;
                    value    = config_response_evt.value.intValue;
                    break;

                case eLeapValueType.eleapValueType_Float:
                    dataType = Config.ValueType.TYPE_FLOAT;
                    value    = config_response_evt.value.floatValue;
                    break;

                default:
                    dataType = Config.ValueType.TYPE_UNKNOWN;
                    value    = new object();
                    break;
                }
            }
            else
            {
                LEAP_CONFIG_RESPONSE_EVENT_WITH_REF_TYPE config_ref_value =
                    LeapC.PtrToStruct <LEAP_CONFIG_RESPONSE_EVENT_WITH_REF_TYPE>(configMsg.eventStructPtr);
                dataType = Config.ValueType.TYPE_STRING;
                value    = config_ref_value.value.stringValue;
            }
            SetConfigResponseEventArgs args = new SetConfigResponseEventArgs(config_key, dataType, value, requestId);

            this.LeapConfigResponse.Dispatch <SetConfigResponseEventArgs>(this, args);
        }
Exemple #2
0
        private void handleImageCompletion(ref LEAP_IMAGE_COMPLETE_EVENT imageMsg)
        {
            LEAP_IMAGE_PROPERTIES props        = LeapC.PtrToStruct <LEAP_IMAGE_PROPERTIES>(imageMsg.properties);
            ImageReference        pendingImage = null;

            lock (lockPendingImageList)
            {
                _pendingImageRequests.TryGetValue(imageMsg.token.requestID, out pendingImage);
            }
            if (pendingImage != null)
            {
                //Update distortion data, if changed
                if ((_currentDistortionData.Version != imageMsg.matrix_version) || !_currentDistortionData.IsValid)
                {
                    _currentDistortionData         = new DistortionData();
                    _currentDistortionData.Version = imageMsg.matrix_version;
                    _currentDistortionData.Width   = LeapC.DistortionSize; //fixed value for now
                    _currentDistortionData.Height  = LeapC.DistortionSize; //fixed value for now
                    if (_currentDistortionData.Data == null || _currentDistortionData.Data.Length != (2 * _currentDistortionData.Width * _currentDistortionData.Height * 2))
                    {
                        _currentDistortionData.Data = new float[(int)(2 * _currentDistortionData.Width * _currentDistortionData.Height * 2)]; //2 float values per map point
                    }
                    LEAP_DISTORTION_MATRIX matrix = LeapC.PtrToStruct <LEAP_DISTORTION_MATRIX>(imageMsg.distortionMatrix);
                    Array.Copy(matrix.matrix_data, _currentDistortionData.Data, matrix.matrix_data.Length);
                    this.LeapDistortionChange.Dispatch <DistortionEventArgs>(this, new DistortionEventArgs(_currentDistortionData));
                }

                pendingImage.imageData.CompleteImageData(props.type,
                                                         props.format,
                                                         props.bpp,
                                                         props.width,
                                                         props.height,
                                                         imageMsg.info.timestamp,
                                                         imageMsg.info.frame_id,
                                                         props.x_offset,
                                                         props.y_offset,
                                                         props.x_scale,
                                                         props.y_scale,
                                                         _currentDistortionData,
                                                         LeapC.DistortionSize,
                                                         imageMsg.matrix_version);

                Image completedImage = pendingImage.imageObject;
                lock (lockPendingImageList)
                {
                    _pendingImageRequests.Remove(imageMsg.token.requestID);
                }
                this.LeapImageReady.Dispatch <ImageEventArgs>(this, new ImageEventArgs(completedImage));
            }
        }
        public Hand makeHand(ref LEAP_HAND hand, Frame owningFrame)
        {
            LEAP_BONE arm    = LeapC.PtrToStruct <LEAP_BONE>(hand.arm);
            Arm       newArm = makeArm(ref arm);
            LEAP_PALM palm   = LeapC.PtrToStruct <LEAP_PALM>(hand.palm);

            Hand newHand = new Hand(
                (int)owningFrame.Id,
                (int)hand.id,
                hand.confidence,
                hand.grab_strength,
                hand.grab_angle,
                hand.pinch_strength,
                hand.pinch_distance,
                palm.width,
                hand.type == eLeapHandType.eLeapHandType_Left,
                hand.visible_time,
                newArm,
                new List <Finger>(5),
                new Vector(palm.position.x, palm.position.y, palm.position.z),
                new Vector(palm.stabilized_position.x, palm.stabilized_position.y, palm.stabilized_position.z),
                new Vector(palm.velocity.x, palm.velocity.y, palm.velocity.z),
                new Vector(palm.normal.x, palm.normal.y, palm.normal.z),
                new Vector(palm.direction.x, palm.direction.y, palm.direction.z),
                newArm.NextJoint //wrist position
                );
            LEAP_DIGIT thumbDigit = LeapC.PtrToStruct <LEAP_DIGIT>(hand.thumb);

            newHand.Fingers.Insert(0, makeFinger(owningFrame, ref hand, ref thumbDigit, Finger.FingerType.TYPE_THUMB));

            LEAP_DIGIT indexDigit = LeapC.PtrToStruct <LEAP_DIGIT>(hand.index);

            newHand.Fingers.Insert(1, makeFinger(owningFrame, ref hand, ref indexDigit, Finger.FingerType.TYPE_INDEX));

            LEAP_DIGIT middleDigit = LeapC.PtrToStruct <LEAP_DIGIT>(hand.middle);

            newHand.Fingers.Insert(2, makeFinger(owningFrame, ref hand, ref middleDigit, Finger.FingerType.TYPE_MIDDLE));

            LEAP_DIGIT ringDigit = LeapC.PtrToStruct <LEAP_DIGIT>(hand.ring);

            newHand.Fingers.Insert(3, makeFinger(owningFrame, ref hand, ref ringDigit, Finger.FingerType.TYPE_RING));

            LEAP_DIGIT pinkyDigit = LeapC.PtrToStruct <LEAP_DIGIT>(hand.pinky);

            newHand.Fingers.Insert(4, makeFinger(owningFrame, ref hand, ref pinkyDigit, Finger.FingerType.TYPE_PINKY));

            return(newHand);
        }
        public Frame makeFrame(ref LEAP_TRACKING_EVENT trackingMsg)
        {
            Frame newFrame = new Leap.Frame((long)trackingMsg.info.frame_id,
                                            (long)trackingMsg.info.timestamp,
                                            trackingMsg.framerate,
                                            new InteractionBox(trackingMsg.interaction_box_center.ToLeapVector(),
                                                               trackingMsg.interaction_box_size.ToLeapVector()),
                                            new List <Hand>((int)trackingMsg.nHands)
                                            );

            int pHandArrayOffset = 0;

            for (int h = 0; h < trackingMsg.nHands; h++)
            {
                LEAP_HAND hand = LeapC.PtrToStruct <LEAP_HAND>(new IntPtr(trackingMsg.pHands.ToInt64() + pHandArrayOffset));
                pHandArrayOffset += handStructSize;
                newFrame.Hands.Add(makeHand(ref hand, newFrame));
            }
            return(newFrame);
        }
Exemple #5
0
        //Run in Polster thread, fills in object queues
        private void processMessages()
        {
            try
            {
                eLeapRS result;
                LeapInit.Dispatch <LeapEventArgs>(this, new LeapEventArgs(LeapEvent.EVENT_INIT));
                while (_isRunning)
                {
                    if (_leapConnection != IntPtr.Zero)
                    {
                        LEAP_CONNECTION_MESSAGE _msg = new LEAP_CONNECTION_MESSAGE();
                        uint timeout = 1000;
                        result = LeapC.PollConnection(_leapConnection, timeout, ref _msg);
                        reportAbnormalResults("LeapC PollConnection call was ", result);
                        if (result == eLeapRS.eLeapRS_Success)
                        {
                            switch (_msg.type)
                            {
                            case eLeapEventType.eLeapEventType_Connection:
                                LEAP_CONNECTION_EVENT connection_evt = LeapC.PtrToStruct <LEAP_CONNECTION_EVENT>(_msg.eventStructPtr);
                                handleConnection(ref connection_evt);
                                break;

                            case eLeapEventType.eLeapEventType_ConnectionLost:
                                LEAP_CONNECTION_LOST_EVENT connection_lost_evt = LeapC.PtrToStruct <LEAP_CONNECTION_LOST_EVENT>(_msg.eventStructPtr);
                                handleConnectionLost(ref connection_lost_evt);
                                break;

                            case eLeapEventType.eLeapEventType_Device:
                                LEAP_DEVICE_EVENT device_evt = LeapC.PtrToStruct <LEAP_DEVICE_EVENT>(_msg.eventStructPtr);
                                handleDevice(ref device_evt);
                                break;

                            case eLeapEventType.eLeapEventType_DeviceLost:
                                LEAP_DEVICE_EVENT device_lost_evt = LeapC.PtrToStruct <LEAP_DEVICE_EVENT>(_msg.eventStructPtr);
                                handleLostDevice(ref device_lost_evt);
                                break;

                            case eLeapEventType.eLeapEventType_DeviceFailure:
                                LEAP_DEVICE_FAILURE_EVENT device_failure_evt = LeapC.PtrToStruct <LEAP_DEVICE_FAILURE_EVENT>(_msg.eventStructPtr);
                                handleFailedDevice(ref device_failure_evt);
                                break;

                            case eLeapEventType.eLeapEventType_Tracking:
                                LEAP_TRACKING_EVENT tracking_evt = LeapC.PtrToStruct <LEAP_TRACKING_EVENT>(_msg.eventStructPtr);
                                handleTrackingMessage(ref tracking_evt);
                                break;

                            case eLeapEventType.eLeapEventType_ImageComplete:
                                completeCount++;
                                LEAP_IMAGE_COMPLETE_EVENT image_complete_evt = LeapC.PtrToStruct <LEAP_IMAGE_COMPLETE_EVENT>(_msg.eventStructPtr);
                                handleImageCompletion(ref image_complete_evt);
                                break;

                            case eLeapEventType.eLeapEventType_ImageRequestError:
                                failedCount++;
                                LEAP_IMAGE_FRAME_REQUEST_ERROR_EVENT failed_image_evt = LeapC.PtrToStruct <LEAP_IMAGE_FRAME_REQUEST_ERROR_EVENT>(_msg.eventStructPtr);
                                handleFailedImageRequest(ref failed_image_evt);
                                break;

                            case eLeapEventType.eLeapEventType_TrackedQuad:
                                LEAP_TRACKED_QUAD_EVENT quad_evt = LeapC.PtrToStruct <LEAP_TRACKED_QUAD_EVENT>(_msg.eventStructPtr);
                                handleQuadMessage(ref quad_evt);
                                break;

                            case eLeapEventType.eLeapEventType_LogEvent:
                                LEAP_LOG_EVENT log_evt = LeapC.PtrToStruct <LEAP_LOG_EVENT>(_msg.eventStructPtr);
                                reportLogMessage(ref log_evt);
                                break;

                            case eLeapEventType.eLeapEventType_PolicyChange:
                                LEAP_POLICY_EVENT policy_evt = LeapC.PtrToStruct <LEAP_POLICY_EVENT>(_msg.eventStructPtr);
                                handlePolicyChange(ref policy_evt);
                                break;

                            case eLeapEventType.eLeapEventType_ConfigChange:
                                LEAP_CONFIG_CHANGE_EVENT config_change_evt = LeapC.PtrToStruct <LEAP_CONFIG_CHANGE_EVENT>(_msg.eventStructPtr);
                                handleConfigChange(ref config_change_evt);
                                break;

                            case eLeapEventType.eLeapEventType_ConfigResponse:
                                handleConfigResponse(ref _msg);
                                break;

                            default:
                                //discard unknown message types
                                Logger.Log("Unhandled message type " + Enum.GetName(typeof(eLeapEventType), _msg.type));
                                break;
                            } //switch on _msg.type
                        }     // if valid _msg.type
                        else if (result == eLeapRS.eLeapRS_NotConnected)
                        {
                            this.LeapConnectionLost.Dispatch <ConnectionLostEventArgs>(this, new ConnectionLostEventArgs());
                            result = LeapC.CreateConnection(out _leapConnection);
                            reportAbnormalResults("LeapC CreateConnection call was ", result);
                            result = LeapC.OpenConnection(_leapConnection);
                            reportAbnormalResults("LeapC OpenConnection call was ", result);
                        }
                    } // if have connection handle
                }     //while running
            }
            catch (Exception e)
            {
                Logger.Log("Exception: " + e);
            }
        }