Scheduler_example04_StatusRequest.ino 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** This test demonstrates interaction between three simple tasks via StatusRequest object.
  2. * Task T1 runs every 5 seconds and signals completion of a status request st.
  3. * Tasks T2 and T3 are waiting on the same request (st)
  4. * Task T3 does not renew its interest in status request st, so it is only invoked once (first iteration)
  5. * Task T2 is invoked every time st completes, because it renews its interest in status of status request object st every iteration of T1
  6. */
  7. #define _TASK_SLEEP_ON_IDLE_RUN
  8. #define _TASK_STATUS_REQUEST
  9. #include <TaskScheduler.h>
  10. StatusRequest st;
  11. Scheduler ts;
  12. // Callback methods prototypes
  13. void Callback1();
  14. void Disable1();
  15. void Callback2();
  16. void Callback3();
  17. void PrepareStatus();
  18. // Tasks
  19. Task t1(5000, TASK_ONCE, &Callback1, &ts, true, NULL, &Disable1);
  20. Task t2(&Callback2, &ts);
  21. Task t3(&Callback3, &ts);
  22. /** T1 callback
  23. * T1 just signals completion of st every 5 seconds
  24. */
  25. void Callback1() {
  26. Serial.println("T1: Signaling completion of ST");
  27. st.signalComplete();
  28. }
  29. /** T1 On Disable callback
  30. * This callback renews the status request and restarts T1 delayed to run again in 5 seconds
  31. */
  32. void Disable1() {
  33. PrepareStatus();
  34. t1.restartDelayed();
  35. }
  36. /** T2 callback
  37. * Invoked when status request st completes
  38. */
  39. void Callback2() {
  40. Serial.println("T2: Invoked due to completion of ST");
  41. }
  42. /** T3 callback
  43. * Invoked when status request st completes.
  44. * This is only run once since T3 does not renew its interest in the status request st after first iteration
  45. */
  46. void Callback3() {
  47. Serial.println("T3: Invoked due to completion of ST");
  48. }
  49. /** Prepare Status request st for another iteration
  50. *
  51. */
  52. void PrepareStatus() {
  53. st.setWaiting(); // set the statusrequest object for waiting
  54. t2.waitFor(&st); // request tasks 1 & 2 to wait on the object st
  55. }
  56. /** Main Arduino code
  57. * Not much to do here. Just init Serial and set the initial status request
  58. */
  59. void setup() {
  60. Serial.begin(115200);
  61. delay(1000);
  62. Serial.println("TaskScheduler: Status Request Test 1. Simple Test.");
  63. ts.startNow();
  64. PrepareStatus();
  65. t3.waitFor(&st);
  66. t1.delay();
  67. }
  68. void loop() {
  69. ts.execute();
  70. }