FastLED 3.9.15
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1#pragma once
2
33
34#include "fl/functional.h"
35#include "fl/string.h"
36#include "fl/trace.h"
37#include "fl/promise.h"
38#include "fl/time.h"
39#include "fl/ptr.h"
40
41namespace fl {
42
49
50// Forward declaration
51class TaskImpl;
52
53class task {
54public:
55
56
57 // Default constructor
58 task() = default;
59
60 // Copy and Move semantics (now possible with shared_ptr)
61 task(const task&) = default;
62 task& operator=(const task&) = default;
63 task(task&&) = default;
64 task& operator=(task&&) = default;
65
66 // Static builders
67 static task every_ms(int interval_ms);
68 static task every_ms(int interval_ms, const fl::TracePoint& trace);
69
70 static task at_framerate(int fps);
71 static task at_framerate(int fps, const fl::TracePoint& trace);
72
73 // For most cases you want after_frame() instead of before_frame(), unless you
74 // are doing operations that need to happen right before the frame is rendered.
75 // Most of the time for ui stuff (button clicks, etc) you want after_frame(), so it
76 // can be available for the next iteration of loop().
77 static task before_frame();
78 static task before_frame(const fl::TracePoint& trace);
79
80 // Example: auto task = fl::task::after_frame().then([]() {...}
81 static task after_frame();
82 static task after_frame(const fl::TracePoint& trace);
83 // Example: auto task = fl::task::after_frame([]() {...}
84 static task after_frame(function<void()> on_then);
85 static task after_frame(function<void()> on_then, const fl::TracePoint& trace);
86
87 // Fluent API
88 task& then(function<void()> on_then);
89 task& catch_(function<void(const Error&)> on_catch);
90 task& cancel();
91
92 // Getters
93 int id() const;
94 bool has_then() const;
95 bool has_catch() const;
96 string trace_label() const;
97 TaskType type() const;
98 int interval_ms() const;
99 uint32_t last_run_time() const;
100 void set_last_run_time(uint32_t time);
101 bool ready_to_run(uint32_t current_time) const;
102 bool is_valid() const { return mImpl != nullptr; }
103
104private:
105 friend class Scheduler;
106
107 // Private constructor for internal use
108 explicit task(shared_ptr<TaskImpl> impl);
109
110 // Access to implementation for Scheduler
112
114};
115
116// Private implementation class
117class TaskImpl {
118private:
119 // Constructors
121 TaskImpl(TaskType type, int interval_ms, const fl::TracePoint& trace);
122
123 // Friend declaration to allow make_shared to access private constructors
124 template<typename T, typename... Args>
125 friend shared_ptr<T> make_shared(Args&&... args);
126
127public:
128 // Non-copyable but moveable
129 TaskImpl(const TaskImpl&) = delete;
130 TaskImpl& operator=(const TaskImpl&) = delete;
131 TaskImpl(TaskImpl&&) = default;
133
134 // Static builders for internal use
138 static shared_ptr<TaskImpl> create_at_framerate(int fps, const fl::TracePoint& trace);
143
144 // Fluent API
145 void set_then(function<void()> on_then);
146 void set_catch(function<void(const Error&)> on_catch);
147 void set_canceled();
148
149 // Getters
150 int id() const { return mTaskId; }
151 bool has_then() const { return mHasThen; }
152 bool has_catch() const { return mHasCatch; }
153 string trace_label() const { return mTraceLabel ? *mTraceLabel : ""; }
154 TaskType type() const { return mType; }
155 int interval_ms() const { return mIntervalMs; }
156 uint32_t last_run_time() const { return mLastRunTime; }
158 bool ready_to_run(uint32_t current_time) const;
159 bool ready_to_run_frame_task(uint32_t current_time) const; // New method for frame tasks
160 bool is_canceled() const { return mCanceled; }
161 bool is_auto_registered() const { return mAutoRegistered; }
162
163 // Execution
164 void execute_then();
165 void execute_catch(const Error& error);
166
167private:
168 friend class Scheduler;
169 friend class task;
170
171 // Auto-registration with scheduler
173
174 int mTaskId = 0;
177 bool mCanceled = false;
178 bool mAutoRegistered = false; // Track if task auto-registered with scheduler
179 unique_ptr<string> mTraceLabel; // Optional trace label (default big so we put it in the heap)
180 bool mHasThen = false;
181 bool mHasCatch = false;
182 uint32_t mLastRunTime = 0; // Last time the task was run
183
186};
187
188} // namespace fl
int id() const
Definition task.h:150
friend class task
Definition task.h:169
bool mHasThen
Definition task.h:180
bool has_catch() const
Definition task.h:152
TaskImpl & operator=(TaskImpl &&)=default
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
TaskImpl(const TaskImpl &)=delete
bool ready_to_run(uint32_t current_time) const
Definition task.cpp:95
TaskImpl(TaskImpl &&)=default
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
TaskImpl & operator=(const TaskImpl &)=delete
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
friend shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
int mTaskId
Definition task.h:174
string trace_label() const
Definition task.h:153
int mIntervalMs
Definition task.h:176
bool mHasCatch
Definition task.h:181
bool is_canceled() const
Definition task.h:160
static shared_ptr< TaskImpl > create_every_ms(int interval_ms)
Definition task.cpp:41
TaskImpl(TaskType type, int interval_ms)
Definition task.cpp:20
uint32_t last_run_time() const
Definition task.h:156
bool has_then() const
Definition task.h:151
int interval_ms() const
Definition task.h:155
void auto_register_with_scheduler()
Definition task.cpp:89
TaskType mType
Definition task.h:175
bool is_auto_registered() const
Definition task.h:161
void set_catch(function< void(const Error &)> on_catch)
Definition task.cpp:80
void set_last_run_time(uint32_t time)
Definition task.h:157
void set_then(function< void()> on_then)
Definition task.cpp:75
static shared_ptr< TaskImpl > create_before_frame()
Definition task.cpp:57
friend class Scheduler
Definition task.h:168
task & cancel()
Definition task.cpp:208
bool is_valid() const
Definition task.h:102
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
shared_ptr< TaskImpl > get_impl() const
Definition task.h:111
task(task &&)=default
bool has_catch() const
Definition task.cpp:224
void set_last_run_time(uint32_t time)
Definition task.cpp:244
task & operator=(const task &)=default
task & operator=(task &&)=default
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
task(const task &)=default
static task every_ms(int interval_ms)
Definition task.cpp:143
friend class Scheduler
Definition task.h:105
Universal timing functions for FastLED.
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
fl::tuple< const char *, int, uint32_t > TracePoint
A structure to hold source trace information.
Definition trace.h:22
IMPORTANT!
Definition crgb.h:20
corkscrew_args args
Definition old.h:150
Promise-based fluent API for FastLED - standalone async primitives.
Error type for promises.
Definition promise.h:53