Scheduler_example4_StatusRequest.ino 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. Task t1(5000, 1, &Callback1, &ts, true, NULL, &Disable1);
  13. Task t2(&Callback2, &ts);
  14. Task t3(&Callback3, &ts);
  15. /** T1 callback
  16. * T1 just signals completion of st every 5 seconds
  17. */
  18. void Callback1() {
  19. Serial.println("T1: Signaling completion of ST");
  20. st.signalComplete();
  21. }
  22. /** T1 On Disable callback
  23. * This callback renews the status request and restarts T1 delayed to run again in 5 seconds
  24. */
  25. void Disable1() {
  26. PrepareStatus();
  27. t1.restartDelayed();
  28. }
  29. /** T2 callback
  30. * Invoked when status request st completes
  31. */
  32. void Callback2() {
  33. Serial.println("T2: Invoked due to completion of ST");
  34. }
  35. /** T3 callback
  36. * Invoked when status request st completes.
  37. * This is only run once since T3 does not renew its interest in the status request st after first iteration
  38. */
  39. void Callback3() {
  40. Serial.println("T3: Invoked due to completion of ST");
  41. }
  42. /** Prepare Status request st for another iteration
  43. *
  44. */
  45. void PrepareStatus() {
  46. st.setWaiting(); // set the statusrequest object for waiting
  47. t2.waitFor(&st); // request tasks 1 & 2 to wait on the object st
  48. }
  49. /** Main Arduino code
  50. * Not much to do here. Just init Serial and set the initial status request
  51. */
  52. void setup() {
  53. Serial.begin(115200);
  54. Serial.println("TaskScheduler: Status Request Test 1. Simple Test.");
  55. PrepareStatus();
  56. t3.waitFor(&st);
  57. t1.delay();
  58. }
  59. void loop() {
  60. ts.execute();
  61. }