Skip to content

Commit 63d78cb

Browse files
committed
Make joystick warning better
1 parent 245186c commit 63d78cb

File tree

4 files changed

+74
-87
lines changed

4 files changed

+74
-87
lines changed

hal/src/main/native/systemcore/FRCDriverStation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ void JoystickDataCache::Update(const mrc::ControlData& data) {
300300
}
301301
}
302302
}
303+
// Mark remaining sticks as unavailable
304+
for (size_t i = sticks.size(); i < HAL_kMaxJoysticks; i++) {
305+
axes[i].available = 0;
306+
povs[i].available = 0;
307+
buttons[i].available = 0;
308+
touchpads[i].count = 0;
309+
}
303310
}
304311

305312
#define CHECK_JOYSTICK_NUMBER(stickNum) \

wpilibc/src/main/native/cpp/driverstation/DriverStation.cpp

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,23 @@ static Instance& GetInstance() {
190190
static void SendMatchData();
191191

192192
template <typename S, typename... Args>
193-
static inline void ReportJoystickUnpluggedError(const S& format,
194-
Args&&... args) {
195-
ReportJoystickUnpluggedErrorV(format, fmt::make_format_args(args...));
193+
static inline void ReportJoystickError(int stick, const S& format,
194+
Args&&... args) {
195+
ReportJoystickErrorV(stick, format, fmt::make_format_args(args...));
196196
}
197197

198198
/**
199-
* Reports errors related to unplugged joysticks.
199+
* Reports errors related to joystick availability.
200200
*
201201
* Throttles the errors so that they don't overwhelm the DS.
202202
*/
203-
static void ReportJoystickUnpluggedWarningV(fmt::string_view format,
204-
fmt::format_args args);
203+
static void ReportJoystickWarningV(int stick, fmt::string_view format,
204+
fmt::format_args args);
205205

206206
template <typename S, typename... Args>
207-
static inline void ReportJoystickUnpluggedWarning(const S& format,
208-
Args&&... args) {
209-
ReportJoystickUnpluggedWarningV(format, fmt::make_format_args(args...));
207+
static inline void ReportJoystickWarning(int stick, const S& format,
208+
Args&&... args) {
209+
ReportJoystickWarningV(stick, format, fmt::make_format_args(args...));
210210
}
211211

212212
Instance::Instance() {
@@ -245,11 +245,8 @@ bool DriverStation::GetStickButton(int stick, int button) {
245245
HAL_GetJoystickButtons(stick, &buttons);
246246

247247
if ((buttons.available & mask) == 0) {
248-
ReportJoystickUnpluggedWarning(
249-
"Joystick Button {} missing (available {}), check if all controllers "
250-
"are "
251-
"plugged in",
252-
button, buttons.available);
248+
ReportJoystickWarning(stick, "Joystick Button {} on port {} not available",
249+
button, stick);
253250
return false;
254251
}
255252

@@ -297,11 +294,8 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) {
297294
uint64_t mask = 1LLU << button;
298295

299296
if ((buttons.available & mask) == 0) {
300-
ReportJoystickUnpluggedWarning(
301-
"Joystick Button {} missing (available {}), check if all controllers "
302-
"are "
303-
"plugged in",
304-
button, buttons.available);
297+
ReportJoystickWarning(stick, "Joystick Button {} on port {} not available",
298+
button, stick);
305299
return false;
306300
}
307301
auto& inst = ::GetInstance();
@@ -331,11 +325,8 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) {
331325
uint64_t mask = 1LLU << button;
332326

333327
if ((buttons.available & mask) == 0) {
334-
ReportJoystickUnpluggedWarning(
335-
"Joystick Button {} missing (available {}), check if all controllers "
336-
"are "
337-
"plugged in",
338-
button, buttons.available);
328+
ReportJoystickWarning(stick, "Joystick Button {} on port {} not available",
329+
button, stick);
339330
return false;
340331
}
341332
auto& inst = ::GetInstance();
@@ -364,10 +355,8 @@ double DriverStation::GetStickAxis(int stick, int axis) {
364355
HAL_GetJoystickAxes(stick, &axes);
365356

366357
if ((axes.available & mask) == 0) {
367-
ReportJoystickUnpluggedWarning(
368-
"Joystick Axis {} missing (available {}), check if all controllers are "
369-
"plugged in",
370-
axis, axes.available);
358+
ReportJoystickWarning(stick, "Joystick axis {} on port {} not available",
359+
axis, stick);
371360
return 0.0;
372361
}
373362

@@ -403,10 +392,10 @@ DriverStation::TouchpadFinger DriverStation::GetStickTouchpadFinger(
403392
}
404393
}
405394

406-
ReportJoystickUnpluggedWarning(
407-
"Joystick Touchpad Finger {} missing, check if all controllers are "
408-
"plugged in",
409-
touchpad);
395+
ReportJoystickWarning(
396+
stick,
397+
"Joystick touchpad finger {} on touchpad {} on port {} not available",
398+
finger, touchpad, stick);
410399
return TouchpadFinger{false, 0.0f, 0.0f};
411400
}
412401

@@ -478,10 +467,8 @@ DriverStation::POVDirection DriverStation::GetStickPOV(int stick, int pov) {
478467
HAL_GetJoystickPOVs(stick, &povs);
479468

480469
if ((povs.available & mask) == 0) {
481-
ReportJoystickUnpluggedWarning(
482-
"Joystick POV {} missing (available {}), check if all controllers are "
483-
"plugged in",
484-
pov, povs.available);
470+
ReportJoystickWarning(stick, "Joystick POV {} on port {} not available",
471+
pov, stick);
485472
return kCenter;
486473
}
487474

@@ -855,13 +842,21 @@ void DriverStation::StartDataLog(wpi::log::DataLog& log, bool logJoysticks) {
855842
}
856843
}
857844

858-
void ReportJoystickUnpluggedWarningV(fmt::string_view format,
859-
fmt::format_args args) {
845+
void ReportJoystickWarningV(int stick, fmt::string_view format,
846+
fmt::format_args args) {
860847
auto& inst = GetInstance();
861848
if (DriverStation::IsFMSAttached() || !inst.silenceJoystickWarning) {
862849
auto currentTime = Timer::GetTimestamp();
863850
if (currentTime > inst.nextMessageTime) {
864-
ReportErrorV(warn::Warning, "", 0, "", format, args);
851+
if (DriverStation::IsJoystickConnected(stick)) {
852+
ReportErrorV(warn::Warning, "", 0, "", format, args);
853+
} else {
854+
ReportError(
855+
warn::Warning, "", 0, "",
856+
"Joystick on port {} not available, check if all controllers are "
857+
"plugged in",
858+
stick);
859+
}
865860
inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval;
866861
}
867862
}

wpilibc/src/test/native/cpp/DriverStationTest.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ INSTANTIATE_TEST_SUITE_P(
6565
DriverStationTests, JoystickConnectionWarningTest,
6666
::testing::Values(
6767
std::make_tuple(false, true, true, ""),
68-
std::make_tuple(
69-
false, false, false,
70-
"Warning: Joystick Button 1 missing (available 0), check if all "
71-
"controllers are plugged in\n"),
72-
std::make_tuple(
73-
true, true, false,
74-
"Warning: Joystick Button 1 missing (available 0), check if all "
75-
"controllers are plugged in\n"),
76-
std::make_tuple(
77-
true, false, false,
78-
"Warning: Joystick Button 1 missing (available 0), check if all "
79-
"controllers are plugged in\n")));
68+
std::make_tuple(false, false, false,
69+
"Warning: Joystick on port 0 not available, check if "
70+
"all controllers are plugged in\n"),
71+
std::make_tuple(true, true, false,
72+
"Warning: Joystick on port 0 not available, check if "
73+
"all controllers are plugged in\n"),
74+
std::make_tuple(true, false, false,
75+
"Warning: Joystick on port 0 not available, check if "
76+
"all controllers are plugged in\n")));

wpilibj/src/main/java/org/wpilib/driverstation/DriverStation.java

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,8 @@ public static boolean getStickButton(final int stick, final int button) {
714714
m_cacheDataMutex.unlock();
715715
}
716716

717-
reportJoystickUnpluggedWarning(
718-
"Joystick Button "
719-
+ button
720-
+ " on port "
721-
+ stick
722-
+ " not available, check if controller is plugged in");
717+
reportJoystickWarning(
718+
stick, "Joystick Button " + button + " on port " + stick + " not available");
723719
return false;
724720
}
725721

@@ -783,12 +779,8 @@ public static boolean getStickButtonPressed(final int stick, final int button) {
783779
m_cacheDataMutex.unlock();
784780
}
785781

786-
reportJoystickUnpluggedWarning(
787-
"Joystick Button "
788-
+ button
789-
+ " on port "
790-
+ stick
791-
+ " not available, check if controller is plugged in");
782+
reportJoystickWarning(
783+
stick, "Joystick Button " + button + " on port " + stick + " not available");
792784
return false;
793785
}
794786

@@ -824,12 +816,8 @@ public static boolean getStickButtonReleased(final int stick, final int button)
824816
m_cacheDataMutex.unlock();
825817
}
826818

827-
reportJoystickUnpluggedWarning(
828-
"Joystick Button "
829-
+ button
830-
+ " on port "
831-
+ stick
832-
+ " not available, check if controller is plugged in");
819+
reportJoystickWarning(
820+
stick, "Joystick Button " + button + " on port " + stick + " not available");
833821
return false;
834822
}
835823

