Skip to content

Commit aefadc6

Browse files
msoucyThadHouse
authored andcommitted
Use more idiomatic C# under the hood (#115)
* Spelling fixes Includes `DSAtached`, being renamed to `DSAttached` with a deprecated property in place temporarily. * Use expression-bodied methods Quite a few methods are simple accessors that can be better summarized in one expression * Sign Preserving Square Simplification Use `Math.Abs` to make the logic easier to read - the time savings from not doing a function call are probably not worth the legibility * Use C# `lock` statement No sense in manually rewriting the try-catch when `lock` does the same thing more succinctly * Use indexers in the Joystick class A joystick's button can now be accessed via `myJoystick[Joystick.ButtonType.Trigger]` etc. All similar accessors forward to the indexer. * Use a dictionary to store sendable choices Instead of lockstep lists, use a dictionary.
1 parent edc388e commit aefadc6

10 files changed

+124
-399
lines changed

WPILib/DriverStation.cs

+21-76
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using System;
2-
using System.IO;
1+
using HAL.Base;
2+
using System;
3+
using System.Runtime.CompilerServices;
34
using System.Threading;
4-
using HAL.Base;
5-
using static WPILib.Timer;
65
using static HAL.Base.HAL;
76
using static HAL.Base.HAL.DriverStationConstants;
8-
using static WPILib.Utility;
97
using static HAL.Base.HALDriverStation;
8+
using static WPILib.Timer;
9+
using static WPILib.Utility;
1010
using HALPower = HAL.Base.HALPower;
11-
using System.Runtime.CompilerServices;
12-
using System.Text;
1311

1412
namespace WPILib
1513
{
@@ -239,7 +237,7 @@ protected void GetData()
239237
UpdateControlWord(true, out controlWord);
240238

241239
lock (m_joystickDataMutex)
242-
{
240+
{
243241

244242
HALJoystickAxes[] currentAxes = m_joystickAxes;
245243
m_joystickAxes = m_joystickAxesCache;
@@ -369,17 +367,8 @@ public int GetStickAxisCount(int stick)
369367
$"Joystick index is out of range, should be 0-{JoystickPorts}");
370368
}
371369

372-
bool lockEntered = false;
373-
try
374-
{
375-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
370+
lock (m_joystickDataMutex)
376371
return m_joystickAxes[stick].count;
377-
}
378-
finally
379-
{
380-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
381-
}
382-
383372
}
384373

385374
/// <summary>
@@ -441,17 +430,9 @@ public int GetStickPOVCount(int stick)
441430
throw new ArgumentOutOfRangeException(nameof(stick),
442431
$"Joystick Index is out of range, should be 0-{JoystickPorts}");
443432
}
444-
bool lockEntered = false;
445-
try
446-
{
447-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
448433

434+
lock (m_joystickDataMutex)
449435
return m_joystickPOVs[stick].count;
450-
}
451-
finally
452-
{
453-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
454-
}
455436

456437
}
457438

@@ -464,23 +445,14 @@ public int GetStickPOVCount(int stick)
464445
/// Thrown if the stick is out of range.</exception>
465446
public int GetStickButtons(int stick)
466447
{
467-
468448
if (stick < 0 || stick >= JoystickPorts)
469449
{
470450
throw new ArgumentOutOfRangeException(nameof(stick),
471451
$"Joystick Index is out of range, should be 0-{JoystickPorts}");
472452
}
473-
bool lockEntered = false;
474-
try
475-
{
476-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
477-
return (int)m_joystickButtons[stick].buttons;
478-
}
479-
finally
480-
{
481-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
482-
}
483453

454+
lock (m_joystickDataMutex)
455+
return (int)m_joystickButtons[stick].buttons;
484456
}
485457

486458
/// <summary>
@@ -536,22 +508,14 @@ public bool GetStickButton(int stick, int button)
536508
/// Thrown if the stick is out of range.</exception>
537509
public int GetStickButtonCount(int stick)
538510
{
539-
540511
if (stick < 0 || stick >= JoystickPorts)
541512
{
542513
throw new ArgumentOutOfRangeException(nameof(stick),
543514
$"Joystick Index is out of range, should be 0-{JoystickPorts}");
544515
}
545-
bool lockEntered = false;
546-
try
547-
{
548-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
516+
517+
lock (m_joystickDataMutex)
549518
return m_joystickButtons[stick].count;
550-
}
551-
finally
552-
{
553-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
554-
}
555519
}
556520

557521
/// <summary>
@@ -568,16 +532,8 @@ public bool GetJoystickIsXbox(int stick)
568532
throw new ArgumentOutOfRangeException(nameof(stick),
569533
$"Joystick Index is out of range, should be 0-{JoystickPorts}");
570534
}
571-
bool lockEntered = false;
572-
try
573-
{
574-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
535+
lock (m_joystickDataMutex)
575536
return m_joystickDescriptors[stick].isXbox != 0;
576-
}
577-
finally
578-
{
579-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
580-
}
581537

582538
}
583539

@@ -595,16 +551,8 @@ public int GetJoystickType(int stick)
595551
throw new ArgumentOutOfRangeException(nameof(stick),
596552
$"Joystick Index is out of range, should be 0-{JoystickPorts}");
597553
}
598-
bool lockEntered = false;
599-
try
600-
{
601-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
554+
lock (m_joystickDataMutex)
602555
return m_joystickDescriptors[stick].type;
603-
}
604-
finally
605-
{
606-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
607-
}
608556
}
609557

610558
/// <summary>
@@ -622,16 +570,8 @@ public string GetJoystickName(int stick)
622570
$"Joystick Index is out of range, should be 0-{JoystickPorts}");
623571
}
624572

