Scheduler_example23_IDLE_Callback.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. An example of using scheduler's custom sleep callback method
  3. An empty loop is executed for 10 seconds with a 10 ms. interval
  4. The first time it is excuted with an empty sleep callback, and
  5. the second time with a 1 ms delay in the custom callback
  6. RESULTS:
  7. Arduino Nano:
  8. =================================
  9. Testing empty sleep callback...
  10. cEmptyCallback=1001
  11. cEmptyTotal=423866
  12. Testing 1 ms delayed sleep callback...
  13. cDelayCallback=1001
  14. cDelayTotal=10669
  15. ESP8266 at 80MHz
  16. =================================
  17. Testing empty sleep callback...
  18. cEmptyCallback=1001
  19. cEmptyTotal=278101
  20. Testing 1 ms delayed sleep callback...
  21. cDelayCallback=1001
  22. cDelayTotal=10493
  23. ESP8266 at 160MHz
  24. =================================
  25. Testing empty sleep callback...
  26. cEmptyCallback=1001
  27. cEmptyTotal=546041
  28. Testing 1 ms delayed sleep callback...
  29. cDelayCallback=1001
  30. cDelayTotal=10746
  31. Maple Mini STM32 board at 70MHz -O3 code optimization
  32. ==================================
  33. Testing empty sleep callback...
  34. cEmptyCallback=1001
  35. cEmptyTotal=2689973
  36. Testing 1 ms delayed sleep callback...
  37. cDelayCallback=1001
  38. cDelayTotal=10958
  39. esp32 at 240MHz
  40. ==================================
  41. Testing empty sleep callback...
  42. cEmptyCallback=1001
  43. cEmptyTotal=492851
  44. Testing 1 ms delayed sleep callback...
  45. cDelayCallback=1001
  46. cDelayTotal=11002
  47. */
  48. #define _TASK_SLEEP_ON_IDLE_RUN
  49. #include <TaskScheduler.h>
  50. Scheduler ts;
  51. // Callback methods prototypes
  52. void Count();
  53. bool tEmptyOn();
  54. void tEmptyOff();
  55. bool tDelayOn();
  56. void tDelayOff();
  57. // Sleep methods prototypes
  58. void sEmpty(unsigned long aT);
  59. void sDelay(unsigned long aT);
  60. // Tasks
  61. Task tCount ( 10, TASK_FOREVER, &Count, &ts, false );
  62. Task tEmpty ( 10000, TASK_ONCE, NULL, &ts, false, &tEmptyOn, &tEmptyOff );
  63. Task tDelay ( 10000, TASK_ONCE, NULL, &ts, false, &tDelayOn, &tDelayOff );
  64. volatile unsigned long cEmptyCallback, cEmptyTotal, cDelayCallback, cDelayTotal;
  65. volatile unsigned long *cCB, *cTL;
  66. void setup() {
  67. Serial.begin(115200);
  68. delay(5000);
  69. Serial.println("Start counting...");
  70. ts.setSleepMethod( &sEmpty );
  71. tEmpty.restartDelayed();
  72. }
  73. void sEmpty(unsigned long aT) {
  74. }
  75. void sDelay(unsigned long aT) {
  76. delay(1);
  77. }
  78. bool tEmptyOn() {
  79. Serial.println("Testing empty sleep callback...");
  80. cCB = &cEmptyCallback;
  81. cTL = &cEmptyTotal;
  82. *cCB = 0;
  83. *cTL = 0;
  84. tCount.restart();
  85. return true;
  86. }
  87. void tEmptyOff() {
  88. tCount.disable();
  89. Serial.print("cEmptyCallback="); Serial.println(*cCB);
  90. Serial.print("cEmptyTotal="); Serial.println(*cTL);
  91. ts.setSleepMethod( &sDelay );
  92. tDelay.restartDelayed();
  93. }
  94. bool tDelayOn() {
  95. Serial.println("Testing 1 ms delayed sleep callback...");
  96. cCB = &cDelayCallback;
  97. cTL = &cDelayTotal;
  98. *cCB = 0;
  99. *cTL = 0;
  100. tCount.restart();
  101. return true;
  102. }
  103. void tDelayOff() {
  104. tCount.disable();
  105. Serial.print("cDelayCallback="); Serial.println(*cCB);
  106. Serial.print("cDelayTotal="); Serial.println(*cTL);
  107. }
  108. void Count() {
  109. (*cCB)++;
  110. }
  111. void loop() {
  112. // put your main code here, to run repeatedly:
  113. ts.execute();
  114. (*cTL)++;
  115. }