Beispiel #1
0
        /// <summary>
        /// Returns whether the connecting succeeded or not.
        ///
        /// NOTE! This function does NOT pair the controller by Bluetooth.
        /// If the controller is not already paired, it can only be connected by USB.
        /// See README for more information.
        /// </summary>
        public PSMoveConnectStatus Init(int index)
        {
            _motionController.Handle = PsMoveApi.psmove_connect_by_id(index);

            // Error check the result!
            if (_motionController.Handle == IntPtr.Zero)
            {
                return(_motionController.ConnectStatus = PSMoveConnectStatus.Error);
            }

            // Make sure the connection is actually sending data. If not, this is probably a controller
            // you need to remove manually from the OSX Bluetooth Control Panel, then re-connect.
            if (PsMoveApi.psmove_update_leds(_motionController.Handle) == 0)
            {
                return(_motionController.ConnectStatus = PSMoveConnectStatus.NoData);
            }

            _motionController.Id             = index;
            _motionController.Remote         = PsMoveApi.psmove_is_remote(_motionController.Handle) > 0;
            _motionController.ConnectionType = PsMoveApi.psmove_connection_type(_motionController.Handle);;

            StringBuilder builder = new StringBuilder(64);

            PsMoveApi.get_moved_host(_motionController.Handle, builder);
            _motionController.HostIp = builder.ToString();
            PsMoveApi.get_serial(_motionController.Handle, builder);
            _motionController.Serial = builder.ToString();
            UpdateController();

            return(_motionController.ConnectStatus = PSMoveConnectStatus.OK);
        }
Beispiel #2
0
        public void UpdateController()
        {
            uint buttons = 0;

            prevButtons = currentButtons;
            // NOTE! There is potentially data waiting in queue.
            // We need to poll *all* of it by calling psmove_poll() until the queue is empty. Otherwise, data might begin to build up.
            while (PsMoveApi.psmove_poll(_motionController.Handle) > 0)
            {
                // We are interested in every button press between the last update and this one:
                buttons = buttons | PsMoveApi.psmove_get_buttons(_motionController.Handle);

                // The events are not really working from the PS Move Api. So we do our own with the prevButtons
                //psmove_get_button_events(handle, ref pressed, ref released);
            }
            currentButtons = buttons;

            // For acceleration, gyroscope, and magnetometer values, we look at only the last value in the queue.
            // We could in theory average all the acceleration (and other) values in the queue for a "smoothing" effect, but we've chosen not to.
            ProcessData();

            // Send a report to the controller to update the LEDs and rumble.
            if (PsMoveApi.psmove_update_leds(_motionController.Handle) == 0)
            {
                //			Debug.Log ("led set");
                // If it returns zero, the controller must have disconnected (i.e. out of battery or out of range),
                // so we should fire off any events and disconnect it.
                if (this != null)
                {
                    OnControllerDisconnected(this, new EventArgs());
                }
                Disconnect();
            }
        }
Beispiel #3
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.");
            }
        }