public void UpdateGamepadState() { if (Controller.IsConnected) { LastState = CurrentState; CurrentState = Controller.GetState(); } }
private int[] calculateOutput() { gamepad = controller.GetState().Gamepad; PadState["leftThumbX"] = (Math.Abs((float)gamepad.LeftThumbX) < deadband) ? 0 : gamepad.LeftThumbX / short.MaxValue * 1000; PadState["leftThumbY"] = (Math.Abs((float)gamepad.LeftThumbY) < deadband) ? 0 : gamepad.LeftThumbY / short.MaxValue * 1000; PadState["rightThumbY"] = (Math.Abs((float)gamepad.RightThumbY) < deadband) ? 0 : gamepad.RightThumbY / short.MaxValue * 1000; PadState["rightThumbX"] = (Math.Abs((float)gamepad.RightThumbX) < deadband) ? 0 : gamepad.RightThumbX / short.MaxValue * 1000; //values are between -1000 and 1000 PadState["leftTrigger"] = gamepad.LeftTrigger / 255 * 1000; PadState["rightTrigger"] = gamepad.RightTrigger / 255 * 1000; controlState[0] = PadState[Assignment["roll"]]; //roll controlState[1] = PadState[Assignment["pitch"]]; //pitch controlState[2] = PadState[Assignment["yaw"]]; //yaw controlState[3] = PadState[Assignment["throttle"]]; //throttle controlState[4] = PadState[Assignment["emerge"]] - PadState[Assignment["submerge"]]; //emerge int[] ret = new int[channels]; for (int i = 0; i < channels; i++) { ret[i] = (int)Math.Round(controlState[i]); if (Math.Abs(ret[i]) > 1000) { ret[i] = Math.Abs(ret[i]) / ret[i] * 1000; } } return(ret); }
private void UpdateInputStates() { KeyboardState = _keyboardManager.GetState(); MouseState = _mouseManager.GetState(); var controller = new Controller(UserIndex.One); if (controller.IsConnected) GamepadState = controller.GetState().Gamepad; }
/// <summary> /// The main entry point for the application. /// </summary> public XInputController() { Console.WriteLine("Start XGamepadApp"); // Initialize XInput var controllers = new[] { new SharpDX.XInput.Controller(UserIndex.One), new SharpDX.XInput.Controller(UserIndex.Two), new SharpDX.XInput.Controller(UserIndex.Three), new SharpDX.XInput.Controller(UserIndex.Four) }; // Get 1st controller available SharpDX.XInput.Controller controller = null; foreach (var selectControler in controllers) { if (selectControler.IsConnected) { controller = selectControler; break; } } if (controller == null) { Console.WriteLine("No XInput controller installed"); } else { Console.WriteLine("Found a XInput controller available"); Console.WriteLine("Press buttons on the controller to display events"); // Poll events from joystick var previousState = controller.GetState(); while (controller.IsConnected) { var state = controller.GetState(); if (previousState.PacketNumber != state.PacketNumber) { Console.WriteLine(state.Gamepad); } previousState = state; Thread.Sleep(8);//8 miliseconds = 125Hz } } Console.WriteLine("End XGamepadApp"); }
static void Main(string[] args) { // box setup Form f = new Form(); f.Opacity = 0.3; f.StartPosition = FormStartPosition.CenterScreen; f.FormBorderStyle = FormBorderStyle.None; f.TopMost = true; bool boxVisible = false; bool wasPressedGuide = false; while (!System.Console.KeyAvailable) { SharpDX.XInput.Controller c = new SharpDX.XInput.Controller(SharpDX.XInput.UserIndex.One); SharpDX.XInput.State s; if (c.GetState(out s)) { //System.Console.WriteLine(s.Gamepad.ToString()); //System.Console.WriteLine(s.Gamepad.Buttons); s.Gamepad.Buttons.HasFlag(SharpDX.XInput.GamepadButtonFlags.A); if (!wasPressedGuide) { if (is_guide_button_down(0) != 0) { wasPressedGuide = true; boxVisible = !boxVisible; //SendKeys.SendWait("{RIGHT}"); } if (boxVisible) { if (!f.Visible) { f.Show(); } } else { f.Hide(); } } else { wasPressedGuide = (is_guide_button_down(0) != 0); } } } }
private void mainLoop() { State gameState; var Controller = new Controller(UserIndex.One); while (running) { Controller.GetState(out gameState); string strState = gameState.Gamepad.Buttons.ToString().Replace(" ", ""); if (strState != "None") { ButtonPressed(getActions(strState), strState.Split(',')); } System.Threading.Thread.Sleep(100); } }
public override byte PollKeysPressed() { int state = (int)controller.GetState().Gamepad.Buttons; byte _A, _B, _Start, _Select, _Up, _Down, _Left, _Right; _A = (byte)(((state & (int)GamepadButtonFlags.A) > 0) ? 0b1000_0000 : 0); _B = (byte)(((state & ((int)GamepadButtonFlags.B | (int)GamepadButtonFlags.X)) > 0) ? 0b0100_0000 : 0); _Start = (byte)(((state & (int)GamepadButtonFlags.Start) > 0) ? 0b0010_0000 : 0); _Select = (byte)(((state & (int)GamepadButtonFlags.Back) > 0) ? 0b0001_0000 : 0); _Up = (byte)(((state & (int)GamepadButtonFlags.DPadUp) > 0) ? 0b0000_1000 : 0); _Down = (byte)(((state & (int)GamepadButtonFlags.DPadDown) > 0) ? 0b0000_0100 : 0); _Left = (byte)(((state & (int)GamepadButtonFlags.DPadLeft) > 0) ? 0b0000_0010 : 0); _Right = (byte)(((state & (int)GamepadButtonFlags.DPadRight) > 0) ? 0b0000_0001 : 0); return((byte)(_A | _B | _Start | _Select | _Up | _Down | _Left | _Right)); }
static void Main(string[] args) { Controller xbox = new Controller(UserIndex.One); Console.WriteLine("Controller connected: " + xbox.IsConnected); BatteryInformation battery = xbox.GetBatteryInformation(BatteryDeviceType.Gamepad); Console.WriteLine("Battery level: " + battery.BatteryLevel); bool isRunning = true; while (isRunning) { Console.Clear(); State state = xbox.GetState(); switch (state.Gamepad.Buttons) { case GamepadButtonFlags.Start: isRunning = false; break; default: break; } Console.Write("Key pressed: " + state.Gamepad.Buttons + "\n"); Console.Write("RightThumbX stick: " + state.Gamepad.RightThumbX + "\n"); Console.Write("RightThumbY stick: " + state.Gamepad.RightThumbY + "\n"); Console.Write("LeftThumbX stick: " + state.Gamepad.LeftThumbX + "\n"); Console.Write("LeftThumbY stick: " + state.Gamepad.LeftThumbY + "\n"); Console.Write("LeftTrigger: " + state.Gamepad.LeftTrigger + "\n"); Console.Write("RightTrigger: " + state.Gamepad.RightTrigger + "\n"); int vibrationLeftMotorSpeed = 0; if (state.Gamepad.LeftThumbX > -1) vibrationLeftMotorSpeed = state.Gamepad.LeftThumbX; vibration.LeftMotorSpeed = (ushort)vibrationLeftMotorSpeed; xbox.SetVibration(vibration); System.Threading.Thread.Sleep(100); } }
public virtual bool UpdateState() { try { Gamepad gamepad = controller.GetState().Gamepad; int ButtonState = (int)gamepad.Buttons; int JoystickX = gamepad.LeftThumbX; int JoystickY = gamepad.LeftThumbY; ushort _A, _B, _Start, _Select, _Up, _Down, _Left, _Right, _R, _L; _A = (ushort)(((ButtonState & (int)GamepadButtonFlags.A) > 0) ? 0b00_0000_0001 : 0); _B = (ushort)(((ButtonState & ((int)GamepadButtonFlags.B | (int)GamepadButtonFlags.X)) > 0) ? 0b00_0000_0010 : 0); _Select = (ushort)(((ButtonState & (int)GamepadButtonFlags.Back) > 0) ? 0b00_0000_0100 : 0); _Start = (ushort)(((ButtonState & (int)GamepadButtonFlags.Start) > 0) ? 0b00_0000_1000 : 0); _Right = (ushort)(((ButtonState & (int)GamepadButtonFlags.DPadRight) > 0) || JoystickX > JoystickThreshold ? 0b00_0001_0000 : 0); _Left = (ushort)(((ButtonState & (int)GamepadButtonFlags.DPadLeft) > 0) || JoystickX < -JoystickThreshold ? 0b00_0010_0000 : 0); _Up = (ushort)(((ButtonState & (int)GamepadButtonFlags.DPadUp) > 0) || JoystickY > JoystickThreshold ? 0b00_0100_0000 : 0); _Down = (ushort)(((ButtonState & (int)GamepadButtonFlags.DPadDown) > 0) || JoystickY < -JoystickThreshold ? 0b00_1000_0000 : 0); _R = (ushort)(((ButtonState & ((int)GamepadButtonFlags.RightShoulder)) > 0) ? 0b01_0000_0000 : 0); _L = (ushort)(((ButtonState & ((int)GamepadButtonFlags.LeftShoulder)) > 0) ? 0b10_0000_0000 : 0); ControllerState = (ushort)(_A | _B | _Start | _Select | _Up | _Down | _Left | _Right | _R | _L); return(true); } catch (SharpDX.SharpDXException) { // otherwise our keys will be stuck on disconnection... ControllerState = 0; return(false); } }
private void UpdateView() { if (sceneView == null) { return; } if (controller == null || !controller.IsConnected) { return; } //The Camera might change whil doing all the calculations... var originalCamera = sceneView.Camera; if (originalCamera == null) { return; } var newCamera = originalCamera; var state = controller.GetState(); var time2 = DateTime.Now; bool isRightShoulderPressed = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.RightShoulder); //Choose if staying at the same alitude or not if (isRightShoulderPressed) { var elapsed = time2 - time; if (elapsed != TimeSpan.Zero && elapsed > TimeSpan.FromSeconds(0.25)) { timer.Stop(); timer.Start(); relativeToDirection = !relativeToDirection; time = time2; } } else { time = time2; } double LeftThumbStickX = 0; double LeftThumbStickY = 0; double RightThumbStickX = 0; double RightThumbStickY = 0; var gamePad = new NormalizedGamepad(state.Gamepad); LeftThumbStickX = gamePad.LeftThumbXNormalized; LeftThumbStickY = gamePad.LeftThumbYNormalized; RightThumbStickX = gamePad.RightThumbXNormalized; RightThumbStickY = gamePad.RightThumbYNormalized; if ((LeftThumbStickX != 0) || (LeftThumbStickY != 0) || (RightThumbStickX != 0) || (RightThumbStickY != 0)) { //Move sideways (crab move), so calling move forward after modifying heading and tilt, then resetting heading and tilt if (LeftThumbStickX != 0) { //Distance depends on the altitude, but must have minimum values (one way and the other) to not be almost nothing when altitude gets to 0) var distance = (double)LeftThumbStickX * newCamera.Location.Z / 40; if (distance > 0 && distance < 0.75) { distance = 0.75; } if (distance < 0 && distance > -0.75) { distance = -0.75; } var pitch = newCamera.Pitch; newCamera = newCamera.RotateTo(newCamera.Heading, 90, newCamera.Roll).RotateTo(90, 0, 0).MoveForward(distance).RotateTo(-90, 0, 0); newCamera = newCamera.RotateTo(newCamera.Heading, pitch, newCamera.Roll); } var distance2 = (double)LeftThumbStickY * newCamera.Location.Z / 40; if (distance2 > 0 && distance2 < 1) { distance2 = 1; } if (distance2 < 0 && distance2 > -1) { distance2 = -1; } //Move in the direction of the camera (modify the elevation) newCamera = newCamera.MoveForward(distance2).RotateTo((double)RightThumbStickX, newCamera.Pitch > 2 ? -(double)RightThumbStickY : RightThumbStickY < 0 ? -(double)RightThumbStickY : 0, 0); //If move in the direction of the camera but staying at the same elevation. if (!relativeToDirection) { newCamera = newCamera.MoveTo(new MapPoint(newCamera.Location.X, newCamera.Location.Y, originalCamera.Location.Z, newCamera.Location.SpatialReference)); } } //Triggers are just elevating the camera double elevationDistance = (double)(state.Gamepad.RightTrigger - state.Gamepad.LeftTrigger); if ((elevationDistance != 0) && (newCamera.Location.Z != double.NaN)) { var distance = elevationDistance * Math.Abs(newCamera.Location.Z) / 10000; newCamera = newCamera.Elevate(distance > 1 ? distance : distance < 1 ? distance : distance < 0 ? -1 : 1); } if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp)) { newCamera = newCamera.RotateTo(0, newCamera.Pitch, newCamera.Roll); } if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown)) { newCamera = newCamera.RotateTo(180, newCamera.Pitch, newCamera.Roll); } if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft)) { newCamera = newCamera.RotateTo(270, newCamera.Pitch, newCamera.Roll); } if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight)) { newCamera = newCamera.RotateTo(90, newCamera.Pitch, newCamera.Roll); } if (!newCamera.IsEqual(originalCamera)) { sceneView.SetViewpointCamera(newCamera); } }
/// <summary> /// Function to poll the joystick for data. /// </summary> protected override void PollJoystick() { if (!_controller.IsConnected) { Gorgon.Log.Print("XInput Controller {0} disconnected.", LoggingLevel.Verbose, _controllerID); IsConnected = false; return; } // If we weren't connected before, then get the caps for the device. if (!IsConnected) { var previousDeadZone = DeadZone; Initialize(); IsConnected = true; // Restore the dead zone. DeadZone.Rudder = new GorgonRange(previousDeadZone.Rudder); DeadZone.Throttle = new GorgonRange(previousDeadZone.Throttle); DeadZone.X = new GorgonRange(previousDeadZone.X); DeadZone.Y = new GorgonRange(previousDeadZone.Y); DeadZone.SecondaryX = new GorgonRange(previousDeadZone.SecondaryX); DeadZone.SecondaryY = new GorgonRange(previousDeadZone.SecondaryY); #if DEBUG XI.Capabilities caps = _controller.GetCapabilities(XI.DeviceQueryType.Any); Gorgon.Log.Print("XInput Controller {0} (ID:{1}) re-connected.", LoggingLevel.Verbose, caps.SubType.ToString(), _controllerID); #endif } XI.State state = _controller.GetState(); // Do nothing if the data has not changed since the last poll. if (LastPacket == state.PacketNumber) { return; } // Get axis data. X = state.Gamepad.LeftThumbX; Y = state.Gamepad.LeftThumbY; SecondaryX = state.Gamepad.RightThumbX; SecondaryY = state.Gamepad.RightThumbY; Throttle = state.Gamepad.RightTrigger; Rudder = state.Gamepad.LeftTrigger; // Get button info. if (state.Gamepad.Buttons != XI.GamepadButtonFlags.None) { // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < _button.Length; i++) { _buttonList.SetButtonState(_button[i], (_button[i] != XI.GamepadButtonFlags.None) && ((state.Gamepad.Buttons & _button[i]) == _button[i])); } } // Get POV values. GetPOVData(state.Gamepad.Buttons); }
uint XInputGetState_Hooked(int dwUserIndex, out State state) { state = new State(); try { var controller1 = new Controller(UserIndex.One); var controller2 = new Controller(UserIndex.Two); var controller3 = new Controller(UserIndex.Three); var controller4 = new Controller(UserIndex.Four); var controller = new Controller((UserIndex)dwUserIndex); if (!controller1.IsConnected && !controller2.IsConnected && !controller3.IsConnected && !controller4.IsConnected && ((UserIndex)dwUserIndex == UserIndex.One || (UserIndex)dwUserIndex == UserIndex.Any)) { ControllerConnected = false; _fakeState.PacketNumber = _fakeState.PacketNumber++; state = _fakeState; } else if (controller.IsConnected) { ControllerConnected = controller.IsConnected; state = controller.GetState(); } else { return ERROR_DEVICE_NOT_CONNECTED; } ControllerState = state; if (Enabled) { state = ProcessState(state); } return ERROR_SUCCESS; } catch { return ERROR_DEVICE_NOT_CONNECTED; } }
// Main XBox processing private void PollGamepad() { if ((m_xbox == null) || !m_xbox.IsConnected) { return; } m_xboxStateLast = m_xboxState; m_xboxState = m_xbox.GetState(); if (ButtonPushed(GamepadButtonFlags.A)) { OnXBoxGamepadButtonPressA.Invoke(this, null); } if (ButtonOneShot(GamepadButtonFlags.A)) { OnXBoxGamepadButtonPressAOneShot.Invoke(this, null); } if (ButtonPushed(GamepadButtonFlags.B)) { OnXBoxGamepadButtonPressB.Invoke(this, null); } if (ButtonOneShot(GamepadButtonFlags.B)) { OnXBoxGamepadButtonPressBOneShot.Invoke(this, null); } var LX = m_xboxState.Gamepad.LeftThumbX; var LY = m_xboxState.Gamepad.LeftThumbY; //determine how far the controller is pushed var magnitude = Math.Sqrt(LX * LX + LY * LY); //determine the direction the controller is pushed var normalizedLX = LX / magnitude; var normalizedLY = LY / magnitude; var normalizedMagnitude = 0.0; //check if the controller is outside a circular dead zone if (magnitude > Gamepad.LeftThumbDeadZone) { //clip the magnitude at its expected maximum value if (magnitude > 32767) { magnitude = 32767; } //adjust magnitude relative to the end of the dead zone magnitude -= Gamepad.LeftThumbDeadZone; //optionally normalize the magnitude with respect to its expected range //giving a magnitude value of 0.0 to 1.0 normalizedMagnitude = magnitude / (32767 - Gamepad.LeftThumbDeadZone); } else //if the controller is in the deadzone zero out the magnitude { magnitude = 0.0; normalizedMagnitude = 0.0; // m_deviceX.Stop(6000); } m_deviceX.MoveContinuous(MotorDirection.Forward); }
public MainPage() { this.InitializeComponent(); this.navigationHelper = new NavigationHelper(this); this.navigationHelper.LoadState += navigationHelper_LoadState; this.navigationHelper.SaveState += navigationHelper_SaveState; connect(); XBOX_MODE = false; Window.Current.CoreWindow.KeyDown += keyDownHandler; Window.Current.CoreWindow.KeyUp += keyUpHandler; rcControl = new Controller(0); TimeSpan period = TimeSpan.FromMilliseconds(100); //Periodic polling of the controller state ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) => { if (!XBOX_MODE) return; State s = rcControl.GetState(); // // Update the UI thread by using the UI core dispatcher. // Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { /** * Checks the status of the dpad and left thumbstick and uses that to send commands * **/ if((s.Gamepad.Buttons & GamepadButtonFlags.DPadUp) > 0 || s.Gamepad.LeftThumbY > 10000) { send_to_board(1); //forward UP_IMAGE.Source = (BitmapSource)Resources["UP_ACTIVE"]; } else { send_to_board(5); //stop forward UP_IMAGE.Source = (BitmapSource)Resources["UP_INACTIVE"]; } if((s.Gamepad.Buttons & GamepadButtonFlags.DPadDown) > 0 || s.Gamepad.LeftThumbY < -10000) { send_to_board(3); //back DOWN_IMAGE.Source = (BitmapSource)Resources["DOWN_ACTIVE"]; } else { send_to_board(7); //stop down DOWN_IMAGE.Source = (BitmapSource)Resources["DOWN_INACTIVE"]; } if((s.Gamepad.Buttons & GamepadButtonFlags.DPadRight) > 0 || s.Gamepad.LeftThumbX > 10000){ send_to_board(2); //right RIGHT_IMAGE.Source = (BitmapSource)Resources["RIGHT_ACTIVE"]; } else { send_to_board(6); //stop right RIGHT_IMAGE.Source = (BitmapSource)Resources["RIGHT_INACTIVE"]; } if ((s.Gamepad.Buttons & GamepadButtonFlags.DPadLeft) > 0 || s.Gamepad.LeftThumbX < -10000) { send_to_board(4); //left LEFT_IMAGE.Source = (BitmapSource)Resources["LEFT_ACTIVE"]; } else { send_to_board(8); //stop left LEFT_IMAGE.Source = (BitmapSource)Resources["LEFT_INACTIVE"]; } }); }, period); }
private void PollAndSendCameraCommands(Controller controller, BlockingCollection<string> commands, Dictionary<int, CameraState> cameraStates) { var previousState = controller.GetState(); int selectedCamera = 0; //Default camera is broom int angleIncrement = 5; while (controller.IsConnected) { var state = controller.GetState(); //Selecting camera to pan/tilt if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.B)) { selectedCamera = 3; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.X)) { selectedCamera = 4; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.RightShoulder)) { selectedCamera = 0; } //De-Activating camera to pan/tilt if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.LeftShoulder)) { string command = null; bool userSelected = false; if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.B)) { selectedCamera = 3; if (cameraStates[3].Active) //Dectivating command = "<C" + 3 + "F>"; else command = "<C" + 3 + "O>"; cameraStates[3].Active = !cameraStates[3].Active; userSelected = true; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.X)) { selectedCamera = 4; if (cameraStates[4].Active) //Dectivating command = "<C" + 4 + "F>"; else command = "<C" + 4 + "O>"; cameraStates[4].Active = !cameraStates[4].Active; userSelected = true; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.RightShoulder)) { selectedCamera = 0; if (cameraStates[0].Active) //Dectivating command = "<C" + 0 + "F>"; else command = "<C" + 0 + "O>"; cameraStates[0].Active = !cameraStates[0].Active; userSelected = true; } //Send command if (userSelected) { commands.Add(command.ToString()); } } bool panning = false; bool tilting = false; //Actually panning/tilting if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft)) //Left Pan { panning = true; cameraStates[selectedCamera].Pan = "000"; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight)) //Right Pan { panning = true; cameraStates[selectedCamera].Pan = "200"; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp)) //Up Tilt { if (cameraStates[selectedCamera].Tilt >= 90 - angleIncrement) cameraStates[selectedCamera].Tilt = 90; else cameraStates[selectedCamera].Tilt += angleIncrement; tilting = true; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown)) //Down Tilt { if (cameraStates[selectedCamera].Tilt <= angleIncrement) cameraStates[selectedCamera].Tilt = 0; else cameraStates[selectedCamera].Tilt -= angleIncrement; tilting = true; } //Sending commands if (panning) { string command = "<P" + selectedCamera + cameraStates[selectedCamera].Pan + ">"; //commands.Add(command.ToString()); CommandSender.Instance.UpdateCommand(command.ToString()); } else { string command = "<P" + selectedCamera + "100>"; //commands.Add(command.ToString()); CommandSender.Instance.UpdateCommand(command.ToString()); } if (tilting) { string command = "<T" + selectedCamera + getPaddedInt(cameraStates[selectedCamera].Tilt) + ">"; // commands.Add(command.ToString()); CommandSender.Instance.UpdateCommand(command.ToString()); } previousState = state; Thread.Sleep(POLLING_RATE); } }
private void PollAndSendMovementCommands(Controller controller, BlockingCollection<string> commands) { var previousState = controller.GetState(); int selectedCamera = 1; //Front camera is default int angleIncrement = 5; while (controller.IsConnected) { ////////////////////////////////////////////////////////////////////////////Movement Control var state = controller.GetState(); double LX = state.Gamepad.LeftThumbX; double LY = state.Gamepad.LeftThumbY; double RX = state.Gamepad.RightThumbX; double RY = state.Gamepad.RightThumbY; //Building commands for left motors string leftDirection = LY > 0 ? "F" : "B"; int leftSpeed = (int)Math.Abs((LY * 255 / 32768)); //Minor deadzone settings if (leftSpeed < 30) leftSpeed = 0; else if (leftSpeed > 230) leftSpeed = 255; if (RoverOperator.Content.MotorsViewModel.MotorVMActive) { StringBuilder leftCommand = new StringBuilder(); leftCommand.Append("<L"); string frontLeftMotorCommand = leftDirection + getPaddedInt(leftSpeed * (int)RoverOperator.Content.MotorsViewModel.FrontLeftMotorVM.Power / 100); leftCommand.Append(frontLeftMotorCommand); string middleLeftMotorCommand = leftDirection + getPaddedInt(leftSpeed * (int)RoverOperator.Content.MotorsViewModel.MiddleLeftMotorVM.Power / 100); leftCommand.Append(middleLeftMotorCommand); string backLeftMotorCommand = leftDirection + getPaddedInt(leftSpeed * (int)RoverOperator.Content.MotorsViewModel.BackLeftMotorVM.Power / 100); leftCommand.Append(backLeftMotorCommand); leftCommand.Append(">"); CommandSender.Instance.UpdateCommand(leftCommand.ToString()); if (logging) logger.Trace(leftCommand.ToString()); } //Building commands for left motors string rightDirection = RY > 0 ? "F" : "B"; int rightSpeed = (int)Math.Abs((RY * 255 / 32768)); //Minor deadzone settings if (rightSpeed < 30) rightSpeed = 0; else if (rightSpeed > 230) rightSpeed = 255; if (RoverOperator.Content.MotorsViewModel.MotorVMActive) { StringBuilder rightCommand = new StringBuilder(); rightCommand.Append("<R"); string frontRightMotorCommand = rightDirection + getPaddedInt(rightSpeed * (int)RoverOperator.Content.MotorsViewModel.FrontRightMotorVM.Power / 100); rightCommand.Append(frontRightMotorCommand); string middleRightMotorCommand = rightDirection + getPaddedInt(rightSpeed * (int)RoverOperator.Content.MotorsViewModel.MiddleRightMotorVM.Power / 100); rightCommand.Append(middleRightMotorCommand); string backRightMotorCommand = rightDirection + getPaddedInt(rightSpeed * (int)RoverOperator.Content.MotorsViewModel.BackRightMotorVM.Power / 100); rightCommand.Append(backRightMotorCommand); rightCommand.Append(">"); CommandSender.Instance.UpdateCommand(rightCommand.ToString()); if (logging) logger.Trace(rightCommand.ToString()); } ////////////////////////////////////////////////////////////////////////////Camera Control //Selecting camera to pan/tilt if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.Y)) { selectedCamera = 1; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.A)) { selectedCamera = 2; } //De-Activating camera to pan/tilt if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.LeftShoulder)) { string command = null; bool userSelected = false; if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.Y)) { selectedCamera = 1; if (cameraStates[1].Active) command = "<C" + 1 + "F>"; else command = "<C" + 1 + "O>"; cameraStates[1].Active = !cameraStates[1].Active; userSelected = true; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.A)) { selectedCamera = 2; if (cameraStates[2].Active) command = "<C" + 2 + "F>"; else command = "<C" + 2 + "O>"; cameraStates[2].Active = !cameraStates[2].Active; userSelected = true; } //Send command if (userSelected) { commands.Add(command.ToString()); } } bool panning = false; bool tilting = false; //Actually panning/tilting if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft)) //Left Pan { panning = true; cameraStates[selectedCamera].Pan = "000"; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight)) //Right Pan { panning = true; cameraStates[selectedCamera].Pan = "200"; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp)) //Up Tilt { if (cameraStates[selectedCamera].Tilt >= 90 - angleIncrement) cameraStates[selectedCamera].Tilt = 90; else cameraStates[selectedCamera].Tilt += angleIncrement; tilting = true; } else if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown)) //Down Tilt { if (cameraStates[selectedCamera].Tilt <= angleIncrement) cameraStates[selectedCamera].Tilt = 0; else cameraStates[selectedCamera].Tilt -= angleIncrement; tilting = true; } //Sending commands if (panning) { string command = "<P" + selectedCamera + cameraStates[selectedCamera].Pan + ">"; if (logging) logger.Trace(command.ToString()); //commands.Add(command.ToString()); CommandSender.Instance.UpdateCommand(command.ToString()); } else { string command = "<P" + selectedCamera + "100>"; if (logging) logger.Trace(command.ToString()); // commands.Add(command.ToString()); CommandSender.Instance.UpdateCommand(command.ToString()); } if (tilting) { string command = "<T" + selectedCamera + getPaddedInt(cameraStates[selectedCamera].Tilt) + ">"; //commands.Add(command.ToString()); CommandSender.Instance.UpdateCommand(command.ToString()); } previousState = state; Thread.Sleep(POLLING_RATE); } }
private void CaptureXInput() { var controller = new SXI.Controller((SXI.UserIndex)_joyInfo.XInputDevice); var inputState = controller.GetState(); bool[] axisMoved = { false, false, false, false, false, false, false, false }; //AXIS axisMoved[0] = GetAxisMovement(JoystickState.Axis[0], -inputState.Gamepad.LeftThumbY); axisMoved[1] = GetAxisMovement(JoystickState.Axis[1], inputState.Gamepad.LeftThumbX); axisMoved[2] = GetAxisMovement(JoystickState.Axis[2], -inputState.Gamepad.RightThumbY); axisMoved[3] = GetAxisMovement(JoystickState.Axis[3], inputState.Gamepad.RightThumbX); axisMoved[4] = GetAxisMovement(JoystickState.Axis[4], inputState.Gamepad.LeftTrigger * 129 < Joystick.Max_Axis ? inputState.Gamepad.LeftTrigger * 129 : Joystick.Max_Axis); axisMoved[5] = GetAxisMovement(JoystickState.Axis[5], inputState.Gamepad.RightTrigger * 129 < Joystick.Max_Axis ? inputState.Gamepad.RightTrigger * 129 : Joystick.Max_Axis); //POV Pov.Position previousPov = JoystickState.Povs[0].Direction; Pov.Position pov = Pov.Position.Centered; if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadUp) != 0) { pov |= Pov.Position.North; } else if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadDown) != 0) { pov |= Pov.Position.South; } if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadLeft) != 0) { pov |= Pov.Position.West; } else if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadRight) != 0) { pov |= Pov.Position.East; } JoystickState.Povs[0].Direction = pov; //BUTTONS // Skip the first 4 as they are the DPad. var previousButtons = JoystickState.Buttons; for (int i = 0; i < 12; i++) { if (((int)inputState.Gamepad.Buttons & (1 << (i + 4))) != 0) { JoystickState.Buttons |= 1 << (i + 4); } else { JoystickState.Buttons &= ~(1 << (i + 4)); } } //Send Events if (IsBuffered && EventListener != null) { var joystickEvent = new JoystickEventArgs(this, JoystickState); // Axes for (int index = 0; index < axisMoved.Length; index++) { if (axisMoved[index] == true && EventListener.AxisMoved(joystickEvent, index)) { return; } } //POV if (previousPov != pov && !EventListener.PovMoved(joystickEvent, 0)) { return; } //Buttons for (int i = 4; i < 16; i++) { if (((previousButtons & (1 << i)) == 0) && JoystickState.IsButtonDown(i)) { if (!EventListener.ButtonPressed(joystickEvent, i)) { return; } } else if (((previousButtons & (1 << i)) != 0) && !JoystickState.IsButtonDown(i)) { if (!EventListener.ButtonReleased(joystickEvent, i)) { return; } } } } }
private static void StatePoll() { // Poll forever (each controller in turn.) while (true) { if (_dispatcher == null) continue; foreach (var activeController in _activeControllers) { // Get the controller. var controller = new Controller(activeController); // Bail if it isn't connected. if (!controller.IsConnected) continue; // Get the current state. var state = controller.GetState(); // Fire events for the diff between states. FireEventsForStateChange(activeController, _lastState.ContainsKey(activeController) ? _lastState[activeController] : (State?)null, state); // Store current state so we can diff it on the next loop. _lastState[activeController] = state; } } }