Scheduler_template.ino 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Your code description here
  3. (c) you, 20XX
  4. All rights reserved.
  5. Functionality:
  6. Version log:
  7. 20XX-MM-DD:
  8. v1.0.0 - Initial release
  9. */
  10. // ==== DEFINES ===================================================================================
  11. // ==== Debug and Test options ==================
  12. #define _DEBUG_
  13. //#define _TEST_
  14. //===== Debugging macros ========================
  15. #ifdef _DEBUG_
  16. #define SerialD Serial
  17. #define _PM(a) SerialD.print(millis()); SerialD.print(": "); SerialD.println(a)
  18. #define _PP(a) SerialD.print(a)
  19. #define _PL(a) SerialD.println(a)
  20. #define _PX(a) SerialD.println(a, HEX)
  21. #else
  22. #define _PM(a)
  23. #define _PP(a)
  24. #define _PL(a)
  25. #define _PX(a)
  26. #endif
  27. // ==== INCLUDES ==================================================================================
  28. // ==== Uncomment desired compile options =================================
  29. // ----------------------------------------
  30. // The following "defines" control library functionality at compile time,
  31. // and should be used in the main sketch depending on the functionality required
  32. // Should be defined BEFORE #include <TaskScheduler.h> !!!
  33. // In PlatformIO - better define those as build_flags in platformio.ini file
  34. //
  35. // #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
  36. // #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
  37. // #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
  38. // #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
  39. // #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
  40. // #define _TASK_PRIORITY // Support for layered scheduling priority
  41. // #define _TASK_MICRO_RES // Support for microsecond resolution
  42. // #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
  43. // #define _TASK_DEBUG // Make all methods and variables public for debug purposes
  44. // #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
  45. // #define _TASK_TIMEOUT // Support for overall task timeout
  46. // #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
  47. // #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
  48. // #define _TASK_SCHEDULING_OPTIONS // Support for multiple scheduling options
  49. // #define _TASK_SELF_DESTRUCT // Enable tasks to "self-destruct" after disable
  50. // #define _TASK_TICKLESS // Enable support for tickless sleep on FreeRTOS
  51. // #define _TASK_DO_NOT_YIELD // Disable yield() method in execute() for ESP chips
  52. // #define _TASK_ISR_SUPPORT // for esp chips - place control methods in IRAM
  53. // #define _TASK_NON_ARDUINO // for non-arduino use
  54. // #define _TASK_HEADER_AND_CPP // compile CPP file (non-Arduino IDE platforms)
  55. // #define _TASK_THREAD_SAFE // Enable additional checking for thread safety
  56. #include <TaskScheduler.h>
  57. // ==== GLOBALS ===================================================================================
  58. // ==== Scheduler ==============================
  59. Scheduler ts;
  60. void task1Callback();
  61. void task2Callback();
  62. // ==== Scheduling defines (cheat sheet) =====================
  63. /*
  64. TASK_MILLISECOND - one millisecond in millisecond/microseconds
  65. TASK_SECOND - one second in millisecond/microseconds
  66. TASK_MINUTE - one minute in millisecond/microseconds
  67. TASK_HOUR - one hour in millisecond/microseconds
  68. TASK_IMMEDIATE - schedule task to runn as soon as possible
  69. TASK_FOREVER - run task indefinitely
  70. TASK_ONCE - run task once
  71. TASK_NOTIMEOUT - set timeout interval to No Timeout
  72. TASK_SCHEDULE - schedule is a priority, with "catch up" (default)
  73. TASK_SCHEDULE_NC - schedule is a priority, without "catch up"
  74. TASK_INTERVAL - interval is a priority, without "catch up"
  75. TASK_SR_OK - status request triggered with an OK code (all good)
  76. TASK_SR_ERROR - status request triggered with an ERROR code
  77. TASK_SR_CANCEL - status request was cancelled
  78. TASK_SR_ABORT - status request was aborted
  79. TASK_SR_TIMEOUT - status request timed out
  80. */
  81. // ==== Task definitions ========================
  82. Task t1 (100 * TASK_MILLISECOND, TASK_FOREVER, &task1Callback, &ts, true);
  83. Task t2 (TASK_IMMEDIATE, 100 /* times */, &task2Callback, &ts, true);
  84. // ==== CODE ======================================================================================
  85. /**************************************************************************/
  86. /*!
  87. @brief Standard Arduino SETUP method - initialize sketch
  88. @param none
  89. @returns none
  90. */
  91. /**************************************************************************/
  92. void setup() {
  93. // put your setup code here, to run once:
  94. #if defined(_DEBUG_) || defined(_TEST_)
  95. Serial.begin(115200);
  96. delay(2000);
  97. _PL("Scheduler Template: setup()");
  98. #endif
  99. }
  100. /**************************************************************************/
  101. /*!
  102. @brief Standard Arduino LOOP method - using with TaskScheduler there
  103. should be nothing here but ts.execute()
  104. @param none
  105. @returns none
  106. */
  107. /**************************************************************************/
  108. void loop() {
  109. ts.execute();
  110. }
  111. /**************************************************************************/
  112. /*!
  113. @brief Callback method of task1 - explain
  114. @param none
  115. @returns none
  116. */
  117. /**************************************************************************/
  118. void task1Callback() {
  119. _PM("task1Callback()");
  120. // task code
  121. }
  122. /**************************************************************************/
  123. /*!
  124. @brief Callback method of task2 - explain
  125. @param none
  126. @returns none
  127. */
  128. /**************************************************************************/
  129. void task2Callback() {
  130. _PM("task2Callback()");
  131. // task code
  132. }