Esempio n. 1
0
        public override void Update()
        {
            base.Update();

            // Update sensors
            // Enable/disable supported sensors and update enabled sensors
            UpdateSensorPair(accelerometerListener, accelerometerSensor,
                             (sensor, listener) => sensor.Acceleration = listener.GetCurrentValuesAsVector());
            UpdateSensorPair(gyroscopeListener, gyroscopeSensor,
                             (sensor, listener) => sensor.RotationRate = -listener.GetCurrentValuesAsVector());
            UpdateSensorPair(linearAccelerationListener, userAccelerationSensor,
                             (sensor, listener) => sensor.Acceleration = listener.GetCurrentValuesAsVector());
            UpdateSensorPair(gravityListener, gravitySensor,
                             (sensor, listener) => sensor.Vector = listener.GetCurrentValuesAsVector());

            // Enabled/Disable/Update Orientation
            if (orientationListener != null)
            {
                bool enable = orientationSensor.IsEnabled || compassSensor.IsEnabled; // Orientation is used for compass as well
                if (enable != orientationListener.Enabled)
                {
                    if (enable)
                    {
                        orientationListener.Enable();
                    }
                    else
                    {
                        orientationListener.Disable();
                    }
                }

                if (enable)
                {
                    // Update orientation
                    base.Update();

                    var orientationValues = orientationListener.GetValues();
                    if (orientationValues == null || orientationValues.Count < 3)
                    {
                        return;
                    }

                    rotationVector[0] = orientationValues[0];
                    rotationVector[1] = orientationValues[1];
                    rotationVector[2] = orientationValues[2];
                    SensorManager.GetQuaternionFromVector(quaternionArray, rotationVector);

                    var quaternion = Quaternion.Identity;
                    quaternion.W = +quaternionArray[0];
                    quaternion.X = -quaternionArray[1];
                    quaternion.Y = +quaternionArray[3];
                    quaternion.Z = +quaternionArray[2];
                    quaternion   = Quaternion.RotationY(MathUtil.Pi) * quaternion; // align the orientation with north.
                    orientationSensor.FromQuaternion(quaternion);

                    // Update compass
                    compassSensor.Heading = -orientationSensor.Yaw;
                }
            }
        }
Esempio n. 2
0
 public override void Pause()
 {
     // Disable all sensors when application is paused
     accelerometerListener?.Disable();
     gyroscopeListener?.Disable();
     linearAccelerationListener?.Disable();
     orientationListener?.Disable();
     gravityListener?.Disable();
 }
Esempio n. 3
0
        /// <summary>
        /// Tries to create a sensor listener if it is supported
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public AndroidSensorListener TryGetSensorListener(SensorType type)
        {
            var listener = new AndroidSensorListener(type);

            if (!listener.Enable())
            {
                listener.Dispose();
                return(null);
            }

            listener.Disable();
            return(listener);
        }
Esempio n. 4
0
        public void UpdateSensorPair <TSensor>(AndroidSensorListener listener, TSensor sensor, Action <TSensor, AndroidSensorListener> updater) where TSensor : Sensor
        {
            if (listener != null)
            {
                bool enable = sensor.IsEnabled;
                if (enable != listener.Enabled)
                {
                    if (enable)
                    {
                        listener.Enable();
                    }
                    else
                    {
                        listener.Disable();
                    }
                }

                if (enable)
                {
                    updater.Invoke(sensor, listener);
                }
            }
        }