Scheduler_example08_LTS.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * TaskScheduler Test sketch - use of task's Local Task Storage pointer
  3. * Test case:
  4. * Overall test runs for 5 seconds
  5. * A number of calculator tasks run every one second, and update their respective variables using Local Task Storage pointer
  6. * All calculator tasks use the same callback code, which obtains reference to appropriate variables via LTS pointer
  7. * Calculaotr tasks perform simple calculation (as an example):
  8. * adding task id number to itself
  9. * multiplying task id number by 10
  10. *
  11. * Upon completion of the overall test, all results are printed out.
  12. * Test could be repeated with various number of calculator tasks.
  13. * All that needs to change is data definitions - code is completely agnostic of number of tasks
  14. */
  15. #define _TASK_SLEEP_ON_IDLE_RUN // Compile with support for entering IDLE SLEEP state for 1 ms if not tasks are scheduled to run
  16. #define _TASK_WDT_IDS // Compile with support for Task IDs and Watchdog timer
  17. #define _TASK_LTS_POINTER // Compile with support for Local Task Storage pointer
  18. #include <TaskScheduler.h>
  19. // Overall number of calculator tasks:
  20. #define NO_TASKS 3
  21. Scheduler ts;
  22. // Callback methods prototypes
  23. void Calculate(); bool CalcOn();
  24. bool WrapperOn(); void WrapperOff();
  25. // Tasks
  26. // Calculator tasks.
  27. // Note that all three tasks use the same callback methods
  28. // They will be updating specific variables based on the
  29. // Locat Task Storage pointers
  30. Task t1(TASK_SECOND, TASK_FOREVER, &Calculate, &ts, false, &CalcOn);
  31. Task t2(TASK_SECOND, TASK_FOREVER, &Calculate, &ts, false, &CalcOn);
  32. Task t3(TASK_SECOND, TASK_FOREVER, &Calculate, &ts, false, &CalcOn);
  33. // add more calc tasks here if necessary
  34. Task tWrapper(5*TASK_SECOND, TASK_ONCE, NULL, &ts, false, &WrapperOn, &WrapperOff);
  35. // The below structure is an object referenced by LTS pointer
  36. typedef struct {
  37. unsigned int id;
  38. long sum;
  39. long product;
  40. } task_var;
  41. // These are actual structures which hold tasks specific values
  42. task_var v1;
  43. task_var v2;
  44. task_var v3;
  45. // Arrays below allow indexed access to specific tasks and tasks variables
  46. Task *tasks[] = { &t1, &t2, &t3 };
  47. task_var *vars[] = { &v1, &v2, &v3 };
  48. /**
  49. * This method is called when a wrapper task is enabled
  50. * The purpose is to supply LTS pointers to all the tasks
  51. */
  52. bool WrapperOn() {
  53. for (int i=0; i < NO_TASKS; i++) {
  54. Task& T = *tasks[i];
  55. T.setLtsPointer( vars[i] );
  56. T.enableDelayed();
  57. }
  58. return true; // Signal that Task could be enabled
  59. }
  60. /**
  61. * This method is called when Wrapper task is disabled (after first and only iteration is executed)
  62. * For each of the calculor tasks the results are printed out.
  63. */
  64. void WrapperOff() {
  65. Serial.println("Finished processing");
  66. ts.disableAll();
  67. for (int i=0; i < NO_TASKS; i++) {
  68. Serial.print("ID: "); Serial.println(vars[i]->id);
  69. Serial.print("Sum: "); Serial.println(vars[i]->sum);
  70. Serial.print("Product: "); Serial.println(vars[i]->product);
  71. Serial.println();
  72. }
  73. }
  74. /**
  75. * This method is executed when each calculator task is enabled
  76. * The purpose is to initiate all local variables
  77. */
  78. bool CalcOn() {
  79. Task& T = ts.currentTask();
  80. task_var& var = *((task_var*) T.getLtsPointer());
  81. // Initialize local variables
  82. var.id = T.getId();
  83. var.sum = 0;
  84. var.product = var.id;
  85. return true;
  86. }
  87. /**
  88. * This method performs simple calculations on task's local variables
  89. */
  90. void Calculate() {
  91. Task& T = ts.currentTask();
  92. // Another way to get to LTS pointer:
  93. task_var& var = *((task_var*) ts.currentLts());
  94. Serial.print("Calculating for task: ");
  95. Serial.print(T.getId());
  96. Serial.print("; Task id per LTS is: ");
  97. Serial.println( var.id );
  98. var.sum += T.getId();
  99. var.product = var.product * 10;
  100. }
  101. /**
  102. * Standard Arduino setup and loop methods
  103. */
  104. void setup() {
  105. Serial.begin(115200);
  106. randomSeed(analogRead(0)+analogRead(5));
  107. pinMode(13, OUTPUT);
  108. digitalWrite(13, LOW);
  109. Serial.println("Local Task Storage pointer test");
  110. tWrapper.enableDelayed();
  111. }
  112. void loop() {
  113. ts.execute();
  114. }