189189// processing in the onDisable method.
190190// feature: Task.cancelled() method - indicates that task was disabled with a cancel() method.
191191//
192+ // v3.2.3:
193+ // 2021-01-01 - feature: discontinued use of 'register' keyword. Depricated in C++ 11
194+ // feature: add STM32 as a platform supporting _TASK_STD_FUNCTION. (PR #105)
195+ //
196+ // v3.3.0:
197+ // 2021-05-11 - feature: Timeout() methods for StatusRequest objects
198+
199+
192200
193201#include < Arduino.h>
194202
@@ -244,7 +252,7 @@ extern "C" {
244252#endif // _TASK_SLEEP_ON_IDLE_RUN
245253
246254
247- #if !defined (ARDUINO_ARCH_ESP8266) && !defined (ARDUINO_ARCH_ESP32)
255+ #if !defined (ARDUINO_ARCH_ESP8266) && !defined (ARDUINO_ARCH_ESP32) && !defined (ARDUINO_ARCH_STM32)
248256#ifdef _TASK_STD_FUNCTION
249257 #error Support for std::function only for ESP8266 or ESP32 architecture
250258#undef _TASK_STD_FUNCTION
@@ -327,7 +335,14 @@ StatusRequest::StatusRequest()
327335 iStatus = 0 ;
328336}
329337
330- void StatusRequest::setWaiting (unsigned int aCount) { iCount = aCount; iStatus = 0 ; }
338+ void StatusRequest::setWaiting (unsigned int aCount) {
339+ iCount = aCount;
340+ iStatus = 0 ;
341+ #ifdef _TASK_TIMEOUT
342+ iStarttime = _TASK_TIME_FUNCTION ();
343+ #endif // #ifdef _TASK_TIMEOUT
344+ }
345+
331346bool StatusRequest::pending () { return (iCount != 0 ); }
332347bool StatusRequest::completed () { return (iCount == 0 ); }
333348int StatusRequest::getStatus () { return iStatus; }
@@ -380,6 +395,19 @@ bool Task::waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval
380395 }
381396 return false ;
382397}
398+
399+ #ifdef _TASK_TIMEOUT
400+ void StatusRequest::resetTimeout () {
401+ iStarttime = _TASK_TIME_FUNCTION ();
402+ }
403+
404+ long StatusRequest::untilTimeout () {
405+ if ( iTimeout ) {
406+ return ( (long ) (iStarttime + iTimeout) - (long ) _TASK_TIME_FUNCTION () );
407+ }
408+ return -1 ;
409+ }
410+ #endif // _TASK_TIMEOUT
383411#endif // _TASK_STATUS_REQUEST
384412
385413bool Task::isEnabled () { return iStatus.enabled ; }
@@ -667,6 +695,7 @@ bool Task::disable() {
667695void Task::abort () {
668696 iStatus.enabled = false ;
669697 iStatus.inonenable = false ;
698+ iStatus.canceled = true ;
670699}
671700
672701
@@ -978,16 +1007,16 @@ void Scheduler::setSleepMethod( SleepCallback aCallback ) {
9781007
9791008bool Scheduler::execute () {
9801009 bool idleRun = true ;
981- register unsigned long m, i; // millis, interval;
1010+ unsigned long m, i; // millis, interval;
9821011
9831012#ifdef _TASK_SLEEP_ON_IDLE_RUN
9841013 unsigned long tFinish;
9851014 unsigned long tStart = micros ();
9861015#endif // _TASK_SLEEP_ON_IDLE_RUN
9871016
9881017#ifdef _TASK_TIMECRITICAL
989- register unsigned long tPassStart;
990- register unsigned long tTaskStart, tTaskFinish;
1018+ unsigned long tPassStart;
1019+ unsigned long tTaskStart, tTaskFinish;
9911020
9921021#ifdef _TASK_SLEEP_ON_IDLE_RUN
9931022 unsigned long tIdleStart = 0 ;
@@ -1048,6 +1077,12 @@ bool Scheduler::execute() {
10481077 // Otherwise, continue with execution as usual. Tasks waiting to StatusRequest need to be rescheduled according to
10491078 // how they were placed into waiting state (waitFor or waitForDelayed)
10501079 if ( iCurrent->iStatus .waiting ) {
1080+ #ifdef _TASK_TIMEOUT
1081+ StatusRequest *sr = iCurrent->iStatusRequest ;
1082+ if ( sr->iTimeout && (m - sr->iStarttime > sr->iTimeout ) ) {
1083+ sr->signalComplete (TASK_SR_TIMEOUT);
1084+ }
1085+ #endif // _TASK_TIMEOUT
10511086 if ( (iCurrent->iStatusRequest )->pending () ) break ;
10521087 if (iCurrent->iStatus .waiting == _TASK_SR_NODELAY) {
10531088 iCurrent->iPreviousMillis = m - (iCurrent->iDelay = i);
@@ -1074,7 +1109,7 @@ bool Scheduler::execute() {
10741109 {
10751110 long ov = (long ) ( iCurrent->iPreviousMillis + i - m );
10761111 if ( ov < 0 ) {
1077- long ii = i == 0 ? 1 : i ;
1112+ long ii = i ? i : 1 ;
10781113 iCurrent->iPreviousMillis += ((m - iCurrent->iPreviousMillis ) / ii) * ii;
10791114 }
10801115 }
0 commit comments