Scheduler_example06_IDLE.ino 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * This is a test to prove that processor really goes into IDLE sleep.
  3. * For this setup:
  4. *
  5. *
  6. Task c(10, -1, &Count, &ts);
  7. Task t(10000, 1, NULL, &ts, true, &tOn, &tOff);
  8. The result are:
  9. 1): With #define _TASK_SLEEP_ON_IDLE_RUN enabled
  10. On Arduino Uno:
  11. Start
  12. c1=10771 - v2.5.0 (v1.9.0: same)
  13. c2=1001
  14. On Teensy 3.5 (120MHz ARM):
  15. Start
  16. c1=21065
  17. c2=1001
  18. On esp8266 (80 MHz)
  19. Start
  20. c1=10492
  21. c2=1001
  22. On STM32F103RCBT6 (Maple Mini @72 MHz)
  23. Start
  24. c1=21004
  25. c2=1001
  26. and
  27. 2): With #define _TASK_SLEEP_ON_IDLE_RUN disabled (commented out)
  28. Arduino Uno:
  29. Start
  30. c1=722426 - v3.0.2
  31. c1=635735 - v2.5.0
  32. c1=551947 - v1.9.0
  33. c2=1001
  34. On Teensy 3.5 (120MHz ARM):
  35. Start
  36. c1=2690322
  37. c2=1001
  38. On esp8266 (80 MHz)
  39. Start
  40. c1=351085 (689833 at 160Mhz)
  41. c2=1001
  42. On STM32F103RCBT6 (Maple Mini @72 MHz)
  43. Start
  44. c1=4665019
  45. c2=1001
  46. C1 in scenario 2) is much higher than in scenario 1) because processor is put to sleep for 1), but not for 2)
  47. */
  48. /**
  49. * Compile and run once with _TASK_SLEEP_ON_IDLE_RUN enabled, then with _TASK_SLEEP_ON_IDLE_RUN disabled.
  50. * Compare the results.
  51. */
  52. //#define _TASK_SLEEP_ON_IDLE_RUN
  53. #include <TaskScheduler.h>
  54. Scheduler ts;
  55. // Callback methods prototypes
  56. void Count();
  57. bool tOn(); void tOff();
  58. // Tasks
  59. Task c(10, TASK_FOREVER, &Count, &ts);
  60. Task t(10000, TASK_ONCE, NULL, &ts, true, &tOn, &tOff);
  61. volatile unsigned long c1, c2;
  62. bool tOn() {
  63. c1 = 0;
  64. c2 = 0;
  65. c.enable();
  66. return true;
  67. }
  68. void tOff() {
  69. c.disable();
  70. Serial.print("c1=");Serial.println(c1);
  71. Serial.print("c2=");Serial.println(c2);
  72. }
  73. void setup() {
  74. // put your setup code here, to run once:
  75. Serial.begin(115200);
  76. delay(1000);
  77. Serial.println("Start");
  78. ts.startNow();
  79. t.delay();
  80. }
  81. void Count() {
  82. c2++;
  83. }
  84. void loop() {
  85. // put your main code here, to run repeatedly:
  86. ts.execute();
  87. c1++;
  88. }