예제 #1
0
        public async void StartMagnetometerCalibrationTask(MetroWindow window)
        {
            CancelUpdateTask();
            _ctsMagnetometerCalibration = new CancellationTokenSource();
            var controller = await window.ShowProgressAsync("Magnetometer Calibration", null, true);

            CancellationToken token = _ctsMagnetometerCalibration.Token;

            try
            {
                await Task.Run(() =>
                {
                    PsMoveApi.psmove_reset_magnetometer_calibration(_motionController.Handle);
                    int oldRange             = 0;
                    bool calibrationFinished = false;
                    Color color = _motionController.Color;
                    while (!token.IsCancellationRequested && !calibrationFinished)
                    {
                        while (PsMoveApi.psmove_poll(_motionController.Handle) > 0)
                        {
                            float ax, ay, az;
                            PsMoveApi.psmove_get_magnetometer_vector(_motionController.Handle, out ax, out ay, out az);

                            int range = PsMoveApi.psmove_get_magnetometer_calibration_range(_motionController.Handle);
                            MagnetometerCalibrationProgress = 100 * range / 320;
                            if (MagnetometerCalibrationProgress > 100)
                            {
                                MagnetometerCalibrationProgress = 100;
                            }
                            else if (MagnetometerCalibrationProgress < 0)
                            {
                                MagnetometerCalibrationProgress = 0;
                            }
                            controller.SetProgress(MagnetometerCalibrationProgress / 100.0);

                            float r = (color.r / 100) * MagnetometerCalibrationProgress;
                            float g = (color.g / 100) * MagnetometerCalibrationProgress;
                            float b = (color.b / 100) * MagnetometerCalibrationProgress;
                            SetLED(new Color(r, g, b));
                            PsMoveApi.psmove_update_leds(_motionController.Handle);

                            if (controller.IsCanceled)
                            {
                                CancelMagnetometerCalibrationTask();
                            }

                            if (range >= 320)
                            {
                                if (oldRange > 0)
                                {
                                    PsMoveApi.psmove_save_magnetometer_calibration(_motionController.Handle);
                                    calibrationFinished = true;
                                    break;
                                }
                            }
                            else if (range > oldRange)
                            {
                                controller.SetMessage(string.Format("Rotate the controller in all directions: {0}%...",
                                                                    MagnetometerCalibrationProgress));
                                oldRange = range;
                            }
                        }
                    }
                });
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }

            await controller.CloseAsync();

            if (controller.IsCanceled)
            {
                await window.ShowMessageAsync("Magnetometer Calibration", "Calibration has been cancelled.");
            }
            else
            {
                await window.ShowMessageAsync("Magnetometer Calibration", "Calibration finished successfully.");
            }
        }