/// <summary> /// needed for detecting a change in joystick state /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (obj == null) { return(false); } JoystickRawState jsObj = (JoystickRawState)obj; if (X != jsObj.X || Y != jsObj.Y || Z != jsObj.Z || XR != jsObj.XR || YR != jsObj.YR || LT != jsObj.LT || RT != jsObj.RT || DpadDirection != jsObj.DpadDirection) { return(false); } for (int i = 0; i < buttons.Length && i < jsObj.buttons.Length; i++) { if (buttons[i] != jsObj.buttons[i]) { return(false); } } // all state elements we were interested in are the same return(true); }
/// <summary> /// Handler for processing/filtering input from the controller /// </summary> /// <param name="sender">HidDevice handle to the controller</param> /// <param name="args">InputReport received from the controller</param> private void inputReportReceived(HidDevice sender, HidInputReportReceivedEventArgs args) { int dPad = (int)args.Report.GetNumericControl(0x01, 0x39).Value; ControllerDpadDirection dpadDirection = (ControllerDpadDirection)dPad; // see http://sviluppomobile.blogspot.com/2013/11/hid-communication-for-windows-81-store.html // Adjust X/Y so (0,0) is neutral position // sticks - left and right: int _leftStickX = (int)(args.Report.GetNumericControl(0x01, 0x30).Value); int _leftStickY = (int)(args.Report.GetNumericControl(0x01, 0x31).Value); int _rightStickX = (int)(args.Report.GetNumericControl(0x01, 0x33).Value); int _rightStickY = (int)(args.Report.GetNumericControl(0x01, 0x34).Value); // triggers - left and right: int _LT = (int)Math.Max(0, args.Report.GetNumericControl(0x01, 0x32).Value - 32768); int _RT = (int)Math.Max(0, (-1) * (args.Report.GetNumericControl(0x01, 0x32).Value - 32768)); JoystickRawState jss = new JoystickRawState() { X = _leftStickX, Y = _leftStickY, Z = 0, XR = _rightStickX, YR = _rightStickY, LT = _LT, RT = _RT, DpadDirection = dpadDirection }; /* * Buttons Boolean ID's mapped to 0-9 array * A (button1) - 5 * B (button2) - 6 * X (button3) - 7 * Y (button4) - 8 * LB (Left Bumper, button5) - 9 * RB (Right Bumper, button6) - 10 * Back (button7) - 11 * Start (button8) - 12 * LStick - 13 * RStick - 14 */ foreach (var btn in args.Report.ActivatedBooleanControls) { // both press and release button event processed here: jss.Buttons[btn.Id - 5] = btn.IsActive; } // only invoke event if there was a change: if (!jss.Equals(jssLast)) { jssLast = jss; //Debug.WriteLine("--------- HID: Joystick event"); JoystickDataChanged?.Invoke(this, new JoystickEventArgs(jss)); } }
/// <summary> /// Handler for processing/filtering input from the controller /// </summary> /// <param name="sender">HidDevice handle to the controller</param> /// <param name="args">InputReport received from the controller</param> private void inputReportReceived(HidDevice sender, HidInputReportReceivedEventArgs args) { int dPad = (int)args.Report.GetNumericControl(0x01, 0x39).Value; ControllerDpadDirection dpadDirection = (ControllerDpadDirection)dPad; // see http://sviluppomobile.blogspot.com/2013/11/hid-communication-for-windows-81-store.html // Adjust X/Y so (0,0) is neutral position // sticks - left and right: int _leftStickX = (int)(args.Report.GetNumericControl(0x01, 0x30).Value); int _leftStickY = (int)(args.Report.GetNumericControl(0x01, 0x31).Value); int _rightStickX = (int)(args.Report.GetNumericControl(0x01, 0x33).Value); int _rightStickY = (int)(args.Report.GetNumericControl(0x01, 0x34).Value); // triggers - left and right: int _LT = (int)Math.Max(0, args.Report.GetNumericControl(0x01, 0x32).Value - 32768); int _RT = (int)Math.Max(0, (-1) * (args.Report.GetNumericControl(0x01, 0x32).Value - 32768)); JoystickRawState jss = new JoystickRawState() { X = _leftStickX, Y = _leftStickY, Z = 0, XR = _rightStickX, YR = _rightStickY, LT = _LT, RT = _RT, DpadDirection = dpadDirection }; /* * Buttons Boolean ID's mapped to 0-9 array * A (button1) - 5 * B (button2) - 6 * X (button3) - 7 * Y (button4) - 8 * LB (Left Bumper, button5) - 9 * RB (Right Bumper, button6) - 10 * Back (button7) - 11 * Start (button8) - 12 * LStick - 13 * RStick - 14 */ foreach (var btn in args.Report.ActivatedBooleanControls) { // both press and release button event processed here: jss.Buttons[btn.Id - 5] = btn.IsActive; } // only invoke event if there was a change: if (!jss.Equals(jssLast)) { jssLast = jss; //Debug.WriteLine("--------- HID: Joystick event"); JoystickDataChanged?.Invoke(this, new JoystickEventArgs(jss)); } }