Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TimedRobot, add support for one-shot actions and cancelling … #7315

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Commits on Oct 30, 2024

  1. Refactor TimedRobot, add support for one-shot actions and cancelling …

    …actions, along with support for actions that cancel themselves. Changes include:
    
    Implement TimedRobot.Cancellable, a functional interface invoked to cancel a scheduled callback.
    
    Implement an enum represention of the schedule status where each enumeration contains a callback handler.
    
    Implement TimedRobot.Callback.invoke() to handle Runnable invocation on behalf of the TimedRobot, where the method returns true if and only if the callback should be invoked again. Have invoke() delegate callback invocation to the callback's status. Replace Callback.func.run() invocations with Callback.invoke(), and reschedule the callback if and only if invoke() returns true.
    
    Enhance TimedRobot.Callback to maintain its schedule status and enhance the addPeriodic() methods to set the scheduling status to PERIODIC (i.e. indefinite periodic invocation) and to return a Cancellable (the Callback that holds the user-provided Runnable) so that applications can cancel invocation at will. Base the enhanced class on Cancellable and implement cancel(). The returned Cancellable is actually the Callable that holds the user's Runnable.
    
    Add TimedRobot.addOneShot(), which schedules a Runnable to be invoked exactly once after a set delay. Like its addPeriodic() counterparts, addOneShot() returns a Cancellable. The enhanced TimedRobot currently provides one addOneShot version that takes a timeout in seconds. Please let me know if users will need to specify delays as a Time.
    
    Simplify the loop in TimedRobot.startCompetition() and enhance it to reschedule a callback when and only when its callback() invocation returns true.
    
    Note that the status enumerations, PERIODIC, ONE_SHOT, and CANCELLED each provide an invoke() method that accepts a Runnable and "does the right thing." The enumerations have the following characteristics
    
    | Name      | Action                | Runs Callback | Returns |
    +-----------+---------------------- +---------------+---------+
    | PERIODIC  | Invoke indefinitately | Yes           | true    |
    +-----------+---------------------- +---------------+---------+
    | ONE_SHOT  | Invoke exactly once   | Yes           | false   |
    +-----------+---------------------- +---------------+---------+
    | CANCELLED | Cancel invocation     | No            | false   |
    +-----------+---------------------- +---------------+---------+
    
    For example, ONE_SHOT.invoke(Runnable callback) invokes callback.run(), but returns false so it will not be rescheduled. The other implications work similarly.
    emintz committed Oct 30, 2024
    Configuration menu
    Copy the full SHA
    318dc1f View commit details
    Browse the repository at this point in the history

Commits on Oct 31, 2024

  1. Refactor TimedRobot, add support for one-shot actions and cancelling …

    …actions, along with support for actions that cancel themselves. Changes include:
    
    Implement TimedRobot.Cancellable, a functional interface invoked to cancel a scheduled callback.
    
    Implement an enum represention of the schedule status where each enumeration contains a callback handler.
    
    Implement TimedRobot.Callback.invoke() to handle Runnable invocation on behalf of the TimedRobot, where the method returns true if and only if the callback should be invoked again. Have invoke() delegate callback invocation to the callback's status. Replace Callback.func.run() invocations with Callback.invoke(), and reschedule the callback if and only if invoke() returns true.
    
    Enhance TimedRobot.Callback to maintain its schedule status and enhance the addPeriodic() methods to set the scheduling status to PERIODIC (i.e. indefinite periodic invocation) and to return a Cancellable (the Callback that holds the user-provided Runnable) so that applications can cancel invocation at will. Base the enhanced class on Cancellable and implement cancel(). The returned Cancellable is actually the Callable that holds the user's Runnable.
    
    Add TimedRobot.addOneShot(), which schedules a Runnable to be invoked exactly once after a set delay. Like its addPeriodic() counterparts, addOneShot() returns a Cancellable. The enhanced TimedRobot currently provides one addOneShot version that takes a timeout in seconds. Please let me know if users will need to specify delays as a Time.
    
    Simplify the loop in TimedRobot.startCompetition() and enhance it to reschedule a callback when and only when its callback() invocation returns true.
    
    Note that the status enumerations, PERIODIC, ONE_SHOT, and CANCELLED each provide an invoke() method that accepts a Runnable and "does the right thing." The enumerations have the following characteristics
    
    | Name      | Action                | Runs Callback | Returns |
    +-----------+---------------------- +---------------+---------+
    | PERIODIC  | Invoke indefinitately | Yes           | true    |
    +-----------+---------------------- +---------------+---------+
    | ONE_SHOT  | Invoke exactly once   | Yes           | false   |
    +-----------+---------------------- +---------------+---------+
    | CANCELLED | Cancel invocation     | No            | false   |
    +-----------+---------------------- +---------------+---------+
    
    For example, ONE_SHOT.invoke(Runnable callback) invokes callback.run(), but returns false so it will not be rescheduled. The other implications work similarly.
    
    Also fix lint errors.
    emintz committed Oct 31, 2024
    Configuration menu
    Copy the full SHA
    3c8db23 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    a4207d8 View commit details
    Browse the repository at this point in the history