2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#include "frc2/command/button/Trigger.h"
|
|
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
#include "frc2/command/InstantCommand.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
using namespace frc2;
|
|
|
|
|
|
2020-12-28 00:37:33 -08:00
|
|
|
Trigger::Trigger(const Trigger& other) = default;
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
Trigger Trigger::WhenActive(Command* command, bool interruptible) {
|
|
|
|
|
CommandScheduler::GetInstance().AddButton(
|
2020-01-12 17:57:28 -05:00
|
|
|
[pressedLast = m_isActive(), *this, command, interruptible]() mutable {
|
|
|
|
|
bool pressed = m_isActive();
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (!pressedLast && pressed) {
|
|
|
|
|
command->Schedule(interruptible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressedLast = pressed;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 21:30:30 -05:00
|
|
|
Trigger Trigger::WhenActive(std::function<void()> toRun,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements) {
|
2021-06-06 19:51:14 -07:00
|
|
|
return WhenActive(std::move(toRun),
|
|
|
|
|
{requirements.begin(), requirements.end()});
|
2020-01-01 20:09:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::WhenActive(std::function<void()> toRun,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<Subsystem* const> requirements) {
|
2019-11-08 21:30:30 -05:00
|
|
|
return WhenActive(InstantCommand(std::move(toRun), requirements));
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::WhileActiveContinous(Command* command, bool interruptible) {
|
|
|
|
|
CommandScheduler::GetInstance().AddButton(
|
2020-01-12 17:57:28 -05:00
|
|
|
[pressedLast = m_isActive(), *this, command, interruptible]() mutable {
|
|
|
|
|
bool pressed = m_isActive();
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (pressed) {
|
|
|
|
|
command->Schedule(interruptible);
|
|
|
|
|
} else if (pressedLast && !pressed) {
|
|
|
|
|
command->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressedLast = pressed;
|
|
|
|
|
});
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
Trigger Trigger::WhileActiveContinous(
|
|
|
|
|
std::function<void()> toRun,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements) {
|
2021-06-06 19:51:14 -07:00
|
|
|
return WhileActiveContinous(std::move(toRun),
|
|
|
|
|
{requirements.begin(), requirements.end()});
|
2020-01-01 20:09:17 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 19:51:14 -07:00
|
|
|
Trigger Trigger::WhileActiveContinous(
|
|
|
|
|
std::function<void()> toRun, wpi::span<Subsystem* const> requirements) {
|
2019-11-08 21:30:30 -05:00
|
|
|
return WhileActiveContinous(InstantCommand(std::move(toRun), requirements));
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::WhileActiveOnce(Command* command, bool interruptible) {
|
|
|
|
|
CommandScheduler::GetInstance().AddButton(
|
2020-01-12 17:57:28 -05:00
|
|
|
[pressedLast = m_isActive(), *this, command, interruptible]() mutable {
|
|
|
|
|
bool pressed = m_isActive();
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (!pressedLast && pressed) {
|
|
|
|
|
command->Schedule(interruptible);
|
|
|
|
|
} else if (pressedLast && !pressed) {
|
|
|
|
|
command->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressedLast = pressed;
|
|
|
|
|
});
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::WhenInactive(Command* command, bool interruptible) {
|
|
|
|
|
CommandScheduler::GetInstance().AddButton(
|
2020-01-12 17:57:28 -05:00
|
|
|
[pressedLast = m_isActive(), *this, command, interruptible]() mutable {
|
|
|
|
|
bool pressed = m_isActive();
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (pressedLast && !pressed) {
|
|
|
|
|
command->Schedule(interruptible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressedLast = pressed;
|
|
|
|
|
});
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 21:30:30 -05:00
|
|
|
Trigger Trigger::WhenInactive(std::function<void()> toRun,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements) {
|
2021-06-06 19:51:14 -07:00
|
|
|
return WhenInactive(std::move(toRun),
|
|
|
|
|
{requirements.begin(), requirements.end()});
|
2020-01-01 20:09:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::WhenInactive(std::function<void()> toRun,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<Subsystem* const> requirements) {
|
2019-11-08 21:30:30 -05:00
|
|
|
return WhenInactive(InstantCommand(std::move(toRun), requirements));
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::ToggleWhenActive(Command* command, bool interruptible) {
|
|
|
|
|
CommandScheduler::GetInstance().AddButton(
|
2020-01-12 17:57:28 -05:00
|
|
|
[pressedLast = m_isActive(), *this, command, interruptible]() mutable {
|
|
|
|
|
bool pressed = m_isActive();
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (!pressedLast && pressed) {
|
|
|
|
|
if (command->IsScheduled()) {
|
|
|
|
|
command->Cancel();
|
|
|
|
|
} else {
|
|
|
|
|
command->Schedule(interruptible);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressedLast = pressed;
|
|
|
|
|
});
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trigger Trigger::CancelWhenActive(Command* command) {
|
|
|
|
|
CommandScheduler::GetInstance().AddButton(
|
2020-01-12 17:57:28 -05:00
|
|
|
[pressedLast = m_isActive(), *this, command]() mutable {
|
|
|
|
|
bool pressed = m_isActive();
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (!pressedLast && pressed) {
|
|
|
|
|
command->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressedLast = pressed;
|
|
|
|
|
});
|
|
|
|
|
return *this;
|
|
|
|
|
}
|