Scheduler_example21_OO_Callbacks.ino 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. This is example 5 rewritten with dynamic binding of callback methods
  3. - 1 second timeout is set for the main calculation task
  4. - LTS is used to address task-specific sensor class object
  5. - WDT is used to set the Task ID and use that for identifying the tasks (debug)
  6. Original description:
  7. ====================
  8. This test emulates querying 1 to 10 sensors once every 10 seconds, each could respond with a different delay
  9. (ultrasonic sensors for instance) and printing a max value of them when all have reported their values.
  10. The overall timeout of 1 second is setup as well.
  11. An error message needs to be printed if a timeout occurred instead of a distance value.
  12. Task and SuperSensor objects are dynamically created and destroyed as needed every 10 seconds
  13. This sketch uses a FreeMemory library: https://github.com/McNeight/MemoryFree
  14. */
  15. #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
  16. #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
  17. #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
  18. #define _TASK_PRIORITY // Support for layered scheduling priority
  19. #define _TASK_TIMEOUT // Support for overall task timeout
  20. #define _TASK_OO_CALLBACKS
  21. #include <TaskScheduler.h>
  22. #include "SuperSensor.h"
  23. #include "Calculator.h"
  24. #include "Ticker.h"
  25. StatusRequest measure;
  26. Scheduler ts, hts;
  27. // Tasks
  28. Calculator* tCalculate;
  29. Ticker* tCycle;
  30. int pins[] = { 1, 9, 3, 7, 5, 6, 4, 8, 2, 10 };
  31. /** Main Arduino code
  32. Not much is left here - everything is taken care of by the framework
  33. */
  34. void setup() {
  35. Serial.begin(115200);
  36. Serial.println("TaskScheduler StatusRequest Sensor Emulation Test. Complex Test.");
  37. randomSeed(analogRead(A0) + millis());
  38. ts.setHighPriorityScheduler(&hts);
  39. tCalculate = new Calculator (&hts, &ts);
  40. tCycle = new Ticker (&hts, (Task*) &tCalculate, &measure);
  41. tCalculate->setTimeout(1 * TASK_SECOND);
  42. tCycle->enable();
  43. }
  44. void loop() {
  45. ts.execute();
  46. }