diff options
author | Nick Brassel <nick@tzarc.org> | 2021-11-16 05:21:09 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-15 18:21:09 +0000 |
commit | 36d123e9c5a9ce0e29b9bc22ef87661bf479e299 (patch) | |
tree | 1033718afb33e1eb7dc3cd7b99db59a90668fa8a /quantum/deferred_exec.h | |
parent | b3ee124da666287ef13787523ea88bf40713ff4c (diff) | |
download | qmk_firmware-36d123e9c5a9ce0e29b9bc22ef87661bf479e299.tar.gz qmk_firmware-36d123e9c5a9ce0e29b9bc22ef87661bf479e299.zip |
Add support for deferred executors. (#14859)
* Add support for deferred executors.
* More docs.
* Include from quantum.h
* Cleanup.
* Parameter checks
* Comments.
* qmk format-c
* I accidentally a few words.
* API name change.
* Apply suggestions from code review
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
* Review comments.
* qmk format-c
* Review comments.
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
Diffstat (limited to 'quantum/deferred_exec.h')
-rw-r--r-- | quantum/deferred_exec.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/quantum/deferred_exec.h b/quantum/deferred_exec.h new file mode 100644 index 0000000000..f80d353169 --- /dev/null +++ b/quantum/deferred_exec.h @@ -0,0 +1,38 @@ +// Copyright 2021 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <stdbool.h> +#include <stdint.h> + +// A token that can be used to cancel an existing deferred execution. +typedef uint8_t deferred_token; +#define INVALID_DEFERRED_TOKEN 0 + +// Callback to execute. +// -- Parameter trigger_time: the intended trigger time to execute the callback -- equivalent time-space as timer_read32() +// cb_arg: the callback argument specified when enqueueing the deferred executor +// -- Return value: Non-zero re-queues the callback to execute after the returned number of milliseconds. Zero cancels repeated execution. +typedef uint32_t (*deferred_exec_callback)(uint32_t trigger_time, void *cb_arg); + +// Configures the supplied deferred executor to be executed after the required number of milliseconds. +// -- Parameter delay_ms: the number of milliseconds before executing the callback +// -- callback: the executor to invoke +// -- cb_arg: the argument to pass to the executor, may be NULL if unused by the executor +// -- Return value: a token usable for cancellation, or INVALID_DEFERRED_TOKEN if an error occurred +deferred_token defer_exec(uint32_t delay_ms, deferred_exec_callback callback, void *cb_arg); + +// Allows for extending the timeframe before an existing deferred execution is invoked. +// -- Parameter token: the returned value from defer_exec for the deferred execution you wish to extend. +// -- delay_ms: the new delay (with respect to the current time) +// -- Return value: if the token was found, and the delay was extended +bool extend_deferred_exec(deferred_token token, uint32_t delay_ms); + +// Allows for cancellation of an existing deferred execution. +// -- Parameter token: the returned value from defer_exec for the deferred execution you wish to cancel. +// -- Return value: if the token was found, and the executor was cancelled +bool cancel_deferred_exec(deferred_token token); + +// Forward declaration for the main loop in order to execute any deferred executors. Should not be invoked by keyboard/user code. +void deferred_exec_task(void); |