RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
BindingScope.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.command3;
6
7import org.wpilib.hardware.hal.RobotMode;
8
13@SuppressWarnings("PMD.ImplicitFunctionalInterface")
14interface BindingScope {
20 boolean active();
21
30 Command currentCommand = scheduler.currentCommand();
31 RobotStateFetcher fetcher = RobotStateFetcher.getFetcher();
32 long currentOpMode = fetcher.getOpModeId();
33 RobotMode currentRobotMode = fetcher.getRobotMode();
34
35 if (currentCommand != null) {
36 // Commands are the narrowest scope, so prioritize them first.
37 return new ForCommand(scheduler, currentCommand);
38 } else if (currentOpMode != 0) {
39 // Opmodes are more specific than general robot mode bindings.
40 return new ForOpmode(currentOpMode);
41 } else {
42 // Not in a command and not in an opmode. Use a robot mode scope, if applicable,
43 // or fall back to the global scope if the robot is disabled or in an unrecognized mode.
44 // The switch statement deliberately does not have a default case. We want the compiler to
45 // tell us to update the switch statement if the enum changes.
46 return switch (currentRobotMode) {
47 case AUTONOMOUS -> AutonomousMode.INSTANCE;
48 case TELEOPERATED -> TeleopMode.INSTANCE;
49 case UTILITY -> UtilityMode.INSTANCE;
50 case UNKNOWN -> Global.INSTANCE;
51 };
52 }
53 }
54
56 final class Global implements BindingScope {
57 // No reason not to be a singleton.
58 public static final Global INSTANCE = new Global();
59
60 @Override
61 public boolean active() {
62 return true;
63 }
64 }
65
73 record ForCommand(Scheduler scheduler, Command command) implements BindingScope {
74 @Override
75 public boolean active() {
76 return scheduler.isRunning(command);
77 }
78 }
79
85 record ForOpmode(long opmodeId) implements BindingScope {
86 @Override
87 public boolean active() {
88 return RobotStateFetcher.getFetcher().getOpModeId() == opmodeId;
89 }
90 }
91
92 interface RobotModeScope extends BindingScope {}
93
98 final class AutonomousMode implements RobotModeScope {
99 public static final AutonomousMode INSTANCE = new AutonomousMode();
100
101 @Override
102 public boolean active() {
103 return RobotStateFetcher.getFetcher().getRobotMode() == RobotMode.AUTONOMOUS;
104 }
105 }
106
111 final class TeleopMode implements RobotModeScope {
112 public static final TeleopMode INSTANCE = new TeleopMode();
113
114 @Override
115 public boolean active() {
116 return RobotStateFetcher.getFetcher().getRobotMode() == RobotMode.TELEOPERATED;
117 }
118 }
119
124 final class UtilityMode implements RobotModeScope {
125 public static final UtilityMode INSTANCE = new UtilityMode();
126
127 @Override
128 public boolean active() {
129 return RobotStateFetcher.getFetcher().getRobotMode() == RobotMode.UTILITY;
130 }
131 }
132
133 // There is no scope for the "disabled" mode, since it would interfere with the global scope
134}
record ForOpmode(long opmodeId) implements BindingScope
static BindingScope createNarrowestScope(Scheduler scheduler)
record ForCommand(Scheduler scheduler, Command command) implements BindingScope