Provide static APIs to retrieve device index by semantic role Same mapping logic as SteamVR_ControllerManager does
        public static void BindAllCurrentDeviceClassMappings(VRModuleDeviceClass deviceClass)
        {
            for (int i = 0, imax = ViveRoleEnum.ValidViveRoleTable.Count; i < imax; ++i)
            {
                var roleMap  = ViveRole.GetMap(ViveRoleEnum.ValidViveRoleTable.GetValueByIndex(i));
                var roleInfo = roleMap.RoleValueInfo;
                for (int rv = roleInfo.MinValidRoleValue, rvmax = roleInfo.MaxValidRoleValue; rv <= rvmax; ++rv)
                {
                    if (!roleInfo.IsValidRoleValue(rv))
                    {
                        continue;
                    }
                    if (roleMap.IsRoleValueBound(rv))
                    {
                        continue;
                    }

                    var mappedDevice      = roleMap.GetMappedDeviceByRoleValue(rv);
                    var mappedDeviceState = VRModule.GetCurrentDeviceState(mappedDevice);
                    if (mappedDeviceState.deviceClass != deviceClass)
                    {
                        continue;
                    }

                    roleMap.BindDeviceToRoleValue(mappedDeviceState.serialNumber, rv);
                }
            }
        }
        private void MappingTrackers()
        {
            var role  = RoleMap.RoleInfo.MinValidRole;
            var index = (uint)1;

            while (true)
            {
                while (!RoleMap.RoleInfo.IsValidRole(role) || RoleMap.IsRoleMapped(role) || RoleMap.IsRoleBound(role))
                {
                    if (++role > RoleMap.RoleInfo.MaxValidRole)
                    {
                        return;
                    }
                }

                while (ViveRole.GetDeviceClass(index) != ETrackedDeviceClass.GenericTracker || RoleMap.IsDeviceMapped(index) || RoleMap.IsDeviceConnectedAndBound(index))
                {
                    if (++index >= ViveRole.MAX_DEVICE_COUNT)
                    {
                        return;
                    }
                }

                MappingRole(role++, index++);

                if (role > RoleMap.RoleInfo.MaxValidRole || index >= ViveRole.MAX_DEVICE_COUNT)
                {
                    return;
                }
            }
        }
 public static void UnbindAllCurrentBindings()
 {
     for (int i = 0, imax = ViveRoleEnum.ValidViveRoleTable.Count; i < imax; ++i)
     {
         var roleMap = ViveRole.GetMap(ViveRoleEnum.ValidViveRoleTable.GetValueByIndex(i));
         roleMap.UnbindAll();
     }
 }
        /// <summary>
        /// Trigger vibration of the controller identified by role
        /// </summary>
        public static void TriggerHapticPulse(HandRole role, ushort intensity = 500)
        {
            var system = Valve.VR.OpenVR.System;

            if (system != null)
            {
                system.TriggerHapticPulse(ViveRole.GetDeviceIndex(role), (uint)Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad - (uint)Valve.VR.EVRButtonId.k_EButton_Axis0, (char)intensity);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Trigger vibration of the controller identified by role
        /// </summary>
        public static void TriggerHapticPulse(HandRole role, ushort durationMicroSec = 500)
        {
            var system = OpenVR.System;

            if (system != null)
            {
                system.TriggerHapticPulse(ViveRole.GetDeviceIndex(role), (uint)EVRButtonId.k_EButton_SteamVR_Touchpad - (uint)EVRButtonId.k_EButton_Axis0, (char)durationMicroSec);
            }
        }
        public static Vector3 GetAngularVelocity(DeviceRole role, Transform origin = null)
        {
            var index    = ViveRole.GetDeviceIndex(role);
            var rawValue = Vector3.zero;

            if (index < rawPoses.Length)
            {
                rawValue = new Vector3(-rawPoses[index].vAngularVelocity.v0, -rawPoses[index].vAngularVelocity.v1, rawPoses[index].vAngularVelocity.v2);
            }

            return(origin == null ? rawValue : origin.TransformVector(rawValue));
        }
        public static int ApplyBindingConfigToRoleMap(params Type[] roleTypeFilter)
        {
            var appliedCount = 0;
            var filterUsed   = roleTypeFilter != null && roleTypeFilter.Length > 0;

            foreach (var roleData in s_bindingConfig.roles)
            {
                Type roleType;
                if (string.IsNullOrEmpty(roleData.type) || !ViveRoleEnum.ValidViveRoleTable.TryGetValue(roleData.type, out roleType))
                {
                    continue;
                }

                if (filterUsed)
                {
                    // apply filter
                    var filtered = false;
                    foreach (var t in roleTypeFilter)
                    {
                        if (roleType == t)
                        {
                            filtered = true; break;
                        }
                    }
                    if (!filtered)
                    {
                        continue;
                    }
                }

                var roleMap = ViveRole.GetMap(roleType);
                roleMap.UnbindAll();

                foreach (var binding in roleData.bindings)
                {
                    // bind device according to role_name first
                    // if role_name is invalid then role_value is used
                    int roleValue;
                    if (string.IsNullOrEmpty(binding.role_name) || !roleMap.RoleValueInfo.TryGetRoleValueByName(binding.role_name, out roleValue))
                    {
                        roleValue = binding.role_value;
                    }

                    roleMap.BindDeviceToRoleValue(binding.device_sn, roleValue);
                    ++appliedCount;
                }
            }

            return(appliedCount);
        }
        public void Refresh()
        {
            UnmappingAll();

            var role        = DeviceRole.Hmd;
            var deviceIndex = 0u;

            for (; role <= DeviceRole.Device15 && deviceIndex < ViveRole.MAX_DEVICE_COUNT; ++role, ++deviceIndex)
            {
                if (ViveRole.GetDeviceClass(deviceIndex) == ETrackedDeviceClass.Invalid)
                {
                    continue;
                }
                MappingRoleIfUnbound(role, deviceIndex);
            }
        }
        /// <summary>
        /// Returns tracking pose of the device identified by role
        /// </summary>
        public static Pose GetPose(DeviceRole role, Transform origin = null)
        {
            var index   = ViveRole.GetDeviceIndex(role);
            var rawPose = new Pose();

            if (index < poses.Length)
            {
                rawPose = poses[index];
            }

            if (origin != null)
            {
                rawPose = new Pose(origin) * rawPose;
                rawPose.pos.Scale(origin.localScale);
            }

            return(rawPose);
        }
            // return true if  frame skipped
            public override bool Update()
            {
                if (Time.frameCount == updatedFrameCount)
                {
                    return(true);
                }
                updatedFrameCount = Time.frameCount;

                var deviceIndex = m_map.GetMappedDeviceByRoleValue(m_roleValue);

                if (deviceIndex == updatedDeviceIndex && !ViveRole.IsValidIndex(deviceIndex))
                {
                    return(false);
                }
                updatedDeviceIndex = deviceIndex;

                previousState = currentState;
                currentState  = ViveRole.IsValidIndex(deviceIndex) ? s_controllerStats[deviceIndex] : default(VRControllerState_t);

                this.UpdateHairTrigger();

                if (GetPressDown(ControllerButton.Pad))
                {
                    padDownAxis = GetAxis();
                }
                if (GetPressDown(ControllerButton.PadTouch))
                {
                    padTouchDownAxis = GetAxis();
                }

                for (int i = 0; i < CONTROLLER_BUTTON_COUNT; ++i)
                {
                    UpdateClickCount((ControllerButton)i);
                }

                for (int i = 0; i < CONTROLLER_BUTTON_COUNT; ++i)
                {
                    InvokeEvent((ControllerButton)i);
                }

                return(false);
            }
Ejemplo n.º 11
0
            public void Update()
            {
                if (Time.frameCount != prevFrameCount)
                {
                    prevFrameCount = Time.frameCount;
                    previousState  = currentState;

                    CVRSystem system;
                    index = ViveRole.GetDeviceIndex(role);
                    if (!ViveRole.IsValidIndex(index) || (system = OpenVR.System) == null || !system.GetControllerState(index, ref currentState))
                    {
                        currentState = default(VRControllerState_t);
                    }

                    UpdateHairTrigger();

                    if (GetPressDown(ControllerButton.Pad))
                    {
                        padDownAxis = GetAxis();
                    }
                    if (GetPressDown(ControllerButton.PadTouch))
                    {
                        padTouchDownAxis = GetAxis();
                    }

                    for (int i = 0; i < ViveInput.CONTROLLER_BUTTON_COUNT; ++i)
                    {
                        UpdateClickCount((ControllerButton)i);
                    }

                    for (int i = 0; i < ViveInput.CONTROLLER_BUTTON_COUNT; ++i)
                    {
                        InvokeEvent((ControllerButton)i);
                    }
                }
            }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static RigidPose GetPoseEx(Type roleType, int roleValue, Transform origin = null)
 {
     return(GetPose(ViveRole.GetDeviceIndexEx(roleType, roleValue), origin));
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static Vector3 GetAngularVelocityEx(Type roleType, int roleValue, Transform origin = null)
 {
     return(GetAngularVelocity(ViveRole.GetDeviceIndexEx(roleType, roleValue), origin));
 }
Ejemplo n.º 14
0
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static void TriggerHapticPulseEx(Type roleType, int roleValue, ushort durationMicroSec = 500)
 {
     VRModule.TriggerViveControllerHaptic(ViveRole.GetDeviceIndexEx(roleType, roleValue), durationMicroSec);
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static void TriggerHapticVibrationEx(Type roleType, int roleValue, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
 {
     VRModule.TriggerHapticVibration(ViveRole.GetDeviceIndexEx(roleType, roleValue), durationSeconds, frequency, amplitude, startSecondsFromNow);
 }
 public static bool IsCalibrating(DeviceRole role)
 {
     return(IsCalibrating(ViveRole.GetDeviceIndexEx(role)));
 }
 /// <summary>
 /// Returns true if tracking data of the device identified by role has valid value.
 /// </summary>
 public static bool HasTracking(DeviceRole role)
 {
     return(HasTracking(ViveRole.GetDeviceIndexEx(role)));
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static void SetPoseEx(Transform target, Type roleType, int roleValue, Transform origin = null)
 {
     SetPose(target, ViveRole.GetDeviceIndexEx(roleType, roleValue), origin);
 }
 /// <summary>
 /// Returns true if the device identified by role is connected.
 /// </summary>
 public static bool IsConnected(DeviceRole role)
 {
     return(IsConnected(ViveRole.GetDeviceIndexEx(role)));
 }
 /// <typeparam name="TRole">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </typeparam>
 /// <param name="role">
 /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static void SetPoseEx <TRole>(Transform target, TRole role, Transform origin = null)
 {
     SetPose(target, ViveRole.GetDeviceIndexEx(role), origin);
 }
 public static bool IsOutOfRange(DeviceRole role)
 {
     return(IsOutOfRange(ViveRole.GetDeviceIndexEx(role)));
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static bool IsConnectedEx(Type roleType, int roleValue)
 {
     return(IsConnected(ViveRole.GetDeviceIndexEx(roleType, roleValue)));
 }
 public static bool IsUninitialized(DeviceRole role)
 {
     return(IsUninitialized(ViveRole.GetDeviceIndexEx(role)));
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static bool HasTrackingEx(Type roleType, int roleValue)
 {
     return(HasTracking(ViveRole.GetDeviceIndexEx(roleType, roleValue)));
 }
 public RCtrlState(Type roleEnumType, int roleValue)
 {
     m_map          = ViveRole.GetMap(roleEnumType);
     m_roleValue    = roleValue;
     m_roleEnumType = roleEnumType;
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static bool IsOutOfRangeEx(Type roleType, int roleValue)
 {
     return(IsOutOfRange(ViveRole.GetDeviceIndexEx(roleType, roleValue)));
 }
Ejemplo n.º 27
0
 /// <typeparam name="TRole">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </typeparam>
 /// <param name="role">
 /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static void TriggerHapticPulseEx <TRole>(TRole role, ushort durationMicroSec = 500)
 {
     VRModule.TriggerViveControllerHaptic(ViveRole.GetDeviceIndexEx(role), durationMicroSec);
 }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static bool IsCalibratingEx(Type roleType, int roleValue)
 {
     return(IsCalibrating(ViveRole.GetDeviceIndexEx(roleType, roleValue)));
 }
Ejemplo n.º 29
0
        // update type and value changes
        public void Update()
        {
            if (m_lockUpdate && (m_isTypeDirty || m_isValueDirty))
            {
                throw new Exception("Can't change value during onChange event callback");
            }

            var oldRoleType        = m_roleType;
            var oldRoleValue       = m_roleValue;
            var roleTypeChanged    = false;
            var roleValueChanged   = false;
            var deviceIndexChanged = false;

            if (m_isTypeDirty || m_roleType == null)
            {
                m_isTypeDirty = false;

                if (string.IsNullOrEmpty(m_roleTypeFullName) || !ViveRoleEnum.ValidViveRoleTable.TryGetValue(m_roleTypeFullName, out m_roleType))
                {
                    m_roleType = DefaultRoleType;
                }

                roleTypeChanged = oldRoleType != m_roleType;
            }

            // maintain m_roleMap cache
            // m_roleMap could be null on first update
            if (roleTypeChanged || m_roleMap == null)
            {
                if (m_onDeviceIndexChanged != null)
                {
                    if (m_roleMap != null)
                    {
                        m_roleMap.onRoleValueMappingChanged -= OnMappingChanged;
                        m_roleMap = ViveRole.GetMap(m_roleType);
                        m_roleMap.onRoleValueMappingChanged += OnMappingChanged;
                    }
                    else
                    {
                        m_roleMap     = ViveRole.GetMap(m_roleType);
                        m_deviceIndex = m_roleMap.GetMappedDeviceByRoleValue(m_roleValue); // update deviceIndex before first time listening to MappingChanged event
                        m_roleMap.onRoleValueMappingChanged += OnMappingChanged;
                    }
                }
                else
                {
                    m_roleMap = ViveRole.GetMap(m_roleType);
                }
            }

            if (m_isValueDirty || roleTypeChanged)
            {
                m_isValueDirty = false;

                var info = m_roleMap.RoleValueInfo;
                if (string.IsNullOrEmpty(m_roleValueName) || !info.TryGetRoleValueByName(m_roleValueName, out m_roleValue))
                {
                    m_roleValue = info.IsValidRoleValue(m_roleValueInt) ? m_roleValueInt : info.InvalidRoleValue;
                }

                roleValueChanged = oldRoleValue != m_roleValue;
            }

            if (roleTypeChanged || roleValueChanged)
            {
                if (m_onDeviceIndexChanged != null)
                {
                    var oldDeviceIndex = m_deviceIndex;
                    m_deviceIndex = m_roleMap.GetMappedDeviceByRoleValue(m_roleValue);

                    if (VRModule.IsValidDeviceIndex(oldDeviceIndex) || VRModule.IsValidDeviceIndex(m_deviceIndex))
                    {
                        deviceIndexChanged = oldDeviceIndex != m_deviceIndex;
                    }
                }

                m_lockUpdate = true;

                if (m_onChanged != null)
                {
                    m_onChanged();
                }

                if (m_onRoleChanged != null)
                {
                    m_onRoleChanged();
                }

                if (m_onRoleChangedEx != null)
                {
                    m_onRoleChangedEx(oldRoleType, oldRoleValue);
                }

                if (deviceIndexChanged)
                {
                    m_onDeviceIndexChanged(m_deviceIndex);
                }

                m_lockUpdate = false;
            }
        }
 /// <param name="roleType">
 /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
 /// Use ViveRole.ValidateViveRoleEnum() to validate role type
 /// </param>
 public static bool IsUninitializedEx(Type roleType, int roleValue)
 {
     return(IsUninitialized(ViveRole.GetDeviceIndexEx(roleType, roleValue)));
 }