PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Metro.cpp 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if defined(ARDUINO) && ARDUINO >= 100
  2. #include "Arduino.h"
  3. #else
  4. #include "WProgram.h"
  5. #endif
  6. #include "Metro.h"
  7. Metro::Metro(unsigned long interval_millis)
  8. {
  9. this->autoreset = 0;
  10. interval(interval_millis);
  11. reset();
  12. }
  13. // New creator so I can use either the original check behavior or benjamin.soelberg's
  14. // suggested one (see below).
  15. // autoreset = 0 is benjamin.soelberg's check behavior
  16. // autoreset != 0 is the original behavior
  17. Metro::Metro(unsigned long interval_millis, uint8_t autoreset)
  18. {
  19. this->autoreset = autoreset; // Fix by Paul Bouchier
  20. interval(interval_millis);
  21. reset();
  22. }
  23. void Metro::interval(unsigned long interval_millis)
  24. {
  25. this->interval_millis = interval_millis;
  26. }
  27. // Benjamin.soelberg's check behavior:
  28. // When a check is true, add the interval to the internal counter.
  29. // This should guarantee a better overall stability.
  30. // Original check behavior:
  31. // When a check is true, add the interval to the current millis() counter.
  32. // This method can add a certain offset over time.
  33. char Metro::check()
  34. {
  35. if (millis() - this->previous_millis >= this->interval_millis) {
  36. // As suggested by benjamin.soelberg@gmail.com, the following line
  37. // this->previous_millis = millis();
  38. // was changed to
  39. // this->previous_millis += this->interval_millis;
  40. // If the interval is set to 0 we revert to the original behavior
  41. if (this->interval_millis <= 0 || this->autoreset ) {
  42. this->previous_millis = millis();
  43. } else {
  44. this->previous_millis += this->interval_millis;
  45. }
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. void Metro::reset()
  51. {
  52. this->previous_millis = millis();
  53. }