|
|
@@ -182,7 +182,13 @@
|
|
|
//
|
|
|
// v3.2.1:
|
|
|
// 2020-10-04 - feature: Task.abort method. Stop task execution without calling OnDisable().
|
|
|
-
|
|
|
+//
|
|
|
+// v3.2.2:
|
|
|
+// 2020-12-14 - feature: enable and restart methods return true if task enabled
|
|
|
+// feature: Task.cancel() method - disable task with a cancel flag (could be used for alt. path
|
|
|
+// processing in the onDisable method.
|
|
|
+// feature: Task.cancelled() method - indicates that task was disabled with a cancel() method.
|
|
|
+//
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
@@ -352,24 +358,26 @@ void StatusRequest::signalComplete(int aStatus) {
|
|
|
* @param: aStatusRequest - a pointer for the StatusRequest to wait for.
|
|
|
* If aStatusRequest is NULL, request for waiting is ignored, and the waiting task is not enabled.
|
|
|
*/
|
|
|
-void Task::waitFor(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
|
|
|
+bool Task::waitFor(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
|
|
|
iStatusRequest = aStatusRequest;
|
|
|
if ( iStatusRequest != NULL ) { // assign internal StatusRequest var and check if it is not NULL
|
|
|
setIterations(aIterations);
|
|
|
setInterval(aInterval);
|
|
|
iStatus.waiting = _TASK_SR_NODELAY; // no delay
|
|
|
- enable();
|
|
|
+ return enable();
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
-void Task::waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
|
|
|
+bool Task::waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval, long aIterations) {
|
|
|
iStatusRequest = aStatusRequest;
|
|
|
if ( iStatusRequest != NULL ) { // assign internal StatusRequest var and check if it is not NULL
|
|
|
setIterations(aIterations);
|
|
|
if ( aInterval ) setInterval(aInterval); // For the dealyed version only set the interval if it was not a zero
|
|
|
iStatus.waiting = _TASK_SR_DELAY; // with delay equal to the current interval
|
|
|
- enable();
|
|
|
+ return enable();
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
#endif // _TASK_STATUS_REQUEST
|
|
|
|
|
|
@@ -403,6 +411,7 @@ void Task::setOnDisable(TaskOnDisable aCallback) { iOnDisable = aCallback; }
|
|
|
void Task::reset() {
|
|
|
iStatus.enabled = false;
|
|
|
iStatus.inonenable = false;
|
|
|
+ iStatus.canceled = false;
|
|
|
iPreviousMillis = 0;
|
|
|
iInterval = iDelay = 0;
|
|
|
iPrev = NULL;
|
|
|
@@ -499,7 +508,7 @@ void Task::yieldOnce (TaskCallback aCallback) {
|
|
|
* schedules it for execution as soon as possible,
|
|
|
* and resets the RunCounter back to zero
|
|
|
*/
|
|
|
-void Task::enable() {
|
|
|
+bool Task::enable() {
|
|
|
if (iScheduler) { // activation without active scheduler does not make sense
|
|
|
iRunCounter = 0;
|
|
|
|
|
|
@@ -532,12 +541,15 @@ void Task::enable() {
|
|
|
resetTimeout();
|
|
|
#endif // _TASK_TIMEOUT
|
|
|
|
|
|
-#ifdef _TASK_STATUS_REQUEST
|
|
|
if ( iStatus.enabled ) {
|
|
|
+#ifdef _TASK_STATUS_REQUEST
|
|
|
iMyStatusRequest.setWaiting();
|
|
|
- }
|
|
|
#endif // _TASK_STATUS_REQUEST
|
|
|
+ iStatus.canceled = false;
|
|
|
+ }
|
|
|
+ return iStatus.enabled;
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/** Enables the task only if it was not enabled already
|
|
|
@@ -552,9 +564,10 @@ bool Task::enableIfNot() {
|
|
|
/** Enables the task
|
|
|
* and schedules it for execution after a delay = aInterval
|
|
|
*/
|
|
|
-void Task::enableDelayed(unsigned long aDelay) {
|
|
|
+bool Task::enableDelayed(unsigned long aDelay) {
|
|
|
enable();
|
|
|
delay(aDelay);
|
|
|
+ return iStatus.enabled;
|
|
|
}
|
|
|
|
|
|
#ifdef _TASK_TIMEOUT
|
|
|
@@ -650,27 +663,39 @@ bool Task::disable() {
|
|
|
/** Aborts task execution
|
|
|
* Task will no longer be executed by the scheduler AND ondisable method will not be called
|
|
|
*/
|
|
|
-
|
|
|
void Task::abort() {
|
|
|
iStatus.enabled = false;
|
|
|
iStatus.inonenable = false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+/** Cancels task execution
|
|
|
+ * Task will no longer be executed by the scheduler. Ondisable method will be called after 'canceled' flag is set
|
|
|
+ */
|
|
|
+void Task::cancel() {
|
|
|
+ iStatus.canceled = true;
|
|
|
+ disable();
|
|
|
+}
|
|
|
+
|
|
|
+bool Task::canceled() {
|
|
|
+ return iStatus.canceled;
|
|
|
+}
|
|
|
+
|
|
|
/** Restarts task
|
|
|
* Task will run number of iterations again
|
|
|
*/
|
|
|
|
|
|
-void Task::restart() {
|
|
|
+bool Task::restart() {
|
|
|
iIterations = iSetIterations;
|
|
|
- enable();
|
|
|
+ return enable();
|
|
|
}
|
|
|
|
|
|
/** Restarts task delayed
|
|
|
* Task will run number of iterations again
|
|
|
*/
|
|
|
-void Task::restartDelayed(unsigned long aDelay) {
|
|
|
- enableDelayed(aDelay);
|
|
|
+bool Task::restartDelayed(unsigned long aDelay) {
|
|
|
iIterations = iSetIterations;
|
|
|
+ return enableDelayed(aDelay);
|
|
|
}
|
|
|
|
|
|
bool Task::isFirstIteration() { return (iRunCounter <= 1); }
|