Skip to content

Commit 31c3889

Browse files
committed
2 parents 06cbf68 + c4a2575 commit 31c3889

6 files changed

+97
-53
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Task Scheduler
22
### Cooperative multitasking for Arduino, ESPx, STM32 and other microcontrollers
3-
#### Version 3.2.0: 2020-08-16 [Latest updates](https://github.com/arkhipenko/TaskScheduler/wiki/Latest-Updates)
3+
#### Version 3.2.2: 2020-12-14 [Latest updates](https://github.com/arkhipenko/TaskScheduler/wiki/Latest-Updates)
44

55
[![arduino-library-badge](https://www.ardu-badge.com/badge/TaskScheduler.svg?)](https://www.ardu-badge.com/TaskScheduler)[![xscode](https://img.shields.io/badge/Available%20on-xs%3Acode-blue?style=?style=plastic&logo=appveyor&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRF////////VXz1bAAAAAJ0Uk5T/wDltzBKAAAAlUlEQVR42uzXSwqAMAwE0Mn9L+3Ggtgkk35QwcnSJo9S+yGwM9DCooCbgn4YrJ4CIPUcQF7/XSBbx2TEz4sAZ2q1RAECBAiYBlCtvwN+KiYAlG7UDGj59MViT9hOwEqAhYCtAsUZvL6I6W8c2wcbd+LIWSCHSTeSAAECngN4xxIDSK9f4B9t377Wd7H5Nt7/Xz8eAgwAvesLRjYYPuUAAAAASUVORK5CYII=)](https://xscode.com/arkhipenko/TaskScheduler)
66

keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ Task KEYWORD1
1414
# Methods and Functions (KEYWORD2)
1515
#######################################
1616

17+
abort KEYWORD2
1718
addTask KEYWORD2
1819
allowSleep KEYWORD2
1920
Callback KEYWORD2
21+
cancel KEYWORD2
22+
canceled KEYWORD2
2023
completed KEYWORD2
2124
cpuLoadReset KEYWORD2
2225
currentLts KEYWORD2

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"maintainer": true
1717
}
1818
],
19-
"version": "3.2.0",
19+
"version": "3.2.2",
2020
"frameworks": "arduino",
2121
"platforms": "*"
2222
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=TaskScheduler
2-
version=3.2.0
2+
version=3.2.2
33
author=Anatoli Arkhipenko <arkhipenko@hotmail.com>
44
maintainer=Anatoli Arkhipenko <arkhipenko@hotmail.com>
55
sentence=Cooperative multitasking for Arduino, ESPx, STM32 and other microcontrollers.

src/TaskScheduler.h

+65-27
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,16 @@
179179
//
180180
// v3.2.0:
181181
// 2020-08-16 - feature: scheduling options
182-
182+
//
183+
// v3.2.1:
184+
// 2020-10-04 - feature: Task.abort method. Stop task execution without calling OnDisable().
185+
//
186+
// v3.2.2:
187+
// 2020-12-14 - feature: enable and restart methods return true if task enabled
188+
// feature: Task.cancel() method - disable task with a cancel flag (could be used for alt. path
189+
// processing in the onDisable method.
190+
// feature: Task.cancelled() method - indicates that task was disabled with a cancel() method.
191+
//
183192

184193
#include <Arduino.h>
185194

