-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskScheduler.h
More file actions
86 lines (63 loc) · 2.66 KB
/
Copy pathTaskScheduler.h
File metadata and controls
86 lines (63 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef TASKSCHEDULER_HEADERGUARD
#define TASKSCHEDULER_HEADERGUARD
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif
#include <inttypes.h>
typedef uint8_t taskToken_t;
typedef void(*pTaskFunc)(void*);
typedef uint8_t eventId_t;
//#define MAX_NUM_DELAYED_TASKS 31
//#define MAX_NUM_EVENT_TASKS 31
#define MAX_NUM_DELAYED_TASKS 8
#define MAX_NUM_EVENT_TASKS 2
// We allocate less slots for tasks and events because the attiny85 has less ram.
class TaskScheduler
{
public:
TaskScheduler();
taskToken_t scheduleDelayedTask(pTaskFunc newtask, void* newClientData, uint32_t delayAmount);
bool removeScheduledTask( taskToken_t taskId );
void rescheduleDelayedTask (taskToken_t taskId , pTaskFunc newtask, void* newClientData, uint32_t delayAmount);
void doEventLoop();
void exit();
eventId_t createEventTrigger(pTaskFunc eventHandler);
// Creates a 'trigger' for an event, which - if it occurs - will be handled (from the event loop) using "eventHandlerProc".
// (Returns 0 iff no such trigger can be created (e.g., because of implementation limits on the number of triggers).)
void triggerEvent(eventId_t eventTriggerId, void* clientData = NULL);
// Causes the (previously-registered) handler function for the specified event to be handled (from the event loop).
// The handler function is called with "clientData" as parameter.
// Note: This function (unlike other library functions) may be called from an external thread - to signal an external event.
void deleteEventTrigger(eventId_t eventId);
// Deletes an existing event trigger. If the event being deleted is triggered and awaiting to be handled it will not be
// handled.
private:
// Private functions
taskToken_t findFreeSpotInDelayedTaskArray();
taskToken_t findFreeSpotInEventTaskArray();
void handleAllTriggeredEvents();
void sync();
// Private types
struct delayedTaskEntry_t
{
pTaskFunc pTask;
void* clientData;
uint32_t scheduledTime;
};
struct eventTaskEntry_t
{
pTaskFunc pTask;
void* clientData;
};
// Private variables
delayedTaskEntry_t arrayOfDelayedTasks[MAX_NUM_DELAYED_TASKS];
delayedTaskEntry_t nextScheduledTask;
taskToken_t idOfNextScheduledTask;
bool shouldExitWhenPossible; // set to true when a task Scheduler exit was requested
eventTaskEntry_t arrayOfEventTasks[MAX_NUM_EVENT_TASKS];
uint32_t eventFlags; // A set flag means the even happened and is pending to be handled
};//end class TaskScheduler
#endif //TASKSCHEDULER_HEADERGUARD