You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

elapsedMillis.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Elapsed time types - for easy-to-use measurements of elapsed time
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #ifndef elapsedMillis_h
  24. #define elapsedMillis_h
  25. #ifdef __cplusplus
  26. #if ARDUINO >= 100
  27. #include "Arduino.h"
  28. #else
  29. #include "WProgram.h"
  30. #endif
  31. class elapsedMillis
  32. {
  33. private:
  34. unsigned long ms;
  35. public:
  36. elapsedMillis(void) { ms = millis(); }
  37. elapsedMillis(unsigned long val) { ms = millis() - val; }
  38. elapsedMillis(const elapsedMillis &orig) { ms = orig.ms; }
  39. operator unsigned long () const { return millis() - ms; }
  40. elapsedMillis & operator = (const elapsedMillis &rhs) { ms = rhs.ms; return *this; }
  41. elapsedMillis & operator = (unsigned long val) { ms = millis() - val; return *this; }
  42. elapsedMillis & operator -= (unsigned long val) { ms += val ; return *this; }
  43. elapsedMillis & operator += (unsigned long val) { ms -= val ; return *this; }
  44. elapsedMillis operator - (int val) const { elapsedMillis r(*this); r.ms += val; return r; }
  45. elapsedMillis operator - (unsigned int val) const { elapsedMillis r(*this); r.ms += val; return r; }
  46. elapsedMillis operator - (long val) const { elapsedMillis r(*this); r.ms += val; return r; }
  47. elapsedMillis operator - (unsigned long val) const { elapsedMillis r(*this); r.ms += val; return r; }
  48. elapsedMillis operator + (int val) const { elapsedMillis r(*this); r.ms -= val; return r; }
  49. elapsedMillis operator + (unsigned int val) const { elapsedMillis r(*this); r.ms -= val; return r; }
  50. elapsedMillis operator + (long val) const { elapsedMillis r(*this); r.ms -= val; return r; }
  51. elapsedMillis operator + (unsigned long val) const { elapsedMillis r(*this); r.ms -= val; return r; }
  52. };
  53. class elapsedMicros
  54. {
  55. private:
  56. unsigned long us;
  57. public:
  58. elapsedMicros(void) { us = micros(); }
  59. elapsedMicros(unsigned long val) { us = micros() - val; }
  60. elapsedMicros(const elapsedMicros &orig) { us = orig.us; }
  61. operator unsigned long () const { return micros() - us; }
  62. elapsedMicros & operator = (const elapsedMicros &rhs) { us = rhs.us; return *this; }
  63. elapsedMicros & operator = (unsigned long val) { us = micros() - val; return *this; }
  64. elapsedMicros & operator -= (unsigned long val) { us += val ; return *this; }
  65. elapsedMicros & operator += (unsigned long val) { us -= val ; return *this; }
  66. elapsedMicros operator - (int val) const { elapsedMicros r(*this); r.us += val; return r; }
  67. elapsedMicros operator - (unsigned int val) const { elapsedMicros r(*this); r.us += val; return r; }
  68. elapsedMicros operator - (long val) const { elapsedMicros r(*this); r.us += val; return r; }
  69. elapsedMicros operator - (unsigned long val) const { elapsedMicros r(*this); r.us += val; return r; }
  70. elapsedMicros operator + (int val) const { elapsedMicros r(*this); r.us -= val; return r; }
  71. elapsedMicros operator + (unsigned int val) const { elapsedMicros r(*this); r.us -= val; return r; }
  72. elapsedMicros operator + (long val) const { elapsedMicros r(*this); r.us -= val; return r; }
  73. elapsedMicros operator + (unsigned long val) const { elapsedMicros r(*this); r.us -= val; return r; }
  74. };
  75. #endif // __cplusplus
  76. #endif // elapsedMillis_h