@@ -199,20 +208,21 @@ extern "C" {
199208
// The following "defines" control library functionality at compile time,
200209
// and should be used in the main sketch depending on the functionality required
201210
//
202-
// #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
203-
// #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
204-
// #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
205-
// #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
206-
// #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
207-
// #define _TASK_PRIORITY // Support for layered scheduling priority
208-
// #define _TASK_MICRO_RES // Support for microsecond resolution
209-
// #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
210-
// #define _TASK_DEBUG // Make all methods and variables public for debug purposes
211-
// #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
212-
// #define _TASK_TIMEOUT // Support for overall task timeout
213-
// #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
214-
// #define _TASK_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
215-
// #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
211+
// #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
212+
// #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
213+
// #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
214+
// #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
215+
// #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
216+
// #define _TASK_PRIORITY // Support for layered scheduling priority
217+
// #define _TASK_MICRO_RES // Support for microsecond resolution
218+
// #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
219+
// #define _TASK_DEBUG // Make all methods and variables public for debug purposes
220+
// #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
221+
// #define _TASK_TIMEOUT // Support for overall task timeout
222+
// #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
223+
// #define _TASK_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
224+
// #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
225+
// #define _TASK_SCHEDULING_OPTIONS // Support for multiple scheduling options
216226

217227
#ifdef _TASK_MICRO_RES
218228

@@ -349,24 +359,26 @@ void StatusRequest::signalComplete(int aStatus) {
349359
* @param: aStatusRequest - a pointer for the StatusRequest to wait for.
350360
* If aStatusRequest is NULL, request for waiting is ignored, and the waiting task is not enabled.
351361
*/
352-
void Task::waitFor(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
362+
bool Task::waitFor(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
353363
iStatusRequest = aStatusRequest;
354364
if ( iStatusRequest != NULL ) { // assign internal StatusRequest var and check if it is not NULL
355365
setIterations(aIterations);
356366
setInterval(aInterval);
357367
iStatus.waiting = _TASK_SR_NODELAY; // no delay
358-
enable();
368+
return enable();
359369
}
370+
return false;
360371
}
361372

362-
void Task::waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
373+
bool Task::waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
363374
iStatusRequest = aStatusRequest;
364375
if ( iStatusRequest != NULL ) { // assign internal StatusRequest var and check if it is not NULL
365376
setIterations(aIterations);
366377
if ( aInterval ) setInterval(aInterval); // For the dealyed version only set the interval if it was not a zero
367378
iStatus.waiting = _TASK_SR_DELAY; // with delay equal to the current interval
368-
enable();
379+
return enable();
369380
}
381+
return false;
370382
}
371383
#endif // _TASK_STATUS_REQUEST
372384

@@ -400,6 +412,7 @@ void Task::setOnDisable(TaskOnDisable aCallback) { iOnDisable = aCallback; }
400412
void Task::reset() {
401413
iStatus.enabled = false;
402414
iStatus.inonenable = false;
415+
iStatus.canceled = false;
403416
iPreviousMillis = 0;
404417
iInterval = iDelay = 0;
405418
iPrev = NULL;
@@ -496,9 +509,10 @@ void Task::yieldOnce (TaskCallback aCallback) {
496509
* schedules it for execution as soon as possible,
497510
* and resets the RunCounter back to zero
498511
*/
499-
void Task::enable() {
512+
bool Task::enable() {
500513
if (iScheduler) { // activation without active scheduler does not make sense
501514
iRunCounter = 0;
515+
iStatus.canceled = false;
502516

503517
#ifdef _TASK_OO_CALLBACKS
504518
if ( !iStatus.inonenable ) {
@@ -529,12 +543,14 @@ void Task::enable() {
529543
resetTimeout();
530544
#endif // _TASK_TIMEOUT
531545

532-
#ifdef _TASK_STATUS_REQUEST
533546
if ( iStatus.enabled ) {
547+
#ifdef _TASK_STATUS_REQUEST
534548
iMyStatusRequest.setWaiting();
535-
}
536549
#endif // _TASK_STATUS_REQUEST
550+
}
551+
return iStatus.enabled;
537552
}
553+
return false;
538554
}
539555

540556
/** Enables the task only if it was not enabled already
@@ -549,9 +565,10 @@ bool Task::enableIfNot() {
549565
/** Enables the task
550566
* and schedules it for execution after a delay = aInterval
551567
*/
552-
void Task::enableDelayed(unsigned long aDelay) {
568+
bool Task::enableDelayed(unsigned long aDelay) {
553569
enable();
554570
delay(aDelay);
571+
return iStatus.enabled;
555572
}
556573

557574
#ifdef _TASK_TIMEOUT
@@ -644,21 +661,42 @@ bool Task::disable() {
644661
return (previousEnabled);
645662
}
646663

664+
/** Aborts task execution
665+
* Task will no longer be executed by the scheduler AND ondisable method will not be called
666+
*/
667+
void Task::abort() {
668+
iStatus.enabled = false;
669+
iStatus.inonenable = false;
670+
}
671+
672+
673+
/** Cancels task execution
674+
* Task will no longer be executed by the scheduler. Ondisable method will be called after 'canceled' flag is set
675+
*/
676+
void Task::cancel() {
677+
iStatus.canceled = true;
678+
disable();
679+
}
680+
681+
bool Task::canceled() {
682+
return iStatus.canceled;
683+
}
684+
647685
/** Restarts task
648686
* Task will run number of iterations again
649687
*/
650688

651-
void Task::restart() {
652-
enable();
689+
bool Task::restart() {
653690
iIterations = iSetIterations;
691+
return enable();
654692
}
655693

656694
/** Restarts task delayed
657695
* Task will run number of iterations again
658696
*/
659-
void Task::restartDelayed(unsigned long aDelay) {
660-
enableDelayed(aDelay);
697+
bool Task::restartDelayed(unsigned long aDelay) {
661698
iIterations = iSetIterations;
699+
return enableDelayed(aDelay);
662700
}
663701

664702
bool Task::isFirstIteration() { return (iRunCounter <= 1); }

src/TaskSchedulerDeclarations.h

+26-23
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
// The following "defines" control library functionality at compile time,
1212
// and should be used in the main sketch depending on the functionality required
1313
//
14-
// #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
15-
// #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
16-
// #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
17-
// #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
18-
// #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
19-
// #define _TASK_PRIORITY // Support for layered scheduling priority
20-
// #define _TASK_MICRO_RES // Support for microsecond resolution
21-
// #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
22-
// #define _TASK_DEBUG // Make all methods and variables public for debug purposes
23-
// #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
24-
// #define _TASK_TIMEOUT // Support for overall task timeout
25-
// #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
26-
// #define _TASK_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
27-
// #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
28-
// #define _TASK_SCHEDULING_OPTIONS // Support for multiple scheduling options
14+
// #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
15+
// #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
16+
// #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
17+
// #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
18+
// #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
19+
// #define _TASK_PRIORITY // Support for layered scheduling priority
20+
// #define _TASK_MICRO_RES // Support for microsecond resolution
21+
// #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
22+
// #define _TASK_DEBUG // Make all methods and variables public for debug purposes
23+
// #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
24+
// #define _TASK_TIMEOUT // Support for overall task timeout
25+
// #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
26+
// #define _TASK_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
27+
// #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
28+
// #define _TASK_SCHEDULING_OPTIONS // Support for multiple scheduling options
2929

3030
class Scheduler;
3131

@@ -119,9 +119,9 @@ typedef bool (*TaskOnEnable)();
119119
typedef struct {
120120
bool enabled : 1; // indicates that task is enabled or not.
121121
bool inonenable : 1; // indicates that task execution is inside OnEnable method (preventing infinite loops)
122-
122+
bool canceled : 1; // indication that tast has been canceled prior to normal end of all iterations or regular call to disable()
123123
#ifdef _TASK_STATUS_REQUEST
124-
uint8_t waiting : 2; // indication if task is waiting on the status request
124+
uint8_t waiting : 2; // indication if task is waiting on the status request
125125
#endif
126126

127127
#ifdef _TASK_TIMEOUT
@@ -164,16 +164,19 @@ class Task {
164164
INLINE bool timedOut();
165165
#endif
166166

167-
INLINE void enable();
167+
INLINE bool enable();
168168
INLINE bool enableIfNot();
169-
INLINE void enableDelayed(unsigned long aDelay=0);
170-
INLINE void restart();
171-
INLINE void restartDelayed(unsigned long aDelay=0);
169+
INLINE bool enableDelayed(unsigned long aDelay=0);
170+
INLINE bool restart();
171+
INLINE bool restartDelayed(unsigned long aDelay=0);
172172

173173
INLINE void delay(unsigned long aDelay=0);
174174
INLINE void forceNextIteration();
175175
INLINE bool disable();
176+
INLINE void abort();
177+
INLINE void cancel();
176178
INLINE bool isEnabled();
179+
INLINE bool canceled();
177180

178181
#ifdef _TASK_SCHEDULING_OPTIONS
179182
INLINE unsigned int getSchedulingOption() { return iOption; }
@@ -212,8 +215,8 @@ class Task {
212215
#endif // _TASK_TIMECRITICAL
213216

214217
#ifdef _TASK_STATUS_REQUEST
215-
INLINE void waitFor(StatusRequest* aStatusRequest, unsigned long aInterval = 0, long aIterations = 1);
216-
INLINE void waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval = 0, long aIterations = 1);
218+
INLINE bool waitFor(StatusRequest* aStatusRequest, unsigned long aInterval = 0, long aIterations = 1);
219+
INLINE bool waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval = 0, long aIterations = 1);
217220
INLINE StatusRequest* getStatusRequest() ;
218221
INLINE StatusRequest* getInternalStatusRequest() ;
219222
#endif // _TASK_STATUS_REQUEST

0 commit comments

Comments
 (0)