main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. - Arduino Nano
  7. - ESP8266
  8. - ESP32
  9. - STM32 Maple Mini
  10. */
  11. #include <TaskSchedulerDeclarations.h>
  12. #include "main.h"
  13. #include "led.h"
  14. // Scheduler
  15. Scheduler ts;
  16. /*
  17. Approach 1: LED is driven by the boolean variable; false = OFF, true = ON
  18. */
  19. void blink1CB();
  20. Task tBlink1 ( PERIOD1 * TASK_MILLISECOND, DURATION / PERIOD1, &blink1CB, &ts, true );
  21. /*
  22. Approach 2: two callback methods: one turns ON, another turns OFF
  23. */
  24. void blink2CB_ON();
  25. void blink2CB_OFF();
  26. Task tBlink2 ( PERIOD2 * TASK_MILLISECOND, DURATION / PERIOD2, &blink2CB_ON, &ts, false );
  27. /*
  28. Approach 3: Use RunCounter
  29. */
  30. void blink3CB();
  31. Task tBlink3 (PERIOD3 * TASK_MILLISECOND, DURATION / PERIOD3, &blink3CB, &ts, false);
  32. /*
  33. Approach 4: Use status request objects to pass control from one task to the other
  34. */
  35. bool blink41OE();
  36. void blink41();
  37. void blink42();
  38. void blink42OD();
  39. Task tBlink4On ( PERIOD4 * TASK_MILLISECOND, TASK_ONCE, blink41, &ts, false, &blink41OE );
  40. Task tBlink4Off ( PERIOD4 * TASK_MILLISECOND, TASK_ONCE, blink42, &ts, false, NULL, &blink42OD );
  41. /*
  42. Approach 5: Two interleaving tasks
  43. */
  44. bool blink51OE();
  45. void blink51();
  46. void blink52();
  47. void blink52OD();
  48. Task tBlink5On ( 600 * TASK_MILLISECOND, DURATION / PERIOD5, &blink51, &ts, false, &blink51OE );
  49. Task tBlink5Off ( 600 * TASK_MILLISECOND, DURATION / PERIOD5, &blink52, &ts, false, NULL, &blink52OD );
  50. /*
  51. Approach 6: RunCounter-based with random intervals
  52. */
  53. void blink6CB();
  54. bool blink6OE();
  55. void blink6OD();
  56. Task tBlink6 ( PERIOD6 * TASK_MILLISECOND, DURATION / PERIOD6, &blink6CB, &ts, false, &blink6OE, &blink6OD );
  57. void setup() {
  58. // put your setup code here, to run once:
  59. #if defined(_DEBUG_) || defined(_TEST_)
  60. Serial.begin(115200);
  61. delay(TASK_SECOND);
  62. _PL("TaskScheduler Blink example");
  63. _PL("Blinking for 10 seconds using various techniques\n");
  64. delay(2 * TASK_SECOND);
  65. #endif
  66. pinMode(LED_BUILTIN, OUTPUT);
  67. }
  68. void loop() {
  69. ts.execute();
  70. }
  71. // === 1 =======================================
  72. bool LED_state = false;
  73. void blink1CB() {
  74. if ( tBlink1.isFirstIteration() ) {
  75. _PP(millis());
  76. _PL(": Blink1 - simple flag driven");
  77. LED_state = false;
  78. }
  79. if ( LED_state ) {
  80. LEDOff();
  81. LED_state = false;
  82. }
  83. else {
  84. LEDOn();
  85. LED_state = true;
  86. }
  87. if ( tBlink1.isLastIteration() ) {
  88. tBlink2.restartDelayed( 2 * TASK_SECOND );
  89. LEDOff();
  90. }
  91. }
  92. // === 2 ======================================
  93. void blink2CB_ON() {
  94. if ( tBlink2.isFirstIteration() ) {
  95. _PP(millis());
  96. _PL(": Blink2 - 2 callback methods");
  97. }
  98. LEDOn();
  99. tBlink2.setCallback( &blink2CB_OFF );
  100. if ( tBlink2.isLastIteration() ) {
  101. tBlink3.restartDelayed( 2 * TASK_SECOND );
  102. LEDOff();
  103. }
  104. }
  105. void blink2CB_OFF() {
  106. LEDOff();
  107. tBlink2.setCallback( &blink2CB_ON );
  108. if ( tBlink2.isLastIteration() ) {
  109. tBlink3.restartDelayed( 2 * TASK_SECOND );
  110. LEDOff();
  111. }
  112. }
  113. // === 3 =====================================
  114. void blink3CB() {
  115. if ( tBlink3.isFirstIteration() ) {
  116. _PP(millis());
  117. _PL(": Blink3 - Run Counter driven");
  118. }
  119. if ( tBlink3.getRunCounter() & 1 ) {
  120. LEDOn();
  121. }
  122. else {
  123. LEDOff();
  124. }
  125. if ( tBlink3.isLastIteration() ) {
  126. tBlink4On.setOnEnable( &blink41OE );
  127. tBlink4On.restartDelayed( 2 * TASK_SECOND );
  128. LEDOff();
  129. }
  130. }
  131. // === 4 =============================================
  132. int counter = 0;
  133. bool blink41OE() {
  134. _PP(millis());
  135. _PL(": Blink4 - Internal status request based");
  136. counter = 0;
  137. tBlink4On.setOnEnable( NULL );
  138. return true;
  139. }
  140. void blink41() {
  141. // _PP(millis());
  142. // _PL(": blink41");
  143. LEDOn();
  144. StatusRequest* r = tBlink4On.getInternalStatusRequest();
  145. tBlink4Off.waitForDelayed( r );
  146. counter++;
  147. }
  148. void blink42() {
  149. // _PP(millis());
  150. // _PL(": blink42");
  151. LEDOff();
  152. StatusRequest* r = tBlink4Off.getInternalStatusRequest();
  153. tBlink4On.waitForDelayed( r );
  154. counter++;
  155. }
  156. void blink42OD() {
  157. if ( counter >= DURATION / PERIOD4 ) {
  158. tBlink4On.disable();
  159. tBlink4Off.disable();
  160. tBlink5On.setOnEnable( &blink51OE );
  161. tBlink5On.restartDelayed( 2 * TASK_SECOND );
  162. tBlink5Off.restartDelayed( 2 * TASK_SECOND + PERIOD5 / 2 );
  163. LEDOff();
  164. }
  165. }
  166. // === 5 ==========================================
  167. bool blink51OE() {
  168. _PP(millis());
  169. _PL(": Blink5 - Two interleaving tasks");
  170. tBlink5On.setOnEnable( NULL );
  171. return true;
  172. }
  173. void blink51() {
  174. // _PP(millis());
  175. // _PL(": blink51");
  176. LEDOn();
  177. }
  178. void blink52() {
  179. // _PP(millis());
  180. // _PL(": blink52");
  181. LEDOff();
  182. }
  183. void blink52OD() {
  184. tBlink6.restartDelayed( 2 * TASK_SECOND );
  185. LEDOff();
  186. }
  187. // === 6 ============================================
  188. long interval6 = 0;
  189. bool blink6OE() {
  190. _PP(millis());
  191. _PP(": Blink6 - RunCounter + Random ON interval = ");
  192. interval6 = random( 100, 901 );
  193. tBlink6.setInterval( interval6 );
  194. _PL( interval6 );
  195. tBlink6.delay( 2 * TASK_SECOND );
  196. return true;
  197. }
  198. void blink6CB() {
  199. if ( tBlink6.getRunCounter() & 1 ) {
  200. LEDOn();
  201. tBlink6.setInterval( interval6 );
  202. }
  203. else {
  204. LEDOff();
  205. tBlink6.setInterval( TASK_SECOND - interval6 );
  206. }
  207. }
  208. void blink6OD() {
  209. tBlink1.restartDelayed( 2 * TASK_SECOND );
  210. LEDOff();
  211. }