@@ -66,6 +66,78 @@ class InterruptAbstraction {
6666
6767class TaskExecutionRecorder ;
6868
69+ /* *
70+ * A scheduling object that makes it easier to represent schedules in taskManager, they can be applied in a neater
71+ * written form that makes for quicker code understanding. The are used along with the helper function to provide a
72+ * single schedule method. For example: `taskManager.schedule(onceMicros(100), [] { workToDo() });`.
73+ */
74+ class TimePeriod {
75+ private:
76+ uint32_t amount: 24 ;
77+ uint32_t unit: 7 ;
78+ uint32_t repeating: 1 ;
79+ public:
80+ TimePeriod ();
81+ TimePeriod (uint32_t amount, TimerUnit unit, bool repeat);
82+
83+ TimePeriod (const TimePeriod& other) =default ;
84+ TimePeriod& operator = (const TimePeriod& other) =default ;
85+
86+ uint32_t getAmount () const {
87+ return amount;
88+ }
89+
90+ TimerUnit getUnit () const {
91+ return (TimerUnit)unit;
92+ }
93+
94+ bool getRepeating () const {
95+ return repeating != 0 ;
96+ }
97+ };
98+
99+ /* *
100+ * Create a repeating time period for scheduling in seconds
101+ * @param seconds the number of seconds to schedule in
102+ * @return the time period for scheduling.
103+ */
104+ inline TimePeriod repeatSeconds (uint32_t seconds) { return {seconds, TIME_SECONDS, true }; }
105+
106+ /* *
107+ * Create a repeating time period for scheduling in millis
108+ * @param millis the number of milliseconds to schedule in
109+ * @return the time period for scheduling.
110+ */
111+ inline TimePeriod repeatMillis (uint32_t millis) { return {millis, TIME_MILLIS, true }; }
112+
113+ /* *
114+ * Create a repeating time period for scheduling in microseconds
115+ * @param micros the number of microseconds to schedule in
116+ * @return the time period for scheduling.
117+ */
118+ inline TimePeriod repeatMicros (uint32_t micros) { return {micros, TIME_MICROS, true }; }
119+
120+ /* *
121+ * Create a time period for scheduling once in seconds
122+ * @param seconds the number of seconds to schedule in
123+ * @return the time period for scheduling.
124+ */
125+ inline TimePeriod onceSeconds (uint32_t seconds) { return {seconds, TIME_SECONDS, false }; }
126+
127+ /* *
128+ * Create a time period for scheduling once in milliseconds
129+ * @param millis the number of milliseconds to schedule in
130+ * @return the time period for scheduling.
131+ */
132+ inline TimePeriod onceMillis (uint32_t millis) { return {millis, TIME_MILLIS, false }; }
133+
134+ /* *
135+ * Create a time period for scheduling once in microseconds
136+ * @param millis the number of microseconds to schedule in
137+ * @return the time period for scheduling.
138+ */
139+ inline TimePeriod onceMicros (uint32_t micros) { return {micros, TIME_MICROS, false }; }
140+
69141/* *
70142 * TaskManager is a lightweight cooperative co-routine implementation for Arduino, it works by scheduling tasks to be
71143 * done either immediately, or at a future point in time. It is quite efficient at scheduling tasks as internally tasks
@@ -134,6 +206,27 @@ class TaskManager {
134206 return scheduleOnce (2 , execToDo, TIME_MICROS, deleteWhenDone);
135207 }
136208
209+ /* *
210+ * Schedule using a time period, normally using the helper functions to quickly create the period, underneath this
211+ * calls one of the existing schedule... methods. This schedules a no parameter function to be called. On larger
212+ * boards with lambda support enabled, it actually schedules a std::function.
213+ * @param when the time period providing the schedule details
214+ * @param timerFunction the function to call
215+ * @return the task ID that can be queried and cancelled.
216+ */
217+ taskid_t schedule (const TimePeriod& when, TimerFn timerFunction);
218+
219+ /* *
220+ * Schedule using a time period, normally using the helper functions to quickly create the period, underneath this
221+ * calls one of the existing schedule... methods. This schedules an executable to be called back.
222+ * @param when the time period providing the schedule details
223+ * @param execRef the function to call
224+ * @param deleteWhenDone if true, once the task ends (cancelled or finished in once mode) it is deleted.
225+ * @return the task ID that can be queried and cancelled.
226+ */
227+ taskid_t schedule (const TimePeriod& when, Executable* execRef, bool deleteWhenDone = false );
228+
229+
137230 /* *
138231 * Schedules a task for one shot execution in the timeframe provided.
139232 * @param millis the time frame in which to schedule the task
0 commit comments