625-
bool lockEntered = false;
626-
try
627-
{
628-
Monitor.Enter(m_joystickDataMutex, ref lockEntered);
573+
lock (m_joystickDataMutex)
629574
return m_joystickDescriptors[stick].name.ToString();
630-
}
631-
finally
632-
{
633-
if (lockEntered) Monitor.Exit(m_joystickDataMutex);
634-
}
635575
}
636576

637577
/// <summary>
@@ -856,7 +796,7 @@ public bool FMSAttached
856796
/// <summary>
857797
/// Gets if the DS is attached.
858798
/// </summary>
859-
public bool DSAtached
799+
public bool DSAttached
860800
{
861801
get
862802
{
@@ -865,6 +805,11 @@ public bool DSAtached
865805
return word.GetDSAttached();
866806
}
867807
}
808+
/// <summary>
809+
/// Gets if the DS is attached.
810+
/// </summary>
811+
[Obsolete("DSAtached is deprecated, use DSAttached instead.")]
812+
public bool DSAtached => DSAttached;
868813

869814
private void UpdateControlWord(bool force, out HALControlWord controlWord)
870815
{

WPILib/GamepadBase.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,13 @@ public abstract class GamepadBase : GenericHID
99
/// Creates a new Gamepad on the specified port
1010
/// </summary>
1111
/// <param name="port">The joystick port</param>
12-
protected GamepadBase(int port) : base(port)
13-
{
14-
15-
}
12+
protected GamepadBase(int port) : base(port) { }
1613

1714
/// <summary>
1815
/// Gets if the bumper is pressed
1916
/// </summary>
2017
/// <returns>True if the bumper is pressed</returns>
21-
public bool GetBumper()
22-
{
23-
return GetBumper(JoystickHand.Right);
24-
}
18+
public bool GetBumper() => GetBumper(JoystickHand.Right);
2519

2620
/// <summary>
2721
/// Gets if the bupper is pressed

WPILib/GenericHID.cs

+13-45
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ protected GenericHID(int port)
7272
/// Get the x position of the HID
7373
/// </summary>
7474
/// <returns>The x position of the HID</returns>
75-
public double GetX()
76-
{
77-
return GetX(JoystickHand.Right);
78-
}
75+
public double GetX() => GetX(JoystickHand.Right);
7976

8077
/// <summary>
8178
/// Get the x position of the HID
@@ -88,10 +85,7 @@ public double GetX()
8885
/// Get the y position of the HID
8986
/// </summary>
9087
/// <returns>The y position of the HID</returns>
91-
public double GetY()
92-
{
93-
return GetY(JoystickHand.Right);
94-
}
88+
public double GetY() => GetY(JoystickHand.Right);
9589

9690
/// <summary>
9791
/// Get the y position of the HID
@@ -105,62 +99,35 @@ public double GetY()
10599
/// </summary>
106100
/// <param name="axis">Index of the axis</param>
107101
/// <returns>The raw value of the selected axis</returns>
108-
public virtual double GetRawAxis(int axis)
109-
{
110-
return m_ds.GetStickAxis(Port, axis);
111-
}
102+
public virtual double GetRawAxis(int axis) => m_ds.GetStickAxis(Port, axis);
112103

113104
/// <summary>
114105
/// Is the given button pressed
115106
/// </summary>
116107
/// <param name="button">Which button number</param>
117108
/// <returns>True if the button is pressed</returns>
118-
public bool GetRawButton(int button)
119-
{
120-
return m_ds.GetStickButton(Port, button);
121-
}
109+
public bool GetRawButton(int button) => m_ds.GetStickButton(Port, button);
122110

123111
/// <summary>
124112
/// Is POV 0 pressed
125113
/// </summary>
126114
/// <returns>the POV value</returns>
127-
public int GetPOV()
128-
{
129-
return GetPOV(0);
130-
}
115+
public int GetPOV() => GetPOV(0);
131116

132117
/// <summary>
133118
/// Is the given POV pressed
134119
/// </summary>
135120
/// <param name="pov">Which POV number</param>
136121
/// <returns>the POV value</returns>
137-
public int GetPOV(int pov)
138-
{
139-
return m_ds.GetStickPOV(Port, pov);
140-
}
122+
public int GetPOV(int pov) => m_ds.GetStickPOV(Port, pov);
141123

142-
public int GetPOVCount()
143-
{
144-
return m_ds.GetStickPOVCount(Port);
145-
}
124+
public int GetPOVCount() => m_ds.GetStickPOVCount(Port);
146125

147-
public HIDType Type
148-
{
149-
get
150-
{
151-
return (HIDType)m_ds.GetJoystickType(Port);
152-
}
153-
}
126+
public HIDType Type => (HIDType)m_ds.GetJoystickType(Port);
154127

155128
public int Port { get; }
156129

157-
public string Name
158-
{
159-
get
160-
{
161-
return m_ds.GetJoystickName(Port);
162-
}
163-
}
130+
public string Name => m_ds.GetJoystickName(Port);
164131

165132
private int m_outputs = 0;
166133
private ushort m_leftRumble = 0;
@@ -182,10 +149,11 @@ public void SetOutputs(int value)
182149

183150
public void SetRumble(RumbleType type, double value)
184151
{
185-
if(value < 0)
186-
value = 0;
187-
else if (value > 1)
152+
if (value < 0)
153+
value = 0;
154+
else if (value > 1)
188155
value = 1;
156+
189157
if (type == RumbleType.LeftRumble)
190158
{
191159
m_leftRumble = (ushort)(value * 65535);

0 commit comments

Comments
 (0)