Explorar el Código

v.3.3.0 - Timeout functionality for StatusRequest objects

Anatoli Arkhipenko hace 4 años
padre
commit
5cd3227470
Se han modificado 6 ficheros con 58 adiciones y 7 borrados
  1. 1 1
      README.md
  2. 3 0
      keywords.txt
  3. 1 1
      library.json
  4. 1 1
      library.properties
  5. 32 1
      src/TaskScheduler.h
  6. 20 3
      src/TaskSchedulerDeclarations.h

+ 1 - 1
README.md

@@ -1,6 +1,6 @@
 # Task Scheduler
 ### Cooperative multitasking for Arduino, ESPx, STM32 and other microcontrollers
-#### Version 3.2.3: 2021-01-01 [Latest updates](https://github.com/arkhipenko/TaskScheduler/wiki/Latest-Updates)
+#### Version 3.3.0: 2021-05-12 [Latest updates](https://github.com/arkhipenko/TaskScheduler/wiki/Latest-Updates)
 
 [![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)
 

+ 3 - 0
keywords.txt

@@ -126,6 +126,9 @@ TaskOnEnable	LITERAL1
 TASK_SCHEDULE	LITERAL1
 TASK_SCHEDULE_NC	LITERAL1
 TASK_INTERVAL	LITERAL1
+TASK_SR_OK	LITERAL1
+TASK_SR_ERROR	LITERAL1
+TASK_SR_TIMEOUT	LITERAL1
 
 #######################################
 

+ 1 - 1
library.json

@@ -16,7 +16,7 @@
       "maintainer": true
     }
   ],
-  "version": "3.2.2",
+  "version": "3.3.0",
   "frameworks": "arduino",
   "platforms": "*"
 }

+ 1 - 1
library.properties

@@ -1,5 +1,5 @@
 name=TaskScheduler
-version=3.2.2
+version=3.3.0
 author=Anatoli Arkhipenko <arkhipenko@hotmail.com>
 maintainer=Anatoli Arkhipenko <arkhipenko@hotmail.com>
 sentence=Cooperative multitasking for Arduino, ESPx, STM32 and other microcontrollers.

+ 32 - 1
src/TaskScheduler.h

@@ -192,6 +192,11 @@
 // v3.2.3:
 //    2021-01-01 - feature: discontinued use of 'register' keyword. Depricated in C++ 11 
 //                 feature: add STM32 as a platform supporting _TASK_STD_FUNCTION. (PR #105)
+//
+// v3.3.0:
+//    2021-05-11 - feature: Timeout() methods for StatusRequest objects 
+
+
 
 #include <Arduino.h>
 
@@ -330,7 +335,14 @@ StatusRequest::StatusRequest()
     iStatus = 0;
 }
 
-void StatusRequest::setWaiting(unsigned int aCount) { iCount = aCount; iStatus = 0; }
+void StatusRequest::setWaiting(unsigned int aCount) { 
+  iCount = aCount; 
+  iStatus = 0; 
+#ifdef _TASK_TIMEOUT
+  iStarttime = _TASK_TIME_FUNCTION();
+#endif  //  #ifdef _TASK_TIMEOUT
+}
+
 bool StatusRequest::pending() { return (iCount != 0); }
 bool StatusRequest::completed() { return (iCount == 0); }
 int StatusRequest::getStatus() { return iStatus; }
@@ -383,6 +395,19 @@ bool Task::waitForDelayed(StatusRequest* aStatusRequest, unsigned long aInterval
     }
     return false;
 }
+
+#ifdef _TASK_TIMEOUT
+void StatusRequest::resetTimeout() {
+    iStarttime = _TASK_TIME_FUNCTION();
+}
+
+long StatusRequest::untilTimeout() {
+    if ( iTimeout ) {
+        return ( (long) (iStarttime + iTimeout) - (long) _TASK_TIME_FUNCTION() );
+    }
+    return -1;
+}
+#endif  // _TASK_TIMEOUT
 #endif  // _TASK_STATUS_REQUEST
 
 bool Task::isEnabled() { return iStatus.enabled; }
@@ -1052,6 +1077,12 @@ bool Scheduler::execute() {
     // Otherwise, continue with execution as usual.  Tasks waiting to StatusRequest need to be rescheduled according to
     // how they were placed into waiting state (waitFor or waitForDelayed)
                 if ( iCurrent->iStatus.waiting ) {
+#ifdef _TASK_TIMEOUT
+                    StatusRequest *sr = iCurrent->iStatusRequest;
+                    if ( sr->iTimeout && (m - sr->iStarttime > sr->iTimeout) ) {
+                      sr->signalComplete(TASK_SR_TIMEOUT);
+                    }
+#endif // _TASK_TIMEOUT
                     if ( (iCurrent->iStatusRequest)->pending() ) break;
                     if (iCurrent->iStatus.waiting == _TASK_SR_NODELAY) {
                         iCurrent->iPreviousMillis = m - (iCurrent->iDelay = i);

+ 20 - 3
src/TaskSchedulerDeclarations.h

@@ -77,10 +77,17 @@ class Scheduler;
 
 #ifdef _TASK_STATUS_REQUEST
 
+#define TASK_SR_OK          0
+#define TASK_SR_ERROR       (-1)
+#define TASK_SR_TIMEOUT     (-99)
+ 
 #define _TASK_SR_NODELAY    1
 #define _TASK_SR_DELAY      2
 
+class Scheduler;
+
 class StatusRequest {
+  friend class Scheduler;
   public:
     INLINE StatusRequest();
     INLINE void setWaiting(unsigned int aCount = 1);
@@ -90,10 +97,22 @@ class StatusRequest {
     INLINE bool completed();
     INLINE int getStatus();
     INLINE int getCount();
+    
+#ifdef _TASK_TIMEOUT
+    INLINE void setTimeout(unsigned long aTimeout) { iTimeout = aTimeout; };
+    INLINE unsigned long getTimeout() { return iTimeout; };
+    INLINE void resetTimeout();
+    INLINE long untilTimeout();
+#endif
 
   _TASK_SCOPE:
     unsigned int  iCount;          // number of statuses to wait for. waiting for more that 65000 events seems unreasonable: unsigned int should be sufficient
     int           iStatus;         // status of the last completed request. negative = error;  zero = OK; positive = OK with a specific status
+
+#ifdef _TASK_TIMEOUT
+    unsigned long            iTimeout;               // Task overall timeout
+    unsigned long            iStarttime;             // millis at task start time
+#endif // _TASK_TIMEOUT
 };
 #endif  // _TASK_STATUS_REQUEST
 
@@ -125,13 +144,11 @@ typedef struct  {
 #endif
 
 #ifdef _TASK_TIMEOUT
-    bool  timeout    : 1;           // indication if task is waiting on the status request
+    bool  timeout    : 1;           // indication if task timed out
 #endif
 
 } __task_status;
 
-class Scheduler;
-
 
 class Task {
   friend class Scheduler;