public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            motionManager = new CMMotionManager();

            StartButton.TouchUpInside += (sender, e) =>
            {
                gyroData = new ObservableCollection <double>();
                motionManager.StartGyroUpdates(NSOperationQueue.CurrentQueue, (data, error) =>
                {
                    gyroData.Add(data.RotationRate.x);
                    gyroData.Add(data.RotationRate.y);
                    gyroData.Add(data.RotationRate.z);
                    var shake = gyroData.Where(gyroData => Math.Abs(gyroData) > 3).Count();
                    if (shake > 3)
                    {
                        motionManager.StopGyroUpdates();

                        var alert = new UIAlertView("Gyroscope", "Shaked!", null, "OK");
                        alert.Show();
                    }

                    this.xLabel.Text = data.RotationRate.x.ToString("0.0000");
                    this.yLabel.Text = data.RotationRate.y.ToString("0.0000");
                    this.zLabel.Text = data.RotationRate.z.ToString("0.0000");
                });
            };

            StopButton.TouchUpInside += (sender, e) =>
            {
                motionManager.StopGyroUpdates();
            };
        }
Exemplo n.º 2
0
        internal override void CheckAndDisableSensors()
        {
            base.CheckAndDisableSensors();

            if (Accelerometer.ShouldBeDisabled)
            {
                motionManager.StopAccelerometerUpdates();
            }

            if ((Compass.ShouldBeDisabled || Orientation.ShouldBeDisabled) && !Compass.IsEnabled && !Orientation.IsEnabled)
            {
                locationManagerActivated = false;
                locationManager.StopUpdatingHeading();
            }

            if (Gyroscope.ShouldBeDisabled)
            {
                motionManager.StopGyroUpdates();
            }

            if ((UserAcceleration.ShouldBeDisabled || Gravity.ShouldBeDisabled || Orientation.ShouldBeDisabled) && !UserAcceleration.IsEnabled && !Gravity.IsEnabled && !Orientation.IsEnabled)
            {
                motionManager.StopDeviceMotionUpdates();
            }
        }
        public void StopUpdatesWithMotionType(MotionDataType type)
        {
            switch (graphDataSource)
            {
            case MotionDataType.AccelerometerData:
                if (mManager.AccelerometerActive)
                {
                    mManager.StopAccelerometerUpdates();
                }
                break;

            case MotionDataType.GyroData:
                if (mManager.GyroActive)
                {
                    mManager.StopGyroUpdates();
                }
                break;

            case MotionDataType.DeviceMotion:
                if (mManager.DeviceMotionActive)
                {
                    mManager.StopDeviceMotionUpdates();
                }
                break;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Stop the specified sensor type reading.
        /// </summary>
        /// <param name="sensorType">Sensor type</param>
        public override void Stop(SensorType sensorType)
        {
            switch (sensorType)
            {
            case SensorType.Accelerometer:
                AccelerometerActive = false;
                motionManager.StopAccelerometerUpdates();
                break;

            case SensorType.Gyroscope:
                GyroActive = false;
                motionManager.StopGyroUpdates();
                break;

            case SensorType.DeviceMotion:
                DeviceMotionActive = false;
                motionManager.StopDeviceMotionUpdates();
                break;

            case SensorType.Compass:
                CompassActive = false;
                locationManager.StopUpdatingHeading();
                break;

            case SensorType.LightLevel:
                LightLevelActive = false;
                break;
            }
        }
 public void Stop()
 {
     motionManager.StopAccelerometerUpdates();
     motionManager.StopGyroUpdates();
     prevAccelTimeStamp = 0;
     prevGyroTimeStamp  = 0;
 }
Exemplo n.º 6
0
        public void StopSensorUpdates()
        {
            if (_motionManager.AccelerometerAvailable)
            {
                _motionManager.StopAccelerometerUpdates();
            }

            if (_motionManager.GyroAvailable)
            {
                _motionManager.StopGyroUpdates();
            }

            if (_motionManager.MagnetometerAvailable)
            {
                _motionManager.StopMagnetometerUpdates();
            }

            if (_motionManager.DeviceMotionAvailable)
            {
                _motionManager.StopDeviceMotionUpdates();
                _pedometer.StopPedometerUpdates();
            }

            IsListening = false;
        }
        /// <summary>
        /// Stop the specified sensorType.
        /// </summary>
        /// <param name="sensorType">Sensor type.</param>
        public void Stop(MotionSensorType sensorType)
        {
            switch (sensorType)
            {
            case MotionSensorType.Accelerometer:
                if (motionManager.AccelerometerActive)
                {
                    motionManager.StopAccelerometerUpdates();
                }
                else
                {
                    Debug.WriteLine("Accelerometer not available");
                }
                break;

            case MotionSensorType.Gyroscope:
                if (motionManager.GyroActive)
                {
                    motionManager.StopGyroUpdates();
                }
                else
                {
                    Debug.WriteLine("Gyroscope not available");
                }
                break;

            case MotionSensorType.Magnetometer:
                if (motionManager.MagnetometerActive)
                {
                    motionManager.StopMagnetometerUpdates();
                }
                else
                {
                    Debug.WriteLine("Magnetometer not available");
                }
                break;

            case MotionSensorType.Compass:
                if (CLLocationManager.HeadingAvailable)
                {
                    locationManager.StopUpdatingHeading();
                    locationManager.UpdatedHeading -= OnHeadingChanged;
                }
                else
                {
                    Debug.WriteLine("Compass not available");
                }
                break;
            }
            sensorStatus[sensorType] = false;
        }
Exemplo n.º 8
0
        public override void Update()
        {
            base.Update();

            // Enable/disable supported sensors and update enabled sensors
            if (accelerometerSensor != null)
            {
                bool enable = accelerometerSensor.IsEnabled;
                if (enable != motionManager.AccelerometerActive)
                {
                    if (accelerometerSensor.IsEnabled)
                    {
                        motionManager.StartAccelerometerUpdates();
                    }
                    else
                    {
                        motionManager.StopAccelerometerUpdates();
                    }
                }

                if (enable)
                {
                    var accelerometerData = motionManager.AccelerometerData;
                    accelerometerSensor.Acceleration = accelerometerData != null?CmAccelerationToVector3(accelerometerData.Acceleration) : Vector3.Zero;
                }
            }
            if (compassSensor != null)
            {
                bool enable = compassSensor.IsEnabled;
                if (enable != locationManagerActivated)
                {
                    if (compassSensor.IsEnabled)
                    {
                        locationManager.StartUpdatingHeading();
                    }
                    else
                    {
                        locationManager.StopUpdatingHeading();
                    }

                    locationManagerActivated = compassSensor.IsEnabled;
                }

                if (enable)
                {
                    compassSensor.Heading = GetNorthInRadian(locationManager);
                }
            }
            if (gyroscopeSensor != null)
            {
                bool enable = gyroscopeSensor.IsEnabled;
                if (enable != motionManager.GyroActive)
                {
                    if (gyroscopeSensor.IsEnabled)
                    {
                        motionManager.StartGyroUpdates();
                    }
                    else
                    {
                        motionManager.StopGyroUpdates();
                    }
                }

                if (enable)
                {
                    var gyroData = motionManager.GyroData;
                    gyroscopeSensor.RotationRate = gyroData != null?CmRotationRateToVector3(gyroData.RotationRate) : Vector3.Zero;
                }
            }
            if (userAccelerationSensor != null)
            {
                bool enable = userAccelerationSensor.IsEnabled || gravitySensor.IsEnabled || orientationSensor.IsEnabled;
                if (enable != motionManager.DeviceMotionActive)
                {
                    if (enable)
                    {
                        motionManager.StartDeviceMotionUpdates();
                    }
                    else
                    {
                        motionManager.StopDeviceMotionUpdates();
                    }
                }

                if (enable)
                {
                    var motion = motionManager.DeviceMotion;
                    // Update orientation sensor
                    if (motion != null && motion.Attitude != null)
                    {
                        var q          = motionManager.DeviceMotion.Attitude.Quaternion;
                        var quaternion = new Quaternion((float)q.x, (float)q.z, -(float)q.y, (float)q.w);

                        if (compassSensor != null)
                        {
                            // re-adjust the orientation to align with the north (common behavior on other platforms) TODO current implementation only takes in account the first value.
                            if (firstNorthValue <= 0)
                            {
                                firstNorthValue = GetNorthInRadian(locationManager);
                            }

                            quaternion = Quaternion.RotationY(-firstNorthValue) * quaternion;
                        }

                        orientationSensor.FromQuaternion(quaternion);
                    }
                    else
                    {
                        orientationSensor.Reset();
                    }

                    // Update gravity sensor
                    gravitySensor.Vector = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero;

                    // Update user acceleration
                    userAccelerationSensor.Acceleration = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero;
                }
            }
        }
Exemplo n.º 9
0
 protected override void Stop(CMMotionManager mgr) => mgr.StopGyroUpdates();
Exemplo n.º 10
0
 /// <summary>
 /// Stops this instance.
 /// </summary>
 partial void Stop()
 {
     _motionManager.StopGyroUpdates();
     _motionManager = null;
 }
Exemplo n.º 11
0
        protected override async Task StopListeningAsync()
        {
            await base.StopListeningAsync();

            _motionManager?.StopGyroUpdates();
        }
Exemplo n.º 12
0
 public void StopReading()
 {
     _motionManager.StopGyroUpdates();
 }