Skip to content

Commit 3538392

Browse files
committed
#44 one time long schedule support
1 parent f70bbb8 commit 3538392

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

examples/longSchedule/longSchedule.ino

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <TaskManagerIO.h>
1111
#include <TmLongSchedule.h>
1212
#include <Wire.h>
13+
#include <IoLogging.h>
1314

1415
//
1516
// Here we create an OO task (a task that is actually a class instance). In this case the exec() method will be
@@ -35,6 +36,7 @@ public:
3536

3637
// Forward references
3738
void dailyScheduleFn();
39+
void fiveMinsOnceFn();
3840

3941
//
4042
// Here we create two schedules to be registered with task manager later. One will fire every days and one will fire
@@ -43,28 +45,29 @@ void dailyScheduleFn();
4345
//
4446
TmLongSchedule hourAndHalfSchedule(makeHourSchedule(1, 30), &myTaskExec);
4547
TmLongSchedule onceADaySchedule(makeDaySchedule(1), dailyScheduleFn);
48+
TmLongSchedule fiveMinsOnceSchedule(makeHourSchedule(0, 5), fiveMinsOnceFn, true);
4649

4750
void setup() {
48-
Serial.begin(115200);
51+
//Serial.begin(115200);
52+
Serial1.begin(115200);
4953

50-
Serial.println("Started long schedule example");
54+
serdebugF("Started long schedule example");
5155

5256
// First two long schedules are global variables.
5357
// IMPORTANT NOTE: If you use references to a variable like this THEY MUST BE GLOBAL
5458
taskManager.registerEvent(&hourAndHalfSchedule);
5559
taskManager.registerEvent(&onceADaySchedule);
60+
taskManager.registerEvent(&fiveMinsOnceSchedule);
5661

5762
// this shows how to create a long schedule event using the new operator, make sure the second parameter is true
5863
// as this will delete the event when it completes.
5964
taskManager.registerEvent(new TmLongSchedule(makeHourSchedule(0, 15), [] {
60-
Serial.print(millis());
61-
Serial.println(": Fifteen minutes passed");
65+
serdebugF(": Fifteen minutes passed");
6266
}), true);
6367

6468
// lastly we show the regular event creation method, this task is enabled and disabled by the OO task.
6569
auto taskId = taskManager.scheduleFixedRate(120, [] {
66-
Serial.print(millis());
67-
Serial.println(": Two minutes");
70+
serdebugF(": Two minutes");
6871
}, TIME_SECONDS);
6972

7073
myTaskExec.setTaskToSuspend(taskId);
@@ -75,6 +78,9 @@ void loop() {
7578
}
7679

7780
void dailyScheduleFn() {
78-
Serial.print(millis());
79-
Serial.println(": Daily schedule");
81+
serdebugF("Daily schedule");
82+
}
83+
84+
void fiveMinsOnceFn() {
85+
serdebugF("Five mins once, should not occur again");
8086
}

src/TmLongSchedule.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ uint32_t makeDaySchedule(int days, int hours) {
1717
return (days * 24UL * HOURS_TO_MILLIS) + (hours * MINUTES_TO_MILLIS);
1818
}
1919

20-
TmLongSchedule::TmLongSchedule(uint32_t milliScheduleNext, Executable* toExecute) : milliSchedule(milliScheduleNext),
21-
fnCallback(nullptr), theExecutable(toExecute), lastScheduleTime(0) { }
20+
TmLongSchedule::TmLongSchedule(uint32_t milliScheduleNext, Executable* toExecute, bool oneTime) : milliSchedule(milliScheduleNext),
21+
fnCallback(nullptr), theExecutable(toExecute), lastScheduleTime(0), oneTime(oneTime) { }
2222

23-
TmLongSchedule::TmLongSchedule(uint32_t milliScheduleNext, TimerFn toExecute) : milliSchedule(milliScheduleNext),
24-
fnCallback(toExecute), theExecutable(nullptr), lastScheduleTime(0) { }
23+
TmLongSchedule::TmLongSchedule(uint32_t milliScheduleNext, TimerFn toExecute, bool oneTime) : milliSchedule(milliScheduleNext),
24+
fnCallback(toExecute), theExecutable(nullptr), lastScheduleTime(0), oneTime(oneTime) { }
2525

2626
void TmLongSchedule::exec() {
2727
lastScheduleTime = millis();
@@ -36,6 +36,7 @@ void TmLongSchedule::exec() {
3636
fnCallback();
3737
}
3838

39+
setCompleted(oneTime);
3940
}
4041

4142
uint32_t TmLongSchedule::timeOfNextCheck() {

src/TmLongSchedule.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ class TmLongSchedule : public BaseEvent {
1818
const TimerFn fnCallback;
1919
Executable *const theExecutable;
2020
uint32_t lastScheduleTime;
21+
bool oneTime;
2122
public:
2223
/** Create a schedule that will call back a TimerFn functional callback.
2324
* @param milliSchedule the schedule to call back on
2425
* @param callee the functional callback
2526
*/
26-
TmLongSchedule(uint32_t milliSchedule, TimerFn callee);
27+
TmLongSchedule(uint32_t milliSchedule, TimerFn callee, bool oneTime = false);
2728
/**
2829
* Create schedule that will call the exec() method on an Executable
2930
* @param milliSchedule the schedule to call back on
3031
* @param callee the object extending from Executable
3132
*/
32-
TmLongSchedule(uint32_t milliSchedule, Executable* callee);
33+
TmLongSchedule(uint32_t milliSchedule, Executable* callee, bool oneTime = false);
3334

3435
void exec() override;
3536

0 commit comments

Comments
 (0)