@@ -860,12 +848,7 @@ public static double getStickAxis(int stick, int axis) {
860848
m_cacheDataMutex.unlock();
861849
}
862850

863-
reportJoystickUnpluggedWarning(
864-
"Joystick axis "
865-
+ axis
866-
+ " on port "
867-
+ stick
868-
+ " not available, check if controller is plugged in");
851+
reportJoystickWarning(stick, "Joystick axis " + axis + " on port " + stick + " not available");
869852
return 0.0;
870853
}
871854

@@ -903,14 +886,15 @@ public static TouchpadFinger getStickTouchpadFinger(int stick, int touchpad, int
903886
m_cacheDataMutex.unlock();
904887
}
905888

906-
reportJoystickUnpluggedWarning(
889+
reportJoystickWarning(
890+
stick,
907891
"Joystick touchpad finger "
908892
+ finger
909893
+ " on touchpad "
910894
+ touchpad
911895
+ " on port "
912896
+ stick
913-
+ " not available, check if controller is plugged in");
897+
+ " not available");
914898
return new TouchpadFinger(false, 0.0f, 0.0f);
915899
}
916900

@@ -1006,12 +990,7 @@ public static POVDirection getStickPOV(int stick, int pov) {
1006990
m_cacheDataMutex.unlock();
1007991
}
1008992

