diff --git a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/CommandBasedBunyipsOpMode.java b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/CommandBasedBunyipsOpMode.java index 98d8f7c99..c12f94a99 100644 --- a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/CommandBasedBunyipsOpMode.java +++ b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/CommandBasedBunyipsOpMode.java @@ -35,6 +35,28 @@ public abstract class CommandBasedBunyipsOpMode extends BunyipsOpMode { @NonNull private HashSet managedSubsystems = new HashSet<>(); + /** + * Unbind a task from the scheduler, based on the index of the task in the scheduler's allocated tasks. + *

+ * This can either be determined by the order in which the tasks were bound, or by the ID of the task via + * the {@code ScheduledTask.id} property, which is the same thing. + * + * @param index The index of the task to unbind. + * @throws IndexOutOfBoundsException If the index is out of bounds. + */ + public void unbind(int index) { + scheduler.unbind(index); + } + + /** + * Unbind a scheduled task from the scheduler. + * + * @param task The {@link Scheduler.ScheduledTask} to unbind. + */ + public void unbind(@NonNull Scheduler.ScheduledTask task) { + scheduler.unbind(task); + } + /** * Create a new controller trigger creator. *

diff --git a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt index e39c2c50a..4144db905 100644 --- a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt +++ b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt @@ -191,6 +191,28 @@ class Scheduler : BunyipsComponent() { } } + /** + * Unbind a task from the scheduler, based on the index of the task in the scheduler's allocated tasks. + * + * This can either be determined by the order in which the tasks were bound, or by the ID of the task via + * the [ScheduledTask.id] property, which is the same thing. + * + * @param index The index of the task to unbind. + * @throws IndexOutOfBoundsException If the index is out of bounds. + */ + fun unbind(index: Int) { + allocatedTasks.removeAt(index) + } + + /** + * Unbind a scheduled task from the scheduler. + * + * @param task The [ScheduledTask] to unbind. + */ + fun unbind(task: ScheduledTask) { + allocatedTasks.remove(task) + } + /** * Create a new controller trigger creator. * @@ -470,7 +492,18 @@ class Scheduler : BunyipsComponent() { * A task that will run when a condition is met. */ inner class ScheduledTask(private val originalRunCondition: Condition) { + /** + * The ID (allocated task index) of the task that can be used to unbind and identify the binding. + */ + @JvmField + val id: Int + + /** + * The task to run when the condition is met. + */ var taskToRun: Task = IdleTask() + private set + internal val runCondition: () -> Boolean internal var debouncing: Boolean = false internal var stopCondition: (() -> Boolean)? = null @@ -488,6 +521,7 @@ class Scheduler : BunyipsComponent() { || or.stream().anyMatch { obj -> obj.asBoolean } } allocatedTasks.add(this) + id = allocatedTasks.size - 1 } /**