[commands] Revert to original Trigger implementation (#4673)

Trigger was refactored to use BooleanEvent when it was introduced in #4104.
This reverts to the original implementation until edge-based BooleanEvents can be fixed.
This commit is contained in:
Starlight220
2022-11-28 23:48:48 +02:00
committed by GitHub
parent 15561338d5
commit cb38bacfe8
14 changed files with 555 additions and 243 deletions

View File

@@ -266,10 +266,9 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Use Rising() as a command end condition with Until() instead.
* @deprecated Pass this as a command end condition with Until() instead.
*/
WPI_DEPRECATED(
"Use Rising() as a command end condition with Until() instead.")
WPI_DEPRECATED("Pass this as a command end condition with Until() instead.")
Button CancelWhenPressed(Command* command);
};
} // namespace frc2

View File

@@ -42,7 +42,7 @@ class Trigger {
* @param condition the condition represented by this trigger
*/
explicit Trigger(std::function<bool()> condition)
: m_event{CommandScheduler::GetInstance().GetDefaultButtonLoop(),
: Trigger{CommandScheduler::GetInstance().GetDefaultButtonLoop(),
std::move(condition)} {}
/**
@@ -52,7 +52,7 @@ class Trigger {
* @param condition the condition represented by this trigger
*/
Trigger(frc::EventLoop* loop, std::function<bool()> condition)
: m_event{loop, std::move(condition)} {}
: m_loop{loop}, m_condition{std::move(condition)} {}
/**
* Create a new trigger that is always `false`.
@@ -70,7 +70,6 @@ class Trigger {
*
* @param command the command to start
* @return this trigger, so calls can be chained
* @see #Rising()
*/
Trigger OnTrue(Command* command);
@@ -92,7 +91,6 @@ class Trigger {
*
* @param command the command to start
* @return this trigger, so calls can be chained
* @see #Falling()
*/
Trigger OnFalse(Command* command);
@@ -233,10 +231,17 @@ class Trigger {
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use OnTrue(Command) instead")
Trigger WhenActive(T&& command) {
m_event.Rising().IfHigh(
[command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))] { command->Schedule(); });
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
if (!previous && current) {
command->Schedule();
}
previous = current;
});
return *this;
}
@@ -298,10 +303,19 @@ class Trigger {
"Use WhileTrue(Command) with RepeatCommand, or bind command::Schedule "
"with IfHigh(std::function<void()>).")
Trigger WhileActiveContinous(T&& command) {
std::shared_ptr<T> ptr =
std::make_shared<std::remove_reference_t<T>>(std::forward<T>(command));
m_event.IfHigh([ptr] { ptr->Schedule(); });
m_event.Falling().IfHigh([ptr] { ptr->Cancel(); });
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
if (current) {
command->Schedule();
} else if (previous && !current) {
command->Cancel();
}
previous = current;
});
return *this;
}
@@ -354,12 +368,19 @@ class Trigger {
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use WhileTrue(Command) instead.")
Trigger WhileActiveOnce(T&& command) {
std::shared_ptr<T> ptr =
std::make_shared<std::remove_reference_t<T>>(std::forward<T>(command));
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
m_event.Rising().IfHigh([ptr] { ptr->Schedule(); });
m_event.Falling().IfHigh([ptr] { ptr->Cancel(); });
if (!previous && current) {
command->Schedule();
} else if (previous && !current) {
command->Cancel();
}
previous = current;
});
return *this;
}
@@ -389,10 +410,17 @@ class Trigger {
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use OnFalse(Command) instead.")
Trigger WhenInactive(T&& command) {
m_event.Falling().IfHigh(
[command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))] { command->Schedule(); });
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
if (previous && !current) {
command->Schedule();
}
previous = current;
});
return *this;
}
@@ -448,15 +476,21 @@ class Trigger {
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use ToggleOnTrue(Command) instead.")
Trigger ToggleWhenActive(T&& command) {
m_event.Rising().IfHigh(
[command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))] {
if (!command->IsScheduled()) {
command->Schedule();
} else {
command->Cancel();
}
});
m_loop->Bind([condition = m_condition, previous = m_condition(),
command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))]() mutable {
bool current = condition();
if (!previous && current) {
if (command->IsScheduled()) {
command->Cancel();
} else {
command->Schedule();
}
}
previous = current;
});
return *this;
}
@@ -468,33 +502,20 @@ class Trigger {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use Rising() as a command end condition with Until() instead.
* @deprecated Pass this as a command end condition with Until() instead.
*/
WPI_DEPRECATED(
"Use Rising() as a command end condition with Until() instead.")
WPI_DEPRECATED("Pass this as a command end condition with Until() instead.")
Trigger CancelWhenActive(Command* command);
/**
* Get a new event that events only when this one newly changes to true.
*
* @return a new event representing when this one newly changes to true.
*/
Trigger Rising() { return m_event.Rising().CastTo<Trigger>(); }
/**
* Get a new event that triggers only when this one newly changes to false.
*
* @return a new event representing when this one newly changes to false.
*/
Trigger Falling() { return m_event.Falling().CastTo<Trigger>(); }
/**
* Composes two triggers with logical AND.
*
* @return A trigger which is active when both component triggers are active.
*/
Trigger operator&&(std::function<bool()> rhs) {
return m_event.operator&&(rhs).CastTo<Trigger>();
return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
return condition() && rhs();
});
}
/**
@@ -502,8 +523,10 @@ class Trigger {
*
* @return A trigger which is active when both component triggers are active.
*/
Trigger operator&&(Trigger& rhs) {
return (m_event && rhs.m_event).CastTo<Trigger>();
Trigger operator&&(Trigger rhs) {
return Trigger(m_loop, [condition = m_condition, rhs] {
return condition() && rhs.m_condition();
});
}
/**
@@ -512,7 +535,9 @@ class Trigger {
* @return A trigger which is active when either component trigger is active.
*/
Trigger operator||(std::function<bool()> rhs) {
return m_event.operator||(rhs).CastTo<Trigger>();
return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
return condition() || rhs();
});
}
/**
@@ -520,8 +545,10 @@ class Trigger {
*
* @return A trigger which is active when either component trigger is active.
*/
Trigger operator||(Trigger& rhs) {
return (m_event || rhs.m_event).CastTo<Trigger>();
Trigger operator||(Trigger rhs) {
return Trigger(m_loop, [condition = m_condition, rhs] {
return condition() || rhs.m_condition();
});
}
/**
@@ -530,7 +557,9 @@ class Trigger {
* @return A trigger which is active when the component trigger is inactive,
* and vice-versa.
*/
Trigger operator!() { return m_event.operator!().CastTo<Trigger>(); }
Trigger operator!() {
return Trigger(m_loop, [condition = m_condition] { return !condition(); });
}
/**
* Creates a new debounced trigger from this trigger - it will become active
@@ -542,16 +571,10 @@ class Trigger {
*/
Trigger Debounce(units::second_t debounceTime,
frc::Debouncer::DebounceType type =
frc::Debouncer::DebounceType::kRising) {
return m_event.Debounce(debounceTime, type).CastTo<Trigger>();
}
/**
* Get the wrapped BooleanEvent instance.
*/
frc::BooleanEvent GetEvent() const;
frc::Debouncer::DebounceType::kRising);
private:
frc::BooleanEvent m_event;
frc::EventLoop* m_loop;
std::function<bool()> m_condition;
};
} // namespace frc2