Scheduler_example28_Tickless.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * TaskScheduler Test
  3. *
  4. * This test illustrates how to use TS's tickless support functionality
  5. * Tickless support enables more deterministic sleep by calculating time delay until next task invocation
  6. * during every pass. That delay could then be used to put microcontroller in sleep mode continously
  7. * instead of in small intervals
  8. *
  9. * Initially only tasks 1 and 2 are enabled
  10. * Task1 runs every 2 seconds 10 times and then stops
  11. * Task2 runs every 3 seconds indefinitely
  12. * Task1 enables Task3 at its first run
  13. * Task3 run every 5 seconds
  14. * Task1 disables Task3 on its last iteration and changed Task2 to run every 1/2 seconds
  15. * At the end Task2 is the only task running every 1/2 seconds
  16. */
  17. #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
  18. #define _TASK_TICKLESS // Enable support for tickless sleep on FreeRTOS
  19. #define _TASK_DO_NOT_YIELD // Disable yield() method in execute()
  20. #include <TaskScheduler.h>
  21. // Callback methods prototypes
  22. void t1Callback();
  23. void t2Callback();
  24. void t3Callback();
  25. //Tasks
  26. Task t4();
  27. Task t1(2000, 10, &t1Callback);
  28. Task t2(3000, TASK_FOREVER, &t2Callback);
  29. Task t3(5000, TASK_FOREVER, &t3Callback);
  30. Scheduler runner;
  31. void t1Callback() {
  32. Serial.print("t1: ");
  33. Serial.println(millis());
  34. if (t1.isFirstIteration()) {
  35. runner.addTask(t3);
  36. t3.enable();
  37. Serial.println("t1: enabled t3 and added to the chain");
  38. }
  39. if (t1.isLastIteration()) {
  40. t3.disable();
  41. runner.deleteTask(t3);
  42. t2.setInterval(500);
  43. Serial.println("t1: disable t3 and delete it from the chain. t2 interval set to 500");
  44. }
  45. }
  46. void t2Callback() {
  47. Serial.print("t2: ");
  48. Serial.println(millis());
  49. // comment this line out if you want to test t2's 500 ms explicit delay
  50. // as-is this delay tests that task in catch up mode will prevent explicit tickless delay
  51. delay(501);
  52. }
  53. void t3Callback() {
  54. Serial.print("t3: ");
  55. Serial.println(millis());
  56. }
  57. void setup () {
  58. Serial.begin(115200);
  59. Serial.println("Scheduler TEST");
  60. runner.init();
  61. Serial.println("Initialized scheduler");
  62. runner.addTask(t1);
  63. Serial.println("added t1");
  64. runner.addTask(t2);
  65. Serial.println("added t2");
  66. delay(1000);
  67. t1.enable();
  68. Serial.println("Enabled t1");
  69. t2.enable();
  70. Serial.println("Enabled t2");
  71. }
  72. unsigned long nr = 0;
  73. void loop () {
  74. runner.execute();
  75. nr = runner.getNextRun();
  76. if ( nr ) {
  77. Serial.println("TS stats:");
  78. Serial.print("\tTotal tasks: "); Serial.println(runner.getTotalTasks() );
  79. Serial.print("\tActive tasks: "); Serial.println(runner.getActiveTasks() );
  80. Serial.print("\tInvoked tasks: "); Serial.println(runner.getInvokedTasks() );
  81. Serial.print("Next scheduling pass in "); Serial.print(nr); Serial.println(" ms");
  82. delay(nr);
  83. }
  84. }