1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-07 06:01:35 +03:00

remove scheduled functions complexity overhead, change recurrent api (#6214)

* remove scheduled functions complexity overhead, change recurrent functions api
This commit is contained in:
david gauchard
2019-06-25 12:53:47 +02:00
committed by GitHub
parent f5a882d03d
commit 05be1a09e6
3 changed files with 131 additions and 113 deletions

View File

@ -3,56 +3,47 @@
#include <functional>
// This API is stabilizing
// Function signatures may change, internal queue will remain FIFO.
//
// * Add the given lambda to a fifo list of lambdas, which is run when
// - `loop` function returns,
// - or `yield` is called,
// - or `run_scheduled_functions` is called.
//
// * Use lambdas to pass arguments to a function, or call a class/static
// member function.
//
// * Please ensure variables or instances used from inside lambda will exist
// when lambda is later called
//
// * There is no mechanism for cancelling scheduled functions.
//
// * `yield` can be called from inside lambdas
//
// * Returns false if the number of scheduled functions exceeds
// SCHEDULED_FN_MAX_COUNT.
#define SCHEDULED_FN_MAX_COUNT 32
enum schedule_e
{
SCHEDULED_FUNCTION_ONCE_PER_LOOP,
SCHEDULED_FUNCTION_WITHOUT_YIELDELAYCALLS
};
// scheduled functions called once:
//
// * internal queue is FIFO.
// * Add the given lambda to a fifo list of lambdas, which is run when
// `loop` function returns.
// * Use lambdas to pass arguments to a function, or call a class/static
// member function.
// * Please ensure variables or instances used from inside lambda will exist
// when lambda is later called.
// * There is no mechanism for cancelling scheduled functions.
// * `yield` can be called from inside lambdas.
// * Returns false if the number of scheduled functions exceeds
// SCHEDULED_FN_MAX_COUNT.
// * Run the lambda only once next time.
// * Run the lambda only once next time
bool schedule_function(std::function<void(void)>&& fn,
schedule_e policy = SCHEDULED_FUNCTION_ONCE_PER_LOOP);
bool schedule_function(const std::function<void(void)>& fn,
schedule_e policy = SCHEDULED_FUNCTION_ONCE_PER_LOOP);
bool schedule_function (const std::function<void(void)>& fn);
// Run all scheduled functions.
// Use this function if your are not using `loop`, or `loop` does not return
// on a regular basis.
void run_scheduled_functions();
// recurrent scheduled function:
//
// * internal queue if not FIFO.
// * Run the lambda periodically about every <repeat_us> microseconds until
// it returns false.
// * Note that it may be more than <repeat_us> microseconds between calls if
// `yield` is not called frequently, and therefore should not be used for
// timing critical operations.
bool schedule_function_us(std::function<bool(void)>&& fn,
uint32_t repeat_us,
schedule_e policy = SCHEDULED_FUNCTION_ONCE_PER_LOOP);
bool schedule_function_us(const std::function<bool(void)>& fn,
uint32_t repeat_us,
schedule_e policy = SCHEDULED_FUNCTION_ONCE_PER_LOOP);
// * There is no mechanism for cancelling recurrent scheduled functions.
// * long running operations or yield() or delay() are not wise in the lambda.
// Run all scheduled functions.
// Use this function if your are not using `loop`, or `loop` does not return
// on a regular basis.
void run_scheduled_functions(schedule_e policy = SCHEDULED_FUNCTION_ONCE_PER_LOOP);
bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32_t repeat_us);
#endif //ESP_SCHEDULE_H
// Test recurrence and run recurrent scheduled functions.
// (internally called at every `yield()` and `loop()`)
void run_scheduled_recurrent_functions ();
#endif // ESP_SCHEDULE_H