FastLED 3.9.15
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1#pragma once
2
102
103// allow-include-after-namespace
104
105#include "fl/stl/functional.h" // IWYU pragma: keep
106#include "fl/stl/string.h"
107#include "fl/system/trace.h"
108#include "fl/task/promise.h" // IWYU pragma: keep
109#include "fl/stl/shared_ptr.h"
110#include "fl/stl/cstddef.h"
111#include "fl/stl/noexcept.h"
112
113namespace fl {
114namespace task {
115
123
124// Forward declarations
125class ITaskImpl;
126class Coroutine; // IWYU pragma: keep
127
130 optional<TracePoint> trace; // where this object was created, optional.
131 function<void()> func;
132 string name = "task";
133 size_t stack_size = 4096;
136};
137
139class Handle {
140public:
141
142 // Default constructor
143 Handle() FL_NOEXCEPT = default;
144
145 // Copy and Move semantics (now possible with shared_ptr)
146 Handle(const Handle&) FL_NOEXCEPT = default;
147 Handle& operator=(const Handle&) FL_NOEXCEPT = default;
149 Handle& operator=(Handle&&) FL_NOEXCEPT = default;
150
151 // Constructor from impl - public because ITaskImpl is only forward-declared
152 // in the header, making this effectively private to external consumers
153 explicit Handle(shared_ptr<ITaskImpl> impl) FL_NOEXCEPT;
154
155 // Fluent API
156 Handle& then(function<void()> on_then) FL_NOEXCEPT;
157 Handle& catch_(function<void(const Error&)> on_catch) FL_NOEXCEPT;
159
160 // Getters
161 int id() const FL_NOEXCEPT;
162 bool has_then() const FL_NOEXCEPT;
163 bool has_catch() const FL_NOEXCEPT;
164 string trace_label() const FL_NOEXCEPT;
165 TaskType type() const FL_NOEXCEPT;
166 int interval_ms() const FL_NOEXCEPT;
168 u32 last_run_time() const FL_NOEXCEPT;
170 bool ready_to_run(u32 current_time) const FL_NOEXCEPT;
171 bool is_valid() const FL_NOEXCEPT;
172 bool isCoroutine() const FL_NOEXCEPT;
173
174 // Coroutine control (only valid if isCoroutine() == true)
175 void stop() FL_NOEXCEPT;
176 bool isRunning() const FL_NOEXCEPT;
177
178private:
179 friend class Scheduler;
180
181 // Internal methods for Scheduler (friend access only)
182 void _set_id(int id) FL_NOEXCEPT;
183 int _id() const FL_NOEXCEPT;
184 bool _is_canceled() const FL_NOEXCEPT;
185 bool _ready_to_run(u32 current_time) const FL_NOEXCEPT;
186 bool _ready_to_run_frame_task(u32 current_time) const FL_NOEXCEPT;
188 bool _has_then() const FL_NOEXCEPT;
190 void _execute_catch(const Error& error) FL_NOEXCEPT;
191 TaskType _type() const FL_NOEXCEPT;
192 string _trace_label() const FL_NOEXCEPT;
193
195};
196
197// Free function builders (were static methods on class fl::task)
200
202Handle at_framerate(int fps, const TracePoint& trace) FL_NOEXCEPT;
203
204// For most cases you want after_frame() instead of before_frame(), unless you
205// are doing operations that need to happen right before the frame is rendered.
206// Most of the time for ui stuff (button clicks, etc) you want after_frame(), so it
207// can be available for the next iteration of loop().
210
211// Example: auto t = fl::task::after_frame().then([]() {...}
214// Example: auto t = fl::task::after_frame([]() {...}
215Handle after_frame(function<void()> on_then) FL_NOEXCEPT;
216Handle after_frame(function<void()> on_then, const TracePoint& trace) FL_NOEXCEPT;
218
219// Static coroutine control
221
224class Coroutine; // IWYU pragma: keep
225
226} // namespace task
227} // namespace fl
Handle & then(function< void()> on_then) FL_NOEXCEPT
Definition task.cpp.hpp:276
Handle() FL_NOEXCEPT=default
string trace_label() const FL_NOEXCEPT
Definition task.cpp.hpp:305
bool _is_canceled() const FL_NOEXCEPT
Definition task.cpp.hpp:374
bool isRunning() const FL_NOEXCEPT
Definition task.cpp.hpp:317
bool _ready_to_run(u32 current_time) const FL_NOEXCEPT
bool ready_to_run(u32 current_time) const FL_NOEXCEPT
Handle & catch_(function< void(const Error &)> on_catch) FL_NOEXCEPT
Definition task.cpp.hpp:287
void _set_last_run_time(u32 time) FL_NOEXCEPT
void _execute_then() FL_NOEXCEPT
Definition task.cpp.hpp:379
bool is_valid() const FL_NOEXCEPT
Definition task.cpp.hpp:312
u32 last_run_time() const FL_NOEXCEPT
Definition task.cpp.hpp:309
bool _ready_to_run_frame_task(u32 current_time) const FL_NOEXCEPT
void stop() FL_NOEXCEPT
Definition task.cpp.hpp:316
int _id() const FL_NOEXCEPT
Definition task.cpp.hpp:373
TaskType type() const FL_NOEXCEPT
Definition task.cpp.hpp:306
bool has_then() const FL_NOEXCEPT
Definition task.cpp.hpp:303
bool has_catch() const FL_NOEXCEPT
Definition task.cpp.hpp:304
bool isCoroutine() const FL_NOEXCEPT
Definition task.cpp.hpp:313
void _execute_catch(const Error &error) FL_NOEXCEPT
Definition task.cpp.hpp:380
bool _has_then() const FL_NOEXCEPT
Definition task.cpp.hpp:378
int interval_ms() const FL_NOEXCEPT
Definition task.cpp.hpp:307
string _trace_label() const FL_NOEXCEPT
Definition task.cpp.hpp:382
Handle & cancel() FL_NOEXCEPT
Definition task.cpp.hpp:294
void _set_id(int id) FL_NOEXCEPT
Definition task.cpp.hpp:372
void set_interval_ms(int interval_ms) FL_NOEXCEPT
Definition task.cpp.hpp:308
void set_last_run_time(u32 time) FL_NOEXCEPT
shared_ptr< ITaskImpl > mImpl
Definition task.h:194
TaskType _type() const FL_NOEXCEPT
Definition task.cpp.hpp:381
friend class Scheduler
Definition task.h:179
Handle every_ms(int interval_ms)
Definition task.cpp.hpp:320
Handle at_framerate(int fps)
Definition task.cpp.hpp:328
Handle before_frame()
Definition task.cpp.hpp:336
Handle coroutine(const CoroutineConfig &config)
Definition task.cpp.hpp:364
Handle after_frame()
Definition task.cpp.hpp:344
void exit_current()
Definition task.cpp.hpp:369
TaskType
Definition task.h:116
function< void()> func
Definition task.h:131
optional< TracePoint > trace
Definition task.h:130
optional< int > core_id
Pin task to specific CPU core (ESP32 and other dual cores in the future)
Definition task.h:135
Configuration for OS-level coroutine tasks.
Definition task.h:129
unsigned char u8
Definition stdint.h:131
fl::u64 time() FL_NOEXCEPT
Alias for millis64() - returns 64-bit millisecond time.
Definition chrono.h:346
Optional< T > optional
Definition optional.h:16
fl::tuple< const char *, int, fl::u32 > TracePoint
A structure to hold source trace information.
Definition trace.h:51
Base definition for an LED controller.
Definition crgb.hpp:179
Promise-based fluent API for FastLED - standalone async primitives.
#define FL_NOEXCEPT
Error type for promises.
Definition promise.h:39