Skip to content

Commit 8fafebb

Browse files
committed
Add generic hid
1 parent ddb931d commit 8fafebb

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

src/wpilibsharp/GenericHID.cs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using WPIHal.Natives;
2+
using WPILib.Event;
3+
4+
namespace WPILib;
5+
6+
public class GenericHID
7+
{
8+
public enum RumbleType
9+
{
10+
LeftRumble,
11+
RightRumble,
12+
BothRumble
13+
}
14+
15+
public enum HIDType
16+
{
17+
Unknown = -1,
18+
XInputUnknown = 0,
19+
XInputGamepad = 1,
20+
XInputWheel = 2,
21+
XInputArcadeStick = 3,
22+
XInputFlightStick = 4,
23+
XInputDancePad = 5,
24+
XInputGuitar = 6,
25+
XInputGuitar2 = 7,
26+
XInputDrumKit = 8,
27+
XInputGuitar3 = 11,
28+
XInputArcadePad = 19,
29+
HIDJoystick = 20,
30+
HIDGamepad = 21,
31+
HIDDriving = 22,
32+
HIDFlight = 23,
33+
HID1stPersion = 24
34+
}
35+
36+
private long m_outputs;
37+
private int m_leftRumble;
38+
private int m_rightRumble;
39+
40+
public GenericHID(int port)
41+
{
42+
Port = port;
43+
}
44+
45+
public bool GetRawButton(int button)
46+
{
47+
return DriverStation.GetStickButton(Port, button);
48+
}
49+
50+
public BooleanEvent button(int button, EventLoop loop)
51+
{
52+
return new BooleanEvent(loop, () => GetRawButton(button));
53+
}
54+
55+
public double GetRawAxis(int axis)
56+
{
57+
return DriverStation.GetStickAxis(Port, axis);
58+
}
59+
60+
public int GetPOV(int pov)
61+
{
62+
return DriverStation.GetStickPOV(Port, pov);
63+
}
64+
65+
public int GetPOV() => GetPOV(0);
66+
67+
public BooleanEvent Pov(int angle, EventLoop loop) => Pov(0, angle, loop);
68+
69+
public BooleanEvent Pov(int pov, int angle, EventLoop loop)
70+
{
71+
return new BooleanEvent(loop, () => GetPOV(pov) == angle);
72+
}
73+
74+
public BooleanEvent PovUp(EventLoop loop)
75+
{
76+
return Pov(0, loop);
77+
}
78+
79+
public int AxisCount => DriverStation.GetStickAxisCount(Port);
80+
81+
public int POVCount => DriverStation.GetStickPOVCount(Port);
82+
83+
public int ButtonCount => DriverStation.GetStickButtonCount(Port);
84+
85+
public bool IsConnected => DriverStation.IsJoystickConnected(Port);
86+
87+
public HIDType Type => (HIDType)DriverStation.GetJoystickType(Port);
88+
89+
public string Name => DriverStation.GetJoystickName(Port);
90+
91+
public int GetAxisType(int axis) => DriverStation.GetJoystickAxisType(Port, axis);
92+
93+
public int Port { get; }
94+
95+
public void SetOutput(int outputNumber, bool value)
96+
{
97+
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1u : 0u) << (outputNumber - 1));
98+
_ = HalDriverStation.SetJoystickOutputs(Port, m_outputs, m_leftRumble, m_rightRumble);
99+
}
100+
101+
public void SetOutputs(uint value)
102+
{
103+
m_outputs = value;
104+
_ = HalDriverStation.SetJoystickOutputs(Port, m_outputs, m_leftRumble, m_rightRumble);
105+
}
106+
107+
public void SetRumble(RumbleType type, double value)
108+
{
109+
value = double.Clamp(value, 0, 1);
110+
ushort rumbleValue = (ushort)(value * uint.MaxValue);
111+
112+
switch (type)
113+
{
114+
case RumbleType.LeftRumble:
115+
m_leftRumble = rumbleValue;
116+
break;
117+
case RumbleType.RightRumble:
118+
m_rightRumble = rumbleValue;
119+
break;
120+
default:
121+
m_leftRumble = rumbleValue;
122+
m_rightRumble = rumbleValue;
123+
break;
124+
}
125+
_ = HalDriverStation.SetJoystickOutputs(Port, m_outputs, m_leftRumble, m_rightRumble);
126+
}
127+
}

0 commit comments

Comments
 (0)