You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
2.1 KiB

  1. // Copyright 2021 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. // A token that can be used to cancel an existing deferred execution.
  7. typedef uint8_t deferred_token;
  8. #define INVALID_DEFERRED_TOKEN 0
  9. // Callback to execute.
  10. // -- Parameter trigger_time: the intended trigger time to execute the callback -- equivalent time-space as timer_read32()
  11. // cb_arg: the callback argument specified when enqueueing the deferred executor
  12. // -- Return value: Non-zero re-queues the callback to execute after the returned number of milliseconds. Zero cancels repeated execution.
  13. typedef uint32_t (*deferred_exec_callback)(uint32_t trigger_time, void *cb_arg);
  14. // Configures the supplied deferred executor to be executed after the required number of milliseconds.
  15. // -- Parameter delay_ms: the number of milliseconds before executing the callback
  16. // -- callback: the executor to invoke
  17. // -- cb_arg: the argument to pass to the executor, may be NULL if unused by the executor
  18. // -- Return value: a token usable for cancellation, or INVALID_DEFERRED_TOKEN if an error occurred
  19. deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg);
  20. // Allows for extending the timeframe before an existing deferred execution is invoked.
  21. // -- Parameter token: the returned value from defer_exec for the deferred execution you wish to extend.
  22. // -- delay_ms: the new delay (with respect to the current time)
  23. // -- Return value: if the token was found, and the delay was extended
  24. bool extend_deferred_exec(deferred_token token, uint32_t delay_ms);
  25. // Allows for cancellation of an existing deferred execution.
  26. // -- Parameter token: the returned value from defer_exec for the deferred execution you wish to cancel.
  27. // -- Return value: if the token was found, and the executor was cancelled
  28. bool cancel_deferred_exec(deferred_token token);
  29. // Forward declaration for the main loop in order to execute any deferred executors. Should not be invoked by keyboard/user code.
  30. void deferred_exec_task(void);