unleashed-firmware/furi/core/event_loop_timer.h
Georgii Surkov 139660d206
[FL-3846] Event Loop Timers (#3721)
* Implement POC event loop tmers (not all edge cases are handled)
* Use a separate ready list to allow for (re)starting and stopping of timers from callback
* Improve the test application
* Improve timer API and test application
* Improve timeout calculation logic
* Improve timer API, update documentation
* Fix API usage error
* Update doxygen comments
* Revert the old (correct) check
* Improve function naming
* Check whether a timer was on the expired list before processing it
* Implement tick callback
* Add critical sections to improve timer consistency
* Simplify event loop timer API
* Remove redundant search
* Refactor timer logic, use message queue
* Simplify FuriEventLoopTimer API
* Improve event loop timer logic
* Update the f18 target
* Remove superfluous clears
* Correct f18 api symbols
* Fix doxygen comments
* Update .pvsconfig
* Use a double push list instead of deque
* Update .pvsconfig
* Add pending callback functionality
* Restore unprocessed flags when applicable
* Refactor Dolphin app to use FuriEventLoop
* Improve naming
* Update naming some more
* Fix a typo Co-authored-by: Silent <CookiePLMonster@users.noreply.github.com>
* Fix wait time in example
* Bump API version
* Debug: multiple of 25 timings in event loop blink test
* Separate FuriEventLoopTimer to its own set of files
* Improve start time calculation for periodic timers
* Do not use dynamic allocations for timer requests
* Split the tick functionality in separate files, rearrange code
* Improve timer queue handling
* Properly reset GPIO pins in the test app
* Properly initialise GPIO pins in the test app too
* Furi: variable naming in event loop
* Furi: fix spelling in event loop

Co-authored-by: あく <alleteam@gmail.com>
Co-authored-by: Silent <CookiePLMonster@users.noreply.github.com>
2024-07-02 13:09:50 +01:00

119 lines
3.6 KiB
C

/**
* @file event_loop_timer.h
* @brief Software timer functionality for FuriEventLoop.
*/
#pragma once
#include "event_loop.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Enumeration of possible timer types.
*/
typedef enum {
FuriEventLoopTimerTypeOnce = 0, /**< One-shot timer. */
FuriEventLoopTimerTypePeriodic = 1, /**< Repeating timer. */
} FuriEventLoopTimerType;
/**
* @brief Timer callback type for functions to be called when a timer expires.
*
* In the timer callback, it is ALLOWED:
* - To start, stop, or restart an existing timer,
* - To create new timers using furi_event_loop_timer_alloc(),
* - To delete timers using furi_event_loop_timer_free().
*
* @param[in,out] context pointer to a user-specific object that was provided during timer creation
*/
typedef void (*FuriEventLoopTimerCallback)(void* context);
/**
* @brief Opaque event loop timer type.
*/
typedef struct FuriEventLoopTimer FuriEventLoopTimer;
/**
* @brief Create a new event loop timer instance.
*
* @param[in,out] instance pointer to the current FuriEventLoop instance
* @param[in] callback pointer to the callback function to be executed upon timer timeout
* @param[in] type timer type value to determine its behavior (single-shot or periodic)
* @param[in,out] context pointer to a user-specific object (will be passed to the callback)
* @returns pointer to the created timer instance
*/
FuriEventLoopTimer* furi_event_loop_timer_alloc(
FuriEventLoop* instance,
FuriEventLoopTimerCallback callback,
FuriEventLoopTimerType type,
void* context);
/**
* @brief Delete an event loop timer instance.
*
* @warning The user code MUST call furi_event_loop_timer_free() on ALL instances
* associated with the current event loop BEFORE calling furi_event_loop_free().
* The event loop may EITHER be running OR stopped when the timers are being deleted.
*
* @param[in,out] timer pointer to the timer instance to be deleted
*/
void furi_event_loop_timer_free(FuriEventLoopTimer* timer);
/**
* @brief Start a timer or restart it with a new interval.
*
* @param[in,out] timer pointer to the timer instance to be (re)started
* @param[in] interval timer interval in ticks
*/
void furi_event_loop_timer_start(FuriEventLoopTimer* timer, uint32_t interval);
/**
* @brief Restart a timer with the previously set interval.
*
* @param[in,out] timer pointer to the timer instance to be restarted
*/
void furi_event_loop_timer_restart(FuriEventLoopTimer* timer);
/**
* @brief Stop a timer without firing its callback.
*
* It is safe to call this function on an already stopped timer (it will do nothing).
*
* @param[in,out] timer pointer to the timer instance to be stopped
*/
void furi_event_loop_timer_stop(FuriEventLoopTimer* timer);
/**
* @brief Get the time remaining before the timer becomes expires.
*
* For stopped or expired timers, this function returns 0.
*
* @param[in] timer pointer to the timer to be queried
* @returns remaining time in ticks
*/
uint32_t furi_event_loop_timer_get_remaining_time(const FuriEventLoopTimer* timer);
/**
* @brief Get the timer interval.
*
* @param[in] timer pointer to the timer to be queried
* @returns timer interval in ticks
*/
uint32_t furi_event_loop_timer_get_interval(const FuriEventLoopTimer* timer);
/**
* @brief Check if the timer is currently running.
*
* A timer is considered running if it has not expired yet.
* @param[in] timer pointer to the timer to be queried
* @returns true if the timer is running, false otherwise
*/
bool furi_event_loop_timer_is_running(const FuriEventLoopTimer* timer);
#ifdef __cplusplus
}
#endif