Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numbers in gyro widgets #754

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
@Description(
group = "edu.wpi.first.shuffleboard",
name = "Base",
version = "1.3.3",
version = "1.3.4",
summary = "Defines all the WPILib data types and stock widgets"
)
@SuppressWarnings("PMD.CouplingBetweenObjects")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;

@Description(name = "Gyro", dataTypes = GyroData.class)
@Description(name = "Gyro", dataTypes = {GyroData.class, Number.class})
@ParametrizedController("GyroWidget.fxml")
public class GyroWidget extends SimpleAnnotatedWidget<GyroData> {
public class GyroWidget extends SimpleAnnotatedWidget<Object> {

@FXML
private Pane root;
Expand All @@ -32,8 +32,34 @@ public class GyroWidget extends SimpleAnnotatedWidget<GyroData> {

@FXML
private void initialize() {
gauge.valueProperty().bind(dataOrDefault.map(GyroData::getWrappedValue));
valueLabel.textProperty().bind(dataOrDefault.map(GyroData::getValue).map(d -> String.format("%.2f°", d)));
dataProperty().addListener((__, prev, cur) -> {
if (cur != null) {
double angle = 0;
if (cur instanceof Number) {
angle = ((Number) cur).doubleValue();
} else if (cur instanceof GyroData) {
angle = ((GyroData) cur).getWrappedValue();
}
angle = wrapAngle(angle);

gauge.setValue(angle);
valueLabel.setText(String.format("%.2f", angle));
}
});
}

/**
* Helper method to keep an angle in the range [0, 360).
* eg wrapAngle(90) -> 90
* wrapAngle(360) -> 0
* wrapAngle(-15) -> 345
*/
private double wrapAngle(double angle) {
if (angle < 0) {
return ((angle % 360) + 360) % 360;
} else {
return angle % 360;
}
}

@Override
Expand Down