Scheduler_example00_Blink_Namespace.ino 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
  27. // #define _TASK_EXTERNAL_TIME // Custom millis() and micros() methods
  28. // #define _TASK_THREAD_SAFE // Enable additional checking for thread safety
  29. // #define _TASK_SELF_DESTRUCT // Enable tasks to "self-destruct" after disable
  30. #include <TScheduler.hpp>
  31. // Debug and Test options
  32. //#define _DEBUG_
  33. //#define _TEST_
  34. #ifdef _DEBUG_
  35. #define _PP(a) Serial.print(a);
  36. #define _PL(a) Serial.println(a);
  37. #else
  38. #define _PP(a)
  39. #define _PL(a)
  40. #endif
  41. // LED_BUILTIN 13
  42. #if defined( ARDUINO_ARCH_ESP32 )
  43. #define LED_BUILTIN 23 // esp32 dev2 kit does not have LED
  44. #endif
  45. // Scheduler
  46. TsScheduler ts;
  47. /*
  48. Approach 1: LED is driven by the boolean variable; false = OFF, true = ON
  49. */
  50. #define PERIOD1 500
  51. #define DURATION 10000
  52. void blink1CB();
  53. TsTask tBlink1 ( PERIOD1 * TASK_MILLISECOND, DURATION / PERIOD1, &blink1CB, &ts, true );
  54. /*
  55. Approac 2: two callback methods: one turns ON, another turns OFF
  56. */
  57. #define PERIOD2 400
  58. void blink2CB_ON();
  59. void blink2CB_OFF();
  60. TsTask tBlink2 ( PERIOD2 * TASK_MILLISECOND, DURATION / PERIOD2, &blink2CB_ON, &ts, false );
  61. /*
  62. Approach 3: Use RunCounter
  63. */
  64. #define PERIOD3 300
  65. void blink3CB();
  66. TsTask tBlink3 (PERIOD3 * TASK_MILLISECOND, DURATION / PERIOD3, &blink3CB, &ts, false);
  67. /*
  68. Approach 4: Use status request objects to pass control from one task to the other
  69. */
  70. #define PERIOD4 200
  71. bool blink41OE();
  72. void blink41();
  73. void blink42();
  74. void blink42OD();
  75. TsTask tBlink4On ( PERIOD4 * TASK_MILLISECOND, TASK_ONCE, blink41, &ts, false, &blink41OE );
  76. TsTask tBlink4Off ( PERIOD4 * TASK_MILLISECOND, TASK_ONCE, blink42, &ts, false, NULL, &blink42OD );
  77. /*
  78. Approach 5: Two interleaving tasks
  79. */
  80. #define PERIOD5 600
  81. bool blink51OE();
  82. void blink51();
  83. void blink52();
  84. void blink52OD();
  85. TsTask tBlink5On ( 600 * TASK_MILLISECOND, DURATION / PERIOD5, &blink51, &ts, false, &blink51OE );
  86. TsTask tBlink5Off ( 600 * TASK_MILLISECOND, DURATION / PERIOD5, &blink52, &ts, false, NULL, &blink52OD );
  87. /*
  88. Approach 6: RunCounter-based with random intervals
  89. */
  90. #define PERIOD6 300
  91. void blink6CB();
  92. bool blink6OE();
  93. void blink6OD();
  94. TsTask tBlink6 ( PERIOD6 * TASK_MILLISECOND, DURATION / PERIOD6, &blink6CB, &ts, false, &blink6OE, &blink6OD );
  95. void setup() {
  96. // put your setup code here, to run once:
  97. #if defined(_DEBUG_) || defined(_TEST_)
  98. Serial.begin(115200);
  99. delay(TASK_SECOND);
  100. _PL("TaskScheduler Blink example");
  101. _PL("Blinking for 10 seconds using various techniques\n");
  102. delay(2 * TASK_SECOND);
  103. #endif
  104. pinMode(LED_BUILTIN, OUTPUT);
  105. }
  106. void loop() {
  107. ts.execute();
  108. }
  109. inline void LEDOn() {
  110. digitalWrite( LED_BUILTIN, HIGH );
  111. }
  112. inline void LEDOff() {
  113. digitalWrite( LED_BUILTIN, LOW );
  114. }
  115. // === 1 =======================================
  116. bool LED_state = false;
  117. void blink1CB() {
  118. if ( tBlink1.isFirstIteration() ) {
  119. _PP(millis());
  120. _PL(": Blink1 - simple flag driven");
  121. LED_state = false;
  122. }
  123. if ( LED_state ) {
  124. LEDOff();
  125. LED_state = false;
  126. }
  127. else {
  128. LEDOn();
  129. LED_state = true;
  130. }
  131. if ( tBlink1.isLastIteration() ) {
  132. tBlink2.restartDelayed( 2 * TASK_SECOND );
  133. LEDOff();
  134. }
  135. }
  136. // === 2 ======================================
  137. void blink2CB_ON() {
  138. if ( tBlink2.isFirstIteration() ) {
  139. _PP(millis());
  140. _PL(": Blink2 - 2 callback methods");
  141. }
  142. LEDOn();
  143. tBlink2.setCallback( &blink2CB_OFF );
  144. if ( tBlink2.isLastIteration() ) {
  145. tBlink3.restartDelayed( 2 * TASK_SECOND );
  146. LEDOff();
  147. }
  148. }
  149. void blink2CB_OFF() {
  150. LEDOff();
  151. tBlink2.setCallback( &blink2CB_ON );
  152. if ( tBlink2.isLastIteration() ) {
  153. tBlink3.restartDelayed( 2 * TASK_SECOND );
  154. LEDOff();
  155. }
  156. }
  157. // === 3 =====================================
  158. void blink3CB() {
  159. if ( tBlink3.isFirstIteration() ) {
  160. _PP(millis());
  161. _PL(": Blink3 - Run Counter driven");
  162. }
  163. if ( tBlink3.getRunCounter() & 1 ) {
  164. LEDOn();
  165. }
  166. else {
  167. LEDOff();
  168. }
  169. if ( tBlink3.isLastIteration() ) {
  170. tBlink4On.setOnEnable( &blink41OE );
  171. tBlink4On.restartDelayed( 2 * TASK_SECOND );
  172. LEDOff();
  173. }
  174. }
  175. // === 4 =============================================
  176. int counter = 0;
  177. bool blink41OE() {
  178. _PP(millis());
  179. _PL(": Blink4 - Internal status request based");
  180. counter = 0;
  181. tBlink4On.setOnEnable( NULL );
  182. return true;
  183. }
  184. void blink41() {
  185. // _PP(millis());
  186. // _PL(": blink41");
  187. LEDOn();
  188. TsStatusRequest* r = tBlink4On.getInternalStatusRequest();
  189. tBlink4Off.waitForDelayed( r );
  190. counter++;
  191. }
  192. void blink42() {
  193. // _PP(millis());
  194. // _PL(": blink42");
  195. LEDOff();
  196. TsStatusRequest* r = tBlink4Off.getInternalStatusRequest();
  197. tBlink4On.waitForDelayed( r );
  198. counter++;
  199. }
  200. void blink42OD() {
  201. if ( counter >= DURATION / PERIOD4 ) {
  202. tBlink4On.disable();
  203. tBlink4Off.disable();
  204. tBlink5On.setOnEnable( &blink51OE );
  205. tBlink5On.restartDelayed( 2 * TASK_SECOND );
  206. tBlink5Off.restartDelayed( 2 * TASK_SECOND + PERIOD5 / 2 );
  207. LEDOff();
  208. }
  209. }
  210. // === 5 ==========================================
  211. bool blink51OE() {
  212. _PP(millis());
  213. _PL(": Blink5 - Two interleaving tasks");
  214. tBlink5On.setOnEnable( NULL );
  215. return true;
  216. }
  217. void blink51() {
  218. // _PP(millis());
  219. // _PL(": blink51");
  220. LEDOn();
  221. }
  222. void blink52() {
  223. // _PP(millis());
  224. // _PL(": blink52");
  225. LEDOff();
  226. }
  227. void blink52OD() {
  228. tBlink6.restartDelayed( 2 * TASK_SECOND );
  229. LEDOff();
  230. }
  231. // === 6 ============================================
  232. long interval6 = 0;
  233. bool blink6OE() {
  234. _PP(millis());
  235. _PP(": Blink6 - RunCounter + Random ON interval = ");
  236. interval6 = random( 100, 901 );
  237. tBlink6.setInterval( interval6 );
  238. _PL( interval6 );
  239. tBlink6.delay( 2 * TASK_SECOND );
  240. return true;
  241. }
  242. void blink6CB() {
  243. if ( tBlink6.getRunCounter() & 1 ) {
  244. LEDOn();
  245. tBlink6.setInterval( interval6 );
  246. }
  247. else {
  248. LEDOff();
  249. tBlink6.setInterval( TASK_SECOND - interval6 );
  250. }
  251. }
  252. void blink6OD() {
  253. tBlink1.restartDelayed( 2 * TASK_SECOND );
  254. LEDOff();
  255. }