Scheduler_example21_OO_Callbacks.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. FreeMemory for ARM32 boards is based on: http://www.stm32duino.com/viewtopic.php?f=18&t=2065
  15. */
  16. #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
  17. #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
  18. #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
  19. #define _TASK_PRIORITY // Support for layered scheduling priority
  20. #define _TASK_TIMEOUT // Support for overall task timeout
  21. #define _TASK_OO_CALLBACKS
  22. #include <TaskScheduler.h>
  23. #include "SuperSensor.h"
  24. #include "Calculator.h"
  25. #include "Ticker.h"
  26. StatusRequest measure;
  27. Scheduler ts, hts;
  28. // Tasks
  29. Calculator* tCalculate;
  30. Ticker* tCycle;
  31. int pins[] = { 1, 9, 3, 7, 5, 6, 4, 8, 2, 10 };
  32. #ifdef ARDUINO_ARCH_STM32F1
  33. #define A0 3
  34. #endif
  35. /** Main Arduino code
  36. Not much is left here - everything is taken care of by the framework
  37. */
  38. void setup() {
  39. Serial.begin(115200);
  40. delay(1000);
  41. while (!Serial) {}
  42. Serial.println("TaskScheduler StatusRequest Sensor Emulation Test. Complex Test.");
  43. #ifdef ARDUINO_ARCH_STM32F1
  44. pinMode(A0, INPUT_ANALOG);
  45. #endif
  46. randomSeed(analogRead(A0) + millis());
  47. ts.setHighPriorityScheduler(&hts);
  48. tCalculate = new Calculator (&hts, &ts);
  49. tCycle = new Ticker (&hts, (Task*) tCalculate, &measure);
  50. tCalculate->setTimeout(1 * TASK_SECOND);
  51. tCycle->enable();
  52. }
  53. void loop() {
  54. ts.execute();
  55. }