[hal] Update runtime enum to allow selecting roborio 2 (#3565)

In some cases, knowing roborio 2 might be useful. This also creates a higher level enum that might be usable later for the discussion on more complex runtime types.
This commit is contained in:
Thad House
2021-09-13 22:05:38 -07:00
committed by GitHub
parent 95a12e0ee8
commit 66abb39880
11 changed files with 96 additions and 5 deletions

View File

@@ -160,6 +160,15 @@ public abstract class RobotBase implements AutoCloseable {
@Override
public void close() {}
/**
* Get the current runtime type.
*
* @return Current runtime type.
*/
public static RuntimeType getRuntimeType() {
return RuntimeType.getValue(HALUtil.getHALRuntimeType());
}
/**
* Get if the robot is a simulation.
*
@@ -175,7 +184,8 @@ public abstract class RobotBase implements AutoCloseable {
* @return If the robot is running in the real world.
*/
public static boolean isReal() {
return HALUtil.getHALRuntimeType() == 0;
RuntimeType runtimeType = getRuntimeType();
return runtimeType == RuntimeType.kRoboRIO || runtimeType == RuntimeType.kRoboRIO2;
}
/**

View File

@@ -0,0 +1,34 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.HALUtil;
public enum RuntimeType {
kRoboRIO(HALUtil.RUNTIME_ROBORIO),
kRoboRIO2(HALUtil.RUNTIME_ROBORIO2),
kSimulation(HALUtil.RUNTIME_SIMULATION);
public final int value;
RuntimeType(int value) {
this.value = value;
}
/**
* Construct a runtime type from an int value.
*
* @param type Runtime type as int
* @return Matching runtime type
*/
public static RuntimeType getValue(int type) {
if (type == HALUtil.RUNTIME_ROBORIO) {
return RuntimeType.kRoboRIO;
} else if (type == HALUtil.RUNTIME_ROBORIO2) {
return RuntimeType.kRoboRIO2;
}
return RuntimeType.kSimulation;
}
}