Skip to content

Commit 344562d

Browse files
committed
v3.8.0 - setIntervalNodelay() method
1 parent 4ccca1a commit 344562d

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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.7.0: 2022-10-10 [Latest updates](https://github.com/arkhipenko/TaskScheduler/wiki/Latest-Updates)
3+
#### Version 3.8.0: 2023-01-24 [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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ TASK_SR_ERROR LITERAL1
146146
TASK_SR_CANCEL LITERAL1
147147
TASK_SR_ABORT LITERAL1
148148
TASK_SR_TIMEOUT LITERAL1
149+
TASK_INTERVAL_KEEP LITERAL1
150+
TASK_INTERVAL_RECALC LITERAL1
151+
TASK_INTERVAL_RESET LITERAL1
149152

150153
#######################################
151154

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"maintainer": true
1717
}
1818
],
19-
"version": "3.7.0",
19+
"version": "3.8.0",
2020
"frameworks": "arduino",
2121
"platforms": "*"
2222
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=TaskScheduler
2-
version=3.7.0
2+
version=3.8.0
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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Cooperative multitasking library for Arduino
3-
Copyright (c) 2015-2022 Anatoli Arkhipenko
3+
Copyright (c) 2015-2023 Anatoli Arkhipenko
44
55
Changelog:
66
v1.0.0:
@@ -222,6 +222,9 @@ v3.7.0:
222222
Added updated example 19 for this functionality. Updated the Sketch Template
223223
(Thanks, https://github.com/vortigont for the idea).
224224
225+
v3.8.0:
226+
2023-01-24 - feature: added setIntervalNodelay() method to dynamically adjust current interval
227+
225228
*/
226229

227230

@@ -876,6 +879,51 @@ void Task::setInterval (unsigned long aInterval) {
876879
#endif // _TASK_THREAD_SAFE
877880
}
878881

882+
/** Sets the execution interval without delaying the task
883+
* Task state does not change
884+
* If Task is disabled, it would remain so
885+
* @param aInterval - new execution interval
886+
*/
887+
void Task::setIntervalNodelay (unsigned long aInterval, unsigned int aOption) {
888+
#ifdef _TASK_THREAD_SAFE
889+
iMutex++;
890+
#endif // _TASK_THREAD_SAFE
891+
892+
// #define TASK_INTERVAL_KEEP 0
893+
// #define TASK_INTERVAL_RECALC 1
894+
// #define TASK_INTERVAL_RESET 2
895+
896+
switch (aOption) {
897+
case TASK_INTERVAL_RECALC:
898+
{
899+
int32_t d = aInterval - iInterval;
900+
// change the delay proportionally
901+
iDelay += d;
902+
iInterval = aInterval;
903+
break;
904+
}
905+
case TASK_INTERVAL_RESET:
906+
iInterval = aInterval;
907+
iDelay = aInterval;
908+
break;
909+
910+
default:
911+
// case TASK_INTERVAL_KEEP:
912+
if ( iInterval == iDelay ) {
913+
iInterval = aInterval;
914+
iDelay = aInterval;
915+
}
916+
else {
917+
iInterval = aInterval;
918+
}
919+
break;
920+
}
921+
922+
#ifdef _TASK_THREAD_SAFE
923+
iMutex--;
924+
#endif // _TASK_THREAD_SAFE
925+
}
926+
879927
/** Disables task
880928
* Task will no longer be executed by the scheduler
881929
* Returns status of the task before disable was called (i.e., if the task was already disabled)
@@ -1382,6 +1430,9 @@ bool Scheduler::execute() {
13821430
}
13831431
#endif // _TASK_STATUS_REQUEST
13841432

1433+
// this is the main scheduling decision point
1434+
// if the interval between current time and previous invokation time is less than the current delay - task should NOT be activated yet.
1435+
// this is millis-rollover-safe way of scheduling
13851436
if ( m - iCurrent->iPreviousMillis < iCurrent->iDelay ) break;
13861437

13871438
if ( iCurrent->iIterations > 0 ) iCurrent->iIterations--; // do not decrement (-1) being a signal of never-ending task

src/TaskSchedulerDeclarations.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Cooperative multitasking library for Arduino
2-
// Copyright (c) 2015-2022 Anatoli Arkhipenko
2+
// Copyright (c) 2015-2023 Anatoli Arkhipenko
33

44
#include <stddef.h>
55
#include <stdint.h>
@@ -42,10 +42,16 @@ class Scheduler;
4242
#define _TASK_SCOPE private
4343
#endif
4444

45+
// task scheduling iteration common options
4546
#define TASK_IMMEDIATE 0
4647
#define TASK_FOREVER (-1)
4748
#define TASK_ONCE 1
4849

50+
// options for setIntervalNodelay() method
51+
#define TASK_INTERVAL_KEEP 0
52+
#define TASK_INTERVAL_RECALC 1
53+
#define TASK_INTERVAL_RESET 2
54+
4955
#ifdef _TASK_TIMEOUT
5056
#define TASK_NOTIMEOUT 0
5157
#endif
@@ -226,6 +232,7 @@ class Task {
226232
INLINE void set(unsigned long aInterval, long aIterations, TaskCallback aCallback,TaskOnEnable aOnEnable=NULL, TaskOnDisable aOnDisable=NULL);
227233
#endif // _TASK_OO_CALLBACKS
228234
INLINE void setInterval(unsigned long aInterval);
235+
INLINE void setIntervalNodelay(unsigned long aInterval, unsigned int aOption = TASK_INTERVAL_KEEP);
229236
INLINE unsigned long getInterval();
230237
INLINE void setIterations(long aIterations);
231238
INLINE long getIterations();

0 commit comments

Comments
 (0)