Skip to content

Commit

Permalink
v.3.3.0 - Timeout functionality for StatusRequest objects
Browse files Browse the repository at this point in the history
  • Loading branch information
arkhipenko committed May 14, 2021
1 parent ae2a37b commit 5cd3227
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

#######################################

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"maintainer": true
}
],
"version": "3.2.2",
"version": "3.3.0",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TaskScheduler
version=3.2.2
version=3.3.0
author=Anatoli Arkhipenko <[email protected]>
maintainer=Anatoli Arkhipenko <[email protected]>
sentence=Cooperative multitasking for Arduino, ESPx, STM32 and other microcontrollers.
Expand Down
33 changes: 32 additions & 1 deletion src/TaskScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 20 additions & 3 deletions src/TaskSchedulerDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5cd3227

Please sign in to comment.