1009-
reportJoystickUnpluggedWarning(
1010-
"Joystick POV "
1011-
+ pov
1012-
+ " on port "
1013-
+ stick
1014-
+ " not available, check if controller is plugged in");
993+
reportJoystickWarning(stick, "Joystick POV " + pov + " on port " + stick + " not available");
1015994
return POVDirection.Center;
1016995
}
1017996

@@ -1906,14 +1885,23 @@ public static void removeRefreshedDataEventHandle(int handle) {
19061885
}
19071886

19081887
/**
1909-
* Reports errors related to unplugged joysticks Throttles the errors so that they don't overwhelm
1910-
* the DS.
1888+
* Reports errors related to joystick availability. Throttles the errors so that they don't
1889+
* overwhelm the DS.
1890+
*
1891+
* @param stick The joystick port.
1892+
* @param message The message to report if the joystick is connected.
19111893
*/
1912-
private static void reportJoystickUnpluggedWarning(String message) {
1894+
private static void reportJoystickWarning(int stick, String message) {
19131895
if (isFMSAttached() || !m_silenceJoystickWarning) {
19141896
double currentTime = Timer.getTimestamp();
19151897
if (currentTime > m_nextMessageTime) {
1916-
reportWarning(message, false);
1898+
if (isJoystickConnected(stick)) {
1899+
reportWarning(message, false);
1900+
} else {
1901+
reportWarning(
1902+
"Joystick on port " + stick + " not available, check if controller is plugged in",
1903+
false);
1904+
}
19171905
m_nextMessageTime = currentTime + JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL;
19181906
}
19191907
}

0 commit comments

Comments
 (0)