1+ /* *
2+ * An example that shows how to create long schedules, in hours and days with task manager. Any schedule over an hour
3+ * in length can be created using a TmLongSchedule instance, these instances are registered as events with task manager
4+ * and other than this work as usual.
5+ *
6+ * You cannot create schedules over 60 minutes using the scheduleFixedRate / scheduleOnce functions, instead you use
7+ * the procedure below.
8+ */
9+
10+ #include < TaskManagerIO.h>
11+ #include < TmLongSchedule.h>
12+ #include < Wire.h>
13+
14+ //
15+ // Here we create an OO task (a task that is actually a class instance). In this case the exec() method will be
16+ // called at the specified time.
17+ //
18+ class MyTaskExecutable : public Executable {
19+ private:
20+ int callCount;
21+ bool enableTask = false ;
22+ taskid_t taskToSuspend = TASKMGR_INVALIDID;
23+ public:
24+ void setTaskToSuspend (taskid_t id) { taskToSuspend = id; }
25+
26+ void exec () override {
27+ callCount++;
28+ Serial.print (" Called my task executable " );
29+ Serial.println (callCount);
30+
31+ taskManager.setTaskEnabled (taskToSuspend, enableTask);
32+ enableTask = !enableTask;
33+ }
34+ } myTaskExec;
35+
36+ // Forward references
37+ void dailyScheduleFn ();
38+
39+ //
40+ // Here we create two schedules to be registered with task manager later. One will fire every days and one will fire
41+ // every hour and a half. You provide the schedule in milliseconds (generally using the two helper functions shown) and
42+ // also either a timer function or class extending executable.
43+ //
44+ TmLongSchedule hourAndHalfSchedule (makeHourSchedule(1 , 30 ), &myTaskExec);
45+ TmLongSchedule onceADaySchedule (makeDaySchedule(1 ), dailyScheduleFn);
46+
47+ void setup () {
48+ Serial.begin (115200 );
49+
50+ Serial.println (" Started long schedule example" );
51+
52+ // First two long schedules are global variables.
53+ // IMPORTANT NOTE: If you use references to a variable like this THEY MUST BE GLOBAL
54+ taskManager.registerEvent (&hourAndHalfSchedule);
55+ taskManager.registerEvent (&onceADaySchedule);
56+
57+ // this shows how to create a long schedule event using the new operator, make sure the second parameter is true
58+ // as this will delete the event when it completes.
59+ taskManager.registerEvent (new TmLongSchedule (makeHourSchedule (0 , 15 ), [] {
60+ Serial.print (millis ());
61+ Serial.println (" : Fifteen minutes passed" );
62+ }), true );
63+
64+ // lastly we show the regular event creation method, this task is enabled and disabled by the OO task.
65+ auto taskId = taskManager.scheduleFixedRate (120 , [] {
66+ Serial.print (millis ());
67+ Serial.println (" : Two minutes" );
68+ }, TIME_SECONDS);
69+
70+ myTaskExec.setTaskToSuspend (taskId);
71+ }
72+
73+ void loop () {
74+ taskManager.runLoop ();
75+ }
76+
77+ void dailyScheduleFn () {
78+ Serial.print (millis ());
79+ Serial.println (" : Daily schedule" );
80+ }
0 commit comments