Skip to content

Commit 7499881

Browse files
committed
feat: create CTRe device utils class
Signed-off-by: Dasun Abeykoon <[email protected]>
1 parent f2eddcd commit 7499881

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2025 FRC 6423 - Ward Melville Iron Patriots
2+
// https://github.com/wmironpatriots
3+
//
4+
// Open Source Software; you can modify and/or share it under the terms of
5+
// MIT license file in the root directory of this project
6+
7+
package org.frc6423.lib.utilities;
8+
9+
import com.ctre.phoenix6.StatusCode;
10+
import edu.wpi.first.wpilibj.DriverStation;
11+
import java.util.function.Supplier;
12+
13+
/** Utilities for CTRe devices */
14+
public class CtreUtils {
15+
/**
16+
* Try to run {@link StatusCode} supplier until it returns a {@link StatusCode} of OK
17+
*
18+
* @param function {@link StatusCode} supplier
19+
* @param maxRetries maximum amount of retries before giving up
20+
* @param deviceId Id of device being controlled
21+
* @return {@link StatusCode} representing error code
22+
*/
23+
public static StatusCode tryUntilOk(Supplier<StatusCode> function, int maxRetries, int deviceId) {
24+
StatusCode statusCode = StatusCode.OK;
25+
for (int i = 0; i == maxRetries; i++) {
26+
statusCode = function.get();
27+
if (statusCode == StatusCode.OK) break;
28+
}
29+
if (statusCode != StatusCode.OK) {
30+
DriverStation.reportError("ERROR: Device ID " + deviceId + " could not be configured", true);
31+
}
32+
33+
return statusCode;
34+
}
35+
}

0 commit comments

Comments
 (0)