RobotCore
1.0
WPILib port to raspberry Pi
Toggle main menu visibility
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
5
package
org.wpilib.event;
6
7
import
static
org.wpilib.units.Units.Seconds;
8
import
static
org.wpilib.util.ErrorMessages.requireNonNullParam;
9
10
import
java.util.concurrent.atomic.AtomicBoolean;
11
import
java.util.function.BiFunction;
12
import
java.util.function.BooleanSupplier;
13
import
org.wpilib.math.filter.Debouncer;
14
import
org.wpilib.units.measure.Time;
15
27
public
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) {
66
m_loop
.
bind
(
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
92
public
BooleanEvent
negate
() {
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
131
public
BooleanEvent
rising
() {
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
152
public
BooleanEvent
falling
() {
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
}
org.wpilib.event.BooleanEvent.m_loop
final EventLoop m_loop
Definition
BooleanEvent.java:29
org.wpilib.event.BooleanEvent.falling
BooleanEvent falling()
Definition
BooleanEvent.java:152
org.wpilib.event.BooleanEvent.debounce
BooleanEvent debounce(double seconds)
Definition
BooleanEvent.java:186
org.wpilib.event.BooleanEvent.or
BooleanEvent or(BooleanSupplier other)
Definition
BooleanEvent.java:121
org.wpilib.event.BooleanEvent.debounce
BooleanEvent debounce(Time period)
Definition
BooleanEvent.java:175
org.wpilib.event.BooleanEvent.ifHigh
final void ifHigh(Runnable action)
Definition
BooleanEvent.java:65
org.wpilib.event.BooleanEvent.rising
BooleanEvent rising()
Definition
BooleanEvent.java:131
org.wpilib.event.BooleanEvent.and
BooleanEvent and(BooleanSupplier other)
Definition
BooleanEvent.java:106
org.wpilib.event.BooleanEvent.BooleanEvent
BooleanEvent(EventLoop loop, BooleanSupplier signal)
Definition
BooleanEvent.java:42
org.wpilib.event.BooleanEvent.negate
BooleanEvent negate()
Definition
BooleanEvent.java:92
org.wpilib.event.BooleanEvent.debounce
BooleanEvent debounce(double seconds, Debouncer.DebounceType type)
Definition
BooleanEvent.java:210
org.wpilib.event.BooleanEvent.debounce
BooleanEvent debounce(Time period, Debouncer.DebounceType type)
Definition
BooleanEvent.java:198
org.wpilib.event.BooleanEvent.getAsBoolean
final boolean getAsBoolean()
Definition
BooleanEvent.java:56
org.wpilib.event.EventLoop
Definition
EventLoop.java:14
org.wpilib.event.EventLoop.bind
void bind(Runnable action)
Definition
EventLoop.java:31
org.wpilib.math.filter.Debouncer
Definition
Debouncer.java:13
org.wpilib.math.filter.Debouncer.calculate
boolean calculate(boolean input)
Definition
Debouncer.java:70
org.wpilib.math.filter.Debouncer.Debouncer
Debouncer(double debounceTime, DebounceType type)
Definition
Debouncer.java:37
org.wpilib.math.filter.Debouncer.DebounceType
Definition
Debouncer.java:15
org.wpilib.math.filter.Debouncer.DebounceType.RISING
RISING
Definition
Debouncer.java:17
org
wpilib
event
BooleanEvent.java
Generated by
1.18.0