unleashed-firmware/furi/core/event_loop_tick.c
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

70 lines
1.9 KiB
C

#include "event_loop_i.h"
#include <FreeRTOS.h>
#include <task.h>
#include <furi.h>
/**
* Private functions
*/
static inline uint32_t furi_event_loop_tick_get_elapsed_time(const FuriEventLoop* instance) {
return xTaskGetTickCount() - instance->tick.prev_time;
}
static inline uint32_t furi_event_loop_tick_get_remaining_time(const FuriEventLoop* instance) {
const uint32_t elapsed_time = furi_event_loop_tick_get_elapsed_time(instance);
return elapsed_time < instance->tick.interval ? instance->tick.interval - elapsed_time : 0;
}
static inline bool furi_event_loop_tick_is_expired(const FuriEventLoop* instance) {
return furi_event_loop_tick_get_elapsed_time(instance) >= instance->tick.interval;
}
/*
* Private tick API
*/
void furi_event_loop_init_tick(FuriEventLoop* instance) {
if(instance->tick.callback) {
instance->tick.prev_time = xTaskGetTickCount();
}
}
void furi_event_loop_process_tick(FuriEventLoop* instance) {
if(instance->tick.callback && furi_event_loop_tick_is_expired(instance)) {
instance->tick.prev_time += instance->tick.interval;
instance->tick.callback(instance->tick.callback_context);
}
}
uint32_t furi_event_loop_get_tick_wait_time(const FuriEventLoop* instance) {
uint32_t wait_time = FuriWaitForever;
if(instance->tick.callback) {
wait_time = furi_event_loop_tick_get_remaining_time(instance);
}
return wait_time;
}
/*
* Public tick API
*/
void furi_event_loop_tick_set(
FuriEventLoop* instance,
uint32_t interval,
FuriEventLoopTickCallback callback,
void* context) {
furi_check(instance);
furi_check(instance->thread_id == furi_thread_get_current_id());
furi_check(callback ? interval > 0 : true);
instance->tick.callback = callback;
instance->tick.callback_context = context;
instance->tick.interval = interval;
instance->tick.prev_time = xTaskGetTickCount();
}