Scheduler_example17_Timeout.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. This eaxmple illustrates the use of overall Task timeout functionality:
  3. Task 1 - runs every 1 seconds and times out in 10 seconds
  4. Task 2 - runs every 5 seconds and resets the timeout every run, so runs continuosly even though the timeout is set to 10 seconds
  5. */
  6. // #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
  7. #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
  8. //#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
  9. // #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
  10. // #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
  11. // #define _TASK_PRIORITY // Support for layered scheduling priority
  12. // #define _TASK_MICRO_RES // Support for microsecond resolution
  13. // #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
  14. // #define _TASK_DEBUG // Make all methods and variables public for debug purposes
  15. // #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
  16. #define _TASK_TIMEOUT
  17. #include <TaskScheduler.h>
  18. Scheduler ts;
  19. void task1Callback();
  20. void task1OnDisable();
  21. void task2Callback();
  22. void task2OnDisable();
  23. Task t1(1 * TASK_SECOND, TASK_FOREVER, &task1Callback, &ts, false, NULL, &task1OnDisable);
  24. Task t2(5 * TASK_SECOND, TASK_FOREVER, &task2Callback, &ts, false, NULL, &task2OnDisable);
  25. void setup() {
  26. // put your setup code here, to run once:
  27. Serial.begin(115200);
  28. Serial.println("TaskScheduler Timeout example");
  29. Serial.println("=============================");
  30. t1.setTimeout(10 * TASK_SECOND);
  31. t2.setTimeout(10 * TASK_SECOND);
  32. ts.enableAll();
  33. }
  34. void loop() {
  35. // put your main code here, to run repeatedly:
  36. ts.execute();
  37. }
  38. void task1Callback() {
  39. Serial.print("Task 1:\t");
  40. Serial.print(millis());
  41. Serial.print(": t/out=");
  42. Serial.print(t1.getTimeout());
  43. Serial.print("\tms until t/out=");
  44. Serial.println( t1.untilTimeout());
  45. }
  46. void task1OnDisable() {
  47. if (t1.timedOut()) {
  48. Serial.println("Task 1 has timed out. Restarting");
  49. t1.setInterval(1 * TASK_SECOND);
  50. t1.setIterations(15);
  51. t1.setTimeout(TASK_NOTIMEOUT);
  52. t1.enable();
  53. }
  54. else {
  55. Serial.println("Task 1 has been disabled");
  56. }
  57. }
  58. void task2Callback() {
  59. Serial.print("Task 2:\t");
  60. Serial.print(millis());
  61. Serial.print(": t/out=");
  62. Serial.print(t2.getTimeout());
  63. Serial.print("\tms until t/out=");
  64. Serial.println( t2.untilTimeout());
  65. t2.resetTimeout();
  66. }
  67. void task2OnDisable() {
  68. if (t2.timedOut()) {
  69. Serial.println("Task 2 has timed out");
  70. }
  71. else {
  72. Serial.println("Task 2 has been disabled");
  73. }
  74. }