RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
BooleanEvent.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.event;
6
7import static org.wpilib.units.Units.Seconds;
8import static org.wpilib.util.ErrorMessages.requireNonNullParam;
9
10import java.util.concurrent.atomic.AtomicBoolean;
11import java.util.function.BiFunction;
12import java.util.function.BooleanSupplier;
13import org.wpilib.math.filter.Debouncer;
14import org.wpilib.units.measure.Time;
15
27public class BooleanEvent implements BooleanSupplier {
29 protected final EventLoop m_loop;
30
31 private final BooleanSupplier m_signal;
32
34 private final AtomicBoolean m_state = new AtomicBoolean(false);
35
42 public BooleanEvent(EventLoop loop, BooleanSupplier signal) {
43 m_loop = requireNonNullParam(loop, "loop", "BooleanEvent");
44 m_signal = requireNonNullParam(signal, "signal", "BooleanEvent");
45 m_state.set(m_signal.getAsBoolean());
46 m_loop.bind(() -> m_state.set(m_signal.getAsBoolean()));
47 }
48
55 @Override
56 public final boolean getAsBoolean() {
57 return m_state.get();
58 }
59
65 public final void ifHigh(Runnable action) {
67 () -> {
68 if (m_state.get()) {
69 action.run();
70 }
71 });
72 }
73
83 public <T extends BooleanSupplier> T castTo(BiFunction<EventLoop, BooleanSupplier, T> ctor) {
84 return ctor.apply(m_loop, m_state::get);
85 }
86
93 return new BooleanEvent(m_loop, () -> !m_state.get());
94 }
95
106 public BooleanEvent and(BooleanSupplier other) {
107 requireNonNullParam(other, "other", "and");
108 return new BooleanEvent(m_loop, () -> m_state.get() && other.getAsBoolean());
109 }
110
121 public BooleanEvent or(BooleanSupplier other) {
122 requireNonNullParam(other, "other", "or");
123 return new BooleanEvent(m_loop, () -> m_state.get() || other.getAsBoolean());
124 }
125
132 return new BooleanEvent(
133 m_loop,
134 new BooleanSupplier() {
135 private boolean m_previous = m_state.get();
136
137 @Override
138 public boolean getAsBoolean() {
139 boolean present = m_state.get();
140 boolean ret = !m_previous && present;
141 m_previous = present;
142 return ret;
143 }
144 });
145 }
146
153 return new BooleanEvent(
154 m_loop,
155 new BooleanSupplier() {
156 private boolean m_previous = m_state.get();
157
158 @Override
159 public boolean getAsBoolean() {
160 boolean present = m_state.get();
161 boolean ret = m_previous && !present;
162 m_previous = present;
163 return ret;
164 }
165 });
166 }
167
175 public BooleanEvent debounce(Time period) {
176 return debounce(period.in(Seconds));
177 }
178
186 public BooleanEvent debounce(double seconds) {
187 return debounce(seconds, Debouncer.DebounceType.RISING);
188 }
189
198 public BooleanEvent debounce(Time period, Debouncer.DebounceType type) {
199 return debounce(period.in(Seconds), type);
200 }
201
210 public BooleanEvent debounce(double seconds, Debouncer.DebounceType type) {
211 return new BooleanEvent(
212 m_loop,
213 new BooleanSupplier() {
214 private final Debouncer m_debouncer = new Debouncer(seconds, type);
215
216 @Override
217 public boolean getAsBoolean() {
218 return m_debouncer.calculate(m_state.get());
219 }
220 });
221 }
222}
BooleanEvent debounce(double seconds)
BooleanEvent or(BooleanSupplier other)
BooleanEvent debounce(Time period)
final void ifHigh(Runnable action)
BooleanEvent and(BooleanSupplier other)
BooleanEvent(EventLoop loop, BooleanSupplier signal)
BooleanEvent debounce(double seconds, Debouncer.DebounceType type)
BooleanEvent debounce(Time period, Debouncer.DebounceType type)
void bind(Runnable action)
boolean calculate(boolean input)
Debouncer(double debounceTime, DebounceType type)