Beispiel #1
0
        /// <summary>
        /// Handler of callback by OS planned to be used with default OSXDriver
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="res">Res.</param>
        /// <param name="sender">Sender.</param>
        /// <param name="valRef">Value reference.</param>
        internal void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef valRef)
        {
            IOHIDElementRef element = Native.IOHIDValueGetElement(valRef);
            uint            uid     = Native.IOHIDElementGetCookie(element);
            int             value;

            Native.IOHIDElementType type = Native.IOHIDElementGetType(element);
            GenericHIDDevice        device;

            value = Native.IOHIDValueGetIntegerValue(valRef);

            //UnityEngine.Debug.Log ("DeviceValueReceived:"+value);


            try{
                GCHandle gch = GCHandle.FromIntPtr(context);
                device = (GenericHIDDevice)gch.Target;
            }
            catch (Exception e) {
                UnityEngine.Debug.LogException(e);
                return;
            }



            byte[] typeBuff  = BitConverter.GetBytes((uint)type);
            byte[] uidBuff   = BitConverter.GetBytes(uid);
            byte[] valueBuff = BitConverter.GetBytes(value);


            byte[] result = new byte[typeBuff.Length + uidBuff.Length + valueBuff.Length];
            System.Buffer.BlockCopy(typeBuff, 0, result, 0, typeBuff.Length);
            System.Buffer.BlockCopy(uidBuff, 0, result, typeBuff.Length, uidBuff.Length);
            System.Buffer.BlockCopy(valueBuff, 0, result, typeBuff.Length + uidBuff.Length, valueBuff.Length);



            device.__lastHIDReport.Status = HIDReport.ReadStatus.Success;
            device.__lastHIDReport.Data   = result;
        }
Beispiel #2
0
        /// <summary>
        /// Devices the value received.
        /// </summary>
        /// <param name="device">Device.</param>
        /// <param name="type">Type.</param>
        /// <param name="uid">Uid.</param>
        /// <param name="value">Value.</param>
        internal void DeviceValueReceived(IDevice device, Native.IOHIDElementType type, uint uid, int value)
        {
            //		UnityEngine.Debug.Log ("OSXDriver>>DeviceValueReceived type="+type+" uid:"+uid+" value:"+value);


            //AXIS
            if (type == Native.IOHIDElementType.kIOHIDElementTypeInput_Misc ||
                type == Native.IOHIDElementType.kIOHIDElementTypeInput_Axis)
            {
                int         numAxes = device.Axis.Count;
                AxisDetails axisDetails;

                //numAxes
                for (int axisIndex = 0; axisIndex < numAxes; axisIndex++)
                {
                    axisDetails = device.Axis[axisIndex] as AxisDetails;

                    //UnityEngine.Debug.Log ("OSXDriver>>DeviceValueReceived axisDetails="+axisDetails.uid);

                    if (axisDetails != null)
                    {
                        if (axisDetails.uid == uid)
                        {
                            //UnityEngine.Debug.Log ("OSXDriver>>DeviceValueReceived Axis["+axisIndex+"] axisDetails.uid="+axisDetails.uid);

                            //check hatch
                            //Check if POV element.
                            if (axisDetails.isHat)
                            {
                                //UnityEngine.Debug.Log ("OSXDriver>>DeviceValueReceived axisDetails.uid="+axisDetails.isHat);

                                //Workaround for POV hat switches that do not have null states.
                                if (!axisDetails.isNullable)
                                {
                                    value = value < axisDetails.min ? axisDetails.max - axisDetails.min + 1 : value - 1;
                                }


                                float outX = 0f;
                                float outY = 0f;

                                if (value < 15)
                                {
                                    hatValueToXY(value, (axisDetails.max - axisDetails.min) + 1, out outX, out outY);
                                }

                                device.Axis[JoystickAxis.AxisPovX].value = outX;
                                device.Axis[JoystickAxis.AxisPovY].value = outY;

                                axisIndex++;                        //cos 2 axis are handled

//							UnityEngine.Debug.Log("POVX:"+device.Axis[JoystickAxis.AxisPovX].value+" POVY:"+device.Axis[JoystickAxis.AxisPovY].value);
                            }
                            else
                            {
//							if(axisIndex==2){
//								UnityEngine.Debug.Log("ORG Axis"+axisIndex+" Counts:"+ (int)value+" min:"+axisDetails.min+" max:"+axisDetails.max);
//
//							}


                                //Sanity check.
                                if (value < axisDetails.min)
                                {
                                    value = axisDetails.min;
                                }
                                if (value > axisDetails.max)
                                {
                                    value = axisDetails.max;
                                }

                                //Calculate the -1 to 1 float from the min and max possible values.
                                float analogValue = 0f;

                                //if trigger
                                if (axisDetails.isTrigger)
                                {
                                    analogValue = (float)value / axisDetails.max;
                                }
                                else
                                {
                                    analogValue = (value - axisDetails.min) / (float)(axisDetails.max - axisDetails.min) * 2.0f - 1.0f;
                                }



                                axisDetails.value = analogValue;
                            }
                        }
                        else                  //cos update of values happen only on "interrupt" and its returning only one element value
                                              //the rest should take previous value which would update ButtonState
                        //!!! side effect inputput might left to "hold" forever

                        {
                            axisDetails.value = axisDetails.value;
                        }
                    }

//					if(axisIndex==1){
//						//UnityEngine.Debug.Log("Axis"+axisIndex+" Counts:"+ value+" min:"+axisDetails.min+" max:"+axisDetails.max);
//						//UnityEngine.Debug.Log("Axis"+axisIndex+" Value:"+axisDetails.value);
//		}
                }                //end for

                //BUTTONS
            }
            else if (type == Native.IOHIDElementType.kIOHIDElementTypeInput_Button)
            {
                int            numButtons = device.Buttons.Count;
                IButtonDetails buttonDetails;

                for (int buttonIndex = 0; buttonIndex < numButtons; buttonIndex++)
                {
                    buttonDetails = device.Buttons[buttonIndex];

                    if (buttonDetails == null)
                    {
                        continue;
                    }

                    if (buttonDetails.uid == uid)
                    {
                        buttonDetails.value = value;

                        //		UnityEngine.Debug.Log("Button "+buttonIndex+" value:"+value+" State:"+device.Buttons[buttonIndex].buttonState);
                    }
                    else                      //cos update of values happen only on "interrupt" and its returning only one element value
                                              //the rest should take previous value which would update ButtonState

                    {
                        buttonDetails.value = buttonDetails.value;
                    }
                }
            }
        }