Explorar el Código

v3.8.0 - setIntervalNodelay() method

Anatoli Arkhipenko hace 3 años
padre
commit
344562d911
Se han modificado 6 ficheros con 66 adiciones y 5 borrados
  1. 1 1
      README.md
  2. 3 0
      keywords.txt
  3. 1 1
      library.json
  4. 1 1
      library.properties
  5. 52 1
      src/TaskScheduler.h
  6. 8 1
      src/TaskSchedulerDeclarations.h

+ 1 - 1
README.md

@@ -1,6 +1,6 @@
 # Task Scheduler
 ### Cooperative multitasking for Arduino, ESPx, STM32 and other microcontrollers
-#### Version 3.7.0: 2022-10-10 [Latest updates](https://github.com/arkhipenko/TaskScheduler/wiki/Latest-Updates)
+#### Version 3.8.0: 2023-01-24 [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

@@ -146,6 +146,9 @@ TASK_SR_ERROR	LITERAL1
 TASK_SR_CANCEL	LITERAL1
 TASK_SR_ABORT	LITERAL1
 TASK_SR_TIMEOUT	LITERAL1
+TASK_INTERVAL_KEEP	LITERAL1
+TASK_INTERVAL_RECALC	LITERAL1
+TASK_INTERVAL_RESET	LITERAL1
 
 #######################################
 

+ 1 - 1
library.json

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

+ 1 - 1
library.properties

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

+ 52 - 1
src/TaskScheduler.h

@@ -1,6 +1,6 @@
 /*
 Cooperative multitasking library for Arduino
-Copyright (c) 2015-2022 Anatoli Arkhipenko
+Copyright (c) 2015-2023 Anatoli Arkhipenko
 
 Changelog:
 v1.0.0:
@@ -222,6 +222,9 @@ v3.7.0:
                 Added updated example 19 for this functionality. Updated the Sketch Template
                 (Thanks, https://github.com/vortigont for the idea).
 
+v3.8.0:
+   2023-01-24 - feature: added setIntervalNodelay() method to dynamically adjust current interval
+
 */
 
 
@@ -876,6 +879,51 @@ void Task::setInterval (unsigned long aInterval) {
 #endif  // _TASK_THREAD_SAFE
 }
 
+/** Sets the execution interval without delaying the task
+ * Task state does not change
+ * If Task is disabled, it would remain so
+ * @param aInterval - new execution interval
+ */
+void Task::setIntervalNodelay (unsigned long aInterval, unsigned int aOption) {
+#ifdef _TASK_THREAD_SAFE
+    iMutex++;
+#endif  // _TASK_THREAD_SAFE
+
+// #define TASK_INTERVAL_KEEP      0
+// #define TASK_INTERVAL_RECALC    1
+// #define TASK_INTERVAL_RESET     2
+
+    switch (aOption) {
+      case TASK_INTERVAL_RECALC:
+      {
+          int32_t d = aInterval - iInterval;
+          // change the delay proportionally
+          iDelay += d;
+          iInterval = aInterval;
+          break;
+      } 
+      case TASK_INTERVAL_RESET:
+          iInterval = aInterval;
+          iDelay = aInterval;
+          break;
+          
+      default:
+//      case TASK_INTERVAL_KEEP:
+          if ( iInterval == iDelay ) {
+              iInterval = aInterval;
+              iDelay = aInterval;
+          }
+          else {
+              iInterval = aInterval;
+          }
+          break;
+    }
+
+#ifdef _TASK_THREAD_SAFE
+    iMutex--;
+#endif  // _TASK_THREAD_SAFE
+}
+
 /** Disables task
  * Task will no longer be executed by the scheduler
  * Returns status of the task before disable was called (i.e., if the task was already disabled)
@@ -1382,6 +1430,9 @@ bool Scheduler::execute() {
                 }
 #endif  // _TASK_STATUS_REQUEST
 
+                // this is the main scheduling decision point
+                // if the interval between current time and previous invokation time is less than the current delay - task should NOT be activated yet.
+                // this is millis-rollover-safe way of scheduling
                 if ( m - iCurrent->iPreviousMillis < iCurrent->iDelay ) break;
 
                 if ( iCurrent->iIterations > 0 ) iCurrent->iIterations--;  // do not decrement (-1) being a signal of never-ending task

+ 8 - 1
src/TaskSchedulerDeclarations.h

@@ -1,5 +1,5 @@
 // Cooperative multitasking library for Arduino
-// Copyright (c) 2015-2022 Anatoli Arkhipenko
+// Copyright (c) 2015-2023 Anatoli Arkhipenko
 
 #include <stddef.h>
 #include <stdint.h>
@@ -42,10 +42,16 @@ class Scheduler;
     #define _TASK_SCOPE  private
 #endif
 
+//  task scheduling iteration common options
 #define TASK_IMMEDIATE          0
 #define TASK_FOREVER         (-1)
 #define TASK_ONCE               1
 
+//  options for setIntervalNodelay() method
+#define TASK_INTERVAL_KEEP      0
+#define TASK_INTERVAL_RECALC    1
+#define TASK_INTERVAL_RESET     2
+
 #ifdef _TASK_TIMEOUT
 #define TASK_NOTIMEOUT          0
 #endif
@@ -226,6 +232,7 @@ class Task {
     INLINE void set(unsigned long aInterval, long aIterations, TaskCallback aCallback,TaskOnEnable aOnEnable=NULL, TaskOnDisable aOnDisable=NULL);
 #endif // _TASK_OO_CALLBACKS
     INLINE void setInterval(unsigned long aInterval);
+    INLINE void setIntervalNodelay(unsigned long aInterval, unsigned int aOption = TASK_INTERVAL_KEEP);
     INLINE unsigned long getInterval();
     INLINE void setIterations(long aIterations);
     INLINE long getIterations();