Scheduler_example19_Dynamic_Tasks.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. TaskScheduler Test sketch - test of Task destructor
  3. Test case:
  4. Main task runs every 100 milliseconds 100 times and in 50% cases generates a task object
  5. which runs 1 to 10 times with 100 ms to 5 s interval, and then destroyed.
  6. Garbage collection deletes all the tasks which have finished (enabled in their respective
  7. OnDisable methods)
  8. This sketch uses the following libraries:
  9. - FreeMemory library: https://github.com/McNeight/MemoryFree
  10. - QueueArray library: https://playground.arduino.cc/Code/QueueArray/
  11. */
  12. #define _TASK_WDT_IDS // To enable task unique IDs
  13. #define _TASK_SLEEP_ON_IDLE_RUN // Compile with support for entering IDLE SLEEP state for 1 ms if not tasks are scheduled to run
  14. #define _TASK_LTS_POINTER // Compile with support for Local Task Storage pointer
  15. #include <TaskScheduler.h>
  16. #include <QueueArray.h>
  17. #if defined (ARDUINO_ARCH_AVR)
  18. #include <MemoryFree.h>
  19. #elif defined(__arm__)
  20. extern "C" char* sbrk(int incr);
  21. static int freeMemory() {
  22. char top = 't';
  23. return &top - reinterpret_cast<char*>(sbrk(0));
  24. }
  25. #else
  26. int freeMemory(); // supply your own
  27. #endif
  28. Scheduler ts;
  29. // Callback methods prototypes
  30. void MainLoop();
  31. void GC();
  32. // Statis task
  33. Task tMain(100 * TASK_MILLISECOND, 100, &MainLoop, &ts, true);
  34. Task tGarbageCollection(200 * TASK_MILLISECOND, TASK_FOREVER, &GC, &ts, false);
  35. void Iteration();
  36. bool OnEnable();
  37. void OnDisable();
  38. int noOfTasks = 0;
  39. QueueArray <Task*> toDelete;
  40. void MainLoop() {
  41. Serial.print(millis()); Serial.print("\t");
  42. Serial.print("MainLoop run: ");
  43. int i = tMain.getRunCounter();
  44. Serial.print(i); Serial.print(F(".\t"));
  45. if ( random(0, 101) > 50 ) { // generate a new task only in 50% of cases
  46. // Generating another task
  47. long p = random(100, 5001); // from 100 ms to 5 seconds
  48. long j = random(1, 11); // from 1 to 10 iterations)
  49. Task *t = new Task(p, j, Iteration, &ts, false, OnEnable, OnDisable);
  50. Serial.print(F("Generated a new task:\t")); Serial.print(t->getId()); Serial.print(F("\tInt, Iter = \t"));
  51. Serial.print(p); Serial.print(", "); Serial.print(j);
  52. Serial.print(F("\tFree mem=")); Serial.print(freeMemory());
  53. Serial.print(F("\tNo of tasks=")); Serial.println(++noOfTasks);
  54. t->enable();
  55. }
  56. else {
  57. Serial.println(F("Skipped generating a task"));
  58. }
  59. }
  60. void Iteration() {
  61. Task &t = ts.currentTask();
  62. Serial.print(millis()); Serial.print("\t");
  63. Serial.print("Task N"); Serial.print(t.getId()); Serial.print(F("\tcurrent iteration: "));
  64. int i = t.getRunCounter();
  65. Serial.println(i);
  66. }
  67. bool OnEnable() {
  68. // to-do: think of something to put in here.
  69. return true;
  70. }
  71. void OnDisable() {
  72. Task *t = &ts.currentTask();
  73. unsigned int tid = t->getId();
  74. toDelete.push(t);
  75. tGarbageCollection.enableIfNot();
  76. Serial.print(millis()); Serial.print("\t");
  77. Serial.print("Task N"); Serial.print(tid); Serial.println(F("\tfinished"));
  78. }
  79. /**
  80. Standard Arduino setup and loop methods
  81. */
  82. void setup() {
  83. Serial.begin(115200);
  84. randomSeed(analogRead(0) + analogRead(5));
  85. noOfTasks = 0;
  86. Serial.println(F("Dynamic Task Creation/Destruction Example"));
  87. Serial.println();
  88. Serial.print(F("Free mem=")); Serial.print(freeMemory());
  89. Serial.print(F("\tNo of tasks=")); Serial.println(noOfTasks);
  90. Serial.println();
  91. }
  92. void GC() {
  93. if ( toDelete.isEmpty() ) {
  94. tGarbageCollection.disable();
  95. return;
  96. }
  97. Task *t = toDelete.pop();
  98. Serial.print(millis()); Serial.print("\t");
  99. Serial.print("Task N"); Serial.print(t->getId()); Serial.println(F("\tdestroyed"));
  100. Serial.print("Free mem="); Serial.print(freeMemory());
  101. Serial.print(F("\tNo of tasks=")); Serial.println(--noOfTasks);
  102. delete t;
  103. }
  104. void loop() {
  105. ts.execute();
  106. }