Scheduler_template.ino 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. //
  34. // #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
  35. // #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
  36. // #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
  37. // #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
  38. // #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
  39. // #define _TASK_PRIORITY // Support for layered scheduling priority
  40. // #define _TASK_MICRO_RES // Support for microsecond resolution
  41. // #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
  42. // #define _TASK_DEBUG // Make all methods and variables public for debug purposes
  43. // #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
  44. // #define _TASK_TIMEOUT // Support for overall task timeout
  45. // #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
  46. // #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
  47. // #define _TASK_SCHEDULING_OPTIONS // Support for multiple scheduling options
  48. // #define _TASK_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
  49. // #define _TASK_EXTERNAL_TIME // Custom millis() and micros() methods
  50. // #define _TASK_THREAD_SAFE // Enable additional checking for thread safety
  51. // #define _TASK_SELF_DESTRUCT // Enable tasks to "self-destruct" after disable
  52. #include <TaskScheduler.h>
  53. // ==== GLOBALS ===================================================================================
  54. // ==== Scheduler ==============================
  55. Scheduler ts;
  56. void task1Callback();
  57. void task2Callback();
  58. // ==== Scheduling defines (cheat sheet) =====================
  59. /*
  60. TASK_MILLISECOND - one millisecond in millisecond/microseconds
  61. TASK_SECOND - one second in millisecond/microseconds
  62. TASK_MINUTE - one minute in millisecond/microseconds
  63. TASK_HOUR - one hour in millisecond/microseconds
  64. TASK_IMMEDIATE - schedule task to runn as soon as possible
  65. TASK_FOREVER - run task indefinitely
  66. TASK_ONCE - run task once
  67. TASK_NOTIMEOUT - set timeout interval to No Timeout
  68. TASK_SCHEDULE - schedule is a priority, with "catch up" (default)
  69. TASK_SCHEDULE_NC - schedule is a priority, without "catch up"
  70. TASK_INTERVAL - interval is a priority, without "catch up"
  71. TASK_SR_OK - status request triggered with an OK code (all good)
  72. TASK_SR_ERROR - status request triggered with an ERROR code
  73. TASK_SR_CANCEL - status request was cancelled
  74. TASK_SR_ABORT - status request was aborted
  75. TASK_SR_TIMEOUT - status request timed out
  76. */
  77. // ==== Task definitions ========================
  78. Task t1 (100 * TASK_MILLISECOND, TASK_FOREVER, &task1Callback, &ts, true);
  79. Task t2 (TASK_IMMEDIATE, 100 /* times */, &task2Callback, &ts, true);
  80. // ==== CODE ======================================================================================
  81. /**************************************************************************/
  82. /*!
  83. @brief Standard Arduino SETUP method - initialize sketch
  84. @param none
  85. @returns none
  86. */
  87. /**************************************************************************/
  88. void setup() {
  89. // put your setup code here, to run once:
  90. #if defined(_DEBUG_) || defined(_TEST_)
  91. Serial.begin(115200);
  92. delay(2000);
  93. _PL("Scheduler Template: setup()");
  94. #endif
  95. }
  96. /**************************************************************************/
  97. /*!
  98. @brief Standard Arduino LOOP method - using with TaskScheduler there
  99. should be nothing here but ts.execute()
  100. @param none
  101. @returns none
  102. */
  103. /**************************************************************************/
  104. void loop() {
  105. ts.execute();
  106. }
  107. /**************************************************************************/
  108. /*!
  109. @brief Callback method of task1 - explain
  110. @param none
  111. @returns none
  112. */
  113. /**************************************************************************/
  114. void task1Callback() {
  115. _PM("task1Callback()");
  116. // task code
  117. }
  118. /**************************************************************************/
  119. /*!
  120. @brief Callback method of task2 - explain
  121. @param none
  122. @returns none
  123. */
  124. /**************************************************************************/
  125. void task2Callback() {
  126. _PM("task2Callback()");
  127. // task code
  128. }