TaskSched
TaskSched.h
Go to the documentation of this file.
1 #ifndef TASKSCHED_H
2 #define TASKSCHED_H
3 
4 //#define DEBUGA 1
5 #include "Arduino.h"
6 //#include <list>
7 #define TASK_SECOND 1000
8 #define TASK_MINUTE 60*TASK_SECOND
9 
10 #include <SimpleList.h>
11 class Task;
12 
13 
14 // was typedef std::function<void(Task *)> voidFuncTypeWith;
15 
21 class InitialState {
22 public:
23 
27  unsigned long mInterval;
31  bool mEnabled;
41  unsigned long mIterations;
42 };
44 
80 class Task {
81 
82  private:
86  void runIt();
87  // make Sched a friend so it can call a private method, runIt
88  friend class Sched;
89 
90  public:
91  // Function pointer types
92  typedef void (*TaskCallback)(Task*);
93  typedef void (*VoidCallback)();
106  Task(TaskCallback func, unsigned long interval = 5000, bool enabled = false,
107  int iterations = 0, const char* name = "Unk", bool runImmediately = false);
108 
109  // Constructor for callback without Task pointer
110  Task(VoidCallback func, unsigned long interval = 5000, bool enabled = false,
111  int iterations = 0, const char* name = "Unk", bool runImmediately = false);
112 
113  private:
118  TaskCallback mProcWithTask;
122  VoidCallback mProcVoid;
126  bool mWithTaskPtr;
127 
131  unsigned long mInterval;
135  bool mEnabled;
139  unsigned long mIterations;
143  String mName;
148  bool mRunImmediately;
152  unsigned long mIterationCount;
156  bool doShow=true;
160  unsigned long mLastStartTime;
164  savedInitial mOrig;
169  public:
170  bool isFirstIteration();
175  bool isLastIteration();
184  void restart();
188  void enable();
192  void disable();
197  bool isEnabled() const;
202  void setCallback(const TaskCallback &callback);
207  void setCallback(const VoidCallback &callback);
211  void setName(String);
215  void showInit();
220  String getName() const;
225  unsigned long getIterationCount() const;
229  void setInterval(unsigned long newInterval);
233  void setIterations(unsigned long newIterations);
237  void setImmediately(bool);
243  String showTaskInfo() const;
248  unsigned long getInterval(void) const;
253  bool getRunImmediately(void) const;
258  unsigned long getLastStartTime(void) const;
263  static String formatMS(unsigned long milliseconds);
264 };
279 class Sched
280 {
281  public:
286  unsigned long getSize();
289  Sched();
299  void begin();
316  void addTask(Task *task);
320  void enable();
324  void disable();
329  bool isEnabled();
331  const SimpleList<Task *>& getTasks() const;
335  void run();
336 
337  private:
338  //SimpleList<Task*> tTasks;
343  SimpleList<Task *> tTasks;
348  bool mSchedEnabled;
349 };
350 
352  /* @param func Function to be called.
353  * @param interval Time between calls to func in milliseconds.
354  * If intval is passed as a floating point number, e. g. 4.5 then it is treated as if it were a number of seconds otherwise the interval is treated as if it were a number of millisconds
355  * @param enabled Flag to be set if the task should start in an enabled state.
356  * @param iterations Number of iterations the task will be run. Set to zero for infinited iterations.
357  * @param name A String value of a name of the task.
358  * @param runImmediately A boolean flag that directs the scheduler to run the callback immediately rather than wait for the interval to expire. False says to wait for the interval to run the task.
359 */
360 #endif
This class holds the initial state of a task.
Definition: TaskSched.h:21
bool mEnabled
saved enable flag
Definition: TaskSched.h:31
unsigned long mIterations
saved run iterations count
Definition: TaskSched.h:41
bool mRunImmediately
saved run immediately flag
Definition: TaskSched.h:36
unsigned long mInterval
This member holds the original interval value.
Definition: TaskSched.h:27
This class runs the scheduled tasks.
Definition: TaskSched.h:280
const SimpleList< Task * > & getTasks() const
Definition: TaskSched.cpp:290
bool isEnabled()
return true if the scheduler is enabled
Definition: TaskSched.cpp:259
unsigned long getSize()
return the number of tasks in the run queue
Definition: TaskSched.cpp:233
void enable()
enable the scheduler
Definition: TaskSched.cpp:253
void begin()
Definition: TaskSched.cpp:241
void run()
called perodically to check if a task should be scheduled
Definition: TaskSched.cpp:265
void addTask(Task *task)
Definition: TaskSched.cpp:244
void disable()
disable the scheduler
Definition: TaskSched.cpp:256
Represents a schedulable task.
Definition: TaskSched.h:80
bool getRunImmediately(void) const
return the run immediately flag
Definition: TaskSched.cpp:178
void(* VoidCallback)()
Definition: TaskSched.h:93
unsigned long getInterval(void) const
return the task interval
Definition: TaskSched.cpp:183
void setInterval(unsigned long newInterval)
function to set a new interval
Definition: TaskSched.cpp:173
void setIterations(unsigned long newIterations)
function to set a new iterations value
Definition: TaskSched.cpp:168
void(* TaskCallback)(Task *)
Definition: TaskSched.h:92
void showInit()
display stuff
Definition: TaskSched.cpp:72
String getName() const
return string containing name of task
Definition: TaskSched.cpp:50
bool fRunImmediately()
return true if the run immediately flag is set
bool isLastIteration()
return true if this is the last iteration
Definition: TaskSched.cpp:55
Task(TaskCallback func, unsigned long interval=5000, bool enabled=false, int iterations=0, const char *name="Unk", bool runImmediately=false)
Constructs a new Task.
Definition: TaskSched.cpp:7
static String formatMS(unsigned long milliseconds)
return a string with a formatted time
Definition: TaskSched.cpp:87
void setName(String)
give the task a new name
Definition: TaskSched.cpp:159
bool isEnabled() const
return true if task is enabled
Definition: TaskSched.cpp:124
unsigned long getIterationCount() const
return the iteration count, that is the number of iterations that the task has been run
Definition: TaskSched.cpp:193
unsigned long getLastStartTime(void) const
return the last start time flag
Definition: TaskSched.cpp:188
void setImmediately(bool)
function to set the run immediately flag
Definition: TaskSched.cpp:163
String showTaskInfo() const
function that displays task info
Definition: TaskSched.cpp:102
bool isFirstIteration()
return true if this is the first iteration
Definition: TaskSched.cpp:43
void setCallback(const TaskCallback &callback)
Sets a new callback function for the task.
Definition: TaskSched.cpp:154
void restart()
restart the task with the original parameters, Enable is not restored
Definition: TaskSched.cpp:137
void disable()
disable the task
Definition: TaskSched.cpp:66
void enable()
enable the task
Definition: TaskSched.cpp:128