Exemple #1
0
        /// <summary>
        /// Start listening for acceleration sensor
        /// </summary>
        public void startWatch(string options)
        {
            try
            {
                accelOptions = JSON.JsonHelper.Deserialize <AccelerometerOptions>(options);
            }
            catch (Exception ex)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message));
                return;
            }

            if (string.IsNullOrEmpty(accelOptions.Id))
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
                return;
            }

            try
            {
                lock (accelerometer)
                {
                    listeners.Add(accelOptions.Id, accelerometer_CurrentValueChanged);
                    accelerometer.CurrentValueChanged += listeners[accelOptions.Id];
                    accelerometer.Start();
                    this.SetStatus(Starting);
                }
            }
            catch (Exception e)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ErrorFailedToStart));
                return;
            }
        }
        /// <summary>
        /// Initializes the Accelerometer for the current game. This method can only be called once per game.
        /// </summary>
        public static void Initialize()
        {
            // make sure we don't initialize the Accelerometer twice
            if (isInitialized)
            {
                return;
            }

            // try to start the sensor only on devices, catching the exception if it fails
            if (Microsoft.Devices.Environment.DeviceType == DeviceType.Device)
            {
                try
                {
                    accelerometer.ReadingChanged +=
                        new EventHandler <Microsoft.Devices.Sensors.AccelerometerReadingEventArgs>(
                            sensor_ReadingChanged);
                    accelerometer.Start();
                    isActive = true;
                }
                catch (Microsoft.Devices.Sensors.AccelerometerFailedException)
                {
                    isActive = false;
                }
            }
            else
            {
                // we always return isActive on emulator because we use the arrow
                // keys for simulation which is always available.
                isActive = true;
            }

            // remember that we are initialized
            isInitialized = true;
        }
        private void Calibrate()
        {
            //Initialize the accelerometer
            accelerometer = new Microsoft.Devices.Sensors.Accelerometer();

            if (accelerometer.State == SensorState.Initializing ||
                accelerometer.State == SensorState.Ready)
            {
                accelerometer.ReadingChanged += (s, e) =>
                {
                    accelerometerState =
                        new Vector3((float)e.X, (float)e.Y, (float)e.Z);

                    samplesCount++;
                    accelerometerCalibrationData += accelerometerState;

                    if (DateTime.Now >= startTime.AddSeconds(5))
                    {
                        accelerometer.Stop();

                        accelerometerCalibrationData.X /= samplesCount;
                        accelerometerCalibrationData.Y /= samplesCount;
                        accelerometerCalibrationData.Z /= samplesCount;

                        isCalibrating = false;
                    }
                };
            }
            accelerometer.Start();
        }
 public void Start()
 {
     if (_accelerometer != null)
     {
         throw new MvxException("Accelerometer already started");
     }
     _accelerometer = new Microsoft.Devices.Sensors.Accelerometer();
     _accelerometer.CurrentValueChanged += AccelerometerOnCurrentValueChanged;
     _accelerometer.Start();
 }
Exemple #5
0
 public void Start()
 {
     if (_accelerometer != null)
     {
         throw new MvxException("Accelerometer already started");
     }
     _accelerometer = new Microsoft.Devices.Sensors.Accelerometer();
     _accelerometer.CurrentValueChanged += AccelerometerOnCurrentValueChanged;
     _accelerometer.Start();
 }
		/// <summary>
		/// Starts this instance.
		/// </summary>
		partial void Start()
		{
			_accelerometer = new Microsoft.Devices.Sensors.Accelerometer
				                 {
					                 TimeBetweenUpdates =
						                 TimeSpan.FromMilliseconds((long)Interval)
				                 };

			_accelerometer.CurrentValueChanged += AccelerometerOnCurrentValueChanged;
			_accelerometer.Start();
		}
        /// <summary>
        /// Starts this instance.
        /// </summary>
        partial void Start()
        {
            _accelerometer = new Microsoft.Devices.Sensors.Accelerometer
            {
                TimeBetweenUpdates =
                    TimeSpan.FromMilliseconds((long)Interval)
            };

            _accelerometer.CurrentValueChanged += AccelerometerOnCurrentValueChanged;
            _accelerometer.Start();
        }
