Scheduler_example00_Blink_Namespace.ino 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Every example set must have a LED blink example
  3. For this one the idea is to have as many ways to blink the LED
  4. as I can think of. So, here we go.
  5. Tested on:
  6. - Bluefruit nRF52840
  7. */
  8. // ----------------------------------------
  9. // The following "defines" control library functionality at compile time,
  10. // and should be used in the main sketch depending on the functionality required
  11. //
  12. // #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
  13. #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
  14. #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
  15. // #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
  16. // #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
  17. // #define _TASK_PRIORITY // Support for layered scheduling priority
  18. // #define _TASK_MICRO_RES // Support for microsecond resolution
  19. // #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 ONLY)
  20. // #define _TASK_DEBUG // Make all methods and variables public for debug purposes
  21. // #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
  22. // #define _TASK_TIMEOUT // Support for overall task timeout
  23. // #define _TASK_OO_CALLBACKS // Support for callbacks via inheritance
  24. // #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
  25. // #define _TASK_SCHEDULING_OPTIONS // Support for multiple scheduling options
  26. // #define _TASK_THREAD_SAFE // Enable additional checking for thread safety
  27. // #define _TASK_SELF_DESTRUCT // Enable tasks to "self-destruct" after disable
  28. #include <TScheduler.hpp>
  29. // Debug and Test options
  30. //#define _DEBUG_
  31. //#define _TEST_
  32. #ifdef _DEBUG_
  33. #define _PP(a) Serial.print(a);
  34. #define _PL(a) Serial.println(a);
  35. #else
  36. #define _PP(a)
  37. #define _PL(a)
  38. #endif
  39. // LED_BUILTIN 13
  40. #if defined( ARDUINO_ARCH_ESP32 )
  41. #define LED_BUILTIN 23 // esp32 dev2 kit does not have LED
  42. #endif
  43. // Scheduler
  44. TsScheduler ts;
  45. /*
  46. Approach 1: LED is driven by the boolean variable; false = OFF, true = ON
  47. */
  48. #define PERIOD1 500
  49. #define DURATION 10000
  50. void blink1CB();
  51. TsTask tBlink1 ( PERIOD1 * TASK_MILLISECOND, DURATION / PERIOD1, &blink1CB, &ts, true );
  52. /*
  53. Approac 2: two callback methods: one turns ON, another turns OFF
  54. */
  55. #define PERIOD2 400
  56. void blink2CB_ON();
  57. void blink2CB_OFF();
  58. TsTask tBlink2 ( PERIOD2 * TASK_MILLISECOND, DURATION / PERIOD2, &blink2CB_ON, &ts, false );
  59. /*
  60. Approach 3: Use RunCounter
  61. */
  62. #define PERIOD3 300
  63. void blink3CB();
  64. TsTask tBlink3 (PERIOD3 * TASK_MILLISECOND, DURATION / PERIOD3, &blink3CB, &ts, false);
  65. /*
  66. Approach 4: Use status request objects to pass control from one task to the other
  67. */
  68. #define PERIOD4 200
  69. bool blink41OE();
  70. void blink41();
  71. void blink42();
  72. void blink42OD();
  73. TsTask tBlink4On ( PERIOD4 * TASK_MILLISECOND, TASK_ONCE, blink41, &ts, false, &blink41OE );
  74. TsTask tBlink4Off ( PERIOD4 * TASK_MILLISECOND, TASK_ONCE, blink42, &ts, false, NULL, &blink42OD );
  75. /*
  76. Approach 5: Two interleaving tasks
  77. */
  78. #define PERIOD5 600
  79. bool blink51OE();
  80. void blink51();
  81. void blink52();
  82. void blink52OD();
  83. TsTask tBlink5On ( 600 * TASK_MILLISECOND, DURATION / PERIOD5, &blink51, &ts, false, &blink51OE );
  84. TsTask tBlink5Off ( 600 * TASK_MILLISECOND, DURATION / PERIOD5, &blink52, &ts, false, NULL, &blink52OD );
  85. /*
  86. Approach 6: RunCounter-based with random intervals
  87. */
  88. #define PERIOD6 300
  89. void blink6CB();
  90. bool blink6OE();
  91. void blink6OD();
  92. TsTask tBlink6 ( PERIOD6 * TASK_MILLISECOND, DURATION / PERIOD6, &blink6CB, &ts, false, &blink6OE, &blink6OD );
  93. void setup() {
  94. // put your setup code here, to run once:
  95. #if defined(_DEBUG_) || defined(_TEST_)
  96. Serial.begin(115200);
  97. delay(TASK_SECOND);
  98. _PL("TaskScheduler Blink example");
  99. _PL("Blinking for 10 seconds using various techniques\n");
  100. delay(2 * TASK_SECOND);
  101. #endif
  102. pinMode(LED_BUILTIN, OUTPUT);
  103. }
  104. void loop() {
  105. ts.execute();
  106. }
  107. inline void LEDOn() {
  108. digitalWrite( LED_BUILTIN, HIGH );
  109. }
  110. inline void LEDOff() {
  111. digitalWrite( LED_BUILTIN, LOW );
  112. }
  113. // === 1 =======================================
  114. bool LED_state = false;
  115. void blink1CB() {
  116. if ( tBlink1.isFirstIteration() ) {
  117. _PP(millis());
  118. _PL(": Blink1 - simple flag driven");
  119. LED_state = false;
  120. }
  121. if ( LED_state ) {
  122. LEDOff();
  123. LED_state = false;
  124. }
  125. else {
  126. LEDOn();
  127. LED_state = true;
  128. }
  129. if ( tBlink1.isLastIteration() ) {
  130. tBlink2.restartDelayed( 2 * TASK_SECOND );
  131. LEDOff();
  132. }
  133. }
  134. // === 2 ======================================
  135. void blink2CB_ON() {
  136. if ( tBlink2.isFirstIteration() ) {
  137. _PP(millis());
  138. _PL(": Blink2 - 2 callback methods");
  139. }
  140. LEDOn();
  141. tBlink2.setCallback( &blink2CB_OFF );
  142. if ( tBlink2.isLastIteration() ) {
  143. tBlink3.restartDelayed( 2 * TASK_SECOND );
  144. LEDOff();
  145. }
  146. }
  147. void blink2CB_OFF() {
  148. LEDOff();
  149. tBlink2.setCallback( &blink2CB_ON );
  150. if ( tBlink2.isLastIteration() ) {
  151. tBlink3.restartDelayed( 2 * TASK_SECOND );
  152. LEDOff();
  153. }
  154. }
  155. // === 3 =====================================
  156. void blink3CB() {
  157. if ( tBlink3.isFirstIteration() ) {
  158. _PP(millis());
  159. _PL(": Blink3 - Run Counter driven");
  160. }
  161. if ( tBlink3.getRunCounter() & 1 ) {
  162. LEDOn();
  163. }
  164. else {
  165. LEDOff();
  166. }
  167. if ( tBlink3.isLastIteration() ) {
  168. tBlink4On.setOnEnable( &blink41OE );
  169. tBlink4On.restartDelayed( 2 * TASK_SECOND );
  170. LEDOff();
  171. }
  172. }
  173. // === 4 =============================================
  174. int counter = 0;
  175. bool blink41OE() {
  176. _PP(millis());
  177. _PL(": Blink4 - Internal status request based");
  178. counter = 0;
  179. tBlink4On.setOnEnable( NULL );
  180. return true;
  181. }
  182. void blink41() {
  183. // _PP(millis());
  184. // _PL(": blink41");
  185. LEDOn();
  186. TsStatusRequest* r = tBlink4On.getInternalStatusRequest();
  187. tBlink4Off.waitForDelayed( r );
  188. counter++;
  189. }
  190. void blink42() {
  191. // _PP(millis());
  192. // _PL(": blink42");
  193. LEDOff();
  194. TsStatusRequest* r = tBlink4Off.getInternalStatusRequest();
  195. tBlink4On.waitForDelayed( r );
  196. counter++;
  197. }
  198. void blink42OD() {
  199. if ( counter >= DURATION / PERIOD4 ) {
  200. tBlink4On.disable();
  201. tBlink4Off.disable();
  202. tBlink5On.setOnEnable( &blink51OE );
  203. tBlink5On.restartDelayed( 2 * TASK_SECOND );
  204. tBlink5Off.restartDelayed( 2 * TASK_SECOND + PERIOD5 / 2 );
  205. LEDOff();
  206. }
  207. }
  208. // === 5 ==========================================
  209. bool blink51OE() {
  210. _PP(millis());
  211. _PL(": Blink5 - Two interleaving tasks");
  212. tBlink5On.setOnEnable( NULL );
  213. return true;
  214. }
  215. void blink51() {
  216. // _PP(millis());
  217. // _PL(": blink51");
  218. LEDOn();
  219. }
  220. void blink52() {
  221. // _PP(millis());
  222. // _PL(": blink52");
  223. LEDOff();
  224. }
  225. void blink52OD() {
  226. tBlink6.restartDelayed( 2 * TASK_SECOND );
  227. LEDOff();
  228. }
  229. // === 6 ============================================
  230. long interval6 = 0;
  231. bool blink6OE() {
  232. _PP(millis());
  233. _PP(": Blink6 - RunCounter + Random ON interval = ");
  234. interval6 = random( 100, 901 );
  235. tBlink6.setInterval( interval6 );
  236. _PL( interval6 );
  237. tBlink6.delay( 2 * TASK_SECOND );
  238. return true;
  239. }
  240. void blink6CB() {
  241. if ( tBlink6.getRunCounter() & 1 ) {
  242. LEDOn();
  243. tBlink6.setInterval( interval6 );
  244. }
  245. else {
  246. LEDOff();
  247. tBlink6.setInterval( TASK_SECOND - interval6 );
  248. }
  249. }
  250. void blink6OD() {
  251. tBlink1.restartDelayed( 2 * TASK_SECOND );
  252. LEDOff();
  253. }