void SetReport(IXbox360Controller controller, GamepadState state) { PreprocessState(state); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Up, state.Up); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Down, state.Down); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Left, state.Left); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Right, state.Right); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.LeftThumbX, (short)state.LX); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.LeftThumbY, (short)state.LY); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.RightThumbX, (short)state.RX); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.RightThumbY, (short)state.RY); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.A, state.A); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.B, state.B); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.X, state.X); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Y, state.Y); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Back, state.Select); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Start, state.Start); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.LeftShoulder, state.L1); controller.SetSliderValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Slider.LeftTrigger, (byte)(state.L2 ? 255 : 0)); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.LeftThumb, state.L3); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.RightShoulder, state.R1); controller.SetSliderValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Slider.RightTrigger, (byte)(state.R2 ? 255 : 0)); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.RightThumb, state.R3); }
internal void UpdateState(StateMapping mapping) { _controller.ResetReport(); _controller.SetButtonState(Xbox360Button.A, mapping.A); _controller.SetButtonState(Xbox360Button.B, mapping.B); _controller.SetButtonState(Xbox360Button.X, mapping.X); _controller.SetButtonState(Xbox360Button.Y, mapping.Y); _controller.SetButtonState(Xbox360Button.Back, mapping.Back); _controller.SetButtonState(Xbox360Button.Start, mapping.Start); _controller.SetButtonState(Xbox360Button.Guide, mapping.Guide); _controller.SetButtonState(Xbox360Button.LeftShoulder, mapping.LeftShoulder); _controller.SetButtonState(Xbox360Button.RightShoulder, mapping.RightShoulder); _controller.SetButtonState(Xbox360Button.LeftThumb, mapping.LeftThumb); _controller.SetButtonState(Xbox360Button.RightThumb, mapping.RightThumb); _controller.SetAxisValue(Xbox360Axis.LeftThumbX, mapping.LeftThumbX); _controller.SetAxisValue(Xbox360Axis.LeftThumbY, mapping.LeftThumbY); _controller.SetAxisValue(Xbox360Axis.RightThumbX, mapping.RightThumbX); _controller.SetAxisValue(Xbox360Axis.RightThumbY, mapping.RightThumbY); _controller.SetSliderValue(Xbox360Slider.LeftTrigger, mapping.LeftTrigger); _controller.SetSliderValue(Xbox360Slider.RightTrigger, mapping.RightTrigger); _controller.SetButtonState(Xbox360Button.Up, mapping.Up); _controller.SetButtonState(Xbox360Button.Down, mapping.Down); _controller.SetButtonState(Xbox360Button.Left, mapping.Left); _controller.SetButtonState(Xbox360Button.Right, mapping.Right); _controller.SubmitReport(); }
private void DoUpdateInput(OutputControllerXbox360InputState new_state) { xbox_controller.SetButtonState(Xbox360Button.LeftThumb, new_state.thumb_stick_left); xbox_controller.SetButtonState(Xbox360Button.RightThumb, new_state.thumb_stick_right); xbox_controller.SetButtonState(Xbox360Button.Y, new_state.y); xbox_controller.SetButtonState(Xbox360Button.X, new_state.x); xbox_controller.SetButtonState(Xbox360Button.B, new_state.b); xbox_controller.SetButtonState(Xbox360Button.A, new_state.a); xbox_controller.SetButtonState(Xbox360Button.Start, new_state.start); xbox_controller.SetButtonState(Xbox360Button.Back, new_state.back); xbox_controller.SetButtonState(Xbox360Button.Guide, new_state.guide); xbox_controller.SetButtonState(Xbox360Button.Up, new_state.dpad_up); xbox_controller.SetButtonState(Xbox360Button.Right, new_state.dpad_right); xbox_controller.SetButtonState(Xbox360Button.Down, new_state.dpad_down); xbox_controller.SetButtonState(Xbox360Button.Left, new_state.dpad_left); xbox_controller.SetButtonState(Xbox360Button.LeftShoulder, new_state.shoulder_left); xbox_controller.SetButtonState(Xbox360Button.RightShoulder, new_state.shoulder_right); xbox_controller.SetAxisValue(Xbox360Axis.LeftThumbX, new_state.axis_left_x); xbox_controller.SetAxisValue(Xbox360Axis.LeftThumbY, new_state.axis_left_y); xbox_controller.SetAxisValue(Xbox360Axis.RightThumbX, new_state.axis_right_x); xbox_controller.SetAxisValue(Xbox360Axis.RightThumbY, new_state.axis_right_y); xbox_controller.SetSliderValue(Xbox360Slider.LeftTrigger, new_state.trigger_left); xbox_controller.SetSliderValue(Xbox360Slider.RightTrigger, new_state.trigger_right); xbox_controller.SubmitReport(); current_state = new_state; }
public void UpdateButtonStatus(byte LX, byte LY, byte RX, byte RY, byte L2, byte R2, byte Button_DPad_State, byte Button2_Status) { controller.SetAxisValue(Xbox360Axis.LeftThumbX, LeftThumbX = (short)((LX << 8) - 32768)); controller.SetAxisValue(Xbox360Axis.LeftThumbY, LeftThumbY = (short)~((LY << 8) - 32768)); controller.SetAxisValue(Xbox360Axis.RightThumbX, RightThumbX = (short)((RX << 8) - 32768)); controller.SetAxisValue(Xbox360Axis.RightThumbY, RightThumbY = (short)~((RY << 8) - 32768)); controller.SetSliderValue(Xbox360Slider.LeftTrigger, LeftTrigger = L2); controller.SetSliderValue(Xbox360Slider.RightTrigger, RightTrigger = R2); // controller.SetButtonState(Xbox360Button.Y, Y = (Button_DPad_State & (1 << 7)) > 0); controller.SetButtonState(Xbox360Button.B, B = (Button_DPad_State & (1 << 6)) > 0); controller.SetButtonState(Xbox360Button.A, A = (Button_DPad_State & (1 << 5)) > 0); controller.SetButtonState(Xbox360Button.X, X = (Button_DPad_State & (1 << 4)) > 0); int dPad = Button_DPad_State & 0x0F; controller.SetButtonState(Xbox360Button.Up, Up = (dPad == 0 || dPad == 1 || dPad == 7)); controller.SetButtonState(Xbox360Button.Right, Right = (dPad == 1 || dPad == 2 || dPad == 3)); controller.SetButtonState(Xbox360Button.Down, Down = (dPad == 3 || dPad == 4 || dPad == 5)); controller.SetButtonState(Xbox360Button.Left, Left = (dPad == 5 || dPad == 6 || dPad == 7)); // controller.SetButtonState(Xbox360Button.RightThumb, RightThumb = (Button2_Status & (1 << 7)) > 0); controller.SetButtonState(Xbox360Button.LeftThumb, LeftThumb = (Button2_Status & (1 << 6)) > 0); controller.SetButtonState(Xbox360Button.Start, Start = (Button2_Status & (1 << 5)) > 0); //Option controller.SetButtonState(Xbox360Button.Back, Back = (Button2_Status & (1 << 4)) > 0); //Share controller.SetButtonState(Xbox360Button.RightShoulder, RightShoulder = (Button2_Status & (1 << 1)) > 0); controller.SetButtonState(Xbox360Button.LeftShoulder, LeftShoulder = (Button2_Status & (1 << 0)) > 0); controller.SubmitReport(); }
private void SetValueIfNeeded(Xbox360Axis axis, double?value) { if (value.HasValue) { var newValue = (short)((value.Value - 0.5) * 2 * short.MaxValue); controller.SetAxisValue(axis, newValue); } }
public void InputReportReceived(IDualShockDevice device, IInputReport report) { _target.ResetReport(); //This may be able to be optimized, look into later... // Convert report to DS3 format and store latest report for this device var ds3Report = (DualShock3InputReport)report; if (device.DeviceIndex == 0) { _Nav0Report = ds3Report; } if (device.DeviceIndex == 1) { _Nav1Report = ds3Report; } // Only combine reports and submit if we've seen input from each controller at least once if (_Nav0Report != null && _Nav1Report != null) { // Map buttons from Navigation #1 into input report _target.SetAxisValue(Xbox360Axis.LeftThumbX, Scale(_Nav0Report[DualShock3Axes.LeftThumbX], false)); _target.SetAxisValue(Xbox360Axis.LeftThumbY, Scale(_Nav0Report[DualShock3Axes.LeftThumbY], true)); _target.SetAxisValue(Xbox360Axis.RightThumbX, Scale(_Nav1Report[DualShock3Axes.LeftThumbX], false)); _target.SetAxisValue(Xbox360Axis.RightThumbY, Scale(_Nav1Report[DualShock3Axes.LeftThumbY], true)); _target.SetSliderValue(Xbox360Slider.LeftTrigger, _Nav0Report[DualShock3Axes.LeftTrigger]); _target.SetSliderValue(Xbox360Slider.RightTrigger, _Nav1Report[DualShock3Axes.LeftTrigger]); foreach (var button in _btnMap0.Where(m => _Nav0Report.EngagedButtons.Contains(m.Key)) .Select(m => m.Value)) { _target.SetButtonState(button, true); } foreach (var button in _btnMap1.Where(m => _Nav1Report.EngagedButtons.Contains(m.Key)) .Select(m => m.Value)) { _target.SetButtonState(button, true); } _target.SubmitReport(); } }
/// <summary> /// Set the value of the axis to the given one /// </summary> /// <param name="state">The state to use for setting the controller</param> public void SetAxis(StatePacket state) { //convert the state to a controller axis var axis = ControllerUtils.StateToXbox360Axis(state); try { var src = state.Value - MaxValue; //convert the ushort value to a short var value = src > (ushort)MaxValue ? MaxValue : (short)src; //NOTE: 0 is the middle point, values go from (-32768 => 32768) //set the value of the axis from the controller _controller.SetAxisValue(axis, value); } catch (Exception e) { Logger.LogError(e.Message); } }
public void updateRoutine() { while (!shouldStop) { // Read 27 bytes from the guitar int bytesRead; byte[] readBuffer = new byte[27]; var reader = device.OpenEndpointReader(ReadEndpointID.Ep01); reader.Read(readBuffer, 100, out bytesRead); // Prevent default 0x00 when no bytes are read if (bytesRead > 0) { // Set the fret inputs on the virtual 360 controller byte frets = readBuffer[0]; controller.SetButtonState(Xbox360Button.A, (frets & 0x02) != 0x00); // B1 controller.SetButtonState(Xbox360Button.B, (frets & 0x04) != 0x00); // B2 controller.SetButtonState(Xbox360Button.Y, (frets & 0x08) != 0x00); // B3 controller.SetButtonState(Xbox360Button.X, (frets & 0x01) != 0x00); // W1 controller.SetButtonState(Xbox360Button.LeftShoulder, (frets & 0x10) != 0x00); // W2 controller.SetButtonState(Xbox360Button.RightShoulder, (frets & 0x20) != 0x00); // W3 // Set the strum bar values - can probably be more efficient but eh byte strum = readBuffer[4]; if (strum == 0xFF) { // Strum Down controller.SetButtonState(Xbox360Button.Down, true); controller.SetAxisValue(Xbox360Axis.LeftThumbY, -32768); controller.SetButtonState(Xbox360Button.Up, false); } else if (strum == 0x00) { // Strum Up controller.SetButtonState(Xbox360Button.Down, false); controller.SetAxisValue(Xbox360Axis.LeftThumbY, 32767); controller.SetButtonState(Xbox360Button.Up, true); } else { // No Strum controller.SetButtonState(Xbox360Button.Down, false); controller.SetAxisValue(Xbox360Axis.LeftThumbY, 0); controller.SetButtonState(Xbox360Button.Up, false); } // Set the buttons (pause/HP only for now) byte buttons = readBuffer[1]; controller.SetButtonState(Xbox360Button.Start, (buttons & 0x02) != 0x00); // Pause controller.SetButtonState(Xbox360Button.Back, (buttons & 0x01) != 0x00); // Hero Power controller.SetButtonState(Xbox360Button.LeftThumb, (buttons & 0x04) != 0x00); // GHTV Button controller.SetButtonState(Xbox360Button.Guide, (buttons & 0x10) != 0x00); // Sync Button // Set the tilt and whammy controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)((readBuffer[6] * 0x101) - 32768)); controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)((readBuffer[19] * 0x101) - 32768)); // TODO: Proper D-Pad emulation } } }
void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { // Read 20 bytes from the characteristic IBuffer buffer = args.CharacteristicValue; var dataReader = DataReader.FromBuffer(buffer); byte[] readBuffer = new byte[buffer.Length]; for (int i = 0; i < buffer.Length; i++) { readBuffer[i] = dataReader.ReadByte(); } // Set the fret inputs on the virtual 360 controller byte frets = readBuffer[0]; controller.SetButtonState(Xbox360Button.A, (frets & 0x02) != 0x00); // B1 controller.SetButtonState(Xbox360Button.B, (frets & 0x04) != 0x00); // B2 controller.SetButtonState(Xbox360Button.Y, (frets & 0x08) != 0x00); // B3 controller.SetButtonState(Xbox360Button.X, (frets & 0x01) != 0x00); // W1 controller.SetButtonState(Xbox360Button.LeftShoulder, (frets & 0x10) != 0x00); // W2 controller.SetButtonState(Xbox360Button.RightShoulder, (frets & 0x20) != 0x00); // W3 // Set the strum bar values - can probably be more efficient but eh byte strum = readBuffer[4]; if (strum == 0xFF) { // Strum Down controller.SetButtonState(Xbox360Button.Down, true); controller.SetAxisValue(Xbox360Axis.LeftThumbY, -32768); controller.SetButtonState(Xbox360Button.Up, false); } else if (strum == 0x00) { // Strum Up controller.SetButtonState(Xbox360Button.Down, false); controller.SetAxisValue(Xbox360Axis.LeftThumbY, 32767); controller.SetButtonState(Xbox360Button.Up, true); } else { // No Strum controller.SetButtonState(Xbox360Button.Down, false); controller.SetAxisValue(Xbox360Axis.LeftThumbY, 0); controller.SetButtonState(Xbox360Button.Up, false); } // Set the buttons (pause/HP only for now) byte buttons = readBuffer[1]; controller.SetButtonState(Xbox360Button.Start, (buttons & 0x02) != 0x00); // Pause controller.SetButtonState(Xbox360Button.Back, (buttons & 0x08) != 0x00); // Hero Power controller.SetButtonState(Xbox360Button.LeftThumb, (buttons & 0x04) != 0x00); // GHTV Button controller.SetButtonState(Xbox360Button.Guide, (buttons & 0x10) != 0x00); // Sync Button // Set the tilt and whammy controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)((readBuffer[6] * 0x101) - 32768)); controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)((readBuffer[19] * 0x101) - 32768)); // TODO: Proper D-Pad emulation }
private void input_thread() { target360.Connect(); bool ss_button_pressed = false; bool ss_button_held = false; _stream.ReadTimeout = Timeout.Infinite; byte[] data = new byte[_device.GetMaxInputReportLength()]; while (running) { int read = 0; try { read = _stream.Read(data); } catch (TimeoutException e) { read = 0; } catch { unplug(joinInputThread: false); } if (read > 0) { if (data[0] == 0x03 && read == 10) { target360.ResetReport(); if ((data[3] & 64) != 0) { target360.SetButtonState(Xbox360Button.A, true); } if ((data[3] & 32) != 0) { target360.SetButtonState(Xbox360Button.B, true); } if ((data[3] & 16) != 0) { target360.SetButtonState(Xbox360Button.X, true); } if ((data[3] & 8) != 0) { target360.SetButtonState(Xbox360Button.Y, true); } if ((data[3] & 4) != 0) { target360.SetButtonState(Xbox360Button.LeftShoulder, true); } if ((data[3] & 2) != 0) { target360.SetButtonState(Xbox360Button.RightShoulder, true); } if ((data[3] & 1) != 0) { target360.SetButtonState(Xbox360Button.LeftThumb, true); } if ((data[2] & 128) != 0) { target360.SetButtonState(Xbox360Button.RightThumb, true); } ss_button_pressed = (data[2] & 1) != 0; //assistant_button_pressed = ( currentState[2] & 2 ) != 0; // [2] & 2 == Assistant, [2] & 1 == Screenshot switch (data[1]) { default: break; case 0: target360.SetButtonState(Xbox360Button.Up, true); break; case 1: target360.SetButtonState(Xbox360Button.Up, true); target360.SetButtonState(Xbox360Button.Right, true); break; case 2: target360.SetButtonState(Xbox360Button.Right, true); break; case 3: target360.SetButtonState(Xbox360Button.Down, true); target360.SetButtonState(Xbox360Button.Right, true); break; case 4: target360.SetButtonState(Xbox360Button.Down, true); break; case 5: target360.SetButtonState(Xbox360Button.Down, true); target360.SetButtonState(Xbox360Button.Left, true); break; case 6: target360.SetButtonState(Xbox360Button.Left, true); break; case 7: target360.SetButtonState(Xbox360Button.Up, true); target360.SetButtonState(Xbox360Button.Left, true); break; } if ((data[2] & 32) != 0) { target360.SetButtonState(Xbox360Button.Start, true); } if ((data[2] & 64) != 0) { target360.SetButtonState(Xbox360Button.Back, true); } if ((data[2] & 16) != 0) { target360.SetButtonState(Xbox360Button.Guide, true); } // Note: The HID reports do not allow stick values of 00. // This seems to make sense: 0x80 is center, so usable values are: // 0x01 to 0x7F and 0x81 to 0xFF. // For our purposes I believe this is undesirable. Subtract 1 from negative // values to allow maxing out the stick values. // TODO: Get an Xbox controller and verify this is standard behavior. for (int i = 4; i <= 7; ++i) { if (data[i] <= 0x7F && data[i] > 0x00) { data[i] -= 0x01; } } ushort LeftStickXunsigned = (ushort)(data[4] << 8 | (data[4] << 1 & 255)); if (LeftStickXunsigned == 0xFFFE) { LeftStickXunsigned = 0xFFFF; } short LeftStickX = (short)(LeftStickXunsigned - 0x8000); ushort LeftStickYunsigned = (ushort)(data[5] << 8 | (data[5] << 1 & 255)); if (LeftStickYunsigned == 0xFFFE) { LeftStickYunsigned = 0xFFFF; } short LeftStickY = (short)(-LeftStickYunsigned + 0x7FFF); if (LeftStickY == -1) { LeftStickY = 0; } ushort RightStickXunsigned = (ushort)(data[6] << 8 | (data[6] << 1 & 255)); if (RightStickXunsigned == 0xFFFE) { RightStickXunsigned = 0xFFFF; } short RightStickX = (short)(RightStickXunsigned - 0x8000); ushort RightStickYunsigned = (ushort)(data[7] << 8 | (data[7] << 1 & 255)); if (RightStickYunsigned == 0xFFFE) { RightStickYunsigned = 0xFFFF; } short RightStickY = (short)(-RightStickYunsigned + 0x7FFF); if (RightStickY == -1) { RightStickY = 0; } target360.SetAxisValue(Xbox360Axis.LeftThumbX, LeftStickX); target360.SetAxisValue(Xbox360Axis.LeftThumbY, LeftStickY); target360.SetAxisValue(Xbox360Axis.RightThumbX, RightStickX); target360.SetAxisValue(Xbox360Axis.RightThumbY, RightStickY); target360.SetSliderValue(Xbox360Slider.LeftTrigger, data[8]); target360.SetSliderValue(Xbox360Slider.RightTrigger, data[9]); target360.SubmitReport(); } } if (ss_button_pressed && !ss_button_held) { ss_button_held = true; try { // TODO: Allow configuring this keybind. ssThread = new Thread(() => System.Windows.Forms.SendKeys.SendWait("^+Z")); ssThread.Start(); } catch { } } else if (ss_button_held && !ss_button_pressed) { ss_button_held = false; } } target360.Disconnect(); }
public virtual bool Report(byte[] input, byte[] output) { byte id = (byte)(input[4] - 1); if (targets.Count <= id || targets[id] == null) { return(false); } IXbox360Controller controller = targets[id]; controller.ResetReport(); if ((input[10] & 32) != 0) { controller.SetButtonState(Xbox360Button.Back, true); } if ((input[10] & 64) != 0) { controller.SetButtonState(Xbox360Button.LeftThumb, true); } if ((input[10] & 128) != 0) { controller.SetButtonState(Xbox360Button.RightThumb, true); } if ((input[10] & 16) != 0) { controller.SetButtonState(Xbox360Button.Start, true); } if ((input[10] & 1) != 0) { controller.SetButtonState(Xbox360Button.Up, true); } if ((input[10] & 2) != 0) { controller.SetButtonState(Xbox360Button.Down, true); } if ((input[10] & 4) != 0) { controller.SetButtonState(Xbox360Button.Left, true); } if ((input[10] & 8) != 0) { controller.SetButtonState(Xbox360Button.Right, true); } if ((input[11] & 1) != 0) { controller.SetButtonState(Xbox360Button.LeftShoulder, true); } if ((input[11] & 2) != 0) { controller.SetButtonState(Xbox360Button.RightShoulder, true); } if ((input[11] & 128) != 0) { controller.SetButtonState(Xbox360Button.Y, true); } if ((input[11] & 32) != 0) { controller.SetButtonState(Xbox360Button.B, true); } if ((input[11] & 16) != 0) { controller.SetButtonState(Xbox360Button.A, true); } if ((input[11] & 64) != 0) { controller.SetButtonState(Xbox360Button.X, true); } if ((input[11] & 4) != 0) { controller.SetButtonState(Xbox360Button.Guide, true); } controller.SetSliderValue(Xbox360Slider.LeftTrigger, input[12]); controller.SetSliderValue(Xbox360Slider.RightTrigger, input[13]); controller.SetAxisValue(Xbox360Axis.LeftThumbX, BitConverter.ToInt16(input, 14)); controller.SetAxisValue(Xbox360Axis.LeftThumbY, BitConverter.ToInt16(input, 16)); controller.SetAxisValue(Xbox360Axis.RightThumbX, BitConverter.ToInt16(input, 18)); controller.SetAxisValue(Xbox360Axis.RightThumbY, BitConverter.ToInt16(input, 20)); controller.SubmitReport(); return(true); }
private void DeviceWorker() { _Logger.Information("Starting worker thread for {Device}", _Device); DisableReEnableDevice(); // Open HID device to read input from the gamepad _Logger.Information("Opening HID device {Device}", _Device); _Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive); ExclusiveMode = true; // If exclusive mode is not available, retry in shared mode. if (!_Device.IsOpen) { _Logger.Warning("Cannot access HID device in exclusive mode, retrying in shared mode: {Device}", _Device); _Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); ExclusiveMode = false; } if (_Device.IsOpen) { // Init Xiaomi Gamepad vibration _Device.WriteFeatureData(new byte[] { 0x20, 0x00, 0x00 }); // Connect the virtual Xbox360 gamepad try { _Logger.Information("Connecting to ViGEm client"); _Target.Connect(); } catch (VigemAlreadyConnectedException e) { _Logger.Warning(e, "ViGEm client was already opened, closing and reopening it"); _Target.Disconnect(); _Target.Connect(); } Started?.Invoke(this, EventArgs.Empty); HidReport hidReport; while (!_CTS.Token.IsCancellationRequested) { // Is device has been closed, exit the loop if (!_Device.IsOpen) { break; } // Otherwise read a report hidReport = _Device.ReadReport(1000); if (hidReport.ReadStatus == HidDeviceData.ReadStatus.WaitTimedOut) { continue; } else if (hidReport.ReadStatus != HidDeviceData.ReadStatus.Success) { _Logger.Error("Cannot read HID report for device {Device}, got {Report}", _Device, hidReport.ReadStatus); break; } var data = hidReport.Data; /* * [0] Buttons state, 1 bit per button * [1] Buttons state, 1 bit per button * [2] 0x00 * [3] D-Pad * [4] Left thumb, X axis * [5] Left thumb, Y axis * [6] Right thumb, X axis * [7] Right thumb, Y axis * [8] 0x00 * [9] 0x00 * [10] L trigger * [11] R trigger * [12] Accelerometer axis 1 * [13] Accelerometer axis 1 * [14] Accelerometer axis 2 * [15] Accelerometer axis 2 * [16] Accelerometer axis 3 * [17] Accelerometer axis 3 * [18] Battery level * [19] MI button */ lock (_Target) { _Target.SetButtonState(Xbox360Button.A, GetBit(data[0], 0)); _Target.SetButtonState(Xbox360Button.B, GetBit(data[0], 1)); _Target.SetButtonState(Xbox360Button.X, GetBit(data[0], 3)); _Target.SetButtonState(Xbox360Button.Y, GetBit(data[0], 4)); _Target.SetButtonState(Xbox360Button.LeftShoulder, GetBit(data[0], 6)); _Target.SetButtonState(Xbox360Button.RightShoulder, GetBit(data[0], 7)); _Target.SetButtonState(Xbox360Button.Back, GetBit(data[1], 2)); _Target.SetButtonState(Xbox360Button.Start, GetBit(data[1], 3)); _Target.SetButtonState(Xbox360Button.LeftThumb, GetBit(data[1], 5)); _Target.SetButtonState(Xbox360Button.RightThumb, GetBit(data[1], 6)); // Reset Hat switch status, as is set to 15 (all directions set, impossible state) _Target.SetButtonState(Xbox360Button.Up, false); _Target.SetButtonState(Xbox360Button.Left, false); _Target.SetButtonState(Xbox360Button.Down, false); _Target.SetButtonState(Xbox360Button.Right, false); if (data[3] < 8) { var btns = HatSwitches[data[3]]; // Hat Switch is a number from 0 to 7, where 0 is Up, 1 is Up-Left, etc. foreach (var b in btns) { _Target.SetButtonState(b, true); } } // Analog axis _Target.SetAxisValue(Xbox360Axis.LeftThumbX, MapAnalog(data[4])); _Target.SetAxisValue(Xbox360Axis.LeftThumbY, MapAnalog(data[5], true)); _Target.SetAxisValue(Xbox360Axis.RightThumbX, MapAnalog(data[6])); _Target.SetAxisValue(Xbox360Axis.RightThumbY, MapAnalog(data[7], true)); // Triggers _Target.SetSliderValue(Xbox360Slider.LeftTrigger, data[10]); _Target.SetSliderValue(Xbox360Slider.RightTrigger, data[11]); // Logo ("home") button if (GetBit(data[19], 0)) { _Target.SetButtonState(Xbox360Button.Guide, true); Task.Delay(200).ContinueWith(DelayedReleaseGuideButton); } // Update battery level BatteryLevel = data[18]; _Target.SubmitReport(); } } // Disconnect the virtual Xbox360 gamepad // Let Dispose handle that, otherwise it will rise a NotPluggedIn exception _Logger.Information("Disconnecting ViGEm client"); _Target.Disconnect(); // Close the HID device _Logger.Information("Closing HID device {Device}", _Device); _Device.CloseDevice(); DisableReEnableDevice(); } else { _Logger.Error("Cannot open HID device {Device}", _Device); Ended?.Invoke(this, EventArgs.Empty); return; } _Logger.Information("Exiting worker thread for {0}", _Device.ToString()); Ended?.Invoke(this, EventArgs.Empty); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); try { client = new ViGEmClient(); controller = client.CreateXbox360Controller(); controller.Connect(); Console.WriteLine("Virtual Xbox360 gamepad connected."); } catch (Exception e) { Console.WriteLine("Could not connect to ViGEmClient\n"); Console.WriteLine(e); Console.WriteLine("\nNote that you need to install ViGemBus for this app to work\n"); Console.Read(); return; } int i = 0; while (true) { // We'll cycle through 8 different stick states i++; i %= 80; int state = i / 10; short amount = Int16.MaxValue / 2; if (i == 0) { controller.SetButtonState(Xbox360Button.A, true); } else { controller.SetButtonState(Xbox360Button.A, false); } if (state == 0) { controller.SetAxisValue(Xbox360Axis.LeftThumbX, amount); } else if (state == 1) { controller.SetAxisValue(Xbox360Axis.LeftThumbX, (short)(-amount)); } else { controller.SetAxisValue(Xbox360Axis.LeftThumbX, 0); } if (state == 2) { controller.SetAxisValue(Xbox360Axis.LeftThumbY, amount); } else if (state == 3) { controller.SetAxisValue(Xbox360Axis.LeftThumbY, (short)(-amount)); } else { controller.SetAxisValue(Xbox360Axis.LeftThumbY, 0); } if (state == 4) { controller.SetAxisValue(Xbox360Axis.RightThumbX, amount); } else if (state == 5) { controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)(-amount)); } else { controller.SetAxisValue(Xbox360Axis.RightThumbX, 0); } if (state == 6) { controller.SetAxisValue(Xbox360Axis.RightThumbY, amount); } else if (state == 7) { controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)(-amount)); } else { controller.SetAxisValue(Xbox360Axis.RightThumbY, 0); } System.Threading.Thread.Sleep(50); } }
public void UpdateController(IXbox360Controller controller) { // Only update controller if it is plugged in. if (IsPlugged) { controller.SetButtonState(Xbox360Button.A, BUTTON_B); // Button layouts on Xbox controllers are swapped from Nintendo layout controller.SetButtonState(Xbox360Button.B, BUTTON_A); controller.SetButtonState(Xbox360Button.X, BUTTON_Y); controller.SetButtonState(Xbox360Button.Y, BUTTON_X); controller.SetButtonState(Xbox360Button.Left, DPAD_LEFT); controller.SetButtonState(Xbox360Button.Right, DPAD_RIGHT); controller.SetButtonState(Xbox360Button.Up, DPAD_UP); controller.SetButtonState(Xbox360Button.Down, DPAD_DOWN); controller.SetButtonState(Xbox360Button.Start, BUTTON_START); // If enabled, the triggers act as digital buttons. if (ApplicationSettings.EnableDigitalPress) { if ((ANALOG_LEFT / 255f) > ApplicationSettings.TriggerDeadzone) { controller.SetButtonState(Xbox360Button.LeftShoulder, true); } else { controller.SetButtonState(Xbox360Button.LeftShoulder, false); } if ((ANALOG_RIGHT / 255f) > ApplicationSettings.TriggerDeadzone) { controller.SetButtonState(Xbox360Button.RightShoulder, true); } else { controller.SetButtonState(Xbox360Button.RightShoulder, false); } if (BUTTON_Z) { controller.SetSliderValue(Xbox360Slider.RightTrigger, 255); } else { controller.SetSliderValue(Xbox360Slider.RightTrigger, 0); } } // Use normal triggerThreshold/deadzone values else { controller.SetSliderValue(Xbox360Slider.LeftTrigger, ANALOG_LEFT); controller.SetSliderValue(Xbox360Slider.RightTrigger, ANALOG_RIGHT); controller.SetButtonState(Xbox360Button.RightShoulder, BUTTON_Z); } // Check if calibration has been done. If not generate it if (_Calibration.CurrentState == CalibrationState.Calibrating) { _Calibration.GenerateCalibrations(); } // Full calibration scales generated if (_Calibration.CurrentState == CalibrationState.Calibrated) { LeftStickX = Math.Round(LEFT_STICK_X * _Calibration.LeftStickCalibration[0] - _Calibration.LeftStickCalibration[2]); LeftStickY = Math.Round(LEFT_STICK_Y * _Calibration.LeftStickCalibration[1] - _Calibration.LeftStickCalibration[3]); CStickX = Math.Round(C_STICK_X * _Calibration.CStickCalibration[0] - _Calibration.CStickCalibration[2]); CStickY = Math.Round(C_STICK_Y * _Calibration.CStickCalibration[1] - _Calibration.CStickCalibration[3]); } // Stick centers generated else if (_Calibration.CurrentState == CalibrationState.SticksCentered) { LeftStickX = LEFT_STICK_X + _Calibration.StickCenters[0]; LeftStickY = LEFT_STICK_Y + _Calibration.StickCenters[1]; CStickX = C_STICK_X + _Calibration.StickCenters[2]; CStickY = C_STICK_Y + _Calibration.StickCenters[3]; } // No calibration done else { LeftStickX = LEFT_STICK_X; LeftStickY = LEFT_STICK_Y; CStickX = C_STICK_X; CStickY = C_STICK_Y; } LeftStickX = Extensions.ByteToShort((byte)Extensions.Clamp(LeftStickX, 0, 255)); // ensure stick values are within byte range LeftStickY = Extensions.ByteToShort((byte)Extensions.Clamp(LeftStickY, 0, 255)); CStickX = Extensions.ByteToShort((byte)Extensions.Clamp(CStickX, 0, 255)); CStickY = Extensions.ByteToShort((byte)Extensions.Clamp(CStickY, 0, 255)); if (GetDeadzone((float)LeftStickX, (float)LeftStickY)) // This means they are within the deadzone, take no input { LeftStickX = 0; LeftStickY = 0; } if (GetDeadzone((float)CStickX, (float)CStickY)) { CStickX = 0; CStickY = 0; } controller.SetAxisValue(Xbox360Axis.LeftThumbX, (short)LeftStickX); controller.SetAxisValue(Xbox360Axis.LeftThumbY, (short)LeftStickY); controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)CStickX); controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)CStickY); controller.FeedbackReceived += Controller_FeedbackReceived; controller.SubmitReport(); } }
public void updateRoutine() { short[] tilt = new short[(10)]; for (int i = 0; i < tilt.Length; i++) { tilt[i] = 0; } short sum = 0; short old_tilt = 0; short st_power = 0; int pos = 0; FixedSizedQueue <int> buffer100 = new FixedSizedQueue <int>(50); FixedSizedQueue <int> buffer10 = new FixedSizedQueue <int>(10); while (!shouldStop) { // Read 27 bytes from the guitar int bytesRead; byte[] readBuffer = new byte[27]; var reader = device.OpenEndpointReader(ReadEndpointID.Ep01); reader.Read(readBuffer, 100, out bytesRead); /*foreach(byte i in readBuffer) * { * Console.Write("{0:X2} ", i); * } * Console.WriteLine(); */ // Prevent default 0x00 when no bytes are read if (bytesRead > 0) { // Set the fret inputs on the virtual 360 controller byte frets = readBuffer[0]; controller.SetButtonState(Xbox360Button.A, (frets & 0x02) != 0x00 || (readBuffer[6] > 0x1F && readBuffer[6] < 0x30)); // B1 controller.SetButtonState(Xbox360Button.B, (frets & 0x04) != 0x00 || (readBuffer[6] > 0x4F && readBuffer[6] < 0x60)); // B2 controller.SetButtonState(Xbox360Button.Y, (frets & 0x08) != 0x00 || (readBuffer[6] > 0xAF && readBuffer[6] <= 0xC0)); // B3 controller.SetButtonState(Xbox360Button.X, (frets & 0x01) != 0x00 || (readBuffer[6] > 0x8F && readBuffer[6] < 0xA0)); // W1 controller.SetButtonState(Xbox360Button.LeftShoulder, (frets & 0x10) != 0x00 || (readBuffer[6] > 0xEF && readBuffer[6] <= 0xFF)); // W2 // Set the strum bar values - can probably be more efficient but eh byte strum = readBuffer[2]; if (strum == 0x04) { // Strum Down controller.SetButtonState(Xbox360Button.Down, true); controller.SetAxisValue(Xbox360Axis.LeftThumbY, -32768); controller.SetButtonState(Xbox360Button.Up, false); } else if (strum == 0x00) { // Strum Up controller.SetButtonState(Xbox360Button.Down, false); controller.SetAxisValue(Xbox360Axis.LeftThumbY, 32767); controller.SetButtonState(Xbox360Button.Up, true); } else { // No Strum controller.SetButtonState(Xbox360Button.Down, false); controller.SetAxisValue(Xbox360Axis.LeftThumbY, 0); controller.SetButtonState(Xbox360Button.Up, false); } // Set the buttons (pause/HP only for now) byte buttons = readBuffer[1]; controller.SetButtonState(Xbox360Button.Start, (buttons & 0x02) != 0x00); // Start controller.SetButtonState(Xbox360Button.Back, (buttons & 0x01) != 0x00); // Select Power controller.SetButtonState(Xbox360Button.LeftThumb, (buttons & 0x04) != 0x00); // GHTV Button controller.SetButtonState(Xbox360Button.Guide, (buttons & 0x10) != 0x00); // Sync Button // Set the tilt and whammy //Console.WriteLine((readBuffer[19] << 8)); controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)(Int16)((readBuffer[5] << 8) - 32768)); //Console.WriteLine(readBuffer[19]); /*controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)((readBuffer[19] << 8)));*/ sum -= tilt[pos]; tilt[pos] = (short)(readBuffer[19]); sum += tilt[pos]; old_tilt = readBuffer[19]; pos = (pos + 1) % 10; // pos10 = pos % 10; //Console.WriteLine(sum); buffer100.Enqueue(readBuffer[19]); buffer10.Enqueue(readBuffer[19]); //Console.WriteLine((short)((Int16)((-buffer10.Average() + buffer100.Average())) > 5 ? 32767 : 0)); controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)((Int16)(-buffer10.Average() + buffer100.Average()) > 5 ? 32767 : 0)); /** * * if (buffer10.Average()<buffer100.Average()-5) * { * st_power++; * } * else * { * st_power = 0; * controller.SetAxisValue(Xbox360Axis.RightThumbX, 0); * } **/ //controller.SetButtonState(Xbox360Button.Back, st_power>0); // TODO: Proper D-Pad emulation } } }