Exemple #8
0
        /// <summary>
        /// Starts listening for acceleration sensor
        /// </summary>
        /// <returns>status of listener</returns>
        public void start(string parameters)
        {
            string[] args = WPCordovaClassLib.Cordova.JSON.JsonHelper.Deserialize <string[]>(parameters);

            string time = "1000";

            if (args.Length > 0)
            {
                time = args[0];
            }

            // account for the callback param
            if (args.Length > 2)
            {
                adjustForRotation = bool.Parse(args[1]);
            }

            // account for the callback param
            if (args.Length > 3)
            {
                orientation = args[2];
            }

            if (!this.isRunning)
            {
                if (this._accelerometer == null)
                {
                    _accelerometer = new Microsoft.Devices.Sensors.Accelerometer();
                }

                int tmpTime;
                int.TryParse(time, out tmpTime);

                this.time = tmpTime;
                try
                {
                    lock (_accelerometer)
                    {
                        _accelerometer = new Microsoft.Devices.Sensors.Accelerometer();
                        _accelerometer.TimeBetweenUpdates   = TimeSpan.FromMilliseconds(this.time);
                        _accelerometer.CurrentValueChanged += new EventHandler <SensorReadingEventArgs <AccelerometerReading> >(accelerometer_CurrentValueChanged);

                        this.isRunning = true;
                        _accelerometer.Start();
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Starts listening for acceleration sensor
        /// </summary>
        /// <returns>status of listener</returns>
        public void start(string options)
        {
            string[] args       = JSON.JsonHelper.Deserialize <string[]>(options);
            string   callbackId = args[0];

            //Debug.WriteLine("start called with callbackId : " + callbackId);

            if ((currentStatus == Running) || (currentStatus == Starting))
            {
                return;
            }
            try
            {
                lock (accelerometer)
                {
                    accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
                    accelerometer.Start();
                    this.SetStatus(Starting);
                }

                long timeout = 2000;
                while ((currentStatus == Starting) && (timeout > 0))
                {
                    timeout = timeout - 100;
                    Thread.Sleep(100);
                }

                if (currentStatus != Running)
                {
                    this.SetStatus(ErrorFailedToStart);
                    DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart), callbackId);
                    return;
                }
            }
            catch (Exception)
            {
                this.SetStatus(ErrorFailedToStart);
                DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart), callbackId);
                return;
            }
            PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);

            result.KeepCallback = true;
            DispatchCommandResult(result, callbackId);
        }
Exemple #10
0
        /// <summary>
        /// Starts listening for acceleration sensor
        /// </summary>
        /// <returns>status of listener</returns>
        public void start(string options)
        {
            if ((currentStatus == Running) || (currentStatus == Starting))
            {
                return;
            }
            try
            {
                lock (accelerometer)
                {
                    accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
                    accelerometer.Start();
                    this.SetStatus(Starting);
                }

                long timeout = 2000;
                while ((currentStatus == Starting) && (timeout > 0))
                {
                    timeout = timeout - 100;
                    Thread.Sleep(100);
                }

                if (currentStatus != Running)
                {
                    this.SetStatus(ErrorFailedToStart);
                    DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart));
                    return;
                }
            }
            catch (Exception)
            {
                this.SetStatus(ErrorFailedToStart);
                DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart));
                return;
            }
            PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);

            result.KeepCallback = true;
            DispatchCommandResult(result);
        }
Exemple #11
0
        /// <summary>
        /// Initializes the Accelerometer for the current game. This method can only be called once per game.
        /// </summary>
        public static void Initialize()
        {
            // make sure we don't initialize the Accelerometer twice
            if (isInitialized)
            {
                throw new InvalidOperationException("Initialize can only be called once");
            }

#if WINDOWS_PHONE
            // ALVIN NOTE: Remove the block to only initialise for devices.  Emulation is now available!
            //// try to start the sensor only on devices, catching the exception if it fails
            //if (Microsoft.Devices.Environment.DeviceType == Microsoft.Devices.DeviceType.Device)
            //{
            try
            {
                // ALVIN NOTE: Updated to use the new types.
                accelerometer.CurrentValueChanged += new EventHandler <SensorReadingEventArgs <AccelerometerReading> >(sensor_ReadingChanged);
                accelerometer.TimeBetweenUpdates   = TimeSpan.FromMilliseconds(20);

                accelerometer.Start();
                isActive = true;
            }
            catch (Microsoft.Devices.Sensors.AccelerometerFailedException)
            {
                isActive = false;
            }

            // ALVIN NOTE: New in MANGO, no need to hide emulators!
            //}
            //else
            //{
            //    // we always return isActive on emulator because we use the arrow
            //    // keys for simulation which is always available.
            //    isActive = true;
            //}
#endif

            // remember that we are initialized
            isInitialized = true;
        }
Exemple #12
0
 public void Initialize()
 {
     if (Motion.IsSupported)
     {
         try
         {
             motion.Start();
         }
         catch (Exception)
         {
         }
     }
     else
     {
         accelerometer.CurrentValueChanged += new EventHandler <SensorReadingEventArgs <AccelerometerReading> >(OnAccelerometerReadingChanged);
         try
         {
             accelerometer.Start();
         }
         catch
         {
         }
     }
 }