FastLED 3.9.15
Loading...
Searching...
No Matches
task.cpp
Go to the documentation of this file.
1#include "fl/task.h"
2#include "fl/async.h"
3#include "fl/time.h"
4#include "fl/sstream.h"
5
6namespace fl {
7
8namespace {
9// Helper to generate a trace label from a TracePoint
11 // Basic implementation: "file:line"
12 // More advanced version could strip common path prefixes
13 fl::sstream ss;
14 ss << fl::get<0>(trace) << ":" << fl::get<1>(trace);
15 return ss.str();
16}
17} // namespace
18
19// TaskImpl implementation
21 : mType(type),
23 mCanceled(false),
25 mHasThen(false),
26 mHasCatch(false),
27 mLastRunTime(UINT32_MAX) { // Use UINT32_MAX to indicate "never run"
28}
29
31 : mType(type),
33 mCanceled(false),
34 mTraceLabel(fl::make_unique<string>(make_trace_label(trace))), // Optional. Put it in the heap.
35 mHasThen(false),
36 mHasCatch(false),
37 mLastRunTime(UINT32_MAX) { // Use UINT32_MAX to indicate "never run"
38}
39
40// TaskImpl static builders
44
48
52
56
60
64
68
69
73
74// TaskImpl fluent API
75void TaskImpl::set_then(fl::function<void()> on_then) {
76 mThenCallback = fl::move(on_then);
77 mHasThen = true;
78}
79
80void TaskImpl::set_catch(fl::function<void(const Error&)> on_catch) {
81 mCatchCallback = fl::move(on_catch);
82 mHasCatch = true;
83}
84
86 mCanceled = true;
87}
88
90 // Auto-registration is now handled at the task wrapper level
91 // Mark as registered to prevent duplicate registrations
92 mAutoRegistered = true;
93}
94
95bool TaskImpl::ready_to_run(uint32_t current_time) const {
96 // For frame-based tasks, they're only ready when explicitly called in frame context
97 // Regular scheduler updates should NOT run frame tasks
99 return false; // Changed: frame tasks are not ready during regular updates
100 }
101
102 // For time-based tasks, check if enough time has passed
103 if (mIntervalMs <= 0) {
104 return true;
105 }
106
107 // Use UINT32_MAX to indicate "never run" instead of 0 to handle cases where time() returns 0
108 if (mLastRunTime == UINT32_MAX) {
109 return true;
110 }
111
112 // Check if enough time has passed since last run
113 return (current_time - mLastRunTime) >= static_cast<uint32_t>(mIntervalMs);
114}
115
116// New method to check if frame tasks are ready (used only during frame events)
117bool TaskImpl::ready_to_run_frame_task(uint32_t current_time) const {
118 // Only frame-based tasks are ready in frame context
119 FL_UNUSED(current_time);
121 return true;
122 }
123 // Non-frame tasks are not run in frame context
124 return false;
125}
126
128 if (mHasThen && mThenCallback) {
130 }
131}
132
133void TaskImpl::execute_catch(const Error& error) {
134 if (mHasCatch && mCatchCallback) {
135 mCatchCallback(error);
136 }
137}
138
139// task wrapper implementation
141
142// task static builders
146
150
154
155task task::at_framerate(int fps, const fl::TracePoint& trace) {
156 return task(TaskImpl::create_at_framerate(fps, trace));
157}
158
162
166
170
174
176 task out = task::after_frame();
177 out.then(on_then);
178 return out;
179}
180
181task task::after_frame(function<void()> on_then, const fl::TracePoint& trace) {
182 task out = task::after_frame(trace);
183 out.then(on_then);
184 return out;
185}
186
187// task fluent API
188task& task::then(fl::function<void()> on_then) {
189 if (mImpl) {
190 mImpl->set_then(fl::move(on_then));
191
192 // Auto-register with scheduler when callback is set
193 if (!mImpl->is_auto_registered()) {
194 mImpl->auto_register_with_scheduler();
196 }
197 }
198 return *this;
199}
200
201task& task::catch_(fl::function<void(const Error&)> on_catch) {
202 if (mImpl) {
203 mImpl->set_catch(fl::move(on_catch));
204 }
205 return *this;
206}
207
209 if (mImpl) {
210 mImpl->set_canceled();
211 }
212 return *this;
213}
214
215// task getters
216int task::id() const {
217 return mImpl ? mImpl->id() : 0;
218}
219
220bool task::has_then() const {
221 return mImpl ? mImpl->has_then() : false;
222}
223
224bool task::has_catch() const {
225 return mImpl ? mImpl->has_catch() : false;
226}
227
228string task::trace_label() const {
229 return mImpl ? mImpl->trace_label() : "";
230}
231
233 return mImpl ? mImpl->type() : TaskType::kEveryMs;
234}
235
236int task::interval_ms() const {
237 return mImpl ? mImpl->interval_ms() : 0;
238}
239
240uint32_t task::last_run_time() const {
241 return mImpl ? mImpl->last_run_time() : 0;
242}
243
245 if (mImpl) {
246 mImpl->set_last_run_time(time);
247 }
248}
249
250bool task::ready_to_run(uint32_t current_time) const {
251 return mImpl ? mImpl->ready_to_run(current_time) : false;
252}
253
254} // namespace fl
static Scheduler & instance()
Definition async.cpp:91
int add_task(task t)
Definition async.cpp:95
const string & str() const
Definition strstream.h:51
bool mHasThen
Definition task.h:180
bool mAutoRegistered
Definition task.h:178
bool ready_to_run_frame_task(uint32_t current_time) const
Definition task.cpp:117
function< void()> mThenCallback
Definition task.h:184
TaskType type() const
Definition task.h:154
unique_ptr< string > mTraceLabel
Definition task.h:179
void execute_catch(const Error &error)
Definition task.cpp:133
bool ready_to_run(uint32_t current_time) const
Definition task.cpp:95
bool mCanceled
Definition task.h:177
uint32_t mLastRunTime
Definition task.h:182
void execute_then()
Definition task.cpp:127
static shared_ptr< TaskImpl > create_at_framerate(int fps)
Definition task.cpp:49
void set_canceled()
Definition task.cpp:85
function< void(const Error &)> mCatchCallback
Definition task.h:185
static shared_ptr< TaskImpl > create_after_frame()
Definition task.cpp:65
int mIntervalMs
Definition task.h:176
bool mHasCatch
Definition task.h:181
static shared_ptr< TaskImpl > create_every_ms(int interval_ms)
Definition task.cpp:41
TaskImpl(TaskType type, int interval_ms)
Definition task.cpp:20
int interval_ms() const
Definition task.h:155
void auto_register_with_scheduler()
Definition task.cpp:89
TaskType mType
Definition task.h:175
void set_catch(function< void(const Error &)> on_catch)
Definition task.cpp:80
void set_then(function< void()> on_then)
Definition task.cpp:75
static shared_ptr< TaskImpl > create_before_frame()
Definition task.cpp:57
task & cancel()
Definition task.cpp:208
static task after_frame()
Definition task.cpp:167
int interval_ms() const
Definition task.cpp:236
shared_ptr< TaskImpl > mImpl
Definition task.h:113
TaskType type() const
Definition task.cpp:232
uint32_t last_run_time() const
Definition task.cpp:240
bool has_then() const
Definition task.cpp:220
int id() const
Definition task.cpp:216
bool ready_to_run(uint32_t current_time) const
Definition task.cpp:250
bool has_catch() const
Definition task.cpp:224
void set_last_run_time(uint32_t time)
Definition task.cpp:244
static task before_frame()
Definition task.cpp:159
task()=default
task & then(function< void()> on_then)
Definition task.cpp:188
task & catch_(function< void(const Error &)> on_catch)
Definition task.cpp:201
string trace_label() const
Definition task.cpp:228
static task at_framerate(int fps)
Definition task.cpp:151
static task every_ms(int interval_ms)
Definition task.cpp:143
Universal timing functions for FastLED.
fl::string make_trace_label(const fl::TracePoint &trace)
Definition task.cpp:10
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args)
Definition memory.h:42
fl::u32 time()
Universal millisecond timer - returns milliseconds since system startup.
Definition time.cpp:136
TaskType
Definition task.h:43
@ kBeforeFrame
Definition task.h:46
@ kAfterFrame
Definition task.h:47
@ kAtFramerate
Definition task.h:45
@ kEveryMs
Definition task.h:44
StrStream sstream
Definition sstream.h:6
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
fl::tuple< const char *, int, uint32_t > TracePoint
A structure to hold source trace information.
Definition trace.h:22
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) noexcept
Definition pair.h:113
IMPORTANT!
Definition crgb.h:20
Generic asynchronous task management for FastLED.
Error type for promises.
Definition promise.h:53
#define FL_UNUSED(x)
Definition unused.h:8