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

[commands] Add static Trigger factories for robot mode changes #5902

Merged
merged 25 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
@@ -0,0 +1,52 @@
// 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.wpilibj2.command.button;

import edu.wpi.first.wpilibj.DriverStation;

/**
* A class containing static {@link Trigger} factories for running callbacks when the robot mode
* changes.
*/
public final class GameTriggers {
// Utility class
private GameTriggers() {}

/**
* Returns a trigger that is true when the robot is enabled in autonomous mode.
*
* @return A trigger that is true when the robot is enabled in autonomous mode.
*/
public static Trigger autonomous() {
return new Trigger(DriverStation::isAutonomousEnabled);
}

/**
* Returns a trigger that is true when the robot is enabled in teleop mode.
*
* @return A trigger that is true when the robot is enabled in teleop mode.
*/
public static Trigger teleop() {
return new Trigger(DriverStation::isTeleopEnabled);
}

/**
* Returns a trigger that is true when the robot is disabled.
*
* @return A trigger that is true when the robot is disabled.
*/
public static Trigger disabled() {
return new Trigger(DriverStation::isDisabled);
}

/**
* Returns a trigger that is true when the robot is enabled in test mode.
*
* @return A trigger that is true when the robot is enabled in test mode.
*/
public static Trigger test() {
return new Trigger(DriverStation::isTestEnabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

#include "frc2/command/button/GameTriggers.h"

#include <frc/DriverStation.h>

using namespace frc2;

Trigger GameTriggers::Autonomous() {
return Trigger{&frc::DriverStation::IsAutonomousEnabled};
}

Trigger GameTriggers::Teleop() {
return Trigger{&frc::DriverStation::IsTeleopEnabled};
}

Trigger GameTriggers::Disabled() {
return Trigger{&frc::DriverStation::IsDisabled};
}

Trigger GameTriggers::Test() {
return Trigger{&frc::DriverStation::IsTestEnabled};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.

#pragma once

#include "frc2/command/button/Trigger.h"

namespace frc2 {
DeltaDizzy marked this conversation as resolved.
Show resolved Hide resolved

/**
* A class containing static Trigger factories for running callbacks when robot
* mode changes.
*/
class GameTriggers {
public:
GameTriggers() = delete;
DeltaDizzy marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a trigger that is true when the robot is enabled in autonomous
* mode.
*
* @return A trigger that is true when the robot is enabled in autonomous
* mode.
*/
static Trigger Autonomous();

/**
* Returns a trigger that is true when the robot is enabled in teleop mode.
*
* @return A trigger that is true when the robot is enabled in teleop mode.
*/
static Trigger Teleop();

/**
* Returns a trigger that is true when the robot is disabled.
*
* @return A trigger that is true when the robot is disabled.
*/
static Trigger Disabled();

/**
* Returns a trigger that is true when the robot is enabled in test mode.
*
* @return A trigger that is true when the robot is enabled in test mode.
*/
static Trigger Test();
};
DeltaDizzy marked this conversation as resolved.
Show resolved Hide resolved

} // namespace frc2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.wpilibj2.command.button;

import static org.junit.jupiter.api.Assertions.assertTrue;

import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.simulation.DriverStationSim;
import edu.wpi.first.wpilibj2.command.CommandTestBase;
import org.junit.jupiter.api.Test;

class GameTriggersTest extends CommandTestBase {
@Test
void autonomousTest() {
Trigger auto = GameTriggers.autonomous();
DriverStationSim.setAutonomous(true);
DriverStation.refreshData();
assertTrue(auto.getAsBoolean());
}
}
Loading