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(); } }
